common.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. #ifdef CONFIG_X86_32
  194. asm("mov %0, %%fs" : : "r" (__KERNEL_PERCPU) : "memory");
  195. #endif
  196. }
  197. static struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  198. static void __cpuinit default_init(struct cpuinfo_x86 *c)
  199. {
  200. /* Not much we can do here... */
  201. /* Check if at least it has cpuid */
  202. if (c->cpuid_level == -1) {
  203. /* No cpuid. It must be an ancient CPU */
  204. if (c->x86 == 4)
  205. strcpy(c->x86_model_id, "486");
  206. else if (c->x86 == 3)
  207. strcpy(c->x86_model_id, "386");
  208. }
  209. }
  210. static struct cpu_dev __cpuinitdata default_cpu = {
  211. .c_init = default_init,
  212. .c_vendor = "Unknown",
  213. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  214. };
  215. int __cpuinit get_model_name(struct cpuinfo_x86 *c)
  216. {
  217. unsigned int *v;
  218. char *p, *q;
  219. if (c->extended_cpuid_level < 0x80000004)
  220. return 0;
  221. v = (unsigned int *) c->x86_model_id;
  222. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  223. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  224. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  225. c->x86_model_id[48] = 0;
  226. /* Intel chips right-justify this string for some dumb reason;
  227. undo that brain damage */
  228. p = q = &c->x86_model_id[0];
  229. while (*p == ' ')
  230. p++;
  231. if (p != q) {
  232. while (*p)
  233. *q++ = *p++;
  234. while (q <= &c->x86_model_id[48])
  235. *q++ = '\0'; /* Zero-pad the rest */
  236. }
  237. return 1;
  238. }
  239. void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
  240. {
  241. unsigned int n, dummy, ebx, ecx, edx, l2size;
  242. n = c->extended_cpuid_level;
  243. if (n >= 0x80000005) {
  244. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  245. printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
  246. edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
  247. c->x86_cache_size = (ecx>>24) + (edx>>24);
  248. }
  249. if (n < 0x80000006) /* Some chips just has a large L1. */
  250. return;
  251. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  252. l2size = ecx >> 16;
  253. /* do processor-specific cache resizing */
  254. if (this_cpu->c_size_cache)
  255. l2size = this_cpu->c_size_cache(c, l2size);
  256. /* Allow user to override all this if necessary. */
  257. if (cachesize_override != -1)
  258. l2size = cachesize_override;
  259. if (l2size == 0)
  260. return; /* Again, no L2 cache is possible */
  261. c->x86_cache_size = l2size;
  262. printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
  263. l2size, ecx & 0xFF);
  264. }
  265. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  266. {
  267. #ifdef CONFIG_X86_HT
  268. u32 eax, ebx, ecx, edx;
  269. int index_msb, core_bits;
  270. if (!cpu_has(c, X86_FEATURE_HT))
  271. return;
  272. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  273. goto out;
  274. cpuid(1, &eax, &ebx, &ecx, &edx);
  275. smp_num_siblings = (ebx & 0xff0000) >> 16;
  276. if (smp_num_siblings == 1) {
  277. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  278. } else if (smp_num_siblings > 1) {
  279. if (smp_num_siblings > NR_CPUS) {
  280. printk(KERN_WARNING "CPU: Unsupported number of siblings %d",
  281. smp_num_siblings);
  282. smp_num_siblings = 1;
  283. return;
  284. }
  285. index_msb = get_count_order(smp_num_siblings);
  286. c->phys_proc_id = phys_pkg_id(c->initial_apicid, index_msb);
  287. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  288. index_msb = get_count_order(smp_num_siblings);
  289. core_bits = get_count_order(c->x86_max_cores);
  290. c->cpu_core_id = phys_pkg_id(c->initial_apicid, index_msb) &
  291. ((1 << core_bits) - 1);
  292. }
  293. out:
  294. if ((c->x86_max_cores * smp_num_siblings) > 1) {
  295. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  296. c->phys_proc_id);
  297. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  298. c->cpu_core_id);
  299. }
  300. #endif
  301. }
  302. static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
  303. {
  304. char *v = c->x86_vendor_id;
  305. int i;
  306. static int printed;
  307. for (i = 0; i < X86_VENDOR_NUM; i++) {
  308. if (!cpu_devs[i])
  309. break;
  310. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  311. (cpu_devs[i]->c_ident[1] &&
  312. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  313. this_cpu = cpu_devs[i];
  314. c->x86_vendor = this_cpu->c_x86_vendor;
  315. return;
  316. }
  317. }
  318. if (!printed) {
  319. printed++;
  320. printk(KERN_ERR "CPU: Vendor unknown, using generic init.\n");
  321. printk(KERN_ERR "CPU: Your system may be unstable.\n");
  322. }
  323. c->x86_vendor = X86_VENDOR_UNKNOWN;
  324. this_cpu = &default_cpu;
  325. }
  326. void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
  327. {
  328. /* Get vendor name */
  329. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  330. (unsigned int *)&c->x86_vendor_id[0],
  331. (unsigned int *)&c->x86_vendor_id[8],
  332. (unsigned int *)&c->x86_vendor_id[4]);
  333. c->x86 = 4;
  334. /* Intel-defined flags: level 0x00000001 */
  335. if (c->cpuid_level >= 0x00000001) {
  336. u32 junk, tfms, cap0, misc;
  337. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  338. c->x86 = (tfms >> 8) & 0xf;
  339. c->x86_model = (tfms >> 4) & 0xf;
  340. c->x86_mask = tfms & 0xf;
  341. if (c->x86 == 0xf)
  342. c->x86 += (tfms >> 20) & 0xff;
  343. if (c->x86 >= 0x6)
  344. c->x86_model += ((tfms >> 16) & 0xf) << 4;
  345. if (cap0 & (1<<19)) {
  346. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  347. c->x86_cache_alignment = c->x86_clflush_size;
  348. }
  349. }
  350. }
  351. static void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
  352. {
  353. u32 tfms, xlvl;
  354. u32 ebx;
  355. /* Intel-defined flags: level 0x00000001 */
  356. if (c->cpuid_level >= 0x00000001) {
  357. u32 capability, excap;
  358. cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  359. c->x86_capability[0] = capability;
  360. c->x86_capability[4] = excap;
  361. }
  362. /* AMD-defined flags: level 0x80000001 */
  363. xlvl = cpuid_eax(0x80000000);
  364. c->extended_cpuid_level = xlvl;
  365. if ((xlvl & 0xffff0000) == 0x80000000) {
  366. if (xlvl >= 0x80000001) {
  367. c->x86_capability[1] = cpuid_edx(0x80000001);
  368. c->x86_capability[6] = cpuid_ecx(0x80000001);
  369. }
  370. }
  371. }
  372. /*
  373. * Do minimum CPU detection early.
  374. * Fields really needed: vendor, cpuid_level, family, model, mask,
  375. * cache alignment.
  376. * The others are not touched to avoid unwanted side effects.
  377. *
  378. * WARNING: this function is only called on the BP. Don't add code here
  379. * that is supposed to run on all CPUs.
  380. */
  381. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  382. {
  383. c->x86_clflush_size = 32;
  384. c->x86_cache_alignment = c->x86_clflush_size;
  385. if (!have_cpuid_p())
  386. return;
  387. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  388. c->extended_cpuid_level = 0;
  389. cpu_detect(c);
  390. get_cpu_vendor(c);
  391. get_cpu_cap(c);
  392. if (this_cpu->c_early_init)
  393. this_cpu->c_early_init(c);
  394. validate_pat_support(c);
  395. }
  396. void __init early_cpu_init(void)
  397. {
  398. struct cpu_dev **cdev;
  399. int count = 0;
  400. printk("KERNEL supported cpus:\n");
  401. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  402. struct cpu_dev *cpudev = *cdev;
  403. unsigned int j;
  404. if (count >= X86_VENDOR_NUM)
  405. break;
  406. cpu_devs[count] = cpudev;
  407. count++;
  408. for (j = 0; j < 2; j++) {
  409. if (!cpudev->c_ident[j])
  410. continue;
  411. printk(" %s %s\n", cpudev->c_vendor,
  412. cpudev->c_ident[j]);
  413. }
  414. }
  415. early_identify_cpu(&boot_cpu_data);
  416. }
  417. /*
  418. * The NOPL instruction is supposed to exist on all CPUs with
  419. * family >= 6, unfortunately, that's not true in practice because
  420. * of early VIA chips and (more importantly) broken virtualizers that
  421. * are not easy to detect. Hence, probe for it based on first
  422. * principles.
  423. */
  424. static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
  425. {
  426. const u32 nopl_signature = 0x888c53b1; /* Random number */
  427. u32 has_nopl = nopl_signature;
  428. clear_cpu_cap(c, X86_FEATURE_NOPL);
  429. if (c->x86 >= 6) {
  430. asm volatile("\n"
  431. "1: .byte 0x0f,0x1f,0xc0\n" /* nopl %eax */
  432. "2:\n"
  433. " .section .fixup,\"ax\"\n"
  434. "3: xor %0,%0\n"
  435. " jmp 2b\n"
  436. " .previous\n"
  437. _ASM_EXTABLE(1b,3b)
  438. : "+a" (has_nopl));
  439. if (has_nopl == nopl_signature)
  440. set_cpu_cap(c, X86_FEATURE_NOPL);
  441. }
  442. }
  443. static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
  444. {
  445. if (!have_cpuid_p())
  446. return;
  447. c->extended_cpuid_level = 0;
  448. cpu_detect(c);
  449. get_cpu_vendor(c);
  450. get_cpu_cap(c);
  451. if (c->cpuid_level >= 0x00000001) {
  452. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  453. #ifdef CONFIG_X86_HT
  454. c->apicid = phys_pkg_id(c->initial_apicid, 0);
  455. c->phys_proc_id = c->initial_apicid;
  456. #else
  457. c->apicid = c->initial_apicid;
  458. #endif
  459. }
  460. if (c->extended_cpuid_level >= 0x80000004)
  461. get_model_name(c); /* Default name */
  462. init_scattered_cpuid_features(c);
  463. detect_nopl(c);
  464. }
  465. /*
  466. * This does the hard work of actually picking apart the CPU stuff...
  467. */
  468. static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  469. {
  470. int i;
  471. c->loops_per_jiffy = loops_per_jiffy;
  472. c->x86_cache_size = -1;
  473. c->x86_vendor = X86_VENDOR_UNKNOWN;
  474. c->cpuid_level = -1; /* CPUID not detected */
  475. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  476. c->x86_vendor_id[0] = '\0'; /* Unset */
  477. c->x86_model_id[0] = '\0'; /* Unset */
  478. c->x86_max_cores = 1;
  479. c->x86_clflush_size = 32;
  480. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  481. if (!have_cpuid_p()) {
  482. /*
  483. * First of all, decide if this is a 486 or higher
  484. * It's a 486 if we can modify the AC flag
  485. */
  486. if (flag_is_changeable_p(X86_EFLAGS_AC))
  487. c->x86 = 4;
  488. else
  489. c->x86 = 3;
  490. }
  491. generic_identify(c);
  492. if (this_cpu->c_identify)
  493. this_cpu->c_identify(c);
  494. /*
  495. * Vendor-specific initialization. In this section we
  496. * canonicalize the feature flags, meaning if there are
  497. * features a certain CPU supports which CPUID doesn't
  498. * tell us, CPUID claiming incorrect flags, or other bugs,
  499. * we handle them here.
  500. *
  501. * At the end of this section, c->x86_capability better
  502. * indicate the features this CPU genuinely supports!
  503. */
  504. if (this_cpu->c_init)
  505. this_cpu->c_init(c);
  506. /* Disable the PN if appropriate */
  507. squash_the_stupid_serial_number(c);
  508. /*
  509. * The vendor-specific functions might have changed features. Now
  510. * we do "generic changes."
  511. */
  512. /* If the model name is still unset, do table lookup. */
  513. if (!c->x86_model_id[0]) {
  514. char *p;
  515. p = table_lookup_model(c);
  516. if (p)
  517. strcpy(c->x86_model_id, p);
  518. else
  519. /* Last resort... */
  520. sprintf(c->x86_model_id, "%02x/%02x",
  521. c->x86, c->x86_model);
  522. }
  523. /*
  524. * On SMP, boot_cpu_data holds the common feature set between
  525. * all CPUs; so make sure that we indicate which features are
  526. * common between the CPUs. The first time this routine gets
  527. * executed, c == &boot_cpu_data.
  528. */
  529. if (c != &boot_cpu_data) {
  530. /* AND the already accumulated flags with these */
  531. for (i = 0; i < NCAPINTS; i++)
  532. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  533. }
  534. /* Clear all flags overriden by options */
  535. for (i = 0; i < NCAPINTS; i++)
  536. c->x86_capability[i] &= ~cleared_cpu_caps[i];
  537. /* Init Machine Check Exception if available. */
  538. mcheck_init(c);
  539. select_idle_routine(c);
  540. }
  541. void __init identify_boot_cpu(void)
  542. {
  543. identify_cpu(&boot_cpu_data);
  544. sysenter_setup();
  545. enable_sep_cpu();
  546. }
  547. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  548. {
  549. BUG_ON(c == &boot_cpu_data);
  550. identify_cpu(c);
  551. enable_sep_cpu();
  552. mtrr_ap_init();
  553. }
  554. struct msr_range {
  555. unsigned min;
  556. unsigned max;
  557. };
  558. static struct msr_range msr_range_array[] __cpuinitdata = {
  559. { 0x00000000, 0x00000418},
  560. { 0xc0000000, 0xc000040b},
  561. { 0xc0010000, 0xc0010142},
  562. { 0xc0011000, 0xc001103b},
  563. };
  564. static void __cpuinit print_cpu_msr(void)
  565. {
  566. unsigned index;
  567. u64 val;
  568. int i;
  569. unsigned index_min, index_max;
  570. for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  571. index_min = msr_range_array[i].min;
  572. index_max = msr_range_array[i].max;
  573. for (index = index_min; index < index_max; index++) {
  574. if (rdmsrl_amd_safe(index, &val))
  575. continue;
  576. printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
  577. }
  578. }
  579. }
  580. static int show_msr __cpuinitdata;
  581. static __init int setup_show_msr(char *arg)
  582. {
  583. int num;
  584. get_option(&arg, &num);
  585. if (num > 0)
  586. show_msr = num;
  587. return 1;
  588. }
  589. __setup("show_msr=", setup_show_msr);
  590. static __init int setup_noclflush(char *arg)
  591. {
  592. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  593. return 1;
  594. }
  595. __setup("noclflush", setup_noclflush);
  596. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  597. {
  598. char *vendor = NULL;
  599. if (c->x86_vendor < X86_VENDOR_NUM)
  600. vendor = this_cpu->c_vendor;
  601. else if (c->cpuid_level >= 0)
  602. vendor = c->x86_vendor_id;
  603. if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
  604. printk(KERN_CONT "%s ", vendor);
  605. if (c->x86_model_id[0])
  606. printk(KERN_CONT "%s", c->x86_model_id);
  607. else
  608. printk(KERN_CONT "%d86", c->x86);
  609. if (c->x86_mask || c->cpuid_level >= 0)
  610. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  611. else
  612. printk(KERN_CONT "\n");
  613. #ifdef CONFIG_SMP
  614. if (c->cpu_index < show_msr)
  615. print_cpu_msr();
  616. #else
  617. if (show_msr)
  618. print_cpu_msr();
  619. #endif
  620. }
  621. static __init int setup_disablecpuid(char *arg)
  622. {
  623. int bit;
  624. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  625. setup_clear_cpu_cap(bit);
  626. else
  627. return 0;
  628. return 1;
  629. }
  630. __setup("clearcpuid=", setup_disablecpuid);
  631. cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
  632. #ifdef CONFIG_X86_64
  633. struct x8664_pda **_cpu_pda __read_mostly;
  634. EXPORT_SYMBOL(_cpu_pda);
  635. struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
  636. char boot_cpu_stack[IRQSTACKSIZE] __page_aligned_bss;
  637. unsigned long __supported_pte_mask __read_mostly = ~0UL;
  638. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  639. static int do_not_nx __cpuinitdata;
  640. /* noexec=on|off
  641. Control non executable mappings for 64bit processes.
  642. on Enable(default)
  643. off Disable
  644. */
  645. static int __init nonx_setup(char *str)
  646. {
  647. if (!str)
  648. return -EINVAL;
  649. if (!strncmp(str, "on", 2)) {
  650. __supported_pte_mask |= _PAGE_NX;
  651. do_not_nx = 0;
  652. } else if (!strncmp(str, "off", 3)) {
  653. do_not_nx = 1;
  654. __supported_pte_mask &= ~_PAGE_NX;
  655. }
  656. return 0;
  657. }
  658. early_param("noexec", nonx_setup);
  659. int force_personality32;
  660. /* noexec32=on|off
  661. Control non executable heap for 32bit processes.
  662. To control the stack too use noexec=off
  663. on PROT_READ does not imply PROT_EXEC for 32bit processes (default)
  664. off PROT_READ implies PROT_EXEC
  665. */
  666. static int __init nonx32_setup(char *str)
  667. {
  668. if (!strcmp(str, "on"))
  669. force_personality32 &= ~READ_IMPLIES_EXEC;
  670. else if (!strcmp(str, "off"))
  671. force_personality32 |= READ_IMPLIES_EXEC;
  672. return 1;
  673. }
  674. __setup("noexec32=", nonx32_setup);
  675. void pda_init(int cpu)
  676. {
  677. struct x8664_pda *pda = cpu_pda(cpu);
  678. /* Setup up data that may be needed in __get_free_pages early */
  679. loadsegment(fs, 0);
  680. loadsegment(gs, 0);
  681. /* Memory clobbers used to order PDA accessed */
  682. mb();
  683. wrmsrl(MSR_GS_BASE, pda);
  684. mb();
  685. pda->cpunumber = cpu;
  686. pda->irqcount = -1;
  687. pda->kernelstack = (unsigned long)stack_thread_info() -
  688. PDA_STACKOFFSET + THREAD_SIZE;
  689. pda->active_mm = &init_mm;
  690. pda->mmu_state = 0;
  691. if (cpu == 0) {
  692. /* others are initialized in smpboot.c */
  693. pda->pcurrent = &init_task;
  694. pda->irqstackptr = boot_cpu_stack;
  695. pda->irqstackptr += IRQSTACKSIZE - 64;
  696. } else {
  697. if (!pda->irqstackptr) {
  698. pda->irqstackptr = (char *)
  699. __get_free_pages(GFP_ATOMIC, IRQSTACK_ORDER);
  700. if (!pda->irqstackptr)
  701. panic("cannot allocate irqstack for cpu %d",
  702. cpu);
  703. pda->irqstackptr += IRQSTACKSIZE - 64;
  704. }
  705. if (pda->nodenumber == 0 && cpu_to_node(cpu) != NUMA_NO_NODE)
  706. pda->nodenumber = cpu_to_node(cpu);
  707. }
  708. }
  709. char boot_exception_stacks[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ +
  710. DEBUG_STKSZ] __page_aligned_bss;
  711. extern asmlinkage void ignore_sysret(void);
  712. /* May not be marked __init: used by software suspend */
  713. void syscall_init(void)
  714. {
  715. /*
  716. * LSTAR and STAR live in a bit strange symbiosis.
  717. * They both write to the same internal register. STAR allows to
  718. * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
  719. */
  720. wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
  721. wrmsrl(MSR_LSTAR, system_call);
  722. wrmsrl(MSR_CSTAR, ignore_sysret);
  723. #ifdef CONFIG_IA32_EMULATION
  724. syscall32_cpu_init();
  725. #endif
  726. /* Flags to clear on syscall */
  727. wrmsrl(MSR_SYSCALL_MASK,
  728. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
  729. }
  730. void __cpuinit check_efer(void)
  731. {
  732. unsigned long efer;
  733. rdmsrl(MSR_EFER, efer);
  734. if (!(efer & EFER_NX) || do_not_nx)
  735. __supported_pte_mask &= ~_PAGE_NX;
  736. }
  737. unsigned long kernel_eflags;
  738. /*
  739. * Copies of the original ist values from the tss are only accessed during
  740. * debugging, no special alignment required.
  741. */
  742. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  743. #else
  744. /* Make sure %fs is initialized properly in idle threads */
  745. struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
  746. {
  747. memset(regs, 0, sizeof(struct pt_regs));
  748. regs->fs = __KERNEL_PERCPU;
  749. return regs;
  750. }
  751. #endif
  752. /*
  753. * cpu_init() initializes state that is per-CPU. Some data is already
  754. * initialized (naturally) in the bootstrap process, such as the GDT
  755. * and IDT. We reload them nevertheless, this function acts as a
  756. * 'CPU state barrier', nothing should get across.
  757. * A lot of state is already set up in PDA init for 64 bit
  758. */
  759. #ifdef CONFIG_X86_64
  760. void __cpuinit cpu_init(void)
  761. {
  762. int cpu = stack_smp_processor_id();
  763. struct tss_struct *t = &per_cpu(init_tss, cpu);
  764. struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
  765. unsigned long v;
  766. char *estacks = NULL;
  767. struct task_struct *me;
  768. int i;
  769. /* CPU 0 is initialised in head64.c */
  770. if (cpu != 0)
  771. pda_init(cpu);
  772. else
  773. estacks = boot_exception_stacks;
  774. me = current;
  775. if (cpu_test_and_set(cpu, cpu_initialized))
  776. panic("CPU#%d already initialized!\n", cpu);
  777. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  778. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  779. /*
  780. * Initialize the per-CPU GDT with the boot GDT,
  781. * and set up the GDT descriptor:
  782. */
  783. switch_to_new_gdt();
  784. load_idt((const struct desc_ptr *)&idt_descr);
  785. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  786. syscall_init();
  787. wrmsrl(MSR_FS_BASE, 0);
  788. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  789. barrier();
  790. check_efer();
  791. if (cpu != 0 && x2apic)
  792. enable_x2apic();
  793. /*
  794. * set up and load the per-CPU TSS
  795. */
  796. if (!orig_ist->ist[0]) {
  797. static const unsigned int order[N_EXCEPTION_STACKS] = {
  798. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STACK_ORDER,
  799. [DEBUG_STACK - 1] = DEBUG_STACK_ORDER
  800. };
  801. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  802. if (cpu) {
  803. estacks = (char *)__get_free_pages(GFP_ATOMIC, order[v]);
  804. if (!estacks)
  805. panic("Cannot allocate exception "
  806. "stack %ld %d\n", v, cpu);
  807. }
  808. estacks += PAGE_SIZE << order[v];
  809. orig_ist->ist[v] = t->x86_tss.ist[v] =
  810. (unsigned long)estacks;
  811. }
  812. }
  813. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  814. /*
  815. * <= is required because the CPU will access up to
  816. * 8 bits beyond the end of the IO permission bitmap.
  817. */
  818. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  819. t->io_bitmap[i] = ~0UL;
  820. atomic_inc(&init_mm.mm_count);
  821. me->active_mm = &init_mm;
  822. if (me->mm)
  823. BUG();
  824. enter_lazy_tlb(&init_mm, me);
  825. load_sp0(t, &current->thread);
  826. set_tss_desc(cpu, t);
  827. load_TR_desc();
  828. load_LDT(&init_mm.context);
  829. #ifdef CONFIG_KGDB
  830. /*
  831. * If the kgdb is connected no debug regs should be altered. This
  832. * is only applicable when KGDB and a KGDB I/O module are built
  833. * into the kernel and you are using early debugging with
  834. * kgdbwait. KGDB will control the kernel HW breakpoint registers.
  835. */
  836. if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
  837. arch_kgdb_ops.correct_hw_break();
  838. else {
  839. #endif
  840. /*
  841. * Clear all 6 debug registers:
  842. */
  843. set_debugreg(0UL, 0);
  844. set_debugreg(0UL, 1);
  845. set_debugreg(0UL, 2);
  846. set_debugreg(0UL, 3);
  847. set_debugreg(0UL, 6);
  848. set_debugreg(0UL, 7);
  849. #ifdef CONFIG_KGDB
  850. /* If the kgdb is connected no debug regs should be altered. */
  851. }
  852. #endif
  853. fpu_init();
  854. raw_local_save_flags(kernel_eflags);
  855. if (is_uv_system())
  856. uv_cpu_init();
  857. }
  858. #else
  859. void __cpuinit cpu_init(void)
  860. {
  861. int cpu = smp_processor_id();
  862. struct task_struct *curr = current;
  863. struct tss_struct *t = &per_cpu(init_tss, cpu);
  864. struct thread_struct *thread = &curr->thread;
  865. if (cpu_test_and_set(cpu, cpu_initialized)) {
  866. printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
  867. for (;;) local_irq_enable();
  868. }
  869. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  870. if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
  871. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  872. load_idt(&idt_descr);
  873. switch_to_new_gdt();
  874. /*
  875. * Set up and load the per-CPU TSS and LDT
  876. */
  877. atomic_inc(&init_mm.mm_count);
  878. curr->active_mm = &init_mm;
  879. if (curr->mm)
  880. BUG();
  881. enter_lazy_tlb(&init_mm, curr);
  882. load_sp0(t, thread);
  883. set_tss_desc(cpu, t);
  884. load_TR_desc();
  885. load_LDT(&init_mm.context);
  886. #ifdef CONFIG_DOUBLEFAULT
  887. /* Set up doublefault TSS pointer in the GDT */
  888. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  889. #endif
  890. /* Clear %gs. */
  891. asm volatile ("mov %0, %%gs" : : "r" (0));
  892. /* Clear all 6 debug registers: */
  893. set_debugreg(0, 0);
  894. set_debugreg(0, 1);
  895. set_debugreg(0, 2);
  896. set_debugreg(0, 3);
  897. set_debugreg(0, 6);
  898. set_debugreg(0, 7);
  899. /*
  900. * Force FPU initialization:
  901. */
  902. if (cpu_has_xsave)
  903. current_thread_info()->status = TS_XSAVE;
  904. else
  905. current_thread_info()->status = 0;
  906. clear_used_math();
  907. mxcsr_feature_mask_init();
  908. /*
  909. * Boot processor to setup the FP and extended state context info.
  910. */
  911. if (!smp_processor_id())
  912. init_thread_xstate();
  913. xsave_init();
  914. }
  915. #ifdef CONFIG_HOTPLUG_CPU
  916. void __cpuinit cpu_uninit(void)
  917. {
  918. int cpu = raw_smp_processor_id();
  919. cpu_clear(cpu, cpu_initialized);
  920. /* lazy TLB state */
  921. per_cpu(cpu_tlbstate, cpu).state = 0;
  922. per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;
  923. }
  924. #endif
  925. #endif