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