fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / i386 / kernel / cpu / common-xen.c
1 #include <linux/init.h>
2 #include <linux/string.h>
3 #include <linux/delay.h>
4 #include <linux/smp.h>
5 #include <linux/module.h>
6 #include <linux/percpu.h>
7 #include <linux/bootmem.h>
8 #include <asm/semaphore.h>
9 #include <asm/processor.h>
10 #include <asm/i387.h>
11 #include <asm/msr.h>
12 #include <asm/io.h>
13 #include <asm/mmu_context.h>
14 #include <asm/mtrr.h>
15 #include <asm/mce.h>
16 #ifdef CONFIG_X86_LOCAL_APIC
17 #include <asm/mpspec.h>
18 #include <asm/apic.h>
19 #include <mach_apic.h>
20 #endif
21 #include <asm/pda.h>
22 #include <asm/hypervisor.h>
23
24 #include "cpu.h"
25
26 DEFINE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
27 EXPORT_PER_CPU_SYMBOL(cpu_gdt_descr);
28
29 struct i386_pda *_cpu_pda[NR_CPUS] __read_mostly;
30 EXPORT_SYMBOL(_cpu_pda);
31
32 static int cachesize_override __cpuinitdata = -1;
33 static int disable_x86_fxsr __cpuinitdata;
34 static int disable_x86_serial_nr __cpuinitdata = 1;
35 static int disable_x86_sep __cpuinitdata;
36
37 struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};
38
39 extern int disable_pse;
40
41 static void __cpuinit default_init(struct cpuinfo_x86 * c)
42 {
43         /* Not much we can do here... */
44         /* Check if at least it has cpuid */
45         if (c->cpuid_level == -1) {
46                 /* No cpuid. It must be an ancient CPU */
47                 if (c->x86 == 4)
48                         strcpy(c->x86_model_id, "486");
49                 else if (c->x86 == 3)
50                         strcpy(c->x86_model_id, "386");
51         }
52 }
53
54 static struct cpu_dev __cpuinitdata default_cpu = {
55         .c_init = default_init,
56         .c_vendor = "Unknown",
57 };
58 static struct cpu_dev * this_cpu __cpuinitdata = &default_cpu;
59
60 static int __init cachesize_setup(char *str)
61 {
62         get_option (&str, &cachesize_override);
63         return 1;
64 }
65 __setup("cachesize=", cachesize_setup);
66
67 int __cpuinit get_model_name(struct cpuinfo_x86 *c)
68 {
69         unsigned int *v;
70         char *p, *q;
71
72         if (cpuid_eax(0x80000000) < 0x80000004)
73                 return 0;
74
75         v = (unsigned int *) c->x86_model_id;
76         cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
77         cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
78         cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
79         c->x86_model_id[48] = 0;
80
81         /* Intel chips right-justify this string for some dumb reason;
82            undo that brain damage */
83         p = q = &c->x86_model_id[0];
84         while ( *p == ' ' )
85              p++;
86         if ( p != q ) {
87              while ( *p )
88                   *q++ = *p++;
89              while ( q <= &c->x86_model_id[48] )
90                   *q++ = '\0';  /* Zero-pad the rest */
91         }
92
93         return 1;
94 }
95
96
97 void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
98 {
99         unsigned int n, dummy, ecx, edx, l2size;
100
101         n = cpuid_eax(0x80000000);
102
103         if (n >= 0x80000005) {
104                 cpuid(0x80000005, &dummy, &dummy, &ecx, &edx);
105                 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
106                         edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
107                 c->x86_cache_size=(ecx>>24)+(edx>>24);  
108         }
109
110         if (n < 0x80000006)     /* Some chips just has a large L1. */
111                 return;
112
113         ecx = cpuid_ecx(0x80000006);
114         l2size = ecx >> 16;
115         
116         /* do processor-specific cache resizing */
117         if (this_cpu->c_size_cache)
118                 l2size = this_cpu->c_size_cache(c,l2size);
119
120         /* Allow user to override all this if necessary. */
121         if (cachesize_override != -1)
122                 l2size = cachesize_override;
123
124         if ( l2size == 0 )
125                 return;         /* Again, no L2 cache is possible */
126
127         c->x86_cache_size = l2size;
128
129         printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
130                l2size, ecx & 0xFF);
131 }
132
133 /* Naming convention should be: <Name> [(<Codename>)] */
134 /* This table only is used unless init_<vendor>() below doesn't set it; */
135 /* in particular, if CPUID levels 0x80000002..4 are supported, this isn't used */
136
137 /* Look up CPU names by table lookup. */
138 static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
139 {
140         struct cpu_model_info *info;
141
142         if ( c->x86_model >= 16 )
143                 return NULL;    /* Range check */
144
145         if (!this_cpu)
146                 return NULL;
147
148         info = this_cpu->c_models;
149
150         while (info && info->family) {
151                 if (info->family == c->x86)
152                         return info->model_names[c->x86_model];
153                 info++;
154         }
155         return NULL;            /* Not found */
156 }
157
158
159 static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c, int early)
160 {
161         char *v = c->x86_vendor_id;
162         int i;
163         static int printed;
164
165         for (i = 0; i < X86_VENDOR_NUM; i++) {
166                 if (cpu_devs[i]) {
167                         if (!strcmp(v,cpu_devs[i]->c_ident[0]) ||
168                             (cpu_devs[i]->c_ident[1] && 
169                              !strcmp(v,cpu_devs[i]->c_ident[1]))) {
170                                 c->x86_vendor = i;
171                                 if (!early)
172                                         this_cpu = cpu_devs[i];
173                                 return;
174                         }
175                 }
176         }
177         if (!printed) {
178                 printed++;
179                 printk(KERN_ERR "CPU: Vendor unknown, using generic init.\n");
180                 printk(KERN_ERR "CPU: Your system may be unstable.\n");
181         }
182         c->x86_vendor = X86_VENDOR_UNKNOWN;
183         this_cpu = &default_cpu;
184 }
185
186
187 static int __init x86_fxsr_setup(char * s)
188 {
189         /* Tell all the other CPU's to not use it... */
190         disable_x86_fxsr = 1;
191
192         /*
193          * ... and clear the bits early in the boot_cpu_data
194          * so that the bootup process doesn't try to do this
195          * either.
196          */
197         clear_bit(X86_FEATURE_FXSR, boot_cpu_data.x86_capability);
198         clear_bit(X86_FEATURE_XMM, boot_cpu_data.x86_capability);
199         return 1;
200 }
201 __setup("nofxsr", x86_fxsr_setup);
202
203
204 static int __init x86_sep_setup(char * s)
205 {
206         disable_x86_sep = 1;
207         return 1;
208 }
209 __setup("nosep", x86_sep_setup);
210
211
212 /* Standard macro to see if a specific flag is changeable */
213 static inline int flag_is_changeable_p(u32 flag)
214 {
215         u32 f1, f2;
216
217         asm("pushfl\n\t"
218             "pushfl\n\t"
219             "popl %0\n\t"
220             "movl %0,%1\n\t"
221             "xorl %2,%0\n\t"
222             "pushl %0\n\t"
223             "popfl\n\t"
224             "pushfl\n\t"
225             "popl %0\n\t"
226             "popfl\n\t"
227             : "=&r" (f1), "=&r" (f2)
228             : "ir" (flag));
229
230         return ((f1^f2) & flag) != 0;
231 }
232
233
234 /* Probe for the CPUID instruction */
235 static int __cpuinit have_cpuid_p(void)
236 {
237         return flag_is_changeable_p(X86_EFLAGS_ID);
238 }
239
240 void __init cpu_detect(struct cpuinfo_x86 *c)
241 {
242         /* Get vendor name */
243         cpuid(0x00000000, &c->cpuid_level,
244               (int *)&c->x86_vendor_id[0],
245               (int *)&c->x86_vendor_id[8],
246               (int *)&c->x86_vendor_id[4]);
247
248         c->x86 = 4;
249         if (c->cpuid_level >= 0x00000001) {
250                 u32 junk, tfms, cap0, misc;
251                 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
252                 c->x86 = (tfms >> 8) & 15;
253                 c->x86_model = (tfms >> 4) & 15;
254                 if (c->x86 == 0xf)
255                         c->x86 += (tfms >> 20) & 0xff;
256                 if (c->x86 >= 0x6)
257                         c->x86_model += ((tfms >> 16) & 0xF) << 4;
258                 c->x86_mask = tfms & 15;
259                 if (cap0 & (1<<19))
260                         c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
261         }
262 }
263
264 /* Do minimum CPU detection early.
265    Fields really needed: vendor, cpuid_level, family, model, mask, cache alignment.
266    The others are not touched to avoid unwanted side effects.
267
268    WARNING: this function is only called on the BP.  Don't add code here
269    that is supposed to run on all CPUs. */
270 static void __init early_cpu_detect(void)
271 {
272         struct cpuinfo_x86 *c = &boot_cpu_data;
273
274         c->x86_cache_alignment = 32;
275
276         if (!have_cpuid_p())
277                 return;
278
279         cpu_detect(c);
280
281         get_cpu_vendor(c, 1);
282 }
283
284 static void __cpuinit generic_identify(struct cpuinfo_x86 * c)
285 {
286         u32 tfms, xlvl;
287         int ebx;
288
289         if (have_cpuid_p()) {
290                 /* Get vendor name */
291                 cpuid(0x00000000, &c->cpuid_level,
292                       (int *)&c->x86_vendor_id[0],
293                       (int *)&c->x86_vendor_id[8],
294                       (int *)&c->x86_vendor_id[4]);
295                 
296                 get_cpu_vendor(c, 0);
297                 /* Initialize the standard set of capabilities */
298                 /* Note that the vendor-specific code below might override */
299         
300                 /* Intel-defined flags: level 0x00000001 */
301                 if ( c->cpuid_level >= 0x00000001 ) {
302                         u32 capability, excap;
303                         cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
304                         c->x86_capability[0] = capability;
305                         c->x86_capability[4] = excap;
306                         c->x86 = (tfms >> 8) & 15;
307                         c->x86_model = (tfms >> 4) & 15;
308                         if (c->x86 == 0xf)
309                                 c->x86 += (tfms >> 20) & 0xff;
310                         if (c->x86 >= 0x6)
311                                 c->x86_model += ((tfms >> 16) & 0xF) << 4;
312                         c->x86_mask = tfms & 15;
313 #ifdef CONFIG_X86_HT
314                         c->apicid = phys_pkg_id((ebx >> 24) & 0xFF, 0);
315 #else
316                         c->apicid = (ebx >> 24) & 0xFF;
317 #endif
318                         if (c->x86_capability[0] & (1<<19))
319                                 c->x86_clflush_size = ((ebx >> 8) & 0xff) * 8;
320                 } else {
321                         /* Have CPUID level 0 only - unheard of */
322                         c->x86 = 4;
323                 }
324
325                 /* AMD-defined flags: level 0x80000001 */
326                 xlvl = cpuid_eax(0x80000000);
327                 if ( (xlvl & 0xffff0000) == 0x80000000 ) {
328                         if ( xlvl >= 0x80000001 ) {
329                                 c->x86_capability[1] = cpuid_edx(0x80000001);
330                                 c->x86_capability[6] = cpuid_ecx(0x80000001);
331                         }
332                         if ( xlvl >= 0x80000004 )
333                                 get_model_name(c); /* Default name */
334                 }
335         }
336
337         early_intel_workaround(c);
338
339 #ifdef CONFIG_X86_HT
340         c->phys_proc_id = (cpuid_ebx(1) >> 24) & 0xff;
341 #endif
342 }
343
344 static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
345 {
346         if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr ) {
347                 /* Disable processor serial number */
348                 unsigned long lo,hi;
349                 rdmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
350                 lo |= 0x200000;
351                 wrmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
352                 printk(KERN_NOTICE "CPU serial number disabled.\n");
353                 clear_bit(X86_FEATURE_PN, c->x86_capability);
354
355                 /* Disabling the serial number may affect the cpuid level */
356                 c->cpuid_level = cpuid_eax(0);
357         }
358 }
359
360 static int __init x86_serial_nr_setup(char *s)
361 {
362         disable_x86_serial_nr = 0;
363         return 1;
364 }
365 __setup("serialnumber", x86_serial_nr_setup);
366
367
368
369 /*
370  * This does the hard work of actually picking apart the CPU stuff...
371  */
372 void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
373 {
374         int i;
375
376         c->loops_per_jiffy = loops_per_jiffy;
377         c->x86_cache_size = -1;
378         c->x86_vendor = X86_VENDOR_UNKNOWN;
379         c->cpuid_level = -1;    /* CPUID not detected */
380         c->x86_model = c->x86_mask = 0; /* So far unknown... */
381         c->x86_vendor_id[0] = '\0'; /* Unset */
382         c->x86_model_id[0] = '\0';  /* Unset */
383         c->x86_max_cores = 1;
384         c->x86_clflush_size = 32;
385         memset(&c->x86_capability, 0, sizeof c->x86_capability);
386
387         if (!have_cpuid_p()) {
388                 /* First of all, decide if this is a 486 or higher */
389                 /* It's a 486 if we can modify the AC flag */
390                 if ( flag_is_changeable_p(X86_EFLAGS_AC) )
391                         c->x86 = 4;
392                 else
393                         c->x86 = 3;
394         }
395
396         generic_identify(c);
397
398         printk(KERN_DEBUG "CPU: After generic identify, caps:");
399         for (i = 0; i < NCAPINTS; i++)
400                 printk(" %08lx", c->x86_capability[i]);
401         printk("\n");
402
403         if (this_cpu->c_identify) {
404                 this_cpu->c_identify(c);
405
406                 printk(KERN_DEBUG "CPU: After vendor identify, caps:");
407                 for (i = 0; i < NCAPINTS; i++)
408                         printk(" %08lx", c->x86_capability[i]);
409                 printk("\n");
410         }
411
412         /*
413          * Vendor-specific initialization.  In this section we
414          * canonicalize the feature flags, meaning if there are
415          * features a certain CPU supports which CPUID doesn't
416          * tell us, CPUID claiming incorrect flags, or other bugs,
417          * we handle them here.
418          *
419          * At the end of this section, c->x86_capability better
420          * indicate the features this CPU genuinely supports!
421          */
422         if (this_cpu->c_init)
423                 this_cpu->c_init(c);
424
425         /* Disable the PN if appropriate */
426         squash_the_stupid_serial_number(c);
427
428         /*
429          * The vendor-specific functions might have changed features.  Now
430          * we do "generic changes."
431          */
432
433         /* TSC disabled? */
434         if ( tsc_disable )
435                 clear_bit(X86_FEATURE_TSC, c->x86_capability);
436
437         /* FXSR disabled? */
438         if (disable_x86_fxsr) {
439                 clear_bit(X86_FEATURE_FXSR, c->x86_capability);
440                 clear_bit(X86_FEATURE_XMM, c->x86_capability);
441         }
442
443         /* SEP disabled? */
444         if (disable_x86_sep)
445                 clear_bit(X86_FEATURE_SEP, c->x86_capability);
446
447         if (disable_pse)
448                 clear_bit(X86_FEATURE_PSE, c->x86_capability);
449
450         if (exec_shield != 0) {
451 #ifdef CONFIG_HIGHMEM64G   /* NX implies PAE */
452                 if (!test_bit(X86_FEATURE_NX, c->x86_capability))
453 #endif
454                 clear_bit(X86_FEATURE_SEP, c->x86_capability);
455         }
456
457         /* If the model name is still unset, do table lookup. */
458         if ( !c->x86_model_id[0] ) {
459                 char *p;
460                 p = table_lookup_model(c);
461                 if ( p )
462                         strcpy(c->x86_model_id, p);
463                 else
464                         /* Last resort... */
465                         sprintf(c->x86_model_id, "%02x/%02x",
466                                 c->x86, c->x86_model);
467         }
468
469         /* Now the feature flags better reflect actual CPU features! */
470
471         printk(KERN_DEBUG "CPU: After all inits, caps:");
472         for (i = 0; i < NCAPINTS; i++)
473                 printk(" %08lx", c->x86_capability[i]);
474         printk("\n");
475
476         /*
477          * On SMP, boot_cpu_data holds the common feature set between
478          * all CPUs; so make sure that we indicate which features are
479          * common between the CPUs.  The first time this routine gets
480          * executed, c == &boot_cpu_data.
481          */
482         if ( c != &boot_cpu_data ) {
483                 /* AND the already accumulated flags with these */
484                 for ( i = 0 ; i < NCAPINTS ; i++ )
485                         boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
486         }
487
488         /* Init Machine Check Exception if available. */
489         mcheck_init(c);
490
491         if (c == &boot_cpu_data)
492                 sysenter_setup();
493         enable_sep_cpu();
494
495         if (c == &boot_cpu_data)
496                 mtrr_bp_init();
497         else
498                 mtrr_ap_init();
499 }
500
501 #ifdef CONFIG_X86_HT
502 void __cpuinit detect_ht(struct cpuinfo_x86 *c)
503 {
504         u32     eax, ebx, ecx, edx;
505         int     index_msb, core_bits;
506
507         cpuid(1, &eax, &ebx, &ecx, &edx);
508
509         if (!cpu_has(c, X86_FEATURE_HT) || cpu_has(c, X86_FEATURE_CMP_LEGACY))
510                 return;
511
512         smp_num_siblings = (ebx & 0xff0000) >> 16;
513
514         if (smp_num_siblings == 1) {
515                 printk(KERN_INFO  "CPU: Hyper-Threading is disabled\n");
516         } else if (smp_num_siblings > 1 ) {
517
518                 if (smp_num_siblings > NR_CPUS) {
519                         printk(KERN_WARNING "CPU: Unsupported number of the "
520                                         "siblings %d", smp_num_siblings);
521                         smp_num_siblings = 1;
522                         return;
523                 }
524
525                 index_msb = get_count_order(smp_num_siblings);
526                 c->phys_proc_id = phys_pkg_id((ebx >> 24) & 0xFF, index_msb);
527
528                 printk(KERN_INFO  "CPU: Physical Processor ID: %d\n",
529                        c->phys_proc_id);
530
531                 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
532
533                 index_msb = get_count_order(smp_num_siblings) ;
534
535                 core_bits = get_count_order(c->x86_max_cores);
536
537                 c->cpu_core_id = phys_pkg_id((ebx >> 24) & 0xFF, index_msb) &
538                                                ((1 << core_bits) - 1);
539
540                 if (c->x86_max_cores > 1)
541                         printk(KERN_INFO  "CPU: Processor Core ID: %d\n",
542                                c->cpu_core_id);
543         }
544 }
545 #endif
546
547 void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
548 {
549         char *vendor = NULL;
550
551         if (c->x86_vendor < X86_VENDOR_NUM)
552                 vendor = this_cpu->c_vendor;
553         else if (c->cpuid_level >= 0)
554                 vendor = c->x86_vendor_id;
555
556         if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
557                 printk("%s ", vendor);
558
559         if (!c->x86_model_id[0])
560                 printk("%d86", c->x86);
561         else
562                 printk("%s", c->x86_model_id);
563
564         if (c->x86_mask || c->cpuid_level >= 0) 
565                 printk(" stepping %02x\n", c->x86_mask);
566         else
567                 printk("\n");
568 }
569
570 cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
571
572 /* This is hacky. :)
573  * We're emulating future behavior.
574  * In the future, the cpu-specific init functions will be called implicitly
575  * via the magic of initcalls.
576  * They will insert themselves into the cpu_devs structure.
577  * Then, when cpu_init() is called, we can just iterate over that array.
578  */
579
580 extern int intel_cpu_init(void);
581 extern int cyrix_init_cpu(void);
582 extern int nsc_init_cpu(void);
583 extern int amd_init_cpu(void);
584 extern int centaur_init_cpu(void);
585 extern int transmeta_init_cpu(void);
586 extern int rise_init_cpu(void);
587 extern int nexgen_init_cpu(void);
588 extern int umc_init_cpu(void);
589
590 void __init early_cpu_init(void)
591 {
592         intel_cpu_init();
593         cyrix_init_cpu();
594         nsc_init_cpu();
595         amd_init_cpu();
596         centaur_init_cpu();
597         transmeta_init_cpu();
598         rise_init_cpu();
599         nexgen_init_cpu();
600         umc_init_cpu();
601         early_cpu_detect();
602
603 #ifdef CONFIG_DEBUG_PAGEALLOC
604         /* pse is not compatible with on-the-fly unmapping,
605          * disable it even if the cpus claim to support it.
606          */
607         clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
608         disable_pse = 1;
609 #endif
610 }
611
612 /* We can't move load_gdt to asm/desc.h because it lacks make_lowmen_page_readonly()
613    definition, and as this is still the only user of load_gdt in xen.
614    ToDo: JQ
615  */
616
617 #ifdef CONFIG_XEN
618 #undef load_gdt
619 static void __cpuinit load_gdt(struct Xgt_desc_struct *gdt_descr)
620 {
621         unsigned long frames[16];
622         unsigned long va;
623         int f;
624
625         for (va = gdt_descr->address, f = 0;
626              va < gdt_descr->address + gdt_descr->size;
627              va += PAGE_SIZE, f++) {
628                 frames[f] = virt_to_mfn(va);
629                 make_lowmem_page_readonly(
630                         (void *)va, XENFEAT_writable_descriptor_tables);
631         }
632         if (HYPERVISOR_set_gdt(frames, gdt_descr->size / 8))
633                 BUG();
634 }
635 #endif /* CONFIG_XEN */
636
637 /* Make sure %gs is initialized properly in idle threads */
638 struct pt_regs * __devinit idle_regs(struct pt_regs *regs)
639 {
640         memset(regs, 0, sizeof(struct pt_regs));
641         regs->xgs = __KERNEL_PDA;
642         return regs;
643 }
644
645 static __cpuinit int alloc_gdt(int cpu)
646 {
647         struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
648         struct desc_struct *gdt;
649         struct i386_pda *pda;
650
651         gdt = (struct desc_struct *)cpu_gdt_descr->address;
652         pda = cpu_pda(cpu);
653
654         /*
655          * This is a horrible hack to allocate the GDT.  The problem
656          * is that cpu_init() is called really early for the boot CPU
657          * (and hence needs bootmem) but much later for the secondary
658          * CPUs, when bootmem will have gone away
659          */
660         if (NODE_DATA(0)->bdata->node_bootmem_map) {
661                 BUG_ON(gdt != NULL || pda != NULL);
662
663                 gdt = alloc_bootmem_pages(PAGE_SIZE);
664                 pda = alloc_bootmem(sizeof(*pda));
665                 /* alloc_bootmem(_pages) panics on failure, so no check */
666
667                 memset(gdt, 0, PAGE_SIZE);
668                 memset(pda, 0, sizeof(*pda));
669 #ifdef CONFIG_XEN
670                 memcpy(gdt, cpu_gdt_table, GDT_SIZE);
671                 cpu_gdt_descr->size = GDT_SIZE;
672 #endif
673         } else {
674                 /* GDT and PDA might already have been allocated if
675                    this is a CPU hotplug re-insertion. */
676                 if (gdt == NULL)
677                         gdt = (struct desc_struct *)get_zeroed_page(GFP_KERNEL);
678
679                 if (pda == NULL)
680                         pda = kmalloc_node(sizeof(*pda), GFP_KERNEL, cpu_to_node(cpu));
681
682                 if (unlikely(!gdt || !pda)) {
683                         free_pages((unsigned long)gdt, 0);
684                         kfree(pda);
685                         return 0;
686                 }
687         }
688
689         cpu_gdt_descr->address = (unsigned long)gdt;
690         cpu_pda(cpu) = pda;
691
692         return 1;
693 }
694
695 /* Initial PDA used by boot CPU */
696 struct i386_pda boot_pda = {
697         ._pda = &boot_pda,
698         .cpu_number = 0,
699         .pcurrent = &init_task,
700 };
701
702 static inline void set_kernel_gs(void)
703 {
704         /* Set %gs for this CPU's PDA.  Memory clobber is to create a
705            barrier with respect to any PDA operations, so the compiler
706            doesn't move any before here. */
707         asm volatile ("mov %0, %%gs" : : "r" (__KERNEL_PDA) : "memory");
708 }
709
710 /* Initialize the CPU's GDT and PDA.  The boot CPU does this for
711    itself, but secondaries find this done for them. */
712 __cpuinit int init_gdt(int cpu, struct task_struct *idle)
713 {
714         struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
715         struct desc_struct *gdt;
716         struct i386_pda *pda;
717
718         /* For non-boot CPUs, the GDT and PDA should already have been
719            allocated. */
720         if (!alloc_gdt(cpu)) {
721                 printk(KERN_CRIT "CPU%d failed to allocate GDT or PDA\n", cpu);
722                 return 0;
723         }
724
725         gdt = (struct desc_struct *)cpu_gdt_descr->address;
726         pda = cpu_pda(cpu);
727
728         BUG_ON(gdt == NULL || pda == NULL);
729
730 #ifndef CONFIG_XEN
731         /*
732          * Initialize the per-CPU GDT with the boot GDT,
733          * and set up the GDT descriptor:
734          */
735         memcpy(gdt, cpu_gdt_table, GDT_SIZE);
736         cpu_gdt_descr->size = GDT_SIZE - 1;
737 #endif
738
739
740         if (cpu == 0)
741                 pack_descriptor((u32 *)&gdt[GDT_ENTRY_PDA].a,
742                         (u32 *)&gdt[GDT_ENTRY_PDA].b,
743                         (unsigned long)pda, sizeof(*pda) - 1,
744                         0x80 | DESCTYPE_S | 0x2, 0); /* present read-write data segment */
745
746         memset(pda, 0, sizeof(*pda));
747         pda->_pda = pda;
748         pda->cpu_number = cpu;
749         pda->pcurrent = idle;
750
751         return 1;
752 }
753
754 void __cpuinit cpu_set_gdt(int cpu)
755 {
756         struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
757
758         /* Reinit these anyway, even if they've already been done (on
759            the boot CPU, this will transition from the boot gdt+pda to
760            the real ones). */
761         load_gdt(cpu_gdt_descr);
762         set_kernel_gs();
763 }
764
765 /* Common CPU init for both boot and secondary CPUs */
766 static void __cpuinit _cpu_init(int cpu, struct task_struct *curr)
767 {
768 #ifndef CONFIG_X86_NO_TSS
769         struct tss_struct * t = &per_cpu(init_tss, cpu);
770 #endif
771         struct thread_struct *thread = &curr->thread;
772
773         if (cpu_test_and_set(cpu, cpu_initialized)) {
774                 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
775                 for (;;) local_irq_enable();
776         }
777
778         printk(KERN_INFO "Initializing CPU#%d\n", cpu);
779
780         if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
781                 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
782         if (tsc_disable && cpu_has_tsc) {
783                 printk(KERN_NOTICE "Disabling TSC...\n");
784                 /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
785                 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
786                 set_in_cr4(X86_CR4_TSD);
787         }
788
789 #ifndef CONFIG_X86_NO_IDT
790         load_idt(&idt_descr);
791 #endif
792
793         /*
794          * Set up and load the per-CPU TSS and LDT
795          */
796         atomic_inc(&init_mm.mm_count);
797         curr->active_mm = &init_mm;
798         if (curr->mm)
799                 BUG();
800         enter_lazy_tlb(&init_mm, curr);
801
802         load_esp0(t, thread);
803 #ifndef CONFIG_X86_NO_TSS
804         set_tss_desc(cpu,t);
805         load_TR_desc();
806 #endif
807         load_LDT(&init_mm.context);
808
809 #ifdef CONFIG_DOUBLEFAULT
810         /* Set up doublefault TSS pointer in the GDT */
811         __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
812 #endif
813
814         /* Clear %fs. */
815         asm volatile ("mov %0, %%fs" : : "r" (0));
816
817         /* Clear all 6 debug registers: */
818         set_debugreg(0, 0);
819         set_debugreg(0, 1);
820         set_debugreg(0, 2);
821         set_debugreg(0, 3);
822         set_debugreg(0, 6);
823         set_debugreg(0, 7);
824
825         /*
826          * Force FPU initialization:
827          */
828         current_thread_info()->status = 0;
829         clear_used_math();
830         mxcsr_feature_mask_init();
831 }
832
833 /* Entrypoint to initialize secondary CPU */
834 void __cpuinit secondary_cpu_init(void)
835 {
836         int cpu = smp_processor_id();
837         struct task_struct *curr = current;
838
839         _cpu_init(cpu, curr);
840 }
841
842 /*
843  * cpu_init() initializes state that is per-CPU. Some data is already
844  * initialized (naturally) in the bootstrap process, such as the GDT
845  * and IDT. We reload them nevertheless, this function acts as a
846  * 'CPU state barrier', nothing should get across.
847  */
848 void __cpuinit cpu_init(void)
849 {
850         int cpu = smp_processor_id();
851         struct task_struct *curr = current;
852
853         /* Set up the real GDT and PDA, so we can transition from the
854            boot versions. */
855         if (!init_gdt(cpu, curr)) {
856                 /* failed to allocate something; not much we can do... */
857                 for (;;)
858                         local_irq_enable();
859         }
860
861         cpu_set_gdt(cpu);
862         _cpu_init(cpu, curr);
863 }
864
865 #ifdef CONFIG_HOTPLUG_CPU
866 void __cpuinit cpu_uninit(void)
867 {
868         int cpu = raw_smp_processor_id();
869         cpu_clear(cpu, cpu_initialized);
870
871         /* lazy TLB state */
872         per_cpu(cpu_tlbstate, cpu).state = 0;
873         per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;
874 }
875 #endif