common.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. #ifdef CONFIG_X86_32
  692. sysenter_setup();
  693. enable_sep_cpu();
  694. #else
  695. vgetcpu_set_mode();
  696. #endif
  697. }
  698. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  699. {
  700. BUG_ON(c == &boot_cpu_data);
  701. identify_cpu(c);
  702. #ifdef CONFIG_X86_32
  703. enable_sep_cpu();
  704. #endif
  705. mtrr_ap_init();
  706. }
  707. struct msr_range {
  708. unsigned min;
  709. unsigned max;
  710. };
  711. static struct msr_range msr_range_array[] __cpuinitdata = {
  712. { 0x00000000, 0x00000418},
  713. { 0xc0000000, 0xc000040b},
  714. { 0xc0010000, 0xc0010142},
  715. { 0xc0011000, 0xc001103b},
  716. };
  717. static void __cpuinit print_cpu_msr(void)
  718. {
  719. unsigned index;
  720. u64 val;
  721. int i;
  722. unsigned index_min, index_max;
  723. for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  724. index_min = msr_range_array[i].min;
  725. index_max = msr_range_array[i].max;
  726. for (index = index_min; index < index_max; index++) {
  727. if (rdmsrl_amd_safe(index, &val))
  728. continue;
  729. printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
  730. }
  731. }
  732. }
  733. static int show_msr __cpuinitdata;
  734. static __init int setup_show_msr(char *arg)
  735. {
  736. int num;
  737. get_option(&arg, &num);
  738. if (num > 0)
  739. show_msr = num;
  740. return 1;
  741. }
  742. __setup("show_msr=", setup_show_msr);
  743. static __init int setup_noclflush(char *arg)
  744. {
  745. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  746. return 1;
  747. }
  748. __setup("noclflush", setup_noclflush);
  749. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  750. {
  751. char *vendor = NULL;
  752. if (c->x86_vendor < X86_VENDOR_NUM)
  753. vendor = this_cpu->c_vendor;
  754. else if (c->cpuid_level >= 0)
  755. vendor = c->x86_vendor_id;
  756. if (vendor && !strstr(c->x86_model_id, vendor))
  757. printk(KERN_CONT "%s ", vendor);
  758. if (c->x86_model_id[0])
  759. printk(KERN_CONT "%s", c->x86_model_id);
  760. else
  761. printk(KERN_CONT "%d86", c->x86);
  762. if (c->x86_mask || c->cpuid_level >= 0)
  763. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  764. else
  765. printk(KERN_CONT "\n");
  766. #ifdef CONFIG_SMP
  767. if (c->cpu_index < show_msr)
  768. print_cpu_msr();
  769. #else
  770. if (show_msr)
  771. print_cpu_msr();
  772. #endif
  773. }
  774. static __init int setup_disablecpuid(char *arg)
  775. {
  776. int bit;
  777. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  778. setup_clear_cpu_cap(bit);
  779. else
  780. return 0;
  781. return 1;
  782. }
  783. __setup("clearcpuid=", setup_disablecpuid);
  784. #ifdef CONFIG_X86_64
  785. struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
  786. DEFINE_PER_CPU_FIRST(union irq_stack_union,
  787. irq_stack_union) __aligned(PAGE_SIZE);
  788. DEFINE_PER_CPU(char *, irq_stack_ptr) =
  789. init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
  790. DEFINE_PER_CPU(unsigned long, kernel_stack) =
  791. (unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
  792. EXPORT_PER_CPU_SYMBOL(kernel_stack);
  793. DEFINE_PER_CPU(unsigned int, irq_count) = -1;
  794. static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
  795. [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ])
  796. __aligned(PAGE_SIZE);
  797. extern asmlinkage void ignore_sysret(void);
  798. /* May not be marked __init: used by software suspend */
  799. void syscall_init(void)
  800. {
  801. /*
  802. * LSTAR and STAR live in a bit strange symbiosis.
  803. * They both write to the same internal register. STAR allows to
  804. * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
  805. */
  806. wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
  807. wrmsrl(MSR_LSTAR, system_call);
  808. wrmsrl(MSR_CSTAR, ignore_sysret);
  809. #ifdef CONFIG_IA32_EMULATION
  810. syscall32_cpu_init();
  811. #endif
  812. /* Flags to clear on syscall */
  813. wrmsrl(MSR_SYSCALL_MASK,
  814. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
  815. }
  816. unsigned long kernel_eflags;
  817. /*
  818. * Copies of the original ist values from the tss are only accessed during
  819. * debugging, no special alignment required.
  820. */
  821. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  822. #else /* x86_64 */
  823. #ifdef CONFIG_CC_STACKPROTECTOR
  824. DEFINE_PER_CPU(unsigned long, stack_canary);
  825. #endif
  826. /* Make sure %fs and %gs are initialized properly in idle threads */
  827. struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
  828. {
  829. memset(regs, 0, sizeof(struct pt_regs));
  830. regs->fs = __KERNEL_PERCPU;
  831. regs->gs = __KERNEL_STACK_CANARY;
  832. return regs;
  833. }
  834. #endif /* x86_64 */
  835. /*
  836. * cpu_init() initializes state that is per-CPU. Some data is already
  837. * initialized (naturally) in the bootstrap process, such as the GDT
  838. * and IDT. We reload them nevertheless, this function acts as a
  839. * 'CPU state barrier', nothing should get across.
  840. * A lot of state is already set up in PDA init for 64 bit
  841. */
  842. #ifdef CONFIG_X86_64
  843. void __cpuinit cpu_init(void)
  844. {
  845. int cpu = stack_smp_processor_id();
  846. struct tss_struct *t = &per_cpu(init_tss, cpu);
  847. struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
  848. unsigned long v;
  849. struct task_struct *me;
  850. int i;
  851. #ifdef CONFIG_NUMA
  852. if (cpu != 0 && percpu_read(node_number) == 0 &&
  853. cpu_to_node(cpu) != NUMA_NO_NODE)
  854. percpu_write(node_number, cpu_to_node(cpu));
  855. #endif
  856. me = current;
  857. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask))
  858. panic("CPU#%d already initialized!\n", cpu);
  859. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  860. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  861. /*
  862. * Initialize the per-CPU GDT with the boot GDT,
  863. * and set up the GDT descriptor:
  864. */
  865. switch_to_new_gdt(cpu);
  866. loadsegment(fs, 0);
  867. load_idt((const struct desc_ptr *)&idt_descr);
  868. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  869. syscall_init();
  870. wrmsrl(MSR_FS_BASE, 0);
  871. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  872. barrier();
  873. check_efer();
  874. if (cpu != 0)
  875. enable_x2apic();
  876. /*
  877. * set up and load the per-CPU TSS
  878. */
  879. if (!orig_ist->ist[0]) {
  880. static const unsigned int sizes[N_EXCEPTION_STACKS] = {
  881. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  882. [DEBUG_STACK - 1] = DEBUG_STKSZ
  883. };
  884. char *estacks = per_cpu(exception_stacks, cpu);
  885. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  886. estacks += sizes[v];
  887. orig_ist->ist[v] = t->x86_tss.ist[v] =
  888. (unsigned long)estacks;
  889. }
  890. }
  891. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  892. /*
  893. * <= is required because the CPU will access up to
  894. * 8 bits beyond the end of the IO permission bitmap.
  895. */
  896. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  897. t->io_bitmap[i] = ~0UL;
  898. atomic_inc(&init_mm.mm_count);
  899. me->active_mm = &init_mm;
  900. if (me->mm)
  901. BUG();
  902. enter_lazy_tlb(&init_mm, me);
  903. load_sp0(t, &current->thread);
  904. set_tss_desc(cpu, t);
  905. load_TR_desc();
  906. load_LDT(&init_mm.context);
  907. #ifdef CONFIG_KGDB
  908. /*
  909. * If the kgdb is connected no debug regs should be altered. This
  910. * is only applicable when KGDB and a KGDB I/O module are built
  911. * into the kernel and you are using early debugging with
  912. * kgdbwait. KGDB will control the kernel HW breakpoint registers.
  913. */
  914. if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
  915. arch_kgdb_ops.correct_hw_break();
  916. else
  917. #endif
  918. {
  919. /*
  920. * Clear all 6 debug registers:
  921. */
  922. set_debugreg(0UL, 0);
  923. set_debugreg(0UL, 1);
  924. set_debugreg(0UL, 2);
  925. set_debugreg(0UL, 3);
  926. set_debugreg(0UL, 6);
  927. set_debugreg(0UL, 7);
  928. }
  929. fpu_init();
  930. raw_local_save_flags(kernel_eflags);
  931. if (is_uv_system())
  932. uv_cpu_init();
  933. }
  934. #else
  935. void __cpuinit cpu_init(void)
  936. {
  937. int cpu = smp_processor_id();
  938. struct task_struct *curr = current;
  939. struct tss_struct *t = &per_cpu(init_tss, cpu);
  940. struct thread_struct *thread = &curr->thread;
  941. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)) {
  942. printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
  943. for (;;) local_irq_enable();
  944. }
  945. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  946. if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
  947. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  948. load_idt(&idt_descr);
  949. switch_to_new_gdt(cpu);
  950. /*
  951. * Set up and load the per-CPU TSS and LDT
  952. */
  953. atomic_inc(&init_mm.mm_count);
  954. curr->active_mm = &init_mm;
  955. if (curr->mm)
  956. BUG();
  957. enter_lazy_tlb(&init_mm, curr);
  958. load_sp0(t, thread);
  959. set_tss_desc(cpu, t);
  960. load_TR_desc();
  961. load_LDT(&init_mm.context);
  962. #ifdef CONFIG_DOUBLEFAULT
  963. /* Set up doublefault TSS pointer in the GDT */
  964. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  965. #endif
  966. /* Clear all 6 debug registers: */
  967. set_debugreg(0, 0);
  968. set_debugreg(0, 1);
  969. set_debugreg(0, 2);
  970. set_debugreg(0, 3);
  971. set_debugreg(0, 6);
  972. set_debugreg(0, 7);
  973. /*
  974. * Force FPU initialization:
  975. */
  976. if (cpu_has_xsave)
  977. current_thread_info()->status = TS_XSAVE;
  978. else
  979. current_thread_info()->status = 0;
  980. clear_used_math();
  981. mxcsr_feature_mask_init();
  982. /*
  983. * Boot processor to setup the FP and extended state context info.
  984. */
  985. if (smp_processor_id() == boot_cpu_id)
  986. init_thread_xstate();
  987. xsave_init();
  988. }
  989. #endif