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