common.c 17 KB

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