common_64.c 19 KB

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