common_64.c 24 KB

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