common.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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/kprobes.h>
  12. #include <linux/kgdb.h>
  13. #include <linux/smp.h>
  14. #include <linux/io.h>
  15. #include <asm/stackprotector.h>
  16. #include <asm/perf_event.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/archrandom.h>
  19. #include <asm/hypervisor.h>
  20. #include <asm/processor.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/debugreg.h>
  23. #include <asm/sections.h>
  24. #include <asm/vsyscall.h>
  25. #include <linux/topology.h>
  26. #include <linux/cpumask.h>
  27. #include <asm/pgtable.h>
  28. #include <linux/atomic.h>
  29. #include <asm/proto.h>
  30. #include <asm/setup.h>
  31. #include <asm/apic.h>
  32. #include <asm/desc.h>
  33. #include <asm/i387.h>
  34. #include <asm/fpu-internal.h>
  35. #include <asm/mtrr.h>
  36. #include <linux/numa.h>
  37. #include <asm/asm.h>
  38. #include <asm/cpu.h>
  39. #include <asm/mce.h>
  40. #include <asm/msr.h>
  41. #include <asm/pat.h>
  42. #include <asm/microcode.h>
  43. #include <asm/microcode_intel.h>
  44. #ifdef CONFIG_X86_LOCAL_APIC
  45. #include <asm/uv/uv.h>
  46. #endif
  47. #include "cpu.h"
  48. /* all of these masks are initialized in setup_cpu_local_masks() */
  49. cpumask_var_t cpu_initialized_mask;
  50. cpumask_var_t cpu_callout_mask;
  51. cpumask_var_t cpu_callin_mask;
  52. /* representing cpus for which sibling maps can be computed */
  53. cpumask_var_t cpu_sibling_setup_mask;
  54. /* correctly size the local cpu masks */
  55. void __init setup_cpu_local_masks(void)
  56. {
  57. alloc_bootmem_cpumask_var(&cpu_initialized_mask);
  58. alloc_bootmem_cpumask_var(&cpu_callin_mask);
  59. alloc_bootmem_cpumask_var(&cpu_callout_mask);
  60. alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
  61. }
  62. static void default_init(struct cpuinfo_x86 *c)
  63. {
  64. #ifdef CONFIG_X86_64
  65. cpu_detect_cache_sizes(c);
  66. #else
  67. /* Not much we can do here... */
  68. /* Check if at least it has cpuid */
  69. if (c->cpuid_level == -1) {
  70. /* No cpuid. It must be an ancient CPU */
  71. if (c->x86 == 4)
  72. strcpy(c->x86_model_id, "486");
  73. else if (c->x86 == 3)
  74. strcpy(c->x86_model_id, "386");
  75. }
  76. #endif
  77. }
  78. static const struct cpu_dev default_cpu = {
  79. .c_init = default_init,
  80. .c_vendor = "Unknown",
  81. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  82. };
  83. static const struct cpu_dev *this_cpu = &default_cpu;
  84. DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
  85. #ifdef CONFIG_X86_64
  86. /*
  87. * We need valid kernel segments for data and code in long mode too
  88. * IRET will check the segment types kkeil 2000/10/28
  89. * Also sysret mandates a special GDT layout
  90. *
  91. * TLS descriptors are currently at a different place compared to i386.
  92. * Hopefully nobody expects them at a fixed place (Wine?)
  93. */
  94. [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
  95. [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
  96. [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
  97. [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
  98. [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
  99. [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
  100. #else
  101. [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
  102. [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  103. [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
  104. [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
  105. /*
  106. * Segments used for calling PnP BIOS have byte granularity.
  107. * They code segments and data segments have fixed 64k limits,
  108. * the transfer segment sizes are set at run time.
  109. */
  110. /* 32-bit code */
  111. [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
  112. /* 16-bit code */
  113. [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
  114. /* 16-bit data */
  115. [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
  116. /* 16-bit data */
  117. [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
  118. /* 16-bit data */
  119. [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
  120. /*
  121. * The APM segments have byte granularity and their bases
  122. * are set at run time. All have 64k limits.
  123. */
  124. /* 32-bit code */
  125. [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
  126. /* 16-bit code */
  127. [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
  128. /* data */
  129. [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
  130. [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  131. [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  132. GDT_STACK_CANARY_INIT
  133. #endif
  134. } };
  135. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  136. static int __init x86_xsave_setup(char *s)
  137. {
  138. if (strlen(s))
  139. return 0;
  140. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  141. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  142. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  143. setup_clear_cpu_cap(X86_FEATURE_AVX);
  144. setup_clear_cpu_cap(X86_FEATURE_AVX2);
  145. return 1;
  146. }
  147. __setup("noxsave", x86_xsave_setup);
  148. static int __init x86_xsaveopt_setup(char *s)
  149. {
  150. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  151. return 1;
  152. }
  153. __setup("noxsaveopt", x86_xsaveopt_setup);
  154. static int __init x86_xsaves_setup(char *s)
  155. {
  156. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  157. return 1;
  158. }
  159. __setup("noxsaves", x86_xsaves_setup);
  160. #ifdef CONFIG_X86_32
  161. static int cachesize_override = -1;
  162. static int disable_x86_serial_nr = 1;
  163. static int __init cachesize_setup(char *str)
  164. {
  165. get_option(&str, &cachesize_override);
  166. return 1;
  167. }
  168. __setup("cachesize=", cachesize_setup);
  169. static int __init x86_fxsr_setup(char *s)
  170. {
  171. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  172. setup_clear_cpu_cap(X86_FEATURE_XMM);
  173. return 1;
  174. }
  175. __setup("nofxsr", x86_fxsr_setup);
  176. static int __init x86_sep_setup(char *s)
  177. {
  178. setup_clear_cpu_cap(X86_FEATURE_SEP);
  179. return 1;
  180. }
  181. __setup("nosep", x86_sep_setup);
  182. /* Standard macro to see if a specific flag is changeable */
  183. static inline int flag_is_changeable_p(u32 flag)
  184. {
  185. u32 f1, f2;
  186. /*
  187. * Cyrix and IDT cpus allow disabling of CPUID
  188. * so the code below may return different results
  189. * when it is executed before and after enabling
  190. * the CPUID. Add "volatile" to not allow gcc to
  191. * optimize the subsequent calls to this function.
  192. */
  193. asm volatile ("pushfl \n\t"
  194. "pushfl \n\t"
  195. "popl %0 \n\t"
  196. "movl %0, %1 \n\t"
  197. "xorl %2, %0 \n\t"
  198. "pushl %0 \n\t"
  199. "popfl \n\t"
  200. "pushfl \n\t"
  201. "popl %0 \n\t"
  202. "popfl \n\t"
  203. : "=&r" (f1), "=&r" (f2)
  204. : "ir" (flag));
  205. return ((f1^f2) & flag) != 0;
  206. }
  207. /* Probe for the CPUID instruction */
  208. int have_cpuid_p(void)
  209. {
  210. return flag_is_changeable_p(X86_EFLAGS_ID);
  211. }
  212. static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  213. {
  214. unsigned long lo, hi;
  215. if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
  216. return;
  217. /* Disable processor serial number: */
  218. rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  219. lo |= 0x200000;
  220. wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  221. printk(KERN_NOTICE "CPU serial number disabled.\n");
  222. clear_cpu_cap(c, X86_FEATURE_PN);
  223. /* Disabling the serial number may affect the cpuid level */
  224. c->cpuid_level = cpuid_eax(0);
  225. }
  226. static int __init x86_serial_nr_setup(char *s)
  227. {
  228. disable_x86_serial_nr = 0;
  229. return 1;
  230. }
  231. __setup("serialnumber", x86_serial_nr_setup);
  232. #else
  233. static inline int flag_is_changeable_p(u32 flag)
  234. {
  235. return 1;
  236. }
  237. static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  238. {
  239. }
  240. #endif
  241. static __init int setup_disable_smep(char *arg)
  242. {
  243. setup_clear_cpu_cap(X86_FEATURE_SMEP);
  244. return 1;
  245. }
  246. __setup("nosmep", setup_disable_smep);
  247. static __always_inline void setup_smep(struct cpuinfo_x86 *c)
  248. {
  249. if (cpu_has(c, X86_FEATURE_SMEP))
  250. cr4_set_bits(X86_CR4_SMEP);
  251. }
  252. static __init int setup_disable_smap(char *arg)
  253. {
  254. setup_clear_cpu_cap(X86_FEATURE_SMAP);
  255. return 1;
  256. }
  257. __setup("nosmap", setup_disable_smap);
  258. static __always_inline void setup_smap(struct cpuinfo_x86 *c)
  259. {
  260. unsigned long eflags;
  261. /* This should have been cleared long ago */
  262. raw_local_save_flags(eflags);
  263. BUG_ON(eflags & X86_EFLAGS_AC);
  264. if (cpu_has(c, X86_FEATURE_SMAP)) {
  265. #ifdef CONFIG_X86_SMAP
  266. cr4_set_bits(X86_CR4_SMAP);
  267. #else
  268. cr4_clear_bits(X86_CR4_SMAP);
  269. #endif
  270. }
  271. }
  272. /*
  273. * Some CPU features depend on higher CPUID levels, which may not always
  274. * be available due to CPUID level capping or broken virtualization
  275. * software. Add those features to this table to auto-disable them.
  276. */
  277. struct cpuid_dependent_feature {
  278. u32 feature;
  279. u32 level;
  280. };
  281. static const struct cpuid_dependent_feature
  282. cpuid_dependent_features[] = {
  283. { X86_FEATURE_MWAIT, 0x00000005 },
  284. { X86_FEATURE_DCA, 0x00000009 },
  285. { X86_FEATURE_XSAVE, 0x0000000d },
  286. { 0, 0 }
  287. };
  288. static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
  289. {
  290. const struct cpuid_dependent_feature *df;
  291. for (df = cpuid_dependent_features; df->feature; df++) {
  292. if (!cpu_has(c, df->feature))
  293. continue;
  294. /*
  295. * Note: cpuid_level is set to -1 if unavailable, but
  296. * extended_extended_level is set to 0 if unavailable
  297. * and the legitimate extended levels are all negative
  298. * when signed; hence the weird messing around with
  299. * signs here...
  300. */
  301. if (!((s32)df->level < 0 ?
  302. (u32)df->level > (u32)c->extended_cpuid_level :
  303. (s32)df->level > (s32)c->cpuid_level))
  304. continue;
  305. clear_cpu_cap(c, df->feature);
  306. if (!warn)
  307. continue;
  308. printk(KERN_WARNING
  309. "CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
  310. x86_cap_flag(df->feature), df->level);
  311. }
  312. }
  313. /*
  314. * Naming convention should be: <Name> [(<Codename>)]
  315. * This table only is used unless init_<vendor>() below doesn't set it;
  316. * in particular, if CPUID levels 0x80000002..4 are supported, this
  317. * isn't used
  318. */
  319. /* Look up CPU names by table lookup. */
  320. static const char *table_lookup_model(struct cpuinfo_x86 *c)
  321. {
  322. #ifdef CONFIG_X86_32
  323. const struct legacy_cpu_model_info *info;
  324. if (c->x86_model >= 16)
  325. return NULL; /* Range check */
  326. if (!this_cpu)
  327. return NULL;
  328. info = this_cpu->legacy_models;
  329. while (info->family) {
  330. if (info->family == c->x86)
  331. return info->model_names[c->x86_model];
  332. info++;
  333. }
  334. #endif
  335. return NULL; /* Not found */
  336. }
  337. __u32 cpu_caps_cleared[NCAPINTS];
  338. __u32 cpu_caps_set[NCAPINTS];
  339. void load_percpu_segment(int cpu)
  340. {
  341. #ifdef CONFIG_X86_32
  342. loadsegment(fs, __KERNEL_PERCPU);
  343. #else
  344. loadsegment(gs, 0);
  345. wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
  346. #endif
  347. load_stack_canary_segment();
  348. }
  349. /*
  350. * Current gdt points %fs at the "master" per-cpu area: after this,
  351. * it's on the real one.
  352. */
  353. void switch_to_new_gdt(int cpu)
  354. {
  355. struct desc_ptr gdt_descr;
  356. gdt_descr.address = (long)get_cpu_gdt_table(cpu);
  357. gdt_descr.size = GDT_SIZE - 1;
  358. load_gdt(&gdt_descr);
  359. /* Reload the per-cpu base */
  360. load_percpu_segment(cpu);
  361. }
  362. static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  363. static void get_model_name(struct cpuinfo_x86 *c)
  364. {
  365. unsigned int *v;
  366. char *p, *q;
  367. if (c->extended_cpuid_level < 0x80000004)
  368. return;
  369. v = (unsigned int *)c->x86_model_id;
  370. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  371. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  372. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  373. c->x86_model_id[48] = 0;
  374. /*
  375. * Intel chips right-justify this string for some dumb reason;
  376. * undo that brain damage:
  377. */
  378. p = q = &c->x86_model_id[0];
  379. while (*p == ' ')
  380. p++;
  381. if (p != q) {
  382. while (*p)
  383. *q++ = *p++;
  384. while (q <= &c->x86_model_id[48])
  385. *q++ = '\0'; /* Zero-pad the rest */
  386. }
  387. }
  388. void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
  389. {
  390. unsigned int n, dummy, ebx, ecx, edx, l2size;
  391. n = c->extended_cpuid_level;
  392. if (n >= 0x80000005) {
  393. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  394. c->x86_cache_size = (ecx>>24) + (edx>>24);
  395. #ifdef CONFIG_X86_64
  396. /* On K8 L1 TLB is inclusive, so don't count it */
  397. c->x86_tlbsize = 0;
  398. #endif
  399. }
  400. if (n < 0x80000006) /* Some chips just has a large L1. */
  401. return;
  402. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  403. l2size = ecx >> 16;
  404. #ifdef CONFIG_X86_64
  405. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  406. #else
  407. /* do processor-specific cache resizing */
  408. if (this_cpu->legacy_cache_size)
  409. l2size = this_cpu->legacy_cache_size(c, l2size);
  410. /* Allow user to override all this if necessary. */
  411. if (cachesize_override != -1)
  412. l2size = cachesize_override;
  413. if (l2size == 0)
  414. return; /* Again, no L2 cache is possible */
  415. #endif
  416. c->x86_cache_size = l2size;
  417. }
  418. u16 __read_mostly tlb_lli_4k[NR_INFO];
  419. u16 __read_mostly tlb_lli_2m[NR_INFO];
  420. u16 __read_mostly tlb_lli_4m[NR_INFO];
  421. u16 __read_mostly tlb_lld_4k[NR_INFO];
  422. u16 __read_mostly tlb_lld_2m[NR_INFO];
  423. u16 __read_mostly tlb_lld_4m[NR_INFO];
  424. u16 __read_mostly tlb_lld_1g[NR_INFO];
  425. static void cpu_detect_tlb(struct cpuinfo_x86 *c)
  426. {
  427. if (this_cpu->c_detect_tlb)
  428. this_cpu->c_detect_tlb(c);
  429. pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
  430. tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
  431. tlb_lli_4m[ENTRIES]);
  432. pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
  433. tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
  434. tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
  435. }
  436. void detect_ht(struct cpuinfo_x86 *c)
  437. {
  438. #ifdef CONFIG_X86_HT
  439. u32 eax, ebx, ecx, edx;
  440. int index_msb, core_bits;
  441. static bool printed;
  442. if (!cpu_has(c, X86_FEATURE_HT))
  443. return;
  444. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  445. goto out;
  446. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  447. return;
  448. cpuid(1, &eax, &ebx, &ecx, &edx);
  449. smp_num_siblings = (ebx & 0xff0000) >> 16;
  450. if (smp_num_siblings == 1) {
  451. printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
  452. goto out;
  453. }
  454. if (smp_num_siblings <= 1)
  455. goto out;
  456. index_msb = get_count_order(smp_num_siblings);
  457. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
  458. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  459. index_msb = get_count_order(smp_num_siblings);
  460. core_bits = get_count_order(c->x86_max_cores);
  461. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
  462. ((1 << core_bits) - 1);
  463. out:
  464. if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
  465. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  466. c->phys_proc_id);
  467. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  468. c->cpu_core_id);
  469. printed = 1;
  470. }
  471. #endif
  472. }
  473. static void get_cpu_vendor(struct cpuinfo_x86 *c)
  474. {
  475. char *v = c->x86_vendor_id;
  476. int i;
  477. for (i = 0; i < X86_VENDOR_NUM; i++) {
  478. if (!cpu_devs[i])
  479. break;
  480. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  481. (cpu_devs[i]->c_ident[1] &&
  482. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  483. this_cpu = cpu_devs[i];
  484. c->x86_vendor = this_cpu->c_x86_vendor;
  485. return;
  486. }
  487. }
  488. printk_once(KERN_ERR
  489. "CPU: vendor_id '%s' unknown, using generic init.\n" \
  490. "CPU: Your system may be unstable.\n", v);
  491. c->x86_vendor = X86_VENDOR_UNKNOWN;
  492. this_cpu = &default_cpu;
  493. }
  494. void cpu_detect(struct cpuinfo_x86 *c)
  495. {
  496. /* Get vendor name */
  497. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  498. (unsigned int *)&c->x86_vendor_id[0],
  499. (unsigned int *)&c->x86_vendor_id[8],
  500. (unsigned int *)&c->x86_vendor_id[4]);
  501. c->x86 = 4;
  502. /* Intel-defined flags: level 0x00000001 */
  503. if (c->cpuid_level >= 0x00000001) {
  504. u32 junk, tfms, cap0, misc;
  505. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  506. c->x86 = (tfms >> 8) & 0xf;
  507. c->x86_model = (tfms >> 4) & 0xf;
  508. c->x86_mask = tfms & 0xf;
  509. if (c->x86 == 0xf)
  510. c->x86 += (tfms >> 20) & 0xff;
  511. if (c->x86 >= 0x6)
  512. c->x86_model += ((tfms >> 16) & 0xf) << 4;
  513. if (cap0 & (1<<19)) {
  514. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  515. c->x86_cache_alignment = c->x86_clflush_size;
  516. }
  517. }
  518. }
  519. void get_cpu_cap(struct cpuinfo_x86 *c)
  520. {
  521. u32 tfms, xlvl;
  522. u32 ebx;
  523. /* Intel-defined flags: level 0x00000001 */
  524. if (c->cpuid_level >= 0x00000001) {
  525. u32 capability, excap;
  526. cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  527. c->x86_capability[0] = capability;
  528. c->x86_capability[4] = excap;
  529. }
  530. /* Additional Intel-defined flags: level 0x00000007 */
  531. if (c->cpuid_level >= 0x00000007) {
  532. u32 eax, ebx, ecx, edx;
  533. cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
  534. c->x86_capability[9] = ebx;
  535. }
  536. /* Extended state features: level 0x0000000d */
  537. if (c->cpuid_level >= 0x0000000d) {
  538. u32 eax, ebx, ecx, edx;
  539. cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
  540. c->x86_capability[10] = eax;
  541. }
  542. /* Additional Intel-defined flags: level 0x0000000F */
  543. if (c->cpuid_level >= 0x0000000F) {
  544. u32 eax, ebx, ecx, edx;
  545. /* QoS sub-leaf, EAX=0Fh, ECX=0 */
  546. cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
  547. c->x86_capability[11] = edx;
  548. if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
  549. /* will be overridden if occupancy monitoring exists */
  550. c->x86_cache_max_rmid = ebx;
  551. /* QoS sub-leaf, EAX=0Fh, ECX=1 */
  552. cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
  553. c->x86_capability[12] = edx;
  554. if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
  555. c->x86_cache_max_rmid = ecx;
  556. c->x86_cache_occ_scale = ebx;
  557. }
  558. } else {
  559. c->x86_cache_max_rmid = -1;
  560. c->x86_cache_occ_scale = -1;
  561. }
  562. }
  563. /* AMD-defined flags: level 0x80000001 */
  564. xlvl = cpuid_eax(0x80000000);
  565. c->extended_cpuid_level = xlvl;
  566. if ((xlvl & 0xffff0000) == 0x80000000) {
  567. if (xlvl >= 0x80000001) {
  568. c->x86_capability[1] = cpuid_edx(0x80000001);
  569. c->x86_capability[6] = cpuid_ecx(0x80000001);
  570. }
  571. }
  572. if (c->extended_cpuid_level >= 0x80000008) {
  573. u32 eax = cpuid_eax(0x80000008);
  574. c->x86_virt_bits = (eax >> 8) & 0xff;
  575. c->x86_phys_bits = eax & 0xff;
  576. }
  577. #ifdef CONFIG_X86_32
  578. else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
  579. c->x86_phys_bits = 36;
  580. #endif
  581. if (c->extended_cpuid_level >= 0x80000007)
  582. c->x86_power = cpuid_edx(0x80000007);
  583. init_scattered_cpuid_features(c);
  584. }
  585. static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  586. {
  587. #ifdef CONFIG_X86_32
  588. int i;
  589. /*
  590. * First of all, decide if this is a 486 or higher
  591. * It's a 486 if we can modify the AC flag
  592. */
  593. if (flag_is_changeable_p(X86_EFLAGS_AC))
  594. c->x86 = 4;
  595. else
  596. c->x86 = 3;
  597. for (i = 0; i < X86_VENDOR_NUM; i++)
  598. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  599. c->x86_vendor_id[0] = 0;
  600. cpu_devs[i]->c_identify(c);
  601. if (c->x86_vendor_id[0]) {
  602. get_cpu_vendor(c);
  603. break;
  604. }
  605. }
  606. #endif
  607. }
  608. /*
  609. * Do minimum CPU detection early.
  610. * Fields really needed: vendor, cpuid_level, family, model, mask,
  611. * cache alignment.
  612. * The others are not touched to avoid unwanted side effects.
  613. *
  614. * WARNING: this function is only called on the BP. Don't add code here
  615. * that is supposed to run on all CPUs.
  616. */
  617. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  618. {
  619. #ifdef CONFIG_X86_64
  620. c->x86_clflush_size = 64;
  621. c->x86_phys_bits = 36;
  622. c->x86_virt_bits = 48;
  623. #else
  624. c->x86_clflush_size = 32;
  625. c->x86_phys_bits = 32;
  626. c->x86_virt_bits = 32;
  627. #endif
  628. c->x86_cache_alignment = c->x86_clflush_size;
  629. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  630. c->extended_cpuid_level = 0;
  631. if (!have_cpuid_p())
  632. identify_cpu_without_cpuid(c);
  633. /* cyrix could have cpuid enabled via c_identify()*/
  634. if (!have_cpuid_p())
  635. return;
  636. cpu_detect(c);
  637. get_cpu_vendor(c);
  638. get_cpu_cap(c);
  639. fpu_detect(c);
  640. if (this_cpu->c_early_init)
  641. this_cpu->c_early_init(c);
  642. c->cpu_index = 0;
  643. filter_cpuid_features(c, false);
  644. if (this_cpu->c_bsp_init)
  645. this_cpu->c_bsp_init(c);
  646. setup_force_cpu_cap(X86_FEATURE_ALWAYS);
  647. }
  648. void __init early_cpu_init(void)
  649. {
  650. const struct cpu_dev *const *cdev;
  651. int count = 0;
  652. #ifdef CONFIG_PROCESSOR_SELECT
  653. printk(KERN_INFO "KERNEL supported cpus:\n");
  654. #endif
  655. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  656. const struct cpu_dev *cpudev = *cdev;
  657. if (count >= X86_VENDOR_NUM)
  658. break;
  659. cpu_devs[count] = cpudev;
  660. count++;
  661. #ifdef CONFIG_PROCESSOR_SELECT
  662. {
  663. unsigned int j;
  664. for (j = 0; j < 2; j++) {
  665. if (!cpudev->c_ident[j])
  666. continue;
  667. printk(KERN_INFO " %s %s\n", cpudev->c_vendor,
  668. cpudev->c_ident[j]);
  669. }
  670. }
  671. #endif
  672. }
  673. early_identify_cpu(&boot_cpu_data);
  674. }
  675. /*
  676. * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
  677. * unfortunately, that's not true in practice because of early VIA
  678. * chips and (more importantly) broken virtualizers that are not easy
  679. * to detect. In the latter case it doesn't even *fail* reliably, so
  680. * probing for it doesn't even work. Disable it completely on 32-bit
  681. * unless we can find a reliable way to detect all the broken cases.
  682. * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
  683. */
  684. static void detect_nopl(struct cpuinfo_x86 *c)
  685. {
  686. #ifdef CONFIG_X86_32
  687. clear_cpu_cap(c, X86_FEATURE_NOPL);
  688. #else
  689. set_cpu_cap(c, X86_FEATURE_NOPL);
  690. #endif
  691. }
  692. static void generic_identify(struct cpuinfo_x86 *c)
  693. {
  694. c->extended_cpuid_level = 0;
  695. if (!have_cpuid_p())
  696. identify_cpu_without_cpuid(c);
  697. /* cyrix could have cpuid enabled via c_identify()*/
  698. if (!have_cpuid_p())
  699. return;
  700. cpu_detect(c);
  701. get_cpu_vendor(c);
  702. get_cpu_cap(c);
  703. if (c->cpuid_level >= 0x00000001) {
  704. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  705. #ifdef CONFIG_X86_32
  706. # ifdef CONFIG_X86_HT
  707. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  708. # else
  709. c->apicid = c->initial_apicid;
  710. # endif
  711. #endif
  712. c->phys_proc_id = c->initial_apicid;
  713. }
  714. get_model_name(c); /* Default name */
  715. detect_nopl(c);
  716. }
  717. static void x86_init_cache_qos(struct cpuinfo_x86 *c)
  718. {
  719. /*
  720. * The heavy lifting of max_rmid and cache_occ_scale are handled
  721. * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
  722. * in case CQM bits really aren't there in this CPU.
  723. */
  724. if (c != &boot_cpu_data) {
  725. boot_cpu_data.x86_cache_max_rmid =
  726. min(boot_cpu_data.x86_cache_max_rmid,
  727. c->x86_cache_max_rmid);
  728. }
  729. }
  730. /*
  731. * This does the hard work of actually picking apart the CPU stuff...
  732. */
  733. static void identify_cpu(struct cpuinfo_x86 *c)
  734. {
  735. int i;
  736. c->loops_per_jiffy = loops_per_jiffy;
  737. c->x86_cache_size = -1;
  738. c->x86_vendor = X86_VENDOR_UNKNOWN;
  739. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  740. c->x86_vendor_id[0] = '\0'; /* Unset */
  741. c->x86_model_id[0] = '\0'; /* Unset */
  742. c->x86_max_cores = 1;
  743. c->x86_coreid_bits = 0;
  744. #ifdef CONFIG_X86_64
  745. c->x86_clflush_size = 64;
  746. c->x86_phys_bits = 36;
  747. c->x86_virt_bits = 48;
  748. #else
  749. c->cpuid_level = -1; /* CPUID not detected */
  750. c->x86_clflush_size = 32;
  751. c->x86_phys_bits = 32;
  752. c->x86_virt_bits = 32;
  753. #endif
  754. c->x86_cache_alignment = c->x86_clflush_size;
  755. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  756. generic_identify(c);
  757. if (this_cpu->c_identify)
  758. this_cpu->c_identify(c);
  759. /* Clear/Set all flags overriden by options, after probe */
  760. for (i = 0; i < NCAPINTS; i++) {
  761. c->x86_capability[i] &= ~cpu_caps_cleared[i];
  762. c->x86_capability[i] |= cpu_caps_set[i];
  763. }
  764. #ifdef CONFIG_X86_64
  765. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  766. #endif
  767. /*
  768. * Vendor-specific initialization. In this section we
  769. * canonicalize the feature flags, meaning if there are
  770. * features a certain CPU supports which CPUID doesn't
  771. * tell us, CPUID claiming incorrect flags, or other bugs,
  772. * we handle them here.
  773. *
  774. * At the end of this section, c->x86_capability better
  775. * indicate the features this CPU genuinely supports!
  776. */
  777. if (this_cpu->c_init)
  778. this_cpu->c_init(c);
  779. /* Disable the PN if appropriate */
  780. squash_the_stupid_serial_number(c);
  781. /* Set up SMEP/SMAP */
  782. setup_smep(c);
  783. setup_smap(c);
  784. /*
  785. * The vendor-specific functions might have changed features.
  786. * Now we do "generic changes."
  787. */
  788. /* Filter out anything that depends on CPUID levels we don't have */
  789. filter_cpuid_features(c, true);
  790. /* If the model name is still unset, do table lookup. */
  791. if (!c->x86_model_id[0]) {
  792. const char *p;
  793. p = table_lookup_model(c);
  794. if (p)
  795. strcpy(c->x86_model_id, p);
  796. else
  797. /* Last resort... */
  798. sprintf(c->x86_model_id, "%02x/%02x",
  799. c->x86, c->x86_model);
  800. }
  801. #ifdef CONFIG_X86_64
  802. detect_ht(c);
  803. #endif
  804. init_hypervisor(c);
  805. x86_init_rdrand(c);
  806. x86_init_cache_qos(c);
  807. /*
  808. * Clear/Set all flags overriden by options, need do it
  809. * before following smp all cpus cap AND.
  810. */
  811. for (i = 0; i < NCAPINTS; i++) {
  812. c->x86_capability[i] &= ~cpu_caps_cleared[i];
  813. c->x86_capability[i] |= cpu_caps_set[i];
  814. }
  815. /*
  816. * On SMP, boot_cpu_data holds the common feature set between
  817. * all CPUs; so make sure that we indicate which features are
  818. * common between the CPUs. The first time this routine gets
  819. * executed, c == &boot_cpu_data.
  820. */
  821. if (c != &boot_cpu_data) {
  822. /* AND the already accumulated flags with these */
  823. for (i = 0; i < NCAPINTS; i++)
  824. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  825. /* OR, i.e. replicate the bug flags */
  826. for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
  827. c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
  828. }
  829. /* Init Machine Check Exception if available. */
  830. mcheck_cpu_init(c);
  831. select_idle_routine(c);
  832. #ifdef CONFIG_NUMA
  833. numa_add_cpu(smp_processor_id());
  834. #endif
  835. }
  836. /*
  837. * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
  838. * on 32-bit kernels:
  839. */
  840. #ifdef CONFIG_X86_32
  841. void enable_sep_cpu(void)
  842. {
  843. struct tss_struct *tss;
  844. int cpu;
  845. cpu = get_cpu();
  846. tss = &per_cpu(cpu_tss, cpu);
  847. if (!boot_cpu_has(X86_FEATURE_SEP))
  848. goto out;
  849. /*
  850. * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
  851. * see the big comment in struct x86_hw_tss's definition.
  852. */
  853. tss->x86_tss.ss1 = __KERNEL_CS;
  854. wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
  855. wrmsr(MSR_IA32_SYSENTER_ESP,
  856. (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
  857. 0);
  858. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)ia32_sysenter_target, 0);
  859. out:
  860. put_cpu();
  861. }
  862. #endif
  863. void __init identify_boot_cpu(void)
  864. {
  865. identify_cpu(&boot_cpu_data);
  866. init_amd_e400_c1e_mask();
  867. #ifdef CONFIG_X86_32
  868. sysenter_setup();
  869. enable_sep_cpu();
  870. #endif
  871. cpu_detect_tlb(&boot_cpu_data);
  872. }
  873. void identify_secondary_cpu(struct cpuinfo_x86 *c)
  874. {
  875. BUG_ON(c == &boot_cpu_data);
  876. identify_cpu(c);
  877. #ifdef CONFIG_X86_32
  878. enable_sep_cpu();
  879. #endif
  880. mtrr_ap_init();
  881. }
  882. struct msr_range {
  883. unsigned min;
  884. unsigned max;
  885. };
  886. static const struct msr_range msr_range_array[] = {
  887. { 0x00000000, 0x00000418},
  888. { 0xc0000000, 0xc000040b},
  889. { 0xc0010000, 0xc0010142},
  890. { 0xc0011000, 0xc001103b},
  891. };
  892. static void __print_cpu_msr(void)
  893. {
  894. unsigned index_min, index_max;
  895. unsigned index;
  896. u64 val;
  897. int i;
  898. for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  899. index_min = msr_range_array[i].min;
  900. index_max = msr_range_array[i].max;
  901. for (index = index_min; index < index_max; index++) {
  902. if (rdmsrl_safe(index, &val))
  903. continue;
  904. printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
  905. }
  906. }
  907. }
  908. static int show_msr;
  909. static __init int setup_show_msr(char *arg)
  910. {
  911. int num;
  912. get_option(&arg, &num);
  913. if (num > 0)
  914. show_msr = num;
  915. return 1;
  916. }
  917. __setup("show_msr=", setup_show_msr);
  918. static __init int setup_noclflush(char *arg)
  919. {
  920. setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
  921. setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
  922. return 1;
  923. }
  924. __setup("noclflush", setup_noclflush);
  925. void print_cpu_info(struct cpuinfo_x86 *c)
  926. {
  927. const char *vendor = NULL;
  928. if (c->x86_vendor < X86_VENDOR_NUM) {
  929. vendor = this_cpu->c_vendor;
  930. } else {
  931. if (c->cpuid_level >= 0)
  932. vendor = c->x86_vendor_id;
  933. }
  934. if (vendor && !strstr(c->x86_model_id, vendor))
  935. printk(KERN_CONT "%s ", vendor);
  936. if (c->x86_model_id[0])
  937. printk(KERN_CONT "%s", strim(c->x86_model_id));
  938. else
  939. printk(KERN_CONT "%d86", c->x86);
  940. printk(KERN_CONT " (fam: %02x, model: %02x", c->x86, c->x86_model);
  941. if (c->x86_mask || c->cpuid_level >= 0)
  942. printk(KERN_CONT ", stepping: %02x)\n", c->x86_mask);
  943. else
  944. printk(KERN_CONT ")\n");
  945. print_cpu_msr(c);
  946. }
  947. void print_cpu_msr(struct cpuinfo_x86 *c)
  948. {
  949. if (c->cpu_index < show_msr)
  950. __print_cpu_msr();
  951. }
  952. static __init int setup_disablecpuid(char *arg)
  953. {
  954. int bit;
  955. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  956. setup_clear_cpu_cap(bit);
  957. else
  958. return 0;
  959. return 1;
  960. }
  961. __setup("clearcpuid=", setup_disablecpuid);
  962. DEFINE_PER_CPU(unsigned long, kernel_stack) =
  963. (unsigned long)&init_thread_union + THREAD_SIZE;
  964. EXPORT_PER_CPU_SYMBOL(kernel_stack);
  965. #ifdef CONFIG_X86_64
  966. struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
  967. struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
  968. (unsigned long) debug_idt_table };
  969. DEFINE_PER_CPU_FIRST(union irq_stack_union,
  970. irq_stack_union) __aligned(PAGE_SIZE) __visible;
  971. /*
  972. * The following percpu variables are hot. Align current_task to
  973. * cacheline size such that they fall in the same cacheline.
  974. */
  975. DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
  976. &init_task;
  977. EXPORT_PER_CPU_SYMBOL(current_task);
  978. DEFINE_PER_CPU(char *, irq_stack_ptr) =
  979. init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
  980. DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
  981. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  982. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  983. DEFINE_PER_CPU(struct task_struct *, fpu_owner_task);
  984. /*
  985. * Special IST stacks which the CPU switches to when it calls
  986. * an IST-marked descriptor entry. Up to 7 stacks (hardware
  987. * limit), all of them are 4K, except the debug stack which
  988. * is 8K.
  989. */
  990. static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
  991. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  992. [DEBUG_STACK - 1] = DEBUG_STKSZ
  993. };
  994. static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
  995. [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
  996. /* May not be marked __init: used by software suspend */
  997. void syscall_init(void)
  998. {
  999. /*
  1000. * LSTAR and STAR live in a bit strange symbiosis.
  1001. * They both write to the same internal register. STAR allows to
  1002. * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
  1003. */
  1004. wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
  1005. wrmsrl(MSR_LSTAR, system_call);
  1006. #ifdef CONFIG_IA32_EMULATION
  1007. wrmsrl(MSR_CSTAR, ia32_cstar_target);
  1008. /*
  1009. * This only works on Intel CPUs.
  1010. * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
  1011. * This does not cause SYSENTER to jump to the wrong location, because
  1012. * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
  1013. */
  1014. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  1015. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  1016. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
  1017. #else
  1018. wrmsrl(MSR_CSTAR, ignore_sysret);
  1019. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
  1020. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  1021. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
  1022. #endif
  1023. /* Flags to clear on syscall */
  1024. wrmsrl(MSR_SYSCALL_MASK,
  1025. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
  1026. X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
  1027. }
  1028. /*
  1029. * Copies of the original ist values from the tss are only accessed during
  1030. * debugging, no special alignment required.
  1031. */
  1032. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  1033. static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
  1034. DEFINE_PER_CPU(int, debug_stack_usage);
  1035. int is_debug_stack(unsigned long addr)
  1036. {
  1037. return __this_cpu_read(debug_stack_usage) ||
  1038. (addr <= __this_cpu_read(debug_stack_addr) &&
  1039. addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
  1040. }
  1041. NOKPROBE_SYMBOL(is_debug_stack);
  1042. DEFINE_PER_CPU(u32, debug_idt_ctr);
  1043. void debug_stack_set_zero(void)
  1044. {
  1045. this_cpu_inc(debug_idt_ctr);
  1046. load_current_idt();
  1047. }
  1048. NOKPROBE_SYMBOL(debug_stack_set_zero);
  1049. void debug_stack_reset(void)
  1050. {
  1051. if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
  1052. return;
  1053. if (this_cpu_dec_return(debug_idt_ctr) == 0)
  1054. load_current_idt();
  1055. }
  1056. NOKPROBE_SYMBOL(debug_stack_reset);
  1057. #else /* CONFIG_X86_64 */
  1058. DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
  1059. EXPORT_PER_CPU_SYMBOL(current_task);
  1060. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  1061. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  1062. DEFINE_PER_CPU(struct task_struct *, fpu_owner_task);
  1063. /*
  1064. * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
  1065. * the top of the kernel stack. Use an extra percpu variable to track the
  1066. * top of the kernel stack directly.
  1067. */
  1068. DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
  1069. (unsigned long)&init_thread_union + THREAD_SIZE;
  1070. EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
  1071. #ifdef CONFIG_CC_STACKPROTECTOR
  1072. DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
  1073. #endif
  1074. #endif /* CONFIG_X86_64 */
  1075. /*
  1076. * Clear all 6 debug registers:
  1077. */
  1078. static void clear_all_debug_regs(void)
  1079. {
  1080. int i;
  1081. for (i = 0; i < 8; i++) {
  1082. /* Ignore db4, db5 */
  1083. if ((i == 4) || (i == 5))
  1084. continue;
  1085. set_debugreg(0, i);
  1086. }
  1087. }
  1088. #ifdef CONFIG_KGDB
  1089. /*
  1090. * Restore debug regs if using kgdbwait and you have a kernel debugger
  1091. * connection established.
  1092. */
  1093. static void dbg_restore_debug_regs(void)
  1094. {
  1095. if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
  1096. arch_kgdb_ops.correct_hw_break();
  1097. }
  1098. #else /* ! CONFIG_KGDB */
  1099. #define dbg_restore_debug_regs()
  1100. #endif /* ! CONFIG_KGDB */
  1101. static void wait_for_master_cpu(int cpu)
  1102. {
  1103. #ifdef CONFIG_SMP
  1104. /*
  1105. * wait for ACK from master CPU before continuing
  1106. * with AP initialization
  1107. */
  1108. WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
  1109. while (!cpumask_test_cpu(cpu, cpu_callout_mask))
  1110. cpu_relax();
  1111. #endif
  1112. }
  1113. /*
  1114. * cpu_init() initializes state that is per-CPU. Some data is already
  1115. * initialized (naturally) in the bootstrap process, such as the GDT
  1116. * and IDT. We reload them nevertheless, this function acts as a
  1117. * 'CPU state barrier', nothing should get across.
  1118. * A lot of state is already set up in PDA init for 64 bit
  1119. */
  1120. #ifdef CONFIG_X86_64
  1121. void cpu_init(void)
  1122. {
  1123. struct orig_ist *oist;
  1124. struct task_struct *me;
  1125. struct tss_struct *t;
  1126. unsigned long v;
  1127. int cpu = stack_smp_processor_id();
  1128. int i;
  1129. wait_for_master_cpu(cpu);
  1130. /*
  1131. * Initialize the CR4 shadow before doing anything that could
  1132. * try to read it.
  1133. */
  1134. cr4_init_shadow();
  1135. /*
  1136. * Load microcode on this cpu if a valid microcode is available.
  1137. * This is early microcode loading procedure.
  1138. */
  1139. load_ucode_ap();
  1140. t = &per_cpu(cpu_tss, cpu);
  1141. oist = &per_cpu(orig_ist, cpu);
  1142. #ifdef CONFIG_NUMA
  1143. if (this_cpu_read(numa_node) == 0 &&
  1144. early_cpu_to_node(cpu) != NUMA_NO_NODE)
  1145. set_numa_node(early_cpu_to_node(cpu));
  1146. #endif
  1147. me = current;
  1148. pr_debug("Initializing CPU#%d\n", cpu);
  1149. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1150. /*
  1151. * Initialize the per-CPU GDT with the boot GDT,
  1152. * and set up the GDT descriptor:
  1153. */
  1154. switch_to_new_gdt(cpu);
  1155. loadsegment(fs, 0);
  1156. load_current_idt();
  1157. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  1158. syscall_init();
  1159. wrmsrl(MSR_FS_BASE, 0);
  1160. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  1161. barrier();
  1162. x86_configure_nx();
  1163. x2apic_setup();
  1164. /*
  1165. * set up and load the per-CPU TSS
  1166. */
  1167. if (!oist->ist[0]) {
  1168. char *estacks = per_cpu(exception_stacks, cpu);
  1169. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  1170. estacks += exception_stack_sizes[v];
  1171. oist->ist[v] = t->x86_tss.ist[v] =
  1172. (unsigned long)estacks;
  1173. if (v == DEBUG_STACK-1)
  1174. per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
  1175. }
  1176. }
  1177. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  1178. /*
  1179. * <= is required because the CPU will access up to
  1180. * 8 bits beyond the end of the IO permission bitmap.
  1181. */
  1182. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  1183. t->io_bitmap[i] = ~0UL;
  1184. atomic_inc(&init_mm.mm_count);
  1185. me->active_mm = &init_mm;
  1186. BUG_ON(me->mm);
  1187. enter_lazy_tlb(&init_mm, me);
  1188. load_sp0(t, &current->thread);
  1189. set_tss_desc(cpu, t);
  1190. load_TR_desc();
  1191. load_LDT(&init_mm.context);
  1192. clear_all_debug_regs();
  1193. dbg_restore_debug_regs();
  1194. fpu_init();
  1195. if (is_uv_system())
  1196. uv_cpu_init();
  1197. }
  1198. #else
  1199. void cpu_init(void)
  1200. {
  1201. int cpu = smp_processor_id();
  1202. struct task_struct *curr = current;
  1203. struct tss_struct *t = &per_cpu(cpu_tss, cpu);
  1204. struct thread_struct *thread = &curr->thread;
  1205. wait_for_master_cpu(cpu);
  1206. /*
  1207. * Initialize the CR4 shadow before doing anything that could
  1208. * try to read it.
  1209. */
  1210. cr4_init_shadow();
  1211. show_ucode_info_early();
  1212. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  1213. if (cpu_feature_enabled(X86_FEATURE_VME) || cpu_has_tsc || cpu_has_de)
  1214. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1215. load_current_idt();
  1216. switch_to_new_gdt(cpu);
  1217. /*
  1218. * Set up and load the per-CPU TSS and LDT
  1219. */
  1220. atomic_inc(&init_mm.mm_count);
  1221. curr->active_mm = &init_mm;
  1222. BUG_ON(curr->mm);
  1223. enter_lazy_tlb(&init_mm, curr);
  1224. load_sp0(t, thread);
  1225. set_tss_desc(cpu, t);
  1226. load_TR_desc();
  1227. load_LDT(&init_mm.context);
  1228. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  1229. #ifdef CONFIG_DOUBLEFAULT
  1230. /* Set up doublefault TSS pointer in the GDT */
  1231. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  1232. #endif
  1233. clear_all_debug_regs();
  1234. dbg_restore_debug_regs();
  1235. fpu_init();
  1236. }
  1237. #endif
  1238. #ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
  1239. void warn_pre_alternatives(void)
  1240. {
  1241. WARN(1, "You're using static_cpu_has before alternatives have run!\n");
  1242. }
  1243. EXPORT_SYMBOL_GPL(warn_pre_alternatives);
  1244. #endif
  1245. inline bool __static_cpu_has_safe(u16 bit)
  1246. {
  1247. return boot_cpu_has(bit);
  1248. }
  1249. EXPORT_SYMBOL_GPL(__static_cpu_has_safe);