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