common.c 35 KB

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