common_64.c 18 KB

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