OSDN Git Service

x86/cpu: Add a helper function x86_read_arch_cap_msr()
[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 u64 x86_read_arch_cap_msr(void)
922 {
923         u64 ia32_cap = 0;
924
925         if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
926                 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
927
928         return ia32_cap;
929 }
930
931 static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
932 {
933         u64 ia32_cap = x86_read_arch_cap_msr();
934
935         if (cpu_matches(NO_SPECULATION))
936                 return;
937
938         setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
939         setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
940
941         if (!cpu_matches(NO_SSB) && !(ia32_cap & ARCH_CAP_SSB_NO) &&
942            !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
943                 setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
944
945         if (ia32_cap & ARCH_CAP_IBRS_ALL)
946                 setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
947
948         if (!cpu_matches(NO_MDS) && !(ia32_cap & ARCH_CAP_MDS_NO)) {
949                 setup_force_cpu_bug(X86_BUG_MDS);
950                 if (cpu_matches(MSBDS_ONLY))
951                         setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
952         }
953
954         if (!cpu_matches(NO_SWAPGS))
955                 setup_force_cpu_bug(X86_BUG_SWAPGS);
956
957         if (cpu_matches(NO_MELTDOWN))
958                 return;
959
960         /* Rogue Data Cache Load? No! */
961         if (ia32_cap & ARCH_CAP_RDCL_NO)
962                 return;
963
964         setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
965
966         if (cpu_matches(NO_L1TF))
967                 return;
968
969         setup_force_cpu_bug(X86_BUG_L1TF);
970 }
971
972 /*
973  * Do minimum CPU detection early.
974  * Fields really needed: vendor, cpuid_level, family, model, mask,
975  * cache alignment.
976  * The others are not touched to avoid unwanted side effects.
977  *
978  * WARNING: this function is only called on the BP.  Don't add code here
979  * that is supposed to run on all CPUs.
980  */
981 static void __init early_identify_cpu(struct cpuinfo_x86 *c)
982 {
983 #ifdef CONFIG_X86_64
984         c->x86_clflush_size = 64;
985         c->x86_phys_bits = 36;
986         c->x86_virt_bits = 48;
987 #else
988         c->x86_clflush_size = 32;
989         c->x86_phys_bits = 32;
990         c->x86_virt_bits = 32;
991 #endif
992         c->x86_cache_alignment = c->x86_clflush_size;
993
994         memset(&c->x86_capability, 0, sizeof c->x86_capability);
995         c->extended_cpuid_level = 0;
996
997         if (!have_cpuid_p())
998                 identify_cpu_without_cpuid(c);
999
1000         /* cyrix could have cpuid enabled via c_identify()*/
1001         if (have_cpuid_p()) {
1002                 cpu_detect(c);
1003                 get_cpu_vendor(c);
1004                 get_cpu_cap(c);
1005
1006                 if (this_cpu->c_early_init)
1007                         this_cpu->c_early_init(c);
1008
1009                 c->cpu_index = 0;
1010                 filter_cpuid_features(c, false);
1011
1012                 if (this_cpu->c_bsp_init)
1013                         this_cpu->c_bsp_init(c);
1014         }
1015
1016         setup_force_cpu_cap(X86_FEATURE_ALWAYS);
1017
1018         cpu_set_bug_bits(c);
1019
1020         fpu__init_system(c);
1021
1022 #ifdef CONFIG_X86_32
1023         /*
1024          * Regardless of whether PCID is enumerated, the SDM says
1025          * that it can't be enabled in 32-bit mode.
1026          */
1027         setup_clear_cpu_cap(X86_FEATURE_PCID);
1028 #endif
1029 }
1030
1031 void __init early_cpu_init(void)
1032 {
1033         const struct cpu_dev *const *cdev;
1034         int count = 0;
1035
1036 #ifdef CONFIG_PROCESSOR_SELECT
1037         printk(KERN_INFO "KERNEL supported cpus:\n");
1038 #endif
1039
1040         for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
1041                 const struct cpu_dev *cpudev = *cdev;
1042
1043                 if (count >= X86_VENDOR_NUM)
1044                         break;
1045                 cpu_devs[count] = cpudev;
1046                 count++;
1047
1048 #ifdef CONFIG_PROCESSOR_SELECT
1049                 {
1050                         unsigned int j;
1051
1052                         for (j = 0; j < 2; j++) {
1053                                 if (!cpudev->c_ident[j])
1054                                         continue;
1055                                 printk(KERN_INFO "  %s %s\n", cpudev->c_vendor,
1056                                         cpudev->c_ident[j]);
1057                         }
1058                 }
1059 #endif
1060         }
1061         early_identify_cpu(&boot_cpu_data);
1062 }
1063
1064 /*
1065  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1066  * unfortunately, that's not true in practice because of early VIA
1067  * chips and (more importantly) broken virtualizers that are not easy
1068  * to detect. In the latter case it doesn't even *fail* reliably, so
1069  * probing for it doesn't even work. Disable it completely on 32-bit
1070  * unless we can find a reliable way to detect all the broken cases.
1071  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
1072  */
1073 static void detect_nopl(struct cpuinfo_x86 *c)
1074 {
1075 #ifdef CONFIG_X86_32
1076         clear_cpu_cap(c, X86_FEATURE_NOPL);
1077 #else
1078         set_cpu_cap(c, X86_FEATURE_NOPL);
1079 #endif
1080 }
1081
1082 static void generic_identify(struct cpuinfo_x86 *c)
1083 {
1084         c->extended_cpuid_level = 0;
1085
1086         if (!have_cpuid_p())
1087                 identify_cpu_without_cpuid(c);
1088
1089         /* cyrix could have cpuid enabled via c_identify()*/
1090         if (!have_cpuid_p())
1091                 return;
1092
1093         cpu_detect(c);
1094
1095         get_cpu_vendor(c);
1096
1097         get_cpu_cap(c);
1098
1099         if (c->cpuid_level >= 0x00000001) {
1100                 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
1101 #ifdef CONFIG_X86_32
1102 # ifdef CONFIG_SMP
1103                 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1104 # else
1105                 c->apicid = c->initial_apicid;
1106 # endif
1107 #endif
1108                 c->phys_proc_id = c->initial_apicid;
1109         }
1110
1111         get_model_name(c); /* Default name */
1112
1113         detect_nopl(c);
1114 }
1115
1116 static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1117 {
1118         /*
1119          * The heavy lifting of max_rmid and cache_occ_scale are handled
1120          * in get_cpu_cap().  Here we just set the max_rmid for the boot_cpu
1121          * in case CQM bits really aren't there in this CPU.
1122          */
1123         if (c != &boot_cpu_data) {
1124                 boot_cpu_data.x86_cache_max_rmid =
1125                         min(boot_cpu_data.x86_cache_max_rmid,
1126                             c->x86_cache_max_rmid);
1127         }
1128 }
1129
1130 /*
1131  * This does the hard work of actually picking apart the CPU stuff...
1132  */
1133 static void identify_cpu(struct cpuinfo_x86 *c)
1134 {
1135         int i;
1136
1137         c->loops_per_jiffy = loops_per_jiffy;
1138         c->x86_cache_size = 0;
1139         c->x86_vendor = X86_VENDOR_UNKNOWN;
1140         c->x86_model = c->x86_mask = 0; /* So far unknown... */
1141         c->x86_vendor_id[0] = '\0'; /* Unset */
1142         c->x86_model_id[0] = '\0';  /* Unset */
1143         c->x86_max_cores = 1;
1144         c->x86_coreid_bits = 0;
1145 #ifdef CONFIG_X86_64
1146         c->x86_clflush_size = 64;
1147         c->x86_phys_bits = 36;
1148         c->x86_virt_bits = 48;
1149 #else
1150         c->cpuid_level = -1;    /* CPUID not detected */
1151         c->x86_clflush_size = 32;
1152         c->x86_phys_bits = 32;
1153         c->x86_virt_bits = 32;
1154 #endif
1155         c->x86_cache_alignment = c->x86_clflush_size;
1156         memset(&c->x86_capability, 0, sizeof c->x86_capability);
1157
1158         generic_identify(c);
1159
1160         if (this_cpu->c_identify)
1161                 this_cpu->c_identify(c);
1162
1163         /* Clear/Set all flags overridden by options, after probe */
1164         apply_forced_caps(c);
1165
1166 #ifdef CONFIG_X86_64
1167         c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1168 #endif
1169
1170         /*
1171          * Vendor-specific initialization.  In this section we
1172          * canonicalize the feature flags, meaning if there are
1173          * features a certain CPU supports which CPUID doesn't
1174          * tell us, CPUID claiming incorrect flags, or other bugs,
1175          * we handle them here.
1176          *
1177          * At the end of this section, c->x86_capability better
1178          * indicate the features this CPU genuinely supports!
1179          */
1180         if (this_cpu->c_init)
1181                 this_cpu->c_init(c);
1182
1183         /* Disable the PN if appropriate */
1184         squash_the_stupid_serial_number(c);
1185
1186         /* Set up SMEP/SMAP */
1187         setup_smep(c);
1188         setup_smap(c);
1189
1190         /* Set up PCID */
1191         setup_pcid(c);
1192
1193         /*
1194          * The vendor-specific functions might have changed features.
1195          * Now we do "generic changes."
1196          */
1197
1198         /* Filter out anything that depends on CPUID levels we don't have */
1199         filter_cpuid_features(c, true);
1200
1201         /* If the model name is still unset, do table lookup. */
1202         if (!c->x86_model_id[0]) {
1203                 const char *p;
1204                 p = table_lookup_model(c);
1205                 if (p)
1206                         strcpy(c->x86_model_id, p);
1207                 else
1208                         /* Last resort... */
1209                         sprintf(c->x86_model_id, "%02x/%02x",
1210                                 c->x86, c->x86_model);
1211         }
1212
1213 #ifdef CONFIG_X86_64
1214         detect_ht(c);
1215 #endif
1216
1217         init_hypervisor(c);
1218         x86_init_rdrand(c);
1219         x86_init_cache_qos(c);
1220
1221         /*
1222          * Clear/Set all flags overriden by options, need do it
1223          * before following smp all cpus cap AND.
1224          */
1225         apply_forced_caps(c);
1226
1227         /*
1228          * On SMP, boot_cpu_data holds the common feature set between
1229          * all CPUs; so make sure that we indicate which features are
1230          * common between the CPUs.  The first time this routine gets
1231          * executed, c == &boot_cpu_data.
1232          */
1233         if (c != &boot_cpu_data) {
1234                 /* AND the already accumulated flags with these */
1235                 for (i = 0; i < NCAPINTS; i++)
1236                         boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
1237
1238                 /* OR, i.e. replicate the bug flags */
1239                 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1240                         c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1241         }
1242
1243         /* Init Machine Check Exception if available. */
1244         mcheck_cpu_init(c);
1245
1246         select_idle_routine(c);
1247
1248 #ifdef CONFIG_NUMA
1249         numa_add_cpu(smp_processor_id());
1250 #endif
1251 }
1252
1253 /*
1254  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1255  * on 32-bit kernels:
1256  */
1257 #ifdef CONFIG_X86_32
1258 void enable_sep_cpu(void)
1259 {
1260         struct tss_struct *tss;
1261         int cpu;
1262
1263         cpu = get_cpu();
1264         tss = &per_cpu(cpu_tss, cpu);
1265
1266         if (!boot_cpu_has(X86_FEATURE_SEP))
1267                 goto out;
1268
1269         /*
1270          * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1271          * see the big comment in struct x86_hw_tss's definition.
1272          */
1273
1274         tss->x86_tss.ss1 = __KERNEL_CS;
1275         wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
1276
1277         wrmsr(MSR_IA32_SYSENTER_ESP,
1278               (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
1279               0);
1280
1281         wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
1282
1283 out:
1284         put_cpu();
1285 }
1286 #endif
1287
1288 void __init identify_boot_cpu(void)
1289 {
1290         identify_cpu(&boot_cpu_data);
1291         init_amd_e400_c1e_mask();
1292 #ifdef CONFIG_X86_32
1293         sysenter_setup();
1294         enable_sep_cpu();
1295 #endif
1296         cpu_detect_tlb(&boot_cpu_data);
1297 }
1298
1299 void identify_secondary_cpu(struct cpuinfo_x86 *c)
1300 {
1301         BUG_ON(c == &boot_cpu_data);
1302         identify_cpu(c);
1303 #ifdef CONFIG_X86_32
1304         enable_sep_cpu();
1305 #endif
1306         mtrr_ap_init();
1307         x86_spec_ctrl_setup_ap();
1308 }
1309
1310 struct msr_range {
1311         unsigned        min;
1312         unsigned        max;
1313 };
1314
1315 static const struct msr_range msr_range_array[] = {
1316         { 0x00000000, 0x00000418},
1317         { 0xc0000000, 0xc000040b},
1318         { 0xc0010000, 0xc0010142},
1319         { 0xc0011000, 0xc001103b},
1320 };
1321
1322 static void __print_cpu_msr(void)
1323 {
1324         unsigned index_min, index_max;
1325         unsigned index;
1326         u64 val;
1327         int i;
1328
1329         for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1330                 index_min = msr_range_array[i].min;
1331                 index_max = msr_range_array[i].max;
1332
1333                 for (index = index_min; index < index_max; index++) {
1334                         if (rdmsrl_safe(index, &val))
1335                                 continue;
1336                         printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1337                 }
1338         }
1339 }
1340
1341 static int show_msr;
1342
1343 static __init int setup_show_msr(char *arg)
1344 {
1345         int num;
1346
1347         get_option(&arg, &num);
1348
1349         if (num > 0)
1350                 show_msr = num;
1351         return 1;
1352 }
1353 __setup("show_msr=", setup_show_msr);
1354
1355 static __init int setup_noclflush(char *arg)
1356 {
1357         setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1358         setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1359         return 1;
1360 }
1361 __setup("noclflush", setup_noclflush);
1362
1363 void print_cpu_info(struct cpuinfo_x86 *c)
1364 {
1365         const char *vendor = NULL;
1366
1367         if (c->x86_vendor < X86_VENDOR_NUM) {
1368                 vendor = this_cpu->c_vendor;
1369         } else {
1370                 if (c->cpuid_level >= 0)
1371                         vendor = c->x86_vendor_id;
1372         }
1373
1374         if (vendor && !strstr(c->x86_model_id, vendor))
1375                 printk(KERN_CONT "%s ", vendor);
1376
1377         if (c->x86_model_id[0])
1378                 printk(KERN_CONT "%s", c->x86_model_id);
1379         else
1380                 printk(KERN_CONT "%d86", c->x86);
1381
1382         printk(KERN_CONT " (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
1383
1384         if (c->x86_mask || c->cpuid_level >= 0)
1385                 printk(KERN_CONT ", stepping: 0x%x)\n", c->x86_mask);
1386         else
1387                 printk(KERN_CONT ")\n");
1388
1389         print_cpu_msr(c);
1390 }
1391
1392 void print_cpu_msr(struct cpuinfo_x86 *c)
1393 {
1394         if (c->cpu_index < show_msr)
1395                 __print_cpu_msr();
1396 }
1397
1398 static __init int setup_disablecpuid(char *arg)
1399 {
1400         int bit;
1401
1402         if (get_option(&arg, &bit) && bit >= 0 && bit < NCAPINTS * 32)
1403                 setup_clear_cpu_cap(bit);
1404         else
1405                 return 0;
1406
1407         return 1;
1408 }
1409 __setup("clearcpuid=", setup_disablecpuid);
1410
1411 #ifdef CONFIG_X86_64
1412 struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
1413 struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1414                                     (unsigned long) debug_idt_table };
1415
1416 DEFINE_PER_CPU_FIRST(union irq_stack_union,
1417                      irq_stack_union) __aligned(PAGE_SIZE) __visible;
1418
1419 /*
1420  * The following percpu variables are hot.  Align current_task to
1421  * cacheline size such that they fall in the same cacheline.
1422  */
1423 DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1424         &init_task;
1425 EXPORT_PER_CPU_SYMBOL(current_task);
1426
1427 DEFINE_PER_CPU(char *, irq_stack_ptr) =
1428         init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1429
1430 DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1431
1432 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1433 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1434
1435 /*
1436  * Special IST stacks which the CPU switches to when it calls
1437  * an IST-marked descriptor entry. Up to 7 stacks (hardware
1438  * limit), all of them are 4K, except the debug stack which
1439  * is 8K.
1440  */
1441 static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1442           [0 ... N_EXCEPTION_STACKS - 1]        = EXCEPTION_STKSZ,
1443           [DEBUG_STACK - 1]                     = DEBUG_STKSZ
1444 };
1445
1446 DEFINE_PER_CPU_PAGE_ALIGNED_USER_MAPPED(char, exception_stacks
1447         [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
1448
1449 /* May not be marked __init: used by software suspend */
1450 void syscall_init(void)
1451 {
1452         /*
1453          * LSTAR and STAR live in a bit strange symbiosis.
1454          * They both write to the same internal register. STAR allows to
1455          * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1456          */
1457         wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32);
1458         wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
1459
1460 #ifdef CONFIG_IA32_EMULATION
1461         wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
1462         /*
1463          * This only works on Intel CPUs.
1464          * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1465          * This does not cause SYSENTER to jump to the wrong location, because
1466          * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1467          */
1468         wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1469         wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1470         wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1471 #else
1472         wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
1473         wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1474         wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1475         wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1476 #endif
1477
1478         /* Flags to clear on syscall */
1479         wrmsrl(MSR_SYSCALL_MASK,
1480                X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
1481                X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1482 }
1483
1484 /*
1485  * Copies of the original ist values from the tss are only accessed during
1486  * debugging, no special alignment required.
1487  */
1488 DEFINE_PER_CPU(struct orig_ist, orig_ist);
1489
1490 static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
1491 DEFINE_PER_CPU(int, debug_stack_usage);
1492
1493 int is_debug_stack(unsigned long addr)
1494 {
1495         return __this_cpu_read(debug_stack_usage) ||
1496                 (addr <= __this_cpu_read(debug_stack_addr) &&
1497                  addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1498 }
1499 NOKPROBE_SYMBOL(is_debug_stack);
1500
1501 DEFINE_PER_CPU(u32, debug_idt_ctr);
1502
1503 void debug_stack_set_zero(void)
1504 {
1505         this_cpu_inc(debug_idt_ctr);
1506         load_current_idt();
1507 }
1508 NOKPROBE_SYMBOL(debug_stack_set_zero);
1509
1510 void debug_stack_reset(void)
1511 {
1512         if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1513                 return;
1514         if (this_cpu_dec_return(debug_idt_ctr) == 0)
1515                 load_current_idt();
1516 }
1517 NOKPROBE_SYMBOL(debug_stack_reset);
1518
1519 #else   /* CONFIG_X86_64 */
1520
1521 DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1522 EXPORT_PER_CPU_SYMBOL(current_task);
1523 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1524 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1525
1526 /*
1527  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1528  * the top of the kernel stack.  Use an extra percpu variable to track the
1529  * top of the kernel stack directly.
1530  */
1531 DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1532         (unsigned long)&init_thread_union + THREAD_SIZE;
1533 EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1534
1535 #ifdef CONFIG_CC_STACKPROTECTOR
1536 DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
1537 #endif
1538
1539 #endif  /* CONFIG_X86_64 */
1540
1541 /*
1542  * Clear all 6 debug registers:
1543  */
1544 static void clear_all_debug_regs(void)
1545 {
1546         int i;
1547
1548         for (i = 0; i < 8; i++) {
1549                 /* Ignore db4, db5 */
1550                 if ((i == 4) || (i == 5))
1551                         continue;
1552
1553                 set_debugreg(0, i);
1554         }
1555 }
1556
1557 #ifdef CONFIG_KGDB
1558 /*
1559  * Restore debug regs if using kgdbwait and you have a kernel debugger
1560  * connection established.
1561  */
1562 static void dbg_restore_debug_regs(void)
1563 {
1564         if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1565                 arch_kgdb_ops.correct_hw_break();
1566 }
1567 #else /* ! CONFIG_KGDB */
1568 #define dbg_restore_debug_regs()
1569 #endif /* ! CONFIG_KGDB */
1570
1571 static void wait_for_master_cpu(int cpu)
1572 {
1573 #ifdef CONFIG_SMP
1574         /*
1575          * wait for ACK from master CPU before continuing
1576          * with AP initialization
1577          */
1578         WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1579         while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1580                 cpu_relax();
1581 #endif
1582 }
1583
1584 /*
1585  * cpu_init() initializes state that is per-CPU. Some data is already
1586  * initialized (naturally) in the bootstrap process, such as the GDT
1587  * and IDT. We reload them nevertheless, this function acts as a
1588  * 'CPU state barrier', nothing should get across.
1589  * A lot of state is already set up in PDA init for 64 bit
1590  */
1591 #ifdef CONFIG_X86_64
1592
1593 void cpu_init(void)
1594 {
1595         struct orig_ist *oist;
1596         struct task_struct *me;
1597         struct tss_struct *t;
1598         unsigned long v;
1599         int cpu = stack_smp_processor_id();
1600         int i;
1601
1602         wait_for_master_cpu(cpu);
1603
1604         /*
1605          * Initialize the CR4 shadow before doing anything that could
1606          * try to read it.
1607          */
1608         cr4_init_shadow();
1609         if (!kaiser_enabled) {
1610                 /*
1611                  * secondary_startup_64() deferred setting PGE in cr4:
1612                  * probe_page_size_mask() sets it on the boot cpu,
1613                  * but it needs to be set on each secondary cpu.
1614                  */
1615                 cr4_set_bits(X86_CR4_PGE);
1616         }
1617
1618         /*
1619          * Load microcode on this cpu if a valid microcode is available.
1620          * This is early microcode loading procedure.
1621          */
1622         load_ucode_ap();
1623
1624         t = &per_cpu(cpu_tss, cpu);
1625         oist = &per_cpu(orig_ist, cpu);
1626
1627 #ifdef CONFIG_NUMA
1628         if (this_cpu_read(numa_node) == 0 &&
1629             early_cpu_to_node(cpu) != NUMA_NO_NODE)
1630                 set_numa_node(early_cpu_to_node(cpu));
1631 #endif
1632
1633         me = current;
1634
1635         pr_debug("Initializing CPU#%d\n", cpu);
1636
1637         cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1638
1639         /*
1640          * Initialize the per-CPU GDT with the boot GDT,
1641          * and set up the GDT descriptor:
1642          */
1643
1644         switch_to_new_gdt(cpu);
1645         loadsegment(fs, 0);
1646
1647         load_current_idt();
1648
1649         memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1650         syscall_init();
1651
1652         wrmsrl(MSR_FS_BASE, 0);
1653         wrmsrl(MSR_KERNEL_GS_BASE, 0);
1654         barrier();
1655
1656         x86_configure_nx();
1657         x2apic_setup();
1658
1659         /*
1660          * set up and load the per-CPU TSS
1661          */
1662         if (!oist->ist[0]) {
1663                 char *estacks = per_cpu(exception_stacks, cpu);
1664
1665                 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
1666                         estacks += exception_stack_sizes[v];
1667                         oist->ist[v] = t->x86_tss.ist[v] =
1668                                         (unsigned long)estacks;
1669                         if (v == DEBUG_STACK-1)
1670                                 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
1671                 }
1672         }
1673
1674         t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1675
1676         /*
1677          * <= is required because the CPU will access up to
1678          * 8 bits beyond the end of the IO permission bitmap.
1679          */
1680         for (i = 0; i <= IO_BITMAP_LONGS; i++)
1681                 t->io_bitmap[i] = ~0UL;
1682
1683         atomic_inc(&init_mm.mm_count);
1684         me->active_mm = &init_mm;
1685         BUG_ON(me->mm);
1686         enter_lazy_tlb(&init_mm, me);
1687
1688         load_sp0(t, &current->thread);
1689         set_tss_desc(cpu, t);
1690         load_TR_desc();
1691         load_mm_ldt(&init_mm);
1692
1693         clear_all_debug_regs();
1694         dbg_restore_debug_regs();
1695
1696         fpu__init_cpu();
1697
1698         if (is_uv_system())
1699                 uv_cpu_init();
1700 }
1701
1702 #else
1703
1704 void cpu_init(void)
1705 {
1706         int cpu = smp_processor_id();
1707         struct task_struct *curr = current;
1708         struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1709         struct thread_struct *thread = &curr->thread;
1710
1711         wait_for_master_cpu(cpu);
1712
1713         /*
1714          * Initialize the CR4 shadow before doing anything that could
1715          * try to read it.
1716          */
1717         cr4_init_shadow();
1718
1719         show_ucode_info_early();
1720
1721         printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1722
1723         if (cpu_feature_enabled(X86_FEATURE_VME) ||
1724             cpu_has_tsc ||
1725             boot_cpu_has(X86_FEATURE_DE))
1726                 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1727
1728         load_current_idt();
1729         switch_to_new_gdt(cpu);
1730
1731         /*
1732          * Set up and load the per-CPU TSS and LDT
1733          */
1734         atomic_inc(&init_mm.mm_count);
1735         curr->active_mm = &init_mm;
1736         BUG_ON(curr->mm);
1737         enter_lazy_tlb(&init_mm, curr);
1738
1739         load_sp0(t, thread);
1740         set_tss_desc(cpu, t);
1741         load_TR_desc();
1742         load_mm_ldt(&init_mm);
1743
1744         t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1745
1746 #ifdef CONFIG_DOUBLEFAULT
1747         /* Set up doublefault TSS pointer in the GDT */
1748         __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1749 #endif
1750
1751         clear_all_debug_regs();
1752         dbg_restore_debug_regs();
1753
1754         fpu__init_cpu();
1755 }
1756 #endif
1757
1758 static void bsp_resume(void)
1759 {
1760         if (this_cpu->c_bsp_resume)
1761                 this_cpu->c_bsp_resume(&boot_cpu_data);
1762 }
1763
1764 static struct syscore_ops cpu_syscore_ops = {
1765         .resume         = bsp_resume,
1766 };
1767
1768 static int __init init_cpu_syscore(void)
1769 {
1770         register_syscore_ops(&cpu_syscore_ops);
1771         return 0;
1772 }
1773 core_initcall(init_cpu_syscore);