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