common.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. #include <asm/smp.h>
  24. #include <asm/cpu.h>
  25. #include <asm/cpumask.h>
  26. #include <asm/apic.h>
  27. #ifdef CONFIG_X86_LOCAL_APIC
  28. #include <asm/uv/uv.h>
  29. #endif
  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 <asm/hypervisor.h>
  38. #include <asm/stackprotector.h>
  39. #include "cpu.h"
  40. /* all of these masks are initialized in setup_cpu_local_masks() */
  41. cpumask_var_t cpu_callin_mask;
  42. cpumask_var_t cpu_callout_mask;
  43. cpumask_var_t cpu_initialized_mask;
  44. /* representing cpus for which sibling maps can be computed */
  45. cpumask_var_t cpu_sibling_setup_mask;
  46. /* correctly size the local cpu masks */
  47. void __init setup_cpu_local_masks(void)
  48. {
  49. alloc_bootmem_cpumask_var(&cpu_initialized_mask);
  50. alloc_bootmem_cpumask_var(&cpu_callin_mask);
  51. alloc_bootmem_cpumask_var(&cpu_callout_mask);
  52. alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
  53. }
  54. static struct cpu_dev *this_cpu __cpuinitdata;
  55. DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
  56. #ifdef CONFIG_X86_64
  57. /*
  58. * We need valid kernel segments for data and code in long mode too
  59. * IRET will check the segment types kkeil 2000/10/28
  60. * Also sysret mandates a special GDT layout
  61. *
  62. * The TLS descriptors are currently at a different place compared to i386.
  63. * Hopefully nobody expects them at a fixed place (Wine?)
  64. */
  65. [GDT_ENTRY_KERNEL32_CS] = { { { 0x0000ffff, 0x00cf9b00 } } },
  66. [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00af9b00 } } },
  67. [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9300 } } },
  68. [GDT_ENTRY_DEFAULT_USER32_CS] = { { { 0x0000ffff, 0x00cffb00 } } },
  69. [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff300 } } },
  70. [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00affb00 } } },
  71. #else
  72. [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00cf9a00 } } },
  73. [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9200 } } },
  74. [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00cffa00 } } },
  75. [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff200 } } },
  76. /*
  77. * Segments used for calling PnP BIOS have byte granularity.
  78. * They code segments and data segments have fixed 64k limits,
  79. * the transfer segment sizes are set at run time.
  80. */
  81. /* 32-bit code */
  82. [GDT_ENTRY_PNPBIOS_CS32] = { { { 0x0000ffff, 0x00409a00 } } },
  83. /* 16-bit code */
  84. [GDT_ENTRY_PNPBIOS_CS16] = { { { 0x0000ffff, 0x00009a00 } } },
  85. /* 16-bit data */
  86. [GDT_ENTRY_PNPBIOS_DS] = { { { 0x0000ffff, 0x00009200 } } },
  87. /* 16-bit data */
  88. [GDT_ENTRY_PNPBIOS_TS1] = { { { 0x00000000, 0x00009200 } } },
  89. /* 16-bit data */
  90. [GDT_ENTRY_PNPBIOS_TS2] = { { { 0x00000000, 0x00009200 } } },
  91. /*
  92. * The APM segments have byte granularity and their bases
  93. * are set at run time. All have 64k limits.
  94. */
  95. /* 32-bit code */
  96. [GDT_ENTRY_APMBIOS_BASE] = { { { 0x0000ffff, 0x00409a00 } } },
  97. /* 16-bit code */
  98. [GDT_ENTRY_APMBIOS_BASE+1] = { { { 0x0000ffff, 0x00009a00 } } },
  99. /* data */
  100. [GDT_ENTRY_APMBIOS_BASE+2] = { { { 0x0000ffff, 0x00409200 } } },
  101. [GDT_ENTRY_ESPFIX_SS] = { { { 0x00000000, 0x00c09200 } } },
  102. [GDT_ENTRY_PERCPU] = { { { 0x0000ffff, 0x00cf9200 } } },
  103. GDT_STACK_CANARY_INIT
  104. #endif
  105. } };
  106. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  107. #ifdef CONFIG_X86_32
  108. static int cachesize_override __cpuinitdata = -1;
  109. static int disable_x86_serial_nr __cpuinitdata = 1;
  110. static int __init cachesize_setup(char *str)
  111. {
  112. get_option(&str, &cachesize_override);
  113. return 1;
  114. }
  115. __setup("cachesize=", cachesize_setup);
  116. static int __init x86_fxsr_setup(char *s)
  117. {
  118. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  119. setup_clear_cpu_cap(X86_FEATURE_XMM);
  120. return 1;
  121. }
  122. __setup("nofxsr", x86_fxsr_setup);
  123. static int __init x86_sep_setup(char *s)
  124. {
  125. setup_clear_cpu_cap(X86_FEATURE_SEP);
  126. return 1;
  127. }
  128. __setup("nosep", x86_sep_setup);
  129. /* Standard macro to see if a specific flag is changeable */
  130. static inline int flag_is_changeable_p(u32 flag)
  131. {
  132. u32 f1, f2;
  133. /*
  134. * Cyrix and IDT cpus allow disabling of CPUID
  135. * so the code below may return different results
  136. * when it is executed before and after enabling
  137. * the CPUID. Add "volatile" to not allow gcc to
  138. * optimize the subsequent calls to this function.
  139. */
  140. asm volatile ("pushfl\n\t"
  141. "pushfl\n\t"
  142. "popl %0\n\t"
  143. "movl %0,%1\n\t"
  144. "xorl %2,%0\n\t"
  145. "pushl %0\n\t"
  146. "popfl\n\t"
  147. "pushfl\n\t"
  148. "popl %0\n\t"
  149. "popfl\n\t"
  150. : "=&r" (f1), "=&r" (f2)
  151. : "ir" (flag));
  152. return ((f1^f2) & flag) != 0;
  153. }
  154. /* Probe for the CPUID instruction */
  155. static int __cpuinit have_cpuid_p(void)
  156. {
  157. return flag_is_changeable_p(X86_EFLAGS_ID);
  158. }
  159. static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  160. {
  161. if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr) {
  162. /* Disable processor serial number */
  163. unsigned long lo, hi;
  164. rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  165. lo |= 0x200000;
  166. wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  167. printk(KERN_NOTICE "CPU serial number disabled.\n");
  168. clear_cpu_cap(c, X86_FEATURE_PN);
  169. /* Disabling the serial number may affect the cpuid level */
  170. c->cpuid_level = cpuid_eax(0);
  171. }
  172. }
  173. static int __init x86_serial_nr_setup(char *s)
  174. {
  175. disable_x86_serial_nr = 0;
  176. return 1;
  177. }
  178. __setup("serialnumber", x86_serial_nr_setup);
  179. #else
  180. static inline int flag_is_changeable_p(u32 flag)
  181. {
  182. return 1;
  183. }
  184. /* Probe for the CPUID instruction */
  185. static inline int have_cpuid_p(void)
  186. {
  187. return 1;
  188. }
  189. static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  190. {
  191. }
  192. #endif
  193. /*
  194. * Some CPU features depend on higher CPUID levels, which may not always
  195. * be available due to CPUID level capping or broken virtualization
  196. * software. Add those features to this table to auto-disable them.
  197. */
  198. struct cpuid_dependent_feature {
  199. u32 feature;
  200. u32 level;
  201. };
  202. static const struct cpuid_dependent_feature __cpuinitconst
  203. cpuid_dependent_features[] = {
  204. { X86_FEATURE_MWAIT, 0x00000005 },
  205. { X86_FEATURE_DCA, 0x00000009 },
  206. { X86_FEATURE_XSAVE, 0x0000000d },
  207. { 0, 0 }
  208. };
  209. static void __cpuinit filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
  210. {
  211. const struct cpuid_dependent_feature *df;
  212. for (df = cpuid_dependent_features; df->feature; df++) {
  213. /*
  214. * Note: cpuid_level is set to -1 if unavailable, but
  215. * extended_extended_level is set to 0 if unavailable
  216. * and the legitimate extended levels are all negative
  217. * when signed; hence the weird messing around with
  218. * signs here...
  219. */
  220. if (cpu_has(c, df->feature) &&
  221. ((s32)df->level < 0 ?
  222. (u32)df->level > (u32)c->extended_cpuid_level :
  223. (s32)df->level > (s32)c->cpuid_level)) {
  224. clear_cpu_cap(c, df->feature);
  225. if (warn)
  226. printk(KERN_WARNING
  227. "CPU: CPU feature %s disabled "
  228. "due to lack of CPUID level 0x%x\n",
  229. x86_cap_flags[df->feature],
  230. df->level);
  231. }
  232. }
  233. }
  234. /*
  235. * Naming convention should be: <Name> [(<Codename>)]
  236. * This table only is used unless init_<vendor>() below doesn't set it;
  237. * in particular, if CPUID levels 0x80000002..4 are supported, this isn't used
  238. *
  239. */
  240. /* Look up CPU names by table lookup. */
  241. static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
  242. {
  243. struct cpu_model_info *info;
  244. if (c->x86_model >= 16)
  245. return NULL; /* Range check */
  246. if (!this_cpu)
  247. return NULL;
  248. info = this_cpu->c_models;
  249. while (info && info->family) {
  250. if (info->family == c->x86)
  251. return info->model_names[c->x86_model];
  252. info++;
  253. }
  254. return NULL; /* Not found */
  255. }
  256. __u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
  257. void load_percpu_segment(int cpu)
  258. {
  259. #ifdef CONFIG_X86_32
  260. loadsegment(fs, __KERNEL_PERCPU);
  261. #else
  262. loadsegment(gs, 0);
  263. wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
  264. #endif
  265. load_stack_canary_segment();
  266. }
  267. /* Current gdt points %fs at the "master" per-cpu area: after this,
  268. * it's on the real one. */
  269. void switch_to_new_gdt(int cpu)
  270. {
  271. struct desc_ptr gdt_descr;
  272. gdt_descr.address = (long)get_cpu_gdt_table(cpu);
  273. gdt_descr.size = GDT_SIZE - 1;
  274. load_gdt(&gdt_descr);
  275. /* Reload the per-cpu base */
  276. load_percpu_segment(cpu);
  277. }
  278. static struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  279. static void __cpuinit default_init(struct cpuinfo_x86 *c)
  280. {
  281. #ifdef CONFIG_X86_64
  282. display_cacheinfo(c);
  283. #else
  284. /* Not much we can do here... */
  285. /* Check if at least it has cpuid */
  286. if (c->cpuid_level == -1) {
  287. /* No cpuid. It must be an ancient CPU */
  288. if (c->x86 == 4)
  289. strcpy(c->x86_model_id, "486");
  290. else if (c->x86 == 3)
  291. strcpy(c->x86_model_id, "386");
  292. }
  293. #endif
  294. }
  295. static struct cpu_dev __cpuinitdata default_cpu = {
  296. .c_init = default_init,
  297. .c_vendor = "Unknown",
  298. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  299. };
  300. static void __cpuinit get_model_name(struct cpuinfo_x86 *c)
  301. {
  302. unsigned int *v;
  303. char *p, *q;
  304. if (c->extended_cpuid_level < 0x80000004)
  305. return;
  306. v = (unsigned int *) c->x86_model_id;
  307. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  308. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  309. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  310. c->x86_model_id[48] = 0;
  311. /* Intel chips right-justify this string for some dumb reason;
  312. undo that brain damage */
  313. p = q = &c->x86_model_id[0];
  314. while (*p == ' ')
  315. p++;
  316. if (p != q) {
  317. while (*p)
  318. *q++ = *p++;
  319. while (q <= &c->x86_model_id[48])
  320. *q++ = '\0'; /* Zero-pad the rest */
  321. }
  322. }
  323. void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
  324. {
  325. unsigned int n, dummy, ebx, ecx, edx, l2size;
  326. n = c->extended_cpuid_level;
  327. if (n >= 0x80000005) {
  328. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  329. printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
  330. edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
  331. c->x86_cache_size = (ecx>>24) + (edx>>24);
  332. #ifdef CONFIG_X86_64
  333. /* On K8 L1 TLB is inclusive, so don't count it */
  334. c->x86_tlbsize = 0;
  335. #endif
  336. }
  337. if (n < 0x80000006) /* Some chips just has a large L1. */
  338. return;
  339. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  340. l2size = ecx >> 16;
  341. #ifdef CONFIG_X86_64
  342. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  343. #else
  344. /* do processor-specific cache resizing */
  345. if (this_cpu->c_size_cache)
  346. l2size = this_cpu->c_size_cache(c, l2size);
  347. /* Allow user to override all this if necessary. */
  348. if (cachesize_override != -1)
  349. l2size = cachesize_override;
  350. if (l2size == 0)
  351. return; /* Again, no L2 cache is possible */
  352. #endif
  353. c->x86_cache_size = l2size;
  354. printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
  355. l2size, ecx & 0xFF);
  356. }
  357. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  358. {
  359. #ifdef CONFIG_X86_HT
  360. u32 eax, ebx, ecx, edx;
  361. int index_msb, core_bits;
  362. if (!cpu_has(c, X86_FEATURE_HT))
  363. return;
  364. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  365. goto out;
  366. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  367. return;
  368. cpuid(1, &eax, &ebx, &ecx, &edx);
  369. smp_num_siblings = (ebx & 0xff0000) >> 16;
  370. if (smp_num_siblings == 1) {
  371. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  372. } else if (smp_num_siblings > 1) {
  373. if (smp_num_siblings > nr_cpu_ids) {
  374. printk(KERN_WARNING "CPU: Unsupported number of siblings %d",
  375. smp_num_siblings);
  376. smp_num_siblings = 1;
  377. return;
  378. }
  379. index_msb = get_count_order(smp_num_siblings);
  380. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
  381. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  382. index_msb = get_count_order(smp_num_siblings);
  383. core_bits = get_count_order(c->x86_max_cores);
  384. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
  385. ((1 << core_bits) - 1);
  386. }
  387. out:
  388. if ((c->x86_max_cores * smp_num_siblings) > 1) {
  389. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  390. c->phys_proc_id);
  391. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  392. c->cpu_core_id);
  393. }
  394. #endif
  395. }
  396. static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
  397. {
  398. char *v = c->x86_vendor_id;
  399. int i;
  400. static int printed;
  401. for (i = 0; i < X86_VENDOR_NUM; i++) {
  402. if (!cpu_devs[i])
  403. break;
  404. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  405. (cpu_devs[i]->c_ident[1] &&
  406. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  407. this_cpu = cpu_devs[i];
  408. c->x86_vendor = this_cpu->c_x86_vendor;
  409. return;
  410. }
  411. }
  412. if (!printed) {
  413. printed++;
  414. printk(KERN_ERR "CPU: vendor_id '%s' unknown, using generic init.\n", v);
  415. printk(KERN_ERR "CPU: Your system may be unstable.\n");
  416. }
  417. c->x86_vendor = X86_VENDOR_UNKNOWN;
  418. this_cpu = &default_cpu;
  419. }
  420. void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
  421. {
  422. /* Get vendor name */
  423. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  424. (unsigned int *)&c->x86_vendor_id[0],
  425. (unsigned int *)&c->x86_vendor_id[8],
  426. (unsigned int *)&c->x86_vendor_id[4]);
  427. c->x86 = 4;
  428. /* Intel-defined flags: level 0x00000001 */
  429. if (c->cpuid_level >= 0x00000001) {
  430. u32 junk, tfms, cap0, misc;
  431. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  432. c->x86 = (tfms >> 8) & 0xf;
  433. c->x86_model = (tfms >> 4) & 0xf;
  434. c->x86_mask = tfms & 0xf;
  435. if (c->x86 == 0xf)
  436. c->x86 += (tfms >> 20) & 0xff;
  437. if (c->x86 >= 0x6)
  438. c->x86_model += ((tfms >> 16) & 0xf) << 4;
  439. if (cap0 & (1<<19)) {
  440. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  441. c->x86_cache_alignment = c->x86_clflush_size;
  442. }
  443. }
  444. }
  445. static void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
  446. {
  447. u32 tfms, xlvl;
  448. u32 ebx;
  449. /* Intel-defined flags: level 0x00000001 */
  450. if (c->cpuid_level >= 0x00000001) {
  451. u32 capability, excap;
  452. cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  453. c->x86_capability[0] = capability;
  454. c->x86_capability[4] = excap;
  455. }
  456. /* AMD-defined flags: level 0x80000001 */
  457. xlvl = cpuid_eax(0x80000000);
  458. c->extended_cpuid_level = xlvl;
  459. if ((xlvl & 0xffff0000) == 0x80000000) {
  460. if (xlvl >= 0x80000001) {
  461. c->x86_capability[1] = cpuid_edx(0x80000001);
  462. c->x86_capability[6] = cpuid_ecx(0x80000001);
  463. }
  464. }
  465. #ifdef CONFIG_X86_64
  466. if (c->extended_cpuid_level >= 0x80000008) {
  467. u32 eax = cpuid_eax(0x80000008);
  468. c->x86_virt_bits = (eax >> 8) & 0xff;
  469. c->x86_phys_bits = eax & 0xff;
  470. }
  471. #endif
  472. if (c->extended_cpuid_level >= 0x80000007)
  473. c->x86_power = cpuid_edx(0x80000007);
  474. }
  475. static void __cpuinit identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  476. {
  477. #ifdef CONFIG_X86_32
  478. int i;
  479. /*
  480. * First of all, decide if this is a 486 or higher
  481. * It's a 486 if we can modify the AC flag
  482. */
  483. if (flag_is_changeable_p(X86_EFLAGS_AC))
  484. c->x86 = 4;
  485. else
  486. c->x86 = 3;
  487. for (i = 0; i < X86_VENDOR_NUM; i++)
  488. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  489. c->x86_vendor_id[0] = 0;
  490. cpu_devs[i]->c_identify(c);
  491. if (c->x86_vendor_id[0]) {
  492. get_cpu_vendor(c);
  493. break;
  494. }
  495. }
  496. #endif
  497. }
  498. /*
  499. * Do minimum CPU detection early.
  500. * Fields really needed: vendor, cpuid_level, family, model, mask,
  501. * cache alignment.
  502. * The others are not touched to avoid unwanted side effects.
  503. *
  504. * WARNING: this function is only called on the BP. Don't add code here
  505. * that is supposed to run on all CPUs.
  506. */
  507. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  508. {
  509. #ifdef CONFIG_X86_64
  510. c->x86_clflush_size = 64;
  511. #else
  512. c->x86_clflush_size = 32;
  513. #endif
  514. c->x86_cache_alignment = c->x86_clflush_size;
  515. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  516. c->extended_cpuid_level = 0;
  517. if (!have_cpuid_p())
  518. identify_cpu_without_cpuid(c);
  519. /* cyrix could have cpuid enabled via c_identify()*/
  520. if (!have_cpuid_p())
  521. return;
  522. cpu_detect(c);
  523. get_cpu_vendor(c);
  524. get_cpu_cap(c);
  525. if (this_cpu->c_early_init)
  526. this_cpu->c_early_init(c);
  527. #ifdef CONFIG_SMP
  528. c->cpu_index = boot_cpu_id;
  529. #endif
  530. filter_cpuid_features(c, false);
  531. }
  532. void __init early_cpu_init(void)
  533. {
  534. struct cpu_dev **cdev;
  535. int count = 0;
  536. printk("KERNEL supported cpus:\n");
  537. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  538. struct cpu_dev *cpudev = *cdev;
  539. unsigned int j;
  540. if (count >= X86_VENDOR_NUM)
  541. break;
  542. cpu_devs[count] = cpudev;
  543. count++;
  544. for (j = 0; j < 2; j++) {
  545. if (!cpudev->c_ident[j])
  546. continue;
  547. printk(" %s %s\n", cpudev->c_vendor,
  548. cpudev->c_ident[j]);
  549. }
  550. }
  551. early_identify_cpu(&boot_cpu_data);
  552. }
  553. /*
  554. * The NOPL instruction is supposed to exist on all CPUs with
  555. * family >= 6; unfortunately, that's not true in practice because
  556. * of early VIA chips and (more importantly) broken virtualizers that
  557. * are not easy to detect. In the latter case it doesn't even *fail*
  558. * reliably, so probing for it doesn't even work. Disable it completely
  559. * unless we can find a reliable way to detect all the broken cases.
  560. */
  561. static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
  562. {
  563. clear_cpu_cap(c, X86_FEATURE_NOPL);
  564. }
  565. static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
  566. {
  567. c->extended_cpuid_level = 0;
  568. if (!have_cpuid_p())
  569. identify_cpu_without_cpuid(c);
  570. /* cyrix could have cpuid enabled via c_identify()*/
  571. if (!have_cpuid_p())
  572. return;
  573. cpu_detect(c);
  574. get_cpu_vendor(c);
  575. get_cpu_cap(c);
  576. if (c->cpuid_level >= 0x00000001) {
  577. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  578. #ifdef CONFIG_X86_32
  579. # ifdef CONFIG_X86_HT
  580. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  581. # else
  582. c->apicid = c->initial_apicid;
  583. # endif
  584. #endif
  585. #ifdef CONFIG_X86_HT
  586. c->phys_proc_id = c->initial_apicid;
  587. #endif
  588. }
  589. get_model_name(c); /* Default name */
  590. init_scattered_cpuid_features(c);
  591. detect_nopl(c);
  592. }
  593. /*
  594. * This does the hard work of actually picking apart the CPU stuff...
  595. */
  596. static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  597. {
  598. int i;
  599. c->loops_per_jiffy = loops_per_jiffy;
  600. c->x86_cache_size = -1;
  601. c->x86_vendor = X86_VENDOR_UNKNOWN;
  602. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  603. c->x86_vendor_id[0] = '\0'; /* Unset */
  604. c->x86_model_id[0] = '\0'; /* Unset */
  605. c->x86_max_cores = 1;
  606. c->x86_coreid_bits = 0;
  607. #ifdef CONFIG_X86_64
  608. c->x86_clflush_size = 64;
  609. #else
  610. c->cpuid_level = -1; /* CPUID not detected */
  611. c->x86_clflush_size = 32;
  612. #endif
  613. c->x86_cache_alignment = c->x86_clflush_size;
  614. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  615. generic_identify(c);
  616. if (this_cpu->c_identify)
  617. this_cpu->c_identify(c);
  618. #ifdef CONFIG_X86_64
  619. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  620. #endif
  621. /*
  622. * Vendor-specific initialization. In this section we
  623. * canonicalize the feature flags, meaning if there are
  624. * features a certain CPU supports which CPUID doesn't
  625. * tell us, CPUID claiming incorrect flags, or other bugs,
  626. * we handle them here.
  627. *
  628. * At the end of this section, c->x86_capability better
  629. * indicate the features this CPU genuinely supports!
  630. */
  631. if (this_cpu->c_init)
  632. this_cpu->c_init(c);
  633. /* Disable the PN if appropriate */
  634. squash_the_stupid_serial_number(c);
  635. /*
  636. * The vendor-specific functions might have changed features. Now
  637. * we do "generic changes."
  638. */
  639. /* Filter out anything that depends on CPUID levels we don't have */
  640. filter_cpuid_features(c, true);
  641. /* If the model name is still unset, do table lookup. */
  642. if (!c->x86_model_id[0]) {
  643. char *p;
  644. p = table_lookup_model(c);
  645. if (p)
  646. strcpy(c->x86_model_id, p);
  647. else
  648. /* Last resort... */
  649. sprintf(c->x86_model_id, "%02x/%02x",
  650. c->x86, c->x86_model);
  651. }
  652. #ifdef CONFIG_X86_64
  653. detect_ht(c);
  654. #endif
  655. init_hypervisor(c);
  656. /*
  657. * On SMP, boot_cpu_data holds the common feature set between
  658. * all CPUs; so make sure that we indicate which features are
  659. * common between the CPUs. The first time this routine gets
  660. * executed, c == &boot_cpu_data.
  661. */
  662. if (c != &boot_cpu_data) {
  663. /* AND the already accumulated flags with these */
  664. for (i = 0; i < NCAPINTS; i++)
  665. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  666. }
  667. /* Clear all flags overriden by options */
  668. for (i = 0; i < NCAPINTS; i++)
  669. c->x86_capability[i] &= ~cleared_cpu_caps[i];
  670. #ifdef CONFIG_X86_MCE
  671. /* Init Machine Check Exception if available. */
  672. mcheck_init(c);
  673. #endif
  674. select_idle_routine(c);
  675. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
  676. numa_add_cpu(smp_processor_id());
  677. #endif
  678. }
  679. #ifdef CONFIG_X86_64
  680. static void vgetcpu_set_mode(void)
  681. {
  682. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  683. vgetcpu_mode = VGETCPU_RDTSCP;
  684. else
  685. vgetcpu_mode = VGETCPU_LSL;
  686. }
  687. #endif
  688. void __init identify_boot_cpu(void)
  689. {
  690. identify_cpu(&boot_cpu_data);
  691. init_c1e_mask();
  692. #ifdef CONFIG_X86_32
  693. sysenter_setup();
  694. enable_sep_cpu();
  695. #else
  696. vgetcpu_set_mode();
  697. #endif
  698. }
  699. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  700. {
  701. BUG_ON(c == &boot_cpu_data);
  702. identify_cpu(c);
  703. #ifdef CONFIG_X86_32
  704. enable_sep_cpu();
  705. #endif
  706. mtrr_ap_init();
  707. }
  708. struct msr_range {
  709. unsigned min;
  710. unsigned max;
  711. };
  712. static struct msr_range msr_range_array[] __cpuinitdata = {
  713. { 0x00000000, 0x00000418},
  714. { 0xc0000000, 0xc000040b},
  715. { 0xc0010000, 0xc0010142},
  716. { 0xc0011000, 0xc001103b},
  717. };
  718. static void __cpuinit print_cpu_msr(void)
  719. {
  720. unsigned index;
  721. u64 val;
  722. int i;
  723. unsigned index_min, index_max;
  724. for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  725. index_min = msr_range_array[i].min;
  726. index_max = msr_range_array[i].max;
  727. for (index = index_min; index < index_max; index++) {
  728. if (rdmsrl_amd_safe(index, &val))
  729. continue;
  730. printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
  731. }
  732. }
  733. }
  734. static int show_msr __cpuinitdata;
  735. static __init int setup_show_msr(char *arg)
  736. {
  737. int num;
  738. get_option(&arg, &num);
  739. if (num > 0)
  740. show_msr = num;
  741. return 1;
  742. }
  743. __setup("show_msr=", setup_show_msr);
  744. static __init int setup_noclflush(char *arg)
  745. {
  746. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  747. return 1;
  748. }
  749. __setup("noclflush", setup_noclflush);
  750. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  751. {
  752. char *vendor = NULL;
  753. if (c->x86_vendor < X86_VENDOR_NUM)
  754. vendor = this_cpu->c_vendor;
  755. else if (c->cpuid_level >= 0)
  756. vendor = c->x86_vendor_id;
  757. if (vendor && !strstr(c->x86_model_id, vendor))
  758. printk(KERN_CONT "%s ", vendor);
  759. if (c->x86_model_id[0])
  760. printk(KERN_CONT "%s", c->x86_model_id);
  761. else
  762. printk(KERN_CONT "%d86", c->x86);
  763. if (c->x86_mask || c->cpuid_level >= 0)
  764. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  765. else
  766. printk(KERN_CONT "\n");
  767. #ifdef CONFIG_SMP
  768. if (c->cpu_index < show_msr)
  769. print_cpu_msr();
  770. #else
  771. if (show_msr)
  772. print_cpu_msr();
  773. #endif
  774. }
  775. static __init int setup_disablecpuid(char *arg)
  776. {
  777. int bit;
  778. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  779. setup_clear_cpu_cap(bit);
  780. else
  781. return 0;
  782. return 1;
  783. }
  784. __setup("clearcpuid=", setup_disablecpuid);
  785. #ifdef CONFIG_X86_64
  786. struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
  787. DEFINE_PER_CPU_FIRST(union irq_stack_union,
  788. irq_stack_union) __aligned(PAGE_SIZE);
  789. DEFINE_PER_CPU(char *, irq_stack_ptr) =
  790. init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
  791. DEFINE_PER_CPU(unsigned long, kernel_stack) =
  792. (unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
  793. EXPORT_PER_CPU_SYMBOL(kernel_stack);
  794. DEFINE_PER_CPU(unsigned int, irq_count) = -1;
  795. static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
  796. [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ])
  797. __aligned(PAGE_SIZE);
  798. extern asmlinkage void ignore_sysret(void);
  799. /* May not be marked __init: used by software suspend */
  800. void syscall_init(void)
  801. {
  802. /*
  803. * LSTAR and STAR live in a bit strange symbiosis.
  804. * They both write to the same internal register. STAR allows to
  805. * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
  806. */
  807. wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
  808. wrmsrl(MSR_LSTAR, system_call);
  809. wrmsrl(MSR_CSTAR, ignore_sysret);
  810. #ifdef CONFIG_IA32_EMULATION
  811. syscall32_cpu_init();
  812. #endif
  813. /* Flags to clear on syscall */
  814. wrmsrl(MSR_SYSCALL_MASK,
  815. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
  816. }
  817. unsigned long kernel_eflags;
  818. /*
  819. * Copies of the original ist values from the tss are only accessed during
  820. * debugging, no special alignment required.
  821. */
  822. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  823. #else /* x86_64 */
  824. #ifdef CONFIG_CC_STACKPROTECTOR
  825. DEFINE_PER_CPU(unsigned long, stack_canary);
  826. #endif
  827. /* Make sure %fs and %gs are initialized properly in idle threads */
  828. struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
  829. {
  830. memset(regs, 0, sizeof(struct pt_regs));
  831. regs->fs = __KERNEL_PERCPU;
  832. regs->gs = __KERNEL_STACK_CANARY;
  833. return regs;
  834. }
  835. #endif /* x86_64 */
  836. /*
  837. * cpu_init() initializes state that is per-CPU. Some data is already
  838. * initialized (naturally) in the bootstrap process, such as the GDT
  839. * and IDT. We reload them nevertheless, this function acts as a
  840. * 'CPU state barrier', nothing should get across.
  841. * A lot of state is already set up in PDA init for 64 bit
  842. */
  843. #ifdef CONFIG_X86_64
  844. void __cpuinit cpu_init(void)
  845. {
  846. int cpu = stack_smp_processor_id();
  847. struct tss_struct *t = &per_cpu(init_tss, cpu);
  848. struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
  849. unsigned long v;
  850. struct task_struct *me;
  851. int i;
  852. #ifdef CONFIG_NUMA
  853. if (cpu != 0 && percpu_read(node_number) == 0 &&
  854. cpu_to_node(cpu) != NUMA_NO_NODE)
  855. percpu_write(node_number, cpu_to_node(cpu));
  856. #endif
  857. me = current;
  858. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask))
  859. panic("CPU#%d already initialized!\n", cpu);
  860. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  861. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  862. /*
  863. * Initialize the per-CPU GDT with the boot GDT,
  864. * and set up the GDT descriptor:
  865. */
  866. switch_to_new_gdt(cpu);
  867. loadsegment(fs, 0);
  868. load_idt((const struct desc_ptr *)&idt_descr);
  869. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  870. syscall_init();
  871. wrmsrl(MSR_FS_BASE, 0);
  872. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  873. barrier();
  874. check_efer();
  875. if (cpu != 0)
  876. enable_x2apic();
  877. /*
  878. * set up and load the per-CPU TSS
  879. */
  880. if (!orig_ist->ist[0]) {
  881. static const unsigned int sizes[N_EXCEPTION_STACKS] = {
  882. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  883. [DEBUG_STACK - 1] = DEBUG_STKSZ
  884. };
  885. char *estacks = per_cpu(exception_stacks, cpu);
  886. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  887. estacks += sizes[v];
  888. orig_ist->ist[v] = t->x86_tss.ist[v] =
  889. (unsigned long)estacks;
  890. }
  891. }
  892. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  893. /*
  894. * <= is required because the CPU will access up to
  895. * 8 bits beyond the end of the IO permission bitmap.
  896. */
  897. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  898. t->io_bitmap[i] = ~0UL;
  899. atomic_inc(&init_mm.mm_count);
  900. me->active_mm = &init_mm;
  901. if (me->mm)
  902. BUG();
  903. enter_lazy_tlb(&init_mm, me);
  904. load_sp0(t, &current->thread);
  905. set_tss_desc(cpu, t);
  906. load_TR_desc();
  907. load_LDT(&init_mm.context);
  908. #ifdef CONFIG_KGDB
  909. /*
  910. * If the kgdb is connected no debug regs should be altered. This
  911. * is only applicable when KGDB and a KGDB I/O module are built
  912. * into the kernel and you are using early debugging with
  913. * kgdbwait. KGDB will control the kernel HW breakpoint registers.
  914. */
  915. if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
  916. arch_kgdb_ops.correct_hw_break();
  917. else
  918. #endif
  919. {
  920. /*
  921. * Clear all 6 debug registers:
  922. */
  923. set_debugreg(0UL, 0);
  924. set_debugreg(0UL, 1);
  925. set_debugreg(0UL, 2);
  926. set_debugreg(0UL, 3);
  927. set_debugreg(0UL, 6);
  928. set_debugreg(0UL, 7);
  929. }
  930. fpu_init();
  931. raw_local_save_flags(kernel_eflags);
  932. if (is_uv_system())
  933. uv_cpu_init();
  934. }
  935. #else
  936. void __cpuinit cpu_init(void)
  937. {
  938. int cpu = smp_processor_id();
  939. struct task_struct *curr = current;
  940. struct tss_struct *t = &per_cpu(init_tss, cpu);
  941. struct thread_struct *thread = &curr->thread;
  942. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)) {
  943. printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
  944. for (;;) local_irq_enable();
  945. }
  946. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  947. if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
  948. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  949. load_idt(&idt_descr);
  950. switch_to_new_gdt(cpu);
  951. /*
  952. * Set up and load the per-CPU TSS and LDT
  953. */
  954. atomic_inc(&init_mm.mm_count);
  955. curr->active_mm = &init_mm;
  956. if (curr->mm)
  957. BUG();
  958. enter_lazy_tlb(&init_mm, curr);
  959. load_sp0(t, thread);
  960. set_tss_desc(cpu, t);
  961. load_TR_desc();
  962. load_LDT(&init_mm.context);
  963. #ifdef CONFIG_DOUBLEFAULT
  964. /* Set up doublefault TSS pointer in the GDT */
  965. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  966. #endif
  967. /* Clear all 6 debug registers: */
  968. set_debugreg(0, 0);
  969. set_debugreg(0, 1);
  970. set_debugreg(0, 2);
  971. set_debugreg(0, 3);
  972. set_debugreg(0, 6);
  973. set_debugreg(0, 7);
  974. /*
  975. * Force FPU initialization:
  976. */
  977. if (cpu_has_xsave)
  978. current_thread_info()->status = TS_XSAVE;
  979. else
  980. current_thread_info()->status = 0;
  981. clear_used_math();
  982. mxcsr_feature_mask_init();
  983. /*
  984. * Boot processor to setup the FP and extended state context info.
  985. */
  986. if (smp_processor_id() == boot_cpu_id)
  987. init_thread_xstate();
  988. xsave_init();
  989. }
  990. #endif