common.c 29 KB

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