1 #include <linux/bootmem.h>
2 #include <linux/linkage.h>
3 #include <linux/bitops.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/percpu.h>
7 #include <linux/string.h>
8 #include <linux/delay.h>
9 #include <linux/sched.h>
10 #include <linux/init.h>
11 #include <linux/kprobes.h>
12 #include <linux/kgdb.h>
13 #include <linux/smp.h>
16 #include <asm/stackprotector.h>
17 #include <asm/perf_event.h>
18 #include <asm/mmu_context.h>
19 #include <asm/archrandom.h>
20 #include <asm/hypervisor.h>
21 #include <asm/processor.h>
22 #include <asm/tlbflush.h>
23 #include <asm/debugreg.h>
24 #include <asm/sections.h>
25 #include <asm/vsyscall.h>
26 #include <linux/topology.h>
27 #include <linux/cpumask.h>
28 #include <asm/pgtable.h>
29 #include <linux/atomic.h>
30 #include <asm/proto.h>
31 #include <asm/setup.h>
34 #include <asm/fpu/internal.h>
36 #include <linux/numa.h>
42 #include <asm/microcode.h>
43 #include <asm/microcode_intel.h>
45 #ifdef CONFIG_X86_LOCAL_APIC
46 #include <asm/uv/uv.h>
51 /* all of these masks are initialized in setup_cpu_local_masks() */
52 cpumask_var_t cpu_initialized_mask;
53 cpumask_var_t cpu_callout_mask;
54 cpumask_var_t cpu_callin_mask;
56 /* representing cpus for which sibling maps can be computed */
57 cpumask_var_t cpu_sibling_setup_mask;
59 /* correctly size the local cpu masks */
60 void __init setup_cpu_local_masks(void)
62 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
63 alloc_bootmem_cpumask_var(&cpu_callin_mask);
64 alloc_bootmem_cpumask_var(&cpu_callout_mask);
65 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
68 static void default_init(struct cpuinfo_x86 *c)
71 cpu_detect_cache_sizes(c);
73 /* Not much we can do here... */
74 /* Check if at least it has cpuid */
75 if (c->cpuid_level == -1) {
76 /* No cpuid. It must be an ancient CPU */
78 strcpy(c->x86_model_id, "486");
80 strcpy(c->x86_model_id, "386");
85 static const struct cpu_dev default_cpu = {
86 .c_init = default_init,
87 .c_vendor = "Unknown",
88 .c_x86_vendor = X86_VENDOR_UNKNOWN,
91 static const struct cpu_dev *this_cpu = &default_cpu;
93 DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
96 * We need valid kernel segments for data and code in long mode too
97 * IRET will check the segment types kkeil 2000/10/28
98 * Also sysret mandates a special GDT layout
100 * TLS descriptors are currently at a different place compared to i386.
101 * Hopefully nobody expects them at a fixed place (Wine?)
103 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
104 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
105 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
106 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
107 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
108 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
110 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
111 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
112 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
113 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
115 * Segments used for calling PnP BIOS have byte granularity.
116 * They code segments and data segments have fixed 64k limits,
117 * the transfer segment sizes are set at run time.
120 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
122 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
124 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
126 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
128 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
130 * The APM segments have byte granularity and their bases
131 * are set at run time. All have 64k limits.
134 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
136 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
138 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
140 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
141 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
142 GDT_STACK_CANARY_INIT
145 EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
148 static int cachesize_override = -1;
149 static int disable_x86_serial_nr = 1;
151 static int __init cachesize_setup(char *str)
153 get_option(&str, &cachesize_override);
156 __setup("cachesize=", cachesize_setup);
158 static int __init x86_sep_setup(char *s)
160 setup_clear_cpu_cap(X86_FEATURE_SEP);
163 __setup("nosep", x86_sep_setup);
165 /* Standard macro to see if a specific flag is changeable */
166 static inline int flag_is_changeable_p(u32 flag)
171 * Cyrix and IDT cpus allow disabling of CPUID
172 * so the code below may return different results
173 * when it is executed before and after enabling
174 * the CPUID. Add "volatile" to not allow gcc to
175 * optimize the subsequent calls to this function.
177 asm volatile ("pushfl \n\t"
188 : "=&r" (f1), "=&r" (f2)
191 return ((f1^f2) & flag) != 0;
194 /* Probe for the CPUID instruction */
195 int have_cpuid_p(void)
197 return flag_is_changeable_p(X86_EFLAGS_ID);
200 static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
202 unsigned long lo, hi;
204 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
207 /* Disable processor serial number: */
209 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
211 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
213 printk(KERN_NOTICE "CPU serial number disabled.\n");
214 clear_cpu_cap(c, X86_FEATURE_PN);
216 /* Disabling the serial number may affect the cpuid level */
217 c->cpuid_level = cpuid_eax(0);
220 static int __init x86_serial_nr_setup(char *s)
222 disable_x86_serial_nr = 0;
225 __setup("serialnumber", x86_serial_nr_setup);
227 static inline int flag_is_changeable_p(u32 flag)
231 static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
236 static __init int setup_disable_smep(char *arg)
238 setup_clear_cpu_cap(X86_FEATURE_SMEP);
241 __setup("nosmep", setup_disable_smep);
243 static __always_inline void setup_smep(struct cpuinfo_x86 *c)
245 if (cpu_has(c, X86_FEATURE_SMEP))
246 cr4_set_bits(X86_CR4_SMEP);
249 static __init int setup_disable_smap(char *arg)
251 setup_clear_cpu_cap(X86_FEATURE_SMAP);
254 __setup("nosmap", setup_disable_smap);
256 static __always_inline void setup_smap(struct cpuinfo_x86 *c)
258 unsigned long eflags;
260 /* This should have been cleared long ago */
261 raw_local_save_flags(eflags);
262 BUG_ON(eflags & X86_EFLAGS_AC);
264 if (cpu_has(c, X86_FEATURE_SMAP)) {
265 #ifdef CONFIG_X86_SMAP
266 cr4_set_bits(X86_CR4_SMAP);
268 cr4_clear_bits(X86_CR4_SMAP);
274 * Some CPU features depend on higher CPUID levels, which may not always
275 * be available due to CPUID level capping or broken virtualization
276 * software. Add those features to this table to auto-disable them.
278 struct cpuid_dependent_feature {
283 static const struct cpuid_dependent_feature
284 cpuid_dependent_features[] = {
285 { X86_FEATURE_MWAIT, 0x00000005 },
286 { X86_FEATURE_DCA, 0x00000009 },
287 { X86_FEATURE_XSAVE, 0x0000000d },
291 static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
293 const struct cpuid_dependent_feature *df;
295 for (df = cpuid_dependent_features; df->feature; df++) {
297 if (!cpu_has(c, df->feature))
300 * Note: cpuid_level is set to -1 if unavailable, but
301 * extended_extended_level is set to 0 if unavailable
302 * and the legitimate extended levels are all negative
303 * when signed; hence the weird messing around with
306 if (!((s32)df->level < 0 ?
307 (u32)df->level > (u32)c->extended_cpuid_level :
308 (s32)df->level > (s32)c->cpuid_level))
311 clear_cpu_cap(c, df->feature);
316 "CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
317 x86_cap_flag(df->feature), df->level);
322 * Naming convention should be: <Name> [(<Codename>)]
323 * This table only is used unless init_<vendor>() below doesn't set it;
324 * in particular, if CPUID levels 0x80000002..4 are supported, this
328 /* Look up CPU names by table lookup. */
329 static const char *table_lookup_model(struct cpuinfo_x86 *c)
332 const struct legacy_cpu_model_info *info;
334 if (c->x86_model >= 16)
335 return NULL; /* Range check */
340 info = this_cpu->legacy_models;
342 while (info->family) {
343 if (info->family == c->x86)
344 return info->model_names[c->x86_model];
348 return NULL; /* Not found */
351 __u32 cpu_caps_cleared[NCAPINTS];
352 __u32 cpu_caps_set[NCAPINTS];
354 void load_percpu_segment(int cpu)
357 loadsegment(fs, __KERNEL_PERCPU);
360 wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
362 load_stack_canary_segment();
366 * Current gdt points %fs at the "master" per-cpu area: after this,
367 * it's on the real one.
369 void switch_to_new_gdt(int cpu)
371 struct desc_ptr gdt_descr;
373 gdt_descr.address = (long)get_cpu_gdt_table(cpu);
374 gdt_descr.size = GDT_SIZE - 1;
375 load_gdt(&gdt_descr);
376 /* Reload the per-cpu base */
378 load_percpu_segment(cpu);
381 static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
383 static void get_model_name(struct cpuinfo_x86 *c)
388 if (c->extended_cpuid_level < 0x80000004)
391 v = (unsigned int *)c->x86_model_id;
392 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
393 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
394 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
395 c->x86_model_id[48] = 0;
398 * Intel chips right-justify this string for some dumb reason;
399 * undo that brain damage:
401 p = q = &c->x86_model_id[0];
407 while (q <= &c->x86_model_id[48])
408 *q++ = '\0'; /* Zero-pad the rest */
412 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
414 unsigned int n, dummy, ebx, ecx, edx, l2size;
416 n = c->extended_cpuid_level;
418 if (n >= 0x80000005) {
419 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
420 c->x86_cache_size = (ecx>>24) + (edx>>24);
422 /* On K8 L1 TLB is inclusive, so don't count it */
427 if (n < 0x80000006) /* Some chips just has a large L1. */
430 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
434 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
436 /* do processor-specific cache resizing */
437 if (this_cpu->legacy_cache_size)
438 l2size = this_cpu->legacy_cache_size(c, l2size);
440 /* Allow user to override all this if necessary. */
441 if (cachesize_override != -1)
442 l2size = cachesize_override;
445 return; /* Again, no L2 cache is possible */
448 c->x86_cache_size = l2size;
451 u16 __read_mostly tlb_lli_4k[NR_INFO];
452 u16 __read_mostly tlb_lli_2m[NR_INFO];
453 u16 __read_mostly tlb_lli_4m[NR_INFO];
454 u16 __read_mostly tlb_lld_4k[NR_INFO];
455 u16 __read_mostly tlb_lld_2m[NR_INFO];
456 u16 __read_mostly tlb_lld_4m[NR_INFO];
457 u16 __read_mostly tlb_lld_1g[NR_INFO];
459 static void cpu_detect_tlb(struct cpuinfo_x86 *c)
461 if (this_cpu->c_detect_tlb)
462 this_cpu->c_detect_tlb(c);
464 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
465 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
466 tlb_lli_4m[ENTRIES]);
468 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
469 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
470 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
473 void detect_ht(struct cpuinfo_x86 *c)
476 u32 eax, ebx, ecx, edx;
477 int index_msb, core_bits;
480 if (!cpu_has(c, X86_FEATURE_HT))
483 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
486 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
489 cpuid(1, &eax, &ebx, &ecx, &edx);
491 smp_num_siblings = (ebx & 0xff0000) >> 16;
493 if (smp_num_siblings == 1) {
494 printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
498 if (smp_num_siblings <= 1)
501 index_msb = get_count_order(smp_num_siblings);
502 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
504 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
506 index_msb = get_count_order(smp_num_siblings);
508 core_bits = get_count_order(c->x86_max_cores);
510 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
511 ((1 << core_bits) - 1);
514 if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
515 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
517 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
524 static void get_cpu_vendor(struct cpuinfo_x86 *c)
526 char *v = c->x86_vendor_id;
529 for (i = 0; i < X86_VENDOR_NUM; i++) {
533 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
534 (cpu_devs[i]->c_ident[1] &&
535 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
537 this_cpu = cpu_devs[i];
538 c->x86_vendor = this_cpu->c_x86_vendor;
544 "CPU: vendor_id '%s' unknown, using generic init.\n" \
545 "CPU: Your system may be unstable.\n", v);
547 c->x86_vendor = X86_VENDOR_UNKNOWN;
548 this_cpu = &default_cpu;
551 void cpu_detect(struct cpuinfo_x86 *c)
553 /* Get vendor name */
554 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
555 (unsigned int *)&c->x86_vendor_id[0],
556 (unsigned int *)&c->x86_vendor_id[8],
557 (unsigned int *)&c->x86_vendor_id[4]);
560 /* Intel-defined flags: level 0x00000001 */
561 if (c->cpuid_level >= 0x00000001) {
562 u32 junk, tfms, cap0, misc;
564 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
565 c->x86 = (tfms >> 8) & 0xf;
566 c->x86_model = (tfms >> 4) & 0xf;
567 c->x86_mask = tfms & 0xf;
570 c->x86 += (tfms >> 20) & 0xff;
572 c->x86_model += ((tfms >> 16) & 0xf) << 4;
574 if (cap0 & (1<<19)) {
575 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
576 c->x86_cache_alignment = c->x86_clflush_size;
581 void get_cpu_cap(struct cpuinfo_x86 *c)
586 /* Intel-defined flags: level 0x00000001 */
587 if (c->cpuid_level >= 0x00000001) {
588 u32 capability, excap;
590 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
591 c->x86_capability[0] = capability;
592 c->x86_capability[4] = excap;
595 /* Additional Intel-defined flags: level 0x00000007 */
596 if (c->cpuid_level >= 0x00000007) {
597 u32 eax, ebx, ecx, edx;
599 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
601 c->x86_capability[9] = ebx;
604 /* Extended state features: level 0x0000000d */
605 if (c->cpuid_level >= 0x0000000d) {
606 u32 eax, ebx, ecx, edx;
608 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
610 c->x86_capability[10] = eax;
613 /* Additional Intel-defined flags: level 0x0000000F */
614 if (c->cpuid_level >= 0x0000000F) {
615 u32 eax, ebx, ecx, edx;
617 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
618 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
619 c->x86_capability[11] = edx;
620 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
621 /* will be overridden if occupancy monitoring exists */
622 c->x86_cache_max_rmid = ebx;
624 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
625 cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
626 c->x86_capability[12] = edx;
627 if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
628 c->x86_cache_max_rmid = ecx;
629 c->x86_cache_occ_scale = ebx;
632 c->x86_cache_max_rmid = -1;
633 c->x86_cache_occ_scale = -1;
637 /* AMD-defined flags: level 0x80000001 */
638 xlvl = cpuid_eax(0x80000000);
639 c->extended_cpuid_level = xlvl;
641 if ((xlvl & 0xffff0000) == 0x80000000) {
642 if (xlvl >= 0x80000001) {
643 c->x86_capability[1] = cpuid_edx(0x80000001);
644 c->x86_capability[6] = cpuid_ecx(0x80000001);
648 if (c->extended_cpuid_level >= 0x80000008) {
649 u32 eax = cpuid_eax(0x80000008);
651 c->x86_virt_bits = (eax >> 8) & 0xff;
652 c->x86_phys_bits = eax & 0xff;
655 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
656 c->x86_phys_bits = 36;
659 if (c->extended_cpuid_level >= 0x80000007)
660 c->x86_power = cpuid_edx(0x80000007);
662 init_scattered_cpuid_features(c);
665 static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
671 * First of all, decide if this is a 486 or higher
672 * It's a 486 if we can modify the AC flag
674 if (flag_is_changeable_p(X86_EFLAGS_AC))
679 for (i = 0; i < X86_VENDOR_NUM; i++)
680 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
681 c->x86_vendor_id[0] = 0;
682 cpu_devs[i]->c_identify(c);
683 if (c->x86_vendor_id[0]) {
692 * Do minimum CPU detection early.
693 * Fields really needed: vendor, cpuid_level, family, model, mask,
695 * The others are not touched to avoid unwanted side effects.
697 * WARNING: this function is only called on the BP. Don't add code here
698 * that is supposed to run on all CPUs.
700 static void __init early_identify_cpu(struct cpuinfo_x86 *c)
703 c->x86_clflush_size = 64;
704 c->x86_phys_bits = 36;
705 c->x86_virt_bits = 48;
707 c->x86_clflush_size = 32;
708 c->x86_phys_bits = 32;
709 c->x86_virt_bits = 32;
711 c->x86_cache_alignment = c->x86_clflush_size;
713 memset(&c->x86_capability, 0, sizeof c->x86_capability);
714 c->extended_cpuid_level = 0;
717 identify_cpu_without_cpuid(c);
719 /* cyrix could have cpuid enabled via c_identify()*/
728 if (this_cpu->c_early_init)
729 this_cpu->c_early_init(c);
732 filter_cpuid_features(c, false);
734 if (this_cpu->c_bsp_init)
735 this_cpu->c_bsp_init(c);
737 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
740 void __init early_cpu_init(void)
742 const struct cpu_dev *const *cdev;
745 #ifdef CONFIG_PROCESSOR_SELECT
746 printk(KERN_INFO "KERNEL supported cpus:\n");
749 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
750 const struct cpu_dev *cpudev = *cdev;
752 if (count >= X86_VENDOR_NUM)
754 cpu_devs[count] = cpudev;
757 #ifdef CONFIG_PROCESSOR_SELECT
761 for (j = 0; j < 2; j++) {
762 if (!cpudev->c_ident[j])
764 printk(KERN_INFO " %s %s\n", cpudev->c_vendor,
770 early_identify_cpu(&boot_cpu_data);
774 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
775 * unfortunately, that's not true in practice because of early VIA
776 * chips and (more importantly) broken virtualizers that are not easy
777 * to detect. In the latter case it doesn't even *fail* reliably, so
778 * probing for it doesn't even work. Disable it completely on 32-bit
779 * unless we can find a reliable way to detect all the broken cases.
780 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
782 static void detect_nopl(struct cpuinfo_x86 *c)
785 clear_cpu_cap(c, X86_FEATURE_NOPL);
787 set_cpu_cap(c, X86_FEATURE_NOPL);
791 static void generic_identify(struct cpuinfo_x86 *c)
793 c->extended_cpuid_level = 0;
796 identify_cpu_without_cpuid(c);
798 /* cyrix could have cpuid enabled via c_identify()*/
808 if (c->cpuid_level >= 0x00000001) {
809 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
811 # ifdef CONFIG_X86_HT
812 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
814 c->apicid = c->initial_apicid;
817 c->phys_proc_id = c->initial_apicid;
820 get_model_name(c); /* Default name */
825 static void x86_init_cache_qos(struct cpuinfo_x86 *c)
828 * The heavy lifting of max_rmid and cache_occ_scale are handled
829 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
830 * in case CQM bits really aren't there in this CPU.
832 if (c != &boot_cpu_data) {
833 boot_cpu_data.x86_cache_max_rmid =
834 min(boot_cpu_data.x86_cache_max_rmid,
835 c->x86_cache_max_rmid);
840 * This does the hard work of actually picking apart the CPU stuff...
842 static void identify_cpu(struct cpuinfo_x86 *c)
846 c->loops_per_jiffy = loops_per_jiffy;
847 c->x86_cache_size = -1;
848 c->x86_vendor = X86_VENDOR_UNKNOWN;
849 c->x86_model = c->x86_mask = 0; /* So far unknown... */
850 c->x86_vendor_id[0] = '\0'; /* Unset */
851 c->x86_model_id[0] = '\0'; /* Unset */
852 c->x86_max_cores = 1;
853 c->x86_coreid_bits = 0;
855 c->x86_clflush_size = 64;
856 c->x86_phys_bits = 36;
857 c->x86_virt_bits = 48;
859 c->cpuid_level = -1; /* CPUID not detected */
860 c->x86_clflush_size = 32;
861 c->x86_phys_bits = 32;
862 c->x86_virt_bits = 32;
864 c->x86_cache_alignment = c->x86_clflush_size;
865 memset(&c->x86_capability, 0, sizeof c->x86_capability);
869 if (this_cpu->c_identify)
870 this_cpu->c_identify(c);
872 /* Clear/Set all flags overriden by options, after probe */
873 for (i = 0; i < NCAPINTS; i++) {
874 c->x86_capability[i] &= ~cpu_caps_cleared[i];
875 c->x86_capability[i] |= cpu_caps_set[i];
879 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
883 * Vendor-specific initialization. In this section we
884 * canonicalize the feature flags, meaning if there are
885 * features a certain CPU supports which CPUID doesn't
886 * tell us, CPUID claiming incorrect flags, or other bugs,
887 * we handle them here.
889 * At the end of this section, c->x86_capability better
890 * indicate the features this CPU genuinely supports!
892 if (this_cpu->c_init)
895 /* Disable the PN if appropriate */
896 squash_the_stupid_serial_number(c);
898 /* Set up SMEP/SMAP */
903 * The vendor-specific functions might have changed features.
904 * Now we do "generic changes."
907 /* Filter out anything that depends on CPUID levels we don't have */
908 filter_cpuid_features(c, true);
910 /* If the model name is still unset, do table lookup. */
911 if (!c->x86_model_id[0]) {
913 p = table_lookup_model(c);
915 strcpy(c->x86_model_id, p);
918 sprintf(c->x86_model_id, "%02x/%02x",
919 c->x86, c->x86_model);
928 x86_init_cache_qos(c);
931 * Clear/Set all flags overriden by options, need do it
932 * before following smp all cpus cap AND.
934 for (i = 0; i < NCAPINTS; i++) {
935 c->x86_capability[i] &= ~cpu_caps_cleared[i];
936 c->x86_capability[i] |= cpu_caps_set[i];
940 * On SMP, boot_cpu_data holds the common feature set between
941 * all CPUs; so make sure that we indicate which features are
942 * common between the CPUs. The first time this routine gets
943 * executed, c == &boot_cpu_data.
945 if (c != &boot_cpu_data) {
946 /* AND the already accumulated flags with these */
947 for (i = 0; i < NCAPINTS; i++)
948 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
950 /* OR, i.e. replicate the bug flags */
951 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
952 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
955 /* Init Machine Check Exception if available. */
958 select_idle_routine(c);
961 numa_add_cpu(smp_processor_id());
966 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
970 void enable_sep_cpu(void)
972 struct tss_struct *tss;
976 tss = &per_cpu(cpu_tss, cpu);
978 if (!boot_cpu_has(X86_FEATURE_SEP))
982 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
983 * see the big comment in struct x86_hw_tss's definition.
986 tss->x86_tss.ss1 = __KERNEL_CS;
987 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
989 wrmsr(MSR_IA32_SYSENTER_ESP,
990 (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
993 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)ia32_sysenter_target, 0);
1000 void __init identify_boot_cpu(void)
1002 identify_cpu(&boot_cpu_data);
1003 init_amd_e400_c1e_mask();
1004 #ifdef CONFIG_X86_32
1008 cpu_detect_tlb(&boot_cpu_data);
1011 void identify_secondary_cpu(struct cpuinfo_x86 *c)
1013 BUG_ON(c == &boot_cpu_data);
1015 #ifdef CONFIG_X86_32
1026 static const struct msr_range msr_range_array[] = {
1027 { 0x00000000, 0x00000418},
1028 { 0xc0000000, 0xc000040b},
1029 { 0xc0010000, 0xc0010142},
1030 { 0xc0011000, 0xc001103b},
1033 static void __print_cpu_msr(void)
1035 unsigned index_min, index_max;
1040 for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1041 index_min = msr_range_array[i].min;
1042 index_max = msr_range_array[i].max;
1044 for (index = index_min; index < index_max; index++) {
1045 if (rdmsrl_safe(index, &val))
1047 printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1052 static int show_msr;
1054 static __init int setup_show_msr(char *arg)
1058 get_option(&arg, &num);
1064 __setup("show_msr=", setup_show_msr);
1066 static __init int setup_noclflush(char *arg)
1068 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1069 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1072 __setup("noclflush", setup_noclflush);
1074 void print_cpu_info(struct cpuinfo_x86 *c)
1076 const char *vendor = NULL;
1078 if (c->x86_vendor < X86_VENDOR_NUM) {
1079 vendor = this_cpu->c_vendor;
1081 if (c->cpuid_level >= 0)
1082 vendor = c->x86_vendor_id;
1085 if (vendor && !strstr(c->x86_model_id, vendor))
1086 printk(KERN_CONT "%s ", vendor);
1088 if (c->x86_model_id[0])
1089 printk(KERN_CONT "%s", strim(c->x86_model_id));
1091 printk(KERN_CONT "%d86", c->x86);
1093 printk(KERN_CONT " (fam: %02x, model: %02x", c->x86, c->x86_model);
1095 if (c->x86_mask || c->cpuid_level >= 0)
1096 printk(KERN_CONT ", stepping: %02x)\n", c->x86_mask);
1098 printk(KERN_CONT ")\n");
1103 void print_cpu_msr(struct cpuinfo_x86 *c)
1105 if (c->cpu_index < show_msr)
1109 static __init int setup_disablecpuid(char *arg)
1113 if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1114 setup_clear_cpu_cap(bit);
1120 __setup("clearcpuid=", setup_disablecpuid);
1122 DEFINE_PER_CPU(unsigned long, kernel_stack) =
1123 (unsigned long)&init_thread_union + THREAD_SIZE;
1124 EXPORT_PER_CPU_SYMBOL(kernel_stack);
1126 #ifdef CONFIG_X86_64
1127 struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
1128 struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1129 (unsigned long) debug_idt_table };
1131 DEFINE_PER_CPU_FIRST(union irq_stack_union,
1132 irq_stack_union) __aligned(PAGE_SIZE) __visible;
1135 * The following percpu variables are hot. Align current_task to
1136 * cacheline size such that they fall in the same cacheline.
1138 DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1140 EXPORT_PER_CPU_SYMBOL(current_task);
1142 DEFINE_PER_CPU(char *, irq_stack_ptr) =
1143 init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1145 DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1147 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1148 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1151 * Special IST stacks which the CPU switches to when it calls
1152 * an IST-marked descriptor entry. Up to 7 stacks (hardware
1153 * limit), all of them are 4K, except the debug stack which
1156 static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1157 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
1158 [DEBUG_STACK - 1] = DEBUG_STKSZ
1161 static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
1162 [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
1164 /* May not be marked __init: used by software suspend */
1165 void syscall_init(void)
1168 * LSTAR and STAR live in a bit strange symbiosis.
1169 * They both write to the same internal register. STAR allows to
1170 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1172 wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
1173 wrmsrl(MSR_LSTAR, system_call);
1175 #ifdef CONFIG_IA32_EMULATION
1176 wrmsrl(MSR_CSTAR, ia32_cstar_target);
1178 * This only works on Intel CPUs.
1179 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1180 * This does not cause SYSENTER to jump to the wrong location, because
1181 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1183 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1184 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1185 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
1187 wrmsrl(MSR_CSTAR, ignore_sysret);
1188 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1189 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1190 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1193 /* Flags to clear on syscall */
1194 wrmsrl(MSR_SYSCALL_MASK,
1195 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
1196 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1200 * Copies of the original ist values from the tss are only accessed during
1201 * debugging, no special alignment required.
1203 DEFINE_PER_CPU(struct orig_ist, orig_ist);
1205 static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
1206 DEFINE_PER_CPU(int, debug_stack_usage);
1208 int is_debug_stack(unsigned long addr)
1210 return __this_cpu_read(debug_stack_usage) ||
1211 (addr <= __this_cpu_read(debug_stack_addr) &&
1212 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1214 NOKPROBE_SYMBOL(is_debug_stack);
1216 DEFINE_PER_CPU(u32, debug_idt_ctr);
1218 void debug_stack_set_zero(void)
1220 this_cpu_inc(debug_idt_ctr);
1223 NOKPROBE_SYMBOL(debug_stack_set_zero);
1225 void debug_stack_reset(void)
1227 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1229 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1232 NOKPROBE_SYMBOL(debug_stack_reset);
1234 #else /* CONFIG_X86_64 */
1236 DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1237 EXPORT_PER_CPU_SYMBOL(current_task);
1238 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1239 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1242 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1243 * the top of the kernel stack. Use an extra percpu variable to track the
1244 * top of the kernel stack directly.
1246 DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1247 (unsigned long)&init_thread_union + THREAD_SIZE;
1248 EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1250 #ifdef CONFIG_CC_STACKPROTECTOR
1251 DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
1254 #endif /* CONFIG_X86_64 */
1257 * Clear all 6 debug registers:
1259 static void clear_all_debug_regs(void)
1263 for (i = 0; i < 8; i++) {
1264 /* Ignore db4, db5 */
1265 if ((i == 4) || (i == 5))
1274 * Restore debug regs if using kgdbwait and you have a kernel debugger
1275 * connection established.
1277 static void dbg_restore_debug_regs(void)
1279 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1280 arch_kgdb_ops.correct_hw_break();
1282 #else /* ! CONFIG_KGDB */
1283 #define dbg_restore_debug_regs()
1284 #endif /* ! CONFIG_KGDB */
1286 static void wait_for_master_cpu(int cpu)
1290 * wait for ACK from master CPU before continuing
1291 * with AP initialization
1293 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1294 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1300 * cpu_init() initializes state that is per-CPU. Some data is already
1301 * initialized (naturally) in the bootstrap process, such as the GDT
1302 * and IDT. We reload them nevertheless, this function acts as a
1303 * 'CPU state barrier', nothing should get across.
1304 * A lot of state is already set up in PDA init for 64 bit
1306 #ifdef CONFIG_X86_64
1310 struct orig_ist *oist;
1311 struct task_struct *me;
1312 struct tss_struct *t;
1314 int cpu = stack_smp_processor_id();
1317 wait_for_master_cpu(cpu);
1320 * Initialize the CR4 shadow before doing anything that could
1326 * Load microcode on this cpu if a valid microcode is available.
1327 * This is early microcode loading procedure.
1331 t = &per_cpu(cpu_tss, cpu);
1332 oist = &per_cpu(orig_ist, cpu);
1335 if (this_cpu_read(numa_node) == 0 &&
1336 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1337 set_numa_node(early_cpu_to_node(cpu));
1342 pr_debug("Initializing CPU#%d\n", cpu);
1344 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1347 * Initialize the per-CPU GDT with the boot GDT,
1348 * and set up the GDT descriptor:
1351 switch_to_new_gdt(cpu);
1356 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1359 wrmsrl(MSR_FS_BASE, 0);
1360 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1367 * set up and load the per-CPU TSS
1369 if (!oist->ist[0]) {
1370 char *estacks = per_cpu(exception_stacks, cpu);
1372 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
1373 estacks += exception_stack_sizes[v];
1374 oist->ist[v] = t->x86_tss.ist[v] =
1375 (unsigned long)estacks;
1376 if (v == DEBUG_STACK-1)
1377 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
1381 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1384 * <= is required because the CPU will access up to
1385 * 8 bits beyond the end of the IO permission bitmap.
1387 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1388 t->io_bitmap[i] = ~0UL;
1390 atomic_inc(&init_mm.mm_count);
1391 me->active_mm = &init_mm;
1393 enter_lazy_tlb(&init_mm, me);
1395 load_sp0(t, ¤t->thread);
1396 set_tss_desc(cpu, t);
1398 load_LDT(&init_mm.context);
1400 clear_all_debug_regs();
1401 dbg_restore_debug_regs();
1413 int cpu = smp_processor_id();
1414 struct task_struct *curr = current;
1415 struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1416 struct thread_struct *thread = &curr->thread;
1418 wait_for_master_cpu(cpu);
1421 * Initialize the CR4 shadow before doing anything that could
1426 show_ucode_info_early();
1428 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1430 if (cpu_feature_enabled(X86_FEATURE_VME) || cpu_has_tsc || cpu_has_de)
1431 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1434 switch_to_new_gdt(cpu);
1437 * Set up and load the per-CPU TSS and LDT
1439 atomic_inc(&init_mm.mm_count);
1440 curr->active_mm = &init_mm;
1442 enter_lazy_tlb(&init_mm, curr);
1444 load_sp0(t, thread);
1445 set_tss_desc(cpu, t);
1447 load_LDT(&init_mm.context);
1449 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1451 #ifdef CONFIG_DOUBLEFAULT
1452 /* Set up doublefault TSS pointer in the GDT */
1453 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1456 clear_all_debug_regs();
1457 dbg_restore_debug_regs();
1463 #ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
1464 void warn_pre_alternatives(void)
1466 WARN(1, "You're using static_cpu_has before alternatives have run!\n");
1468 EXPORT_SYMBOL_GPL(warn_pre_alternatives);
1471 inline bool __static_cpu_has_safe(u16 bit)
1473 return boot_cpu_has(bit);
1475 EXPORT_SYMBOL_GPL(__static_cpu_has_safe);