common.c 36 KB

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