common_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. #include <linux/init.h>
  2. #include <linux/kernel.h>
  3. #include <linux/sched.h>
  4. #include <linux/string.h>
  5. #include <linux/bootmem.h>
  6. #include <linux/bitops.h>
  7. #include <linux/module.h>
  8. #include <linux/kgdb.h>
  9. #include <linux/topology.h>
  10. #include <linux/delay.h>
  11. #include <linux/smp.h>
  12. #include <linux/percpu.h>
  13. #include <asm/i387.h>
  14. #include <asm/msr.h>
  15. #include <asm/io.h>
  16. #include <asm/linkage.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/mtrr.h>
  19. #include <asm/mce.h>
  20. #include <asm/pat.h>
  21. #include <asm/asm.h>
  22. #include <asm/numa.h>
  23. #ifdef CONFIG_X86_LOCAL_APIC
  24. #include <asm/mpspec.h>
  25. #include <asm/apic.h>
  26. #include <mach_apic.h>
  27. #include <asm/genapic.h>
  28. #endif
  29. #include <asm/pda.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/processor.h>
  32. #include <asm/desc.h>
  33. #include <asm/atomic.h>
  34. #include <asm/proto.h>
  35. #include <asm/sections.h>
  36. #include <asm/setup.h>
  37. #include "cpu.h"
  38. static struct cpu_dev *this_cpu __cpuinitdata;
  39. /* We need valid kernel segments for data and code in long mode too
  40. * IRET will check the segment types kkeil 2000/10/28
  41. * Also sysret mandates a special GDT layout
  42. */
  43. /* The TLS descriptors are currently at a different place compared to i386.
  44. Hopefully nobody expects them at a fixed place (Wine?) */
  45. DEFINE_PER_CPU(struct gdt_page, gdt_page) = { .gdt = {
  46. [GDT_ENTRY_KERNEL32_CS] = { { { 0x0000ffff, 0x00cf9b00 } } },
  47. [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00af9b00 } } },
  48. [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9300 } } },
  49. [GDT_ENTRY_DEFAULT_USER32_CS] = { { { 0x0000ffff, 0x00cffb00 } } },
  50. [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff300 } } },
  51. [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00affb00 } } },
  52. } };
  53. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  54. __u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
  55. /* Current gdt points %fs at the "master" per-cpu area: after this,
  56. * it's on the real one. */
  57. void switch_to_new_gdt(void)
  58. {
  59. struct desc_ptr gdt_descr;
  60. gdt_descr.address = (long)get_cpu_gdt_table(smp_processor_id());
  61. gdt_descr.size = GDT_SIZE - 1;
  62. load_gdt(&gdt_descr);
  63. }
  64. static struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  65. static void __cpuinit default_init(struct cpuinfo_x86 *c)
  66. {
  67. display_cacheinfo(c);
  68. }
  69. static struct cpu_dev __cpuinitdata default_cpu = {
  70. .c_init = default_init,
  71. .c_vendor = "Unknown",
  72. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  73. };
  74. int __cpuinit get_model_name(struct cpuinfo_x86 *c)
  75. {
  76. unsigned int *v;
  77. char *p, *q;
  78. if (c->extended_cpuid_level < 0x80000004)
  79. return 0;
  80. v = (unsigned int *) c->x86_model_id;
  81. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  82. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  83. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  84. c->x86_model_id[48] = 0;
  85. /* Intel chips right-justify this string for some dumb reason;
  86. undo that brain damage */
  87. p = q = &c->x86_model_id[0];
  88. while (*p == ' ')
  89. p++;
  90. if (p != q) {
  91. while (*p)
  92. *q++ = *p++;
  93. while (q <= &c->x86_model_id[48])
  94. *q++ = '\0'; /* Zero-pad the rest */
  95. }
  96. return 1;
  97. }
  98. void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
  99. {
  100. unsigned int n, dummy, ebx, ecx, edx, l2size;
  101. n = c->extended_cpuid_level;
  102. if (n >= 0x80000005) {
  103. cpuid(0x80000005, &dummy, &ebx, &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. /* On K8 L1 TLB is inclusive, so don't count it */
  108. c->x86_tlbsize = 0;
  109. }
  110. if (n < 0x80000006) /* Some chips just has a large L1. */
  111. return;
  112. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  113. l2size = ecx >> 16;
  114. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  115. c->x86_cache_size = l2size;
  116. printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
  117. l2size, ecx & 0xFF);
  118. }
  119. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  120. {
  121. #ifdef CONFIG_X86_HT
  122. u32 eax, ebx, ecx, edx;
  123. int index_msb, core_bits;
  124. if (!cpu_has(c, X86_FEATURE_HT))
  125. return;
  126. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  127. goto out;
  128. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  129. return;
  130. cpuid(1, &eax, &ebx, &ecx, &edx);
  131. smp_num_siblings = (ebx & 0xff0000) >> 16;
  132. if (smp_num_siblings == 1) {
  133. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  134. } else if (smp_num_siblings > 1) {
  135. if (smp_num_siblings > NR_CPUS) {
  136. printk(KERN_WARNING "CPU: Unsupported number of siblings %d",
  137. smp_num_siblings);
  138. smp_num_siblings = 1;
  139. return;
  140. }
  141. index_msb = get_count_order(smp_num_siblings);
  142. c->phys_proc_id = phys_pkg_id(index_msb);
  143. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  144. index_msb = get_count_order(smp_num_siblings);
  145. core_bits = get_count_order(c->x86_max_cores);
  146. c->cpu_core_id = phys_pkg_id(index_msb) &
  147. ((1 << core_bits) - 1);
  148. }
  149. out:
  150. if ((c->x86_max_cores * smp_num_siblings) > 1) {
  151. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  152. c->phys_proc_id);
  153. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  154. c->cpu_core_id);
  155. }
  156. #endif
  157. }
  158. static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
  159. {
  160. char *v = c->x86_vendor_id;
  161. int i;
  162. static int printed;
  163. for (i = 0; i < X86_VENDOR_NUM; i++) {
  164. if (!cpu_devs[i])
  165. break;
  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. this_cpu = cpu_devs[i];
  170. c->x86_vendor = this_cpu->c_x86_vendor;
  171. return;
  172. }
  173. }
  174. if (!printed) {
  175. printed++;
  176. printk(KERN_ERR "CPU: Vendor unknown, using generic init.\n");
  177. printk(KERN_ERR "CPU: Your system may be unstable.\n");
  178. }
  179. c->x86_vendor = X86_VENDOR_UNKNOWN;
  180. this_cpu = &default_cpu;
  181. }
  182. void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
  183. {
  184. /* Get vendor name */
  185. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  186. (unsigned int *)&c->x86_vendor_id[0],
  187. (unsigned int *)&c->x86_vendor_id[8],
  188. (unsigned int *)&c->x86_vendor_id[4]);
  189. c->x86 = 4;
  190. /* Intel-defined flags: level 0x00000001 */
  191. if (c->cpuid_level >= 0x00000001) {
  192. u32 junk, tfms, cap0, misc;
  193. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  194. c->x86 = (tfms >> 8) & 0xf;
  195. c->x86_model = (tfms >> 4) & 0xf;
  196. c->x86_mask = tfms & 0xf;
  197. if (c->x86 == 0xf)
  198. c->x86 += (tfms >> 20) & 0xff;
  199. if (c->x86 >= 0x6)
  200. c->x86_model += ((tfms >> 16) & 0xf) << 4;
  201. if (cap0 & (1<<19)) {
  202. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  203. c->x86_cache_alignment = c->x86_clflush_size;
  204. }
  205. }
  206. }
  207. static void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
  208. {
  209. u32 tfms, xlvl;
  210. u32 ebx;
  211. /* Intel-defined flags: level 0x00000001 */
  212. if (c->cpuid_level >= 0x00000001) {
  213. u32 capability, excap;
  214. cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  215. c->x86_capability[0] = capability;
  216. c->x86_capability[4] = excap;
  217. }
  218. /* AMD-defined flags: level 0x80000001 */
  219. xlvl = cpuid_eax(0x80000000);
  220. c->extended_cpuid_level = xlvl;
  221. if ((xlvl & 0xffff0000) == 0x80000000) {
  222. if (xlvl >= 0x80000001) {
  223. c->x86_capability[1] = cpuid_edx(0x80000001);
  224. c->x86_capability[6] = cpuid_ecx(0x80000001);
  225. }
  226. }
  227. /* Transmeta-defined flags: level 0x80860001 */
  228. xlvl = cpuid_eax(0x80860000);
  229. if ((xlvl & 0xffff0000) == 0x80860000) {
  230. /* Don't set x86_cpuid_level here for now to not confuse. */
  231. if (xlvl >= 0x80860001)
  232. c->x86_capability[2] = cpuid_edx(0x80860001);
  233. }
  234. if (c->extended_cpuid_level >= 0x80000007)
  235. c->x86_power = cpuid_edx(0x80000007);
  236. if (c->extended_cpuid_level >= 0x80000008) {
  237. u32 eax = cpuid_eax(0x80000008);
  238. c->x86_virt_bits = (eax >> 8) & 0xff;
  239. c->x86_phys_bits = eax & 0xff;
  240. }
  241. }
  242. /* Do some early cpuid on the boot CPU to get some parameter that are
  243. needed before check_bugs. Everything advanced is in identify_cpu
  244. below. */
  245. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  246. {
  247. c->x86_clflush_size = 64;
  248. c->x86_cache_alignment = c->x86_clflush_size;
  249. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  250. c->extended_cpuid_level = 0;
  251. cpu_detect(c);
  252. get_cpu_vendor(c);
  253. get_cpu_cap(c);
  254. if (this_cpu->c_early_init)
  255. this_cpu->c_early_init(c);
  256. validate_pat_support(c);
  257. }
  258. void __init early_cpu_init(void)
  259. {
  260. struct cpu_dev **cdev;
  261. int count = 0;
  262. printk("KERNEL supported cpus:\n");
  263. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  264. struct cpu_dev *cpudev = *cdev;
  265. unsigned int j;
  266. if (count >= X86_VENDOR_NUM)
  267. break;
  268. cpu_devs[count] = cpudev;
  269. count++;
  270. for (j = 0; j < 2; j++) {
  271. if (!cpudev->c_ident[j])
  272. continue;
  273. printk(" %s %s\n", cpudev->c_vendor,
  274. cpudev->c_ident[j]);
  275. }
  276. }
  277. early_identify_cpu(&boot_cpu_data);
  278. }
  279. /*
  280. * The NOPL instruction is supposed to exist on all CPUs with
  281. * family >= 6, unfortunately, that's not true in practice because
  282. * of early VIA chips and (more importantly) broken virtualizers that
  283. * are not easy to detect. Hence, probe for it based on first
  284. * principles.
  285. *
  286. * Note: no 64-bit chip is known to lack these, but put the code here
  287. * for consistency with 32 bits, and to make it utterly trivial to
  288. * diagnose the problem should it ever surface.
  289. */
  290. static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
  291. {
  292. const u32 nopl_signature = 0x888c53b1; /* Random number */
  293. u32 has_nopl = nopl_signature;
  294. clear_cpu_cap(c, X86_FEATURE_NOPL);
  295. if (c->x86 >= 6) {
  296. asm volatile("\n"
  297. "1: .byte 0x0f,0x1f,0xc0\n" /* nopl %eax */
  298. "2:\n"
  299. " .section .fixup,\"ax\"\n"
  300. "3: xor %0,%0\n"
  301. " jmp 2b\n"
  302. " .previous\n"
  303. _ASM_EXTABLE(1b,3b)
  304. : "+a" (has_nopl));
  305. if (has_nopl == nopl_signature)
  306. set_cpu_cap(c, X86_FEATURE_NOPL);
  307. }
  308. }
  309. static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
  310. {
  311. c->extended_cpuid_level = 0;
  312. cpu_detect(c);
  313. get_cpu_vendor(c);
  314. get_cpu_cap(c);
  315. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xff;
  316. #ifdef CONFIG_SMP
  317. c->phys_proc_id = c->initial_apicid;
  318. #endif
  319. if (c->extended_cpuid_level >= 0x80000004)
  320. get_model_name(c); /* Default name */
  321. init_scattered_cpuid_features(c);
  322. detect_nopl(c);
  323. }
  324. /*
  325. * This does the hard work of actually picking apart the CPU stuff...
  326. */
  327. static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  328. {
  329. int i;
  330. c->loops_per_jiffy = loops_per_jiffy;
  331. c->x86_cache_size = -1;
  332. c->x86_vendor = X86_VENDOR_UNKNOWN;
  333. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  334. c->x86_vendor_id[0] = '\0'; /* Unset */
  335. c->x86_model_id[0] = '\0'; /* Unset */
  336. c->x86_max_cores = 1;
  337. c->x86_coreid_bits = 0;
  338. c->x86_clflush_size = 64;
  339. c->x86_cache_alignment = c->x86_clflush_size;
  340. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  341. generic_identify(c);
  342. c->apicid = phys_pkg_id(0);
  343. /*
  344. * Vendor-specific initialization. In this section we
  345. * canonicalize the feature flags, meaning if there are
  346. * features a certain CPU supports which CPUID doesn't
  347. * tell us, CPUID claiming incorrect flags, or other bugs,
  348. * we handle them here.
  349. *
  350. * At the end of this section, c->x86_capability better
  351. * indicate the features this CPU genuinely supports!
  352. */
  353. if (this_cpu->c_init)
  354. this_cpu->c_init(c);
  355. detect_ht(c);
  356. /*
  357. * On SMP, boot_cpu_data holds the common feature set between
  358. * all CPUs; so make sure that we indicate which features are
  359. * common between the CPUs. The first time this routine gets
  360. * executed, c == &boot_cpu_data.
  361. */
  362. if (c != &boot_cpu_data) {
  363. /* AND the already accumulated flags with these */
  364. for (i = 0; i < NCAPINTS; i++)
  365. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  366. }
  367. /* Clear all flags overriden by options */
  368. for (i = 0; i < NCAPINTS; i++)
  369. c->x86_capability[i] &= ~cleared_cpu_caps[i];
  370. #ifdef CONFIG_X86_MCE
  371. mcheck_init(c);
  372. #endif
  373. select_idle_routine(c);
  374. #ifdef CONFIG_NUMA
  375. numa_add_cpu(smp_processor_id());
  376. #endif
  377. }
  378. void __init identify_boot_cpu(void)
  379. {
  380. identify_cpu(&boot_cpu_data);
  381. }
  382. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  383. {
  384. BUG_ON(c == &boot_cpu_data);
  385. identify_cpu(c);
  386. mtrr_ap_init();
  387. }
  388. struct msr_range {
  389. unsigned min;
  390. unsigned max;
  391. };
  392. static struct msr_range msr_range_array[] __cpuinitdata = {
  393. { 0x00000000, 0x00000418},
  394. { 0xc0000000, 0xc000040b},
  395. { 0xc0010000, 0xc0010142},
  396. { 0xc0011000, 0xc001103b},
  397. };
  398. static void __cpuinit print_cpu_msr(void)
  399. {
  400. unsigned index;
  401. u64 val;
  402. int i;
  403. unsigned index_min, index_max;
  404. for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  405. index_min = msr_range_array[i].min;
  406. index_max = msr_range_array[i].max;
  407. for (index = index_min; index < index_max; index++) {
  408. if (rdmsrl_amd_safe(index, &val))
  409. continue;
  410. printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
  411. }
  412. }
  413. }
  414. static int show_msr __cpuinitdata;
  415. static __init int setup_show_msr(char *arg)
  416. {
  417. int num;
  418. get_option(&arg, &num);
  419. if (num > 0)
  420. show_msr = num;
  421. return 1;
  422. }
  423. __setup("show_msr=", setup_show_msr);
  424. static __init int setup_noclflush(char *arg)
  425. {
  426. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  427. return 1;
  428. }
  429. __setup("noclflush", setup_noclflush);
  430. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  431. {
  432. if (c->x86_model_id[0])
  433. printk(KERN_CONT "%s", c->x86_model_id);
  434. if (c->x86_mask || c->cpuid_level >= 0)
  435. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  436. else
  437. printk(KERN_CONT "\n");
  438. #ifdef CONFIG_SMP
  439. if (c->cpu_index < show_msr)
  440. print_cpu_msr();
  441. #else
  442. if (show_msr)
  443. print_cpu_msr();
  444. #endif
  445. }
  446. static __init int setup_disablecpuid(char *arg)
  447. {
  448. int bit;
  449. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  450. setup_clear_cpu_cap(bit);
  451. else
  452. return 0;
  453. return 1;
  454. }
  455. __setup("clearcpuid=", setup_disablecpuid);
  456. cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
  457. struct x8664_pda **_cpu_pda __read_mostly;
  458. EXPORT_SYMBOL(_cpu_pda);
  459. struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
  460. char boot_cpu_stack[IRQSTACKSIZE] __page_aligned_bss;
  461. unsigned long __supported_pte_mask __read_mostly = ~0UL;
  462. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  463. static int do_not_nx __cpuinitdata;
  464. /* noexec=on|off
  465. Control non executable mappings for 64bit processes.
  466. on Enable(default)
  467. off Disable
  468. */
  469. static int __init nonx_setup(char *str)
  470. {
  471. if (!str)
  472. return -EINVAL;
  473. if (!strncmp(str, "on", 2)) {
  474. __supported_pte_mask |= _PAGE_NX;
  475. do_not_nx = 0;
  476. } else if (!strncmp(str, "off", 3)) {
  477. do_not_nx = 1;
  478. __supported_pte_mask &= ~_PAGE_NX;
  479. }
  480. return 0;
  481. }
  482. early_param("noexec", nonx_setup);
  483. int force_personality32;
  484. /* noexec32=on|off
  485. Control non executable heap for 32bit processes.
  486. To control the stack too use noexec=off
  487. on PROT_READ does not imply PROT_EXEC for 32bit processes (default)
  488. off PROT_READ implies PROT_EXEC
  489. */
  490. static int __init nonx32_setup(char *str)
  491. {
  492. if (!strcmp(str, "on"))
  493. force_personality32 &= ~READ_IMPLIES_EXEC;
  494. else if (!strcmp(str, "off"))
  495. force_personality32 |= READ_IMPLIES_EXEC;
  496. return 1;
  497. }
  498. __setup("noexec32=", nonx32_setup);
  499. void pda_init(int cpu)
  500. {
  501. struct x8664_pda *pda = cpu_pda(cpu);
  502. /* Setup up data that may be needed in __get_free_pages early */
  503. loadsegment(fs, 0);
  504. loadsegment(gs, 0);
  505. /* Memory clobbers used to order PDA accessed */
  506. mb();
  507. wrmsrl(MSR_GS_BASE, pda);
  508. mb();
  509. pda->cpunumber = cpu;
  510. pda->irqcount = -1;
  511. pda->kernelstack = (unsigned long)stack_thread_info() -
  512. PDA_STACKOFFSET + THREAD_SIZE;
  513. pda->active_mm = &init_mm;
  514. pda->mmu_state = 0;
  515. if (cpu == 0) {
  516. /* others are initialized in smpboot.c */
  517. pda->pcurrent = &init_task;
  518. pda->irqstackptr = boot_cpu_stack;
  519. pda->irqstackptr += IRQSTACKSIZE - 64;
  520. } else {
  521. if (!pda->irqstackptr) {
  522. pda->irqstackptr = (char *)
  523. __get_free_pages(GFP_ATOMIC, IRQSTACK_ORDER);
  524. if (!pda->irqstackptr)
  525. panic("cannot allocate irqstack for cpu %d",
  526. cpu);
  527. pda->irqstackptr += IRQSTACKSIZE - 64;
  528. }
  529. if (pda->nodenumber == 0 && cpu_to_node(cpu) != NUMA_NO_NODE)
  530. pda->nodenumber = cpu_to_node(cpu);
  531. }
  532. }
  533. char boot_exception_stacks[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ +
  534. DEBUG_STKSZ] __page_aligned_bss;
  535. extern asmlinkage void ignore_sysret(void);
  536. /* May not be marked __init: used by software suspend */
  537. void syscall_init(void)
  538. {
  539. /*
  540. * LSTAR and STAR live in a bit strange symbiosis.
  541. * They both write to the same internal register. STAR allows to
  542. * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
  543. */
  544. wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
  545. wrmsrl(MSR_LSTAR, system_call);
  546. wrmsrl(MSR_CSTAR, ignore_sysret);
  547. #ifdef CONFIG_IA32_EMULATION
  548. syscall32_cpu_init();
  549. #endif
  550. /* Flags to clear on syscall */
  551. wrmsrl(MSR_SYSCALL_MASK,
  552. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
  553. }
  554. void __cpuinit check_efer(void)
  555. {
  556. unsigned long efer;
  557. rdmsrl(MSR_EFER, efer);
  558. if (!(efer & EFER_NX) || do_not_nx)
  559. __supported_pte_mask &= ~_PAGE_NX;
  560. }
  561. unsigned long kernel_eflags;
  562. /*
  563. * Copies of the original ist values from the tss are only accessed during
  564. * debugging, no special alignment required.
  565. */
  566. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  567. /*
  568. * cpu_init() initializes state that is per-CPU. Some data is already
  569. * initialized (naturally) in the bootstrap process, such as the GDT
  570. * and IDT. We reload them nevertheless, this function acts as a
  571. * 'CPU state barrier', nothing should get across.
  572. * A lot of state is already set up in PDA init.
  573. */
  574. void __cpuinit cpu_init(void)
  575. {
  576. int cpu = stack_smp_processor_id();
  577. struct tss_struct *t = &per_cpu(init_tss, cpu);
  578. struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
  579. unsigned long v;
  580. char *estacks = NULL;
  581. struct task_struct *me;
  582. int i;
  583. /* CPU 0 is initialised in head64.c */
  584. if (cpu != 0)
  585. pda_init(cpu);
  586. else
  587. estacks = boot_exception_stacks;
  588. me = current;
  589. if (cpu_test_and_set(cpu, cpu_initialized))
  590. panic("CPU#%d already initialized!\n", cpu);
  591. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  592. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  593. /*
  594. * Initialize the per-CPU GDT with the boot GDT,
  595. * and set up the GDT descriptor:
  596. */
  597. switch_to_new_gdt();
  598. load_idt((const struct desc_ptr *)&idt_descr);
  599. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  600. syscall_init();
  601. wrmsrl(MSR_FS_BASE, 0);
  602. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  603. barrier();
  604. check_efer();
  605. if (cpu != 0 && x2apic)
  606. enable_x2apic();
  607. /*
  608. * set up and load the per-CPU TSS
  609. */
  610. if (!orig_ist->ist[0]) {
  611. static const unsigned int order[N_EXCEPTION_STACKS] = {
  612. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STACK_ORDER,
  613. [DEBUG_STACK - 1] = DEBUG_STACK_ORDER
  614. };
  615. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  616. if (cpu) {
  617. estacks = (char *)__get_free_pages(GFP_ATOMIC, order[v]);
  618. if (!estacks)
  619. panic("Cannot allocate exception "
  620. "stack %ld %d\n", v, cpu);
  621. }
  622. estacks += PAGE_SIZE << order[v];
  623. orig_ist->ist[v] = t->x86_tss.ist[v] =
  624. (unsigned long)estacks;
  625. }
  626. }
  627. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  628. /*
  629. * <= is required because the CPU will access up to
  630. * 8 bits beyond the end of the IO permission bitmap.
  631. */
  632. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  633. t->io_bitmap[i] = ~0UL;
  634. atomic_inc(&init_mm.mm_count);
  635. me->active_mm = &init_mm;
  636. if (me->mm)
  637. BUG();
  638. enter_lazy_tlb(&init_mm, me);
  639. load_sp0(t, &current->thread);
  640. set_tss_desc(cpu, t);
  641. load_TR_desc();
  642. load_LDT(&init_mm.context);
  643. #ifdef CONFIG_KGDB
  644. /*
  645. * If the kgdb is connected no debug regs should be altered. This
  646. * is only applicable when KGDB and a KGDB I/O module are built
  647. * into the kernel and you are using early debugging with
  648. * kgdbwait. KGDB will control the kernel HW breakpoint registers.
  649. */
  650. if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
  651. arch_kgdb_ops.correct_hw_break();
  652. else {
  653. #endif
  654. /*
  655. * Clear all 6 debug registers:
  656. */
  657. set_debugreg(0UL, 0);
  658. set_debugreg(0UL, 1);
  659. set_debugreg(0UL, 2);
  660. set_debugreg(0UL, 3);
  661. set_debugreg(0UL, 6);
  662. set_debugreg(0UL, 7);
  663. #ifdef CONFIG_KGDB
  664. /* If the kgdb is connected no debug regs should be altered. */
  665. }
  666. #endif
  667. fpu_init();
  668. raw_local_save_flags(kernel_eflags);
  669. if (is_uv_system())
  670. uv_cpu_init();
  671. }