common.c 18 KB

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