common.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  1. #include <linux/bootmem.h>
  2. #include <linux/linkage.h>
  3. #include <linux/bitops.h>
  4. #include <linux/kernel.h>
  5. #include <linux/export.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/mm.h>
  11. #include <linux/sched/clock.h>
  12. #include <linux/sched/task.h>
  13. #include <linux/init.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/kgdb.h>
  16. #include <linux/smp.h>
  17. #include <linux/io.h>
  18. #include <linux/syscore_ops.h>
  19. #include <asm/stackprotector.h>
  20. #include <asm/perf_event.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/archrandom.h>
  23. #include <asm/hypervisor.h>
  24. #include <asm/processor.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/debugreg.h>
  27. #include <asm/sections.h>
  28. #include <asm/vsyscall.h>
  29. #include <linux/topology.h>
  30. #include <linux/cpumask.h>
  31. #include <asm/pgtable.h>
  32. #include <linux/atomic.h>
  33. #include <asm/proto.h>
  34. #include <asm/setup.h>
  35. #include <asm/apic.h>
  36. #include <asm/desc.h>
  37. #include <asm/fpu/internal.h>
  38. #include <asm/mtrr.h>
  39. #include <asm/hwcap2.h>
  40. #include <linux/numa.h>
  41. #include <asm/asm.h>
  42. #include <asm/bugs.h>
  43. #include <asm/cpu.h>
  44. #include <asm/mce.h>
  45. #include <asm/msr.h>
  46. #include <asm/pat.h>
  47. #include <asm/microcode.h>
  48. #include <asm/microcode_intel.h>
  49. #ifdef CONFIG_X86_LOCAL_APIC
  50. #include <asm/uv/uv.h>
  51. #endif
  52. #include "cpu.h"
  53. u32 elf_hwcap2 __read_mostly;
  54. /* all of these masks are initialized in setup_cpu_local_masks() */
  55. cpumask_var_t cpu_initialized_mask;
  56. cpumask_var_t cpu_callout_mask;
  57. cpumask_var_t cpu_callin_mask;
  58. /* representing cpus for which sibling maps can be computed */
  59. cpumask_var_t cpu_sibling_setup_mask;
  60. /* correctly size the local cpu masks */
  61. void __init setup_cpu_local_masks(void)
  62. {
  63. alloc_bootmem_cpumask_var(&cpu_initialized_mask);
  64. alloc_bootmem_cpumask_var(&cpu_callin_mask);
  65. alloc_bootmem_cpumask_var(&cpu_callout_mask);
  66. alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
  67. }
  68. static void default_init(struct cpuinfo_x86 *c)
  69. {
  70. #ifdef CONFIG_X86_64
  71. cpu_detect_cache_sizes(c);
  72. #else
  73. /* Not much we can do here... */
  74. /* Check if at least it has cpuid */
  75. if (c->cpuid_level == -1) {
  76. /* No cpuid. It must be an ancient CPU */
  77. if (c->x86 == 4)
  78. strcpy(c->x86_model_id, "486");
  79. else if (c->x86 == 3)
  80. strcpy(c->x86_model_id, "386");
  81. }
  82. #endif
  83. }
  84. static const struct cpu_dev default_cpu = {
  85. .c_init = default_init,
  86. .c_vendor = "Unknown",
  87. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  88. };
  89. static const struct cpu_dev *this_cpu = &default_cpu;
  90. DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
  91. #ifdef CONFIG_X86_64
  92. /*
  93. * We need valid kernel segments for data and code in long mode too
  94. * IRET will check the segment types kkeil 2000/10/28
  95. * Also sysret mandates a special GDT layout
  96. *
  97. * TLS descriptors are currently at a different place compared to i386.
  98. * Hopefully nobody expects them at a fixed place (Wine?)
  99. */
  100. [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
  101. [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
  102. [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
  103. [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
  104. [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
  105. [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
  106. #else
  107. [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
  108. [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  109. [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
  110. [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
  111. /*
  112. * Segments used for calling PnP BIOS have byte granularity.
  113. * They code segments and data segments have fixed 64k limits,
  114. * the transfer segment sizes are set at run time.
  115. */
  116. /* 32-bit code */
  117. [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
  118. /* 16-bit code */
  119. [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
  120. /* 16-bit data */
  121. [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
  122. /* 16-bit data */
  123. [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
  124. /* 16-bit data */
  125. [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
  126. /*
  127. * The APM segments have byte granularity and their bases
  128. * are set at run time. All have 64k limits.
  129. */
  130. /* 32-bit code */
  131. [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
  132. /* 16-bit code */
  133. [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
  134. /* data */
  135. [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
  136. [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  137. [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  138. GDT_STACK_CANARY_INIT
  139. #endif
  140. } };
  141. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  142. static int __init x86_mpx_setup(char *s)
  143. {
  144. /* require an exact match without trailing characters */
  145. if (strlen(s))
  146. return 0;
  147. /* do not emit a message if the feature is not present */
  148. if (!boot_cpu_has(X86_FEATURE_MPX))
  149. return 1;
  150. setup_clear_cpu_cap(X86_FEATURE_MPX);
  151. pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
  152. return 1;
  153. }
  154. __setup("nompx", x86_mpx_setup);
  155. #ifdef CONFIG_X86_64
  156. static int __init x86_nopcid_setup(char *s)
  157. {
  158. /* nopcid doesn't accept parameters */
  159. if (s)
  160. return -EINVAL;
  161. /* do not emit a message if the feature is not present */
  162. if (!boot_cpu_has(X86_FEATURE_PCID))
  163. return 0;
  164. setup_clear_cpu_cap(X86_FEATURE_PCID);
  165. pr_info("nopcid: PCID feature disabled\n");
  166. return 0;
  167. }
  168. early_param("nopcid", x86_nopcid_setup);
  169. #endif
  170. static int __init x86_noinvpcid_setup(char *s)
  171. {
  172. /* noinvpcid doesn't accept parameters */
  173. if (s)
  174. return -EINVAL;
  175. /* do not emit a message if the feature is not present */
  176. if (!boot_cpu_has(X86_FEATURE_INVPCID))
  177. return 0;
  178. setup_clear_cpu_cap(X86_FEATURE_INVPCID);
  179. pr_info("noinvpcid: INVPCID feature disabled\n");
  180. return 0;
  181. }
  182. early_param("noinvpcid", x86_noinvpcid_setup);
  183. #ifdef CONFIG_X86_32
  184. static int cachesize_override = -1;
  185. static int disable_x86_serial_nr = 1;
  186. static int __init cachesize_setup(char *str)
  187. {
  188. get_option(&str, &cachesize_override);
  189. return 1;
  190. }
  191. __setup("cachesize=", cachesize_setup);
  192. static int __init x86_sep_setup(char *s)
  193. {
  194. setup_clear_cpu_cap(X86_FEATURE_SEP);
  195. return 1;
  196. }
  197. __setup("nosep", x86_sep_setup);
  198. /* Standard macro to see if a specific flag is changeable */
  199. static inline int flag_is_changeable_p(u32 flag)
  200. {
  201. u32 f1, f2;
  202. /*
  203. * Cyrix and IDT cpus allow disabling of CPUID
  204. * so the code below may return different results
  205. * when it is executed before and after enabling
  206. * the CPUID. Add "volatile" to not allow gcc to
  207. * optimize the subsequent calls to this function.
  208. */
  209. asm volatile ("pushfl \n\t"
  210. "pushfl \n\t"
  211. "popl %0 \n\t"
  212. "movl %0, %1 \n\t"
  213. "xorl %2, %0 \n\t"
  214. "pushl %0 \n\t"
  215. "popfl \n\t"
  216. "pushfl \n\t"
  217. "popl %0 \n\t"
  218. "popfl \n\t"
  219. : "=&r" (f1), "=&r" (f2)
  220. : "ir" (flag));
  221. return ((f1^f2) & flag) != 0;
  222. }
  223. /* Probe for the CPUID instruction */
  224. int have_cpuid_p(void)
  225. {
  226. return flag_is_changeable_p(X86_EFLAGS_ID);
  227. }
  228. static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  229. {
  230. unsigned long lo, hi;
  231. if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
  232. return;
  233. /* Disable processor serial number: */
  234. rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  235. lo |= 0x200000;
  236. wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  237. pr_notice("CPU serial number disabled.\n");
  238. clear_cpu_cap(c, X86_FEATURE_PN);
  239. /* Disabling the serial number may affect the cpuid level */
  240. c->cpuid_level = cpuid_eax(0);
  241. }
  242. static int __init x86_serial_nr_setup(char *s)
  243. {
  244. disable_x86_serial_nr = 0;
  245. return 1;
  246. }
  247. __setup("serialnumber", x86_serial_nr_setup);
  248. #else
  249. static inline int flag_is_changeable_p(u32 flag)
  250. {
  251. return 1;
  252. }
  253. static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  254. {
  255. }
  256. #endif
  257. static __init int setup_disable_smep(char *arg)
  258. {
  259. setup_clear_cpu_cap(X86_FEATURE_SMEP);
  260. /* Check for things that depend on SMEP being enabled: */
  261. check_mpx_erratum(&boot_cpu_data);
  262. return 1;
  263. }
  264. __setup("nosmep", setup_disable_smep);
  265. static __always_inline void setup_smep(struct cpuinfo_x86 *c)
  266. {
  267. if (cpu_has(c, X86_FEATURE_SMEP))
  268. cr4_set_bits(X86_CR4_SMEP);
  269. }
  270. static __init int setup_disable_smap(char *arg)
  271. {
  272. setup_clear_cpu_cap(X86_FEATURE_SMAP);
  273. return 1;
  274. }
  275. __setup("nosmap", setup_disable_smap);
  276. static __always_inline void setup_smap(struct cpuinfo_x86 *c)
  277. {
  278. unsigned long eflags = native_save_fl();
  279. /* This should have been cleared long ago */
  280. BUG_ON(eflags & X86_EFLAGS_AC);
  281. if (cpu_has(c, X86_FEATURE_SMAP)) {
  282. #ifdef CONFIG_X86_SMAP
  283. cr4_set_bits(X86_CR4_SMAP);
  284. #else
  285. cr4_clear_bits(X86_CR4_SMAP);
  286. #endif
  287. }
  288. }
  289. /*
  290. * Protection Keys are not available in 32-bit mode.
  291. */
  292. static bool pku_disabled;
  293. static __always_inline void setup_pku(struct cpuinfo_x86 *c)
  294. {
  295. /* check the boot processor, plus compile options for PKU: */
  296. if (!cpu_feature_enabled(X86_FEATURE_PKU))
  297. return;
  298. /* checks the actual processor's cpuid bits: */
  299. if (!cpu_has(c, X86_FEATURE_PKU))
  300. return;
  301. if (pku_disabled)
  302. return;
  303. cr4_set_bits(X86_CR4_PKE);
  304. /*
  305. * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
  306. * cpuid bit to be set. We need to ensure that we
  307. * update that bit in this CPU's "cpu_info".
  308. */
  309. get_cpu_cap(c);
  310. }
  311. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  312. static __init int setup_disable_pku(char *arg)
  313. {
  314. /*
  315. * Do not clear the X86_FEATURE_PKU bit. All of the
  316. * runtime checks are against OSPKE so clearing the
  317. * bit does nothing.
  318. *
  319. * This way, we will see "pku" in cpuinfo, but not
  320. * "ospke", which is exactly what we want. It shows
  321. * that the CPU has PKU, but the OS has not enabled it.
  322. * This happens to be exactly how a system would look
  323. * if we disabled the config option.
  324. */
  325. pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
  326. pku_disabled = true;
  327. return 1;
  328. }
  329. __setup("nopku", setup_disable_pku);
  330. #endif /* CONFIG_X86_64 */
  331. /*
  332. * Some CPU features depend on higher CPUID levels, which may not always
  333. * be available due to CPUID level capping or broken virtualization
  334. * software. Add those features to this table to auto-disable them.
  335. */
  336. struct cpuid_dependent_feature {
  337. u32 feature;
  338. u32 level;
  339. };
  340. static const struct cpuid_dependent_feature
  341. cpuid_dependent_features[] = {
  342. { X86_FEATURE_MWAIT, 0x00000005 },
  343. { X86_FEATURE_DCA, 0x00000009 },
  344. { X86_FEATURE_XSAVE, 0x0000000d },
  345. { 0, 0 }
  346. };
  347. static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
  348. {
  349. const struct cpuid_dependent_feature *df;
  350. for (df = cpuid_dependent_features; df->feature; df++) {
  351. if (!cpu_has(c, df->feature))
  352. continue;
  353. /*
  354. * Note: cpuid_level is set to -1 if unavailable, but
  355. * extended_extended_level is set to 0 if unavailable
  356. * and the legitimate extended levels are all negative
  357. * when signed; hence the weird messing around with
  358. * signs here...
  359. */
  360. if (!((s32)df->level < 0 ?
  361. (u32)df->level > (u32)c->extended_cpuid_level :
  362. (s32)df->level > (s32)c->cpuid_level))
  363. continue;
  364. clear_cpu_cap(c, df->feature);
  365. if (!warn)
  366. continue;
  367. pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
  368. x86_cap_flag(df->feature), df->level);
  369. }
  370. }
  371. /*
  372. * Naming convention should be: <Name> [(<Codename>)]
  373. * This table only is used unless init_<vendor>() below doesn't set it;
  374. * in particular, if CPUID levels 0x80000002..4 are supported, this
  375. * isn't used
  376. */
  377. /* Look up CPU names by table lookup. */
  378. static const char *table_lookup_model(struct cpuinfo_x86 *c)
  379. {
  380. #ifdef CONFIG_X86_32
  381. const struct legacy_cpu_model_info *info;
  382. if (c->x86_model >= 16)
  383. return NULL; /* Range check */
  384. if (!this_cpu)
  385. return NULL;
  386. info = this_cpu->legacy_models;
  387. while (info->family) {
  388. if (info->family == c->x86)
  389. return info->model_names[c->x86_model];
  390. info++;
  391. }
  392. #endif
  393. return NULL; /* Not found */
  394. }
  395. __u32 cpu_caps_cleared[NCAPINTS];
  396. __u32 cpu_caps_set[NCAPINTS];
  397. void load_percpu_segment(int cpu)
  398. {
  399. #ifdef CONFIG_X86_32
  400. loadsegment(fs, __KERNEL_PERCPU);
  401. #else
  402. __loadsegment_simple(gs, 0);
  403. wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
  404. #endif
  405. load_stack_canary_segment();
  406. }
  407. static void set_percpu_fixmap_pages(int fixmap_index, void *ptr,
  408. int pages, pgprot_t prot)
  409. {
  410. int i;
  411. for (i = 0; i < pages; i++) {
  412. __set_fixmap(fixmap_index - i,
  413. per_cpu_ptr_to_phys(ptr + i * PAGE_SIZE), prot);
  414. }
  415. }
  416. #ifdef CONFIG_X86_32
  417. /* The 32-bit entry code needs to find cpu_entry_area. */
  418. DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
  419. #endif
  420. /* Setup the fixmap mappings only once per-processor */
  421. static inline void setup_cpu_entry_area(int cpu)
  422. {
  423. #ifdef CONFIG_X86_64
  424. /* On 64-bit systems, we use a read-only fixmap GDT. */
  425. pgprot_t gdt_prot = PAGE_KERNEL_RO;
  426. #else
  427. /*
  428. * On native 32-bit systems, the GDT cannot be read-only because
  429. * our double fault handler uses a task gate, and entering through
  430. * a task gate needs to change an available TSS to busy. If the GDT
  431. * is read-only, that will triple fault.
  432. *
  433. * On Xen PV, the GDT must be read-only because the hypervisor requires
  434. * it.
  435. */
  436. pgprot_t gdt_prot = boot_cpu_has(X86_FEATURE_XENPV) ?
  437. PAGE_KERNEL_RO : PAGE_KERNEL;
  438. #endif
  439. __set_fixmap(get_cpu_entry_area_index(cpu, gdt), get_cpu_gdt_paddr(cpu), gdt_prot);
  440. /*
  441. * The Intel SDM says (Volume 3, 7.2.1):
  442. *
  443. * Avoid placing a page boundary in the part of the TSS that the
  444. * processor reads during a task switch (the first 104 bytes). The
  445. * processor may not correctly perform address translations if a
  446. * boundary occurs in this area. During a task switch, the processor
  447. * reads and writes into the first 104 bytes of each TSS (using
  448. * contiguous physical addresses beginning with the physical address
  449. * of the first byte of the TSS). So, after TSS access begins, if
  450. * part of the 104 bytes is not physically contiguous, the processor
  451. * will access incorrect information without generating a page-fault
  452. * exception.
  453. *
  454. * There are also a lot of errata involving the TSS spanning a page
  455. * boundary. Assert that we're not doing that.
  456. */
  457. BUILD_BUG_ON((offsetof(struct tss_struct, x86_tss) ^
  458. offsetofend(struct tss_struct, x86_tss)) & PAGE_MASK);
  459. BUILD_BUG_ON(sizeof(struct tss_struct) % PAGE_SIZE != 0);
  460. set_percpu_fixmap_pages(get_cpu_entry_area_index(cpu, tss),
  461. &per_cpu(cpu_tss, cpu),
  462. sizeof(struct tss_struct) / PAGE_SIZE,
  463. PAGE_KERNEL);
  464. #ifdef CONFIG_X86_32
  465. this_cpu_write(cpu_entry_area, get_cpu_entry_area(cpu));
  466. #endif
  467. }
  468. /* Load the original GDT from the per-cpu structure */
  469. void load_direct_gdt(int cpu)
  470. {
  471. struct desc_ptr gdt_descr;
  472. gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
  473. gdt_descr.size = GDT_SIZE - 1;
  474. load_gdt(&gdt_descr);
  475. }
  476. EXPORT_SYMBOL_GPL(load_direct_gdt);
  477. /* Load a fixmap remapping of the per-cpu GDT */
  478. void load_fixmap_gdt(int cpu)
  479. {
  480. struct desc_ptr gdt_descr;
  481. gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
  482. gdt_descr.size = GDT_SIZE - 1;
  483. load_gdt(&gdt_descr);
  484. }
  485. EXPORT_SYMBOL_GPL(load_fixmap_gdt);
  486. /*
  487. * Current gdt points %fs at the "master" per-cpu area: after this,
  488. * it's on the real one.
  489. */
  490. void switch_to_new_gdt(int cpu)
  491. {
  492. /* Load the original GDT */
  493. load_direct_gdt(cpu);
  494. /* Reload the per-cpu base */
  495. load_percpu_segment(cpu);
  496. }
  497. static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  498. static void get_model_name(struct cpuinfo_x86 *c)
  499. {
  500. unsigned int *v;
  501. char *p, *q, *s;
  502. if (c->extended_cpuid_level < 0x80000004)
  503. return;
  504. v = (unsigned int *)c->x86_model_id;
  505. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  506. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  507. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  508. c->x86_model_id[48] = 0;
  509. /* Trim whitespace */
  510. p = q = s = &c->x86_model_id[0];
  511. while (*p == ' ')
  512. p++;
  513. while (*p) {
  514. /* Note the last non-whitespace index */
  515. if (!isspace(*p))
  516. s = q;
  517. *q++ = *p++;
  518. }
  519. *(s + 1) = '\0';
  520. }
  521. void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
  522. {
  523. unsigned int n, dummy, ebx, ecx, edx, l2size;
  524. n = c->extended_cpuid_level;
  525. if (n >= 0x80000005) {
  526. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  527. c->x86_cache_size = (ecx>>24) + (edx>>24);
  528. #ifdef CONFIG_X86_64
  529. /* On K8 L1 TLB is inclusive, so don't count it */
  530. c->x86_tlbsize = 0;
  531. #endif
  532. }
  533. if (n < 0x80000006) /* Some chips just has a large L1. */
  534. return;
  535. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  536. l2size = ecx >> 16;
  537. #ifdef CONFIG_X86_64
  538. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  539. #else
  540. /* do processor-specific cache resizing */
  541. if (this_cpu->legacy_cache_size)
  542. l2size = this_cpu->legacy_cache_size(c, l2size);
  543. /* Allow user to override all this if necessary. */
  544. if (cachesize_override != -1)
  545. l2size = cachesize_override;
  546. if (l2size == 0)
  547. return; /* Again, no L2 cache is possible */
  548. #endif
  549. c->x86_cache_size = l2size;
  550. }
  551. u16 __read_mostly tlb_lli_4k[NR_INFO];
  552. u16 __read_mostly tlb_lli_2m[NR_INFO];
  553. u16 __read_mostly tlb_lli_4m[NR_INFO];
  554. u16 __read_mostly tlb_lld_4k[NR_INFO];
  555. u16 __read_mostly tlb_lld_2m[NR_INFO];
  556. u16 __read_mostly tlb_lld_4m[NR_INFO];
  557. u16 __read_mostly tlb_lld_1g[NR_INFO];
  558. static void cpu_detect_tlb(struct cpuinfo_x86 *c)
  559. {
  560. if (this_cpu->c_detect_tlb)
  561. this_cpu->c_detect_tlb(c);
  562. pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
  563. tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
  564. tlb_lli_4m[ENTRIES]);
  565. pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
  566. tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
  567. tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
  568. }
  569. void detect_ht(struct cpuinfo_x86 *c)
  570. {
  571. #ifdef CONFIG_SMP
  572. u32 eax, ebx, ecx, edx;
  573. int index_msb, core_bits;
  574. static bool printed;
  575. if (!cpu_has(c, X86_FEATURE_HT))
  576. return;
  577. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  578. goto out;
  579. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  580. return;
  581. cpuid(1, &eax, &ebx, &ecx, &edx);
  582. smp_num_siblings = (ebx & 0xff0000) >> 16;
  583. if (smp_num_siblings == 1) {
  584. pr_info_once("CPU0: Hyper-Threading is disabled\n");
  585. goto out;
  586. }
  587. if (smp_num_siblings <= 1)
  588. goto out;
  589. index_msb = get_count_order(smp_num_siblings);
  590. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
  591. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  592. index_msb = get_count_order(smp_num_siblings);
  593. core_bits = get_count_order(c->x86_max_cores);
  594. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
  595. ((1 << core_bits) - 1);
  596. out:
  597. if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
  598. pr_info("CPU: Physical Processor ID: %d\n",
  599. c->phys_proc_id);
  600. pr_info("CPU: Processor Core ID: %d\n",
  601. c->cpu_core_id);
  602. printed = 1;
  603. }
  604. #endif
  605. }
  606. static void get_cpu_vendor(struct cpuinfo_x86 *c)
  607. {
  608. char *v = c->x86_vendor_id;
  609. int i;
  610. for (i = 0; i < X86_VENDOR_NUM; i++) {
  611. if (!cpu_devs[i])
  612. break;
  613. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  614. (cpu_devs[i]->c_ident[1] &&
  615. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  616. this_cpu = cpu_devs[i];
  617. c->x86_vendor = this_cpu->c_x86_vendor;
  618. return;
  619. }
  620. }
  621. pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
  622. "CPU: Your system may be unstable.\n", v);
  623. c->x86_vendor = X86_VENDOR_UNKNOWN;
  624. this_cpu = &default_cpu;
  625. }
  626. void cpu_detect(struct cpuinfo_x86 *c)
  627. {
  628. /* Get vendor name */
  629. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  630. (unsigned int *)&c->x86_vendor_id[0],
  631. (unsigned int *)&c->x86_vendor_id[8],
  632. (unsigned int *)&c->x86_vendor_id[4]);
  633. c->x86 = 4;
  634. /* Intel-defined flags: level 0x00000001 */
  635. if (c->cpuid_level >= 0x00000001) {
  636. u32 junk, tfms, cap0, misc;
  637. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  638. c->x86 = x86_family(tfms);
  639. c->x86_model = x86_model(tfms);
  640. c->x86_mask = x86_stepping(tfms);
  641. if (cap0 & (1<<19)) {
  642. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  643. c->x86_cache_alignment = c->x86_clflush_size;
  644. }
  645. }
  646. }
  647. static void apply_forced_caps(struct cpuinfo_x86 *c)
  648. {
  649. int i;
  650. for (i = 0; i < NCAPINTS; i++) {
  651. c->x86_capability[i] &= ~cpu_caps_cleared[i];
  652. c->x86_capability[i] |= cpu_caps_set[i];
  653. }
  654. }
  655. void get_cpu_cap(struct cpuinfo_x86 *c)
  656. {
  657. u32 eax, ebx, ecx, edx;
  658. /* Intel-defined flags: level 0x00000001 */
  659. if (c->cpuid_level >= 0x00000001) {
  660. cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
  661. c->x86_capability[CPUID_1_ECX] = ecx;
  662. c->x86_capability[CPUID_1_EDX] = edx;
  663. }
  664. /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
  665. if (c->cpuid_level >= 0x00000006)
  666. c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
  667. /* Additional Intel-defined flags: level 0x00000007 */
  668. if (c->cpuid_level >= 0x00000007) {
  669. cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
  670. c->x86_capability[CPUID_7_0_EBX] = ebx;
  671. c->x86_capability[CPUID_7_ECX] = ecx;
  672. }
  673. /* Extended state features: level 0x0000000d */
  674. if (c->cpuid_level >= 0x0000000d) {
  675. cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
  676. c->x86_capability[CPUID_D_1_EAX] = eax;
  677. }
  678. /* Additional Intel-defined flags: level 0x0000000F */
  679. if (c->cpuid_level >= 0x0000000F) {
  680. /* QoS sub-leaf, EAX=0Fh, ECX=0 */
  681. cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
  682. c->x86_capability[CPUID_F_0_EDX] = edx;
  683. if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
  684. /* will be overridden if occupancy monitoring exists */
  685. c->x86_cache_max_rmid = ebx;
  686. /* QoS sub-leaf, EAX=0Fh, ECX=1 */
  687. cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
  688. c->x86_capability[CPUID_F_1_EDX] = edx;
  689. if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
  690. ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
  691. (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
  692. c->x86_cache_max_rmid = ecx;
  693. c->x86_cache_occ_scale = ebx;
  694. }
  695. } else {
  696. c->x86_cache_max_rmid = -1;
  697. c->x86_cache_occ_scale = -1;
  698. }
  699. }
  700. /* AMD-defined flags: level 0x80000001 */
  701. eax = cpuid_eax(0x80000000);
  702. c->extended_cpuid_level = eax;
  703. if ((eax & 0xffff0000) == 0x80000000) {
  704. if (eax >= 0x80000001) {
  705. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  706. c->x86_capability[CPUID_8000_0001_ECX] = ecx;
  707. c->x86_capability[CPUID_8000_0001_EDX] = edx;
  708. }
  709. }
  710. if (c->extended_cpuid_level >= 0x80000007) {
  711. cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
  712. c->x86_capability[CPUID_8000_0007_EBX] = ebx;
  713. c->x86_power = edx;
  714. }
  715. if (c->extended_cpuid_level >= 0x80000008) {
  716. cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
  717. c->x86_virt_bits = (eax >> 8) & 0xff;
  718. c->x86_phys_bits = eax & 0xff;
  719. c->x86_capability[CPUID_8000_0008_EBX] = ebx;
  720. }
  721. #ifdef CONFIG_X86_32
  722. else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
  723. c->x86_phys_bits = 36;
  724. #endif
  725. if (c->extended_cpuid_level >= 0x8000000a)
  726. c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
  727. init_scattered_cpuid_features(c);
  728. /*
  729. * Clear/Set all flags overridden by options, after probe.
  730. * This needs to happen each time we re-probe, which may happen
  731. * several times during CPU initialization.
  732. */
  733. apply_forced_caps(c);
  734. }
  735. static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  736. {
  737. #ifdef CONFIG_X86_32
  738. int i;
  739. /*
  740. * First of all, decide if this is a 486 or higher
  741. * It's a 486 if we can modify the AC flag
  742. */
  743. if (flag_is_changeable_p(X86_EFLAGS_AC))
  744. c->x86 = 4;
  745. else
  746. c->x86 = 3;
  747. for (i = 0; i < X86_VENDOR_NUM; i++)
  748. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  749. c->x86_vendor_id[0] = 0;
  750. cpu_devs[i]->c_identify(c);
  751. if (c->x86_vendor_id[0]) {
  752. get_cpu_vendor(c);
  753. break;
  754. }
  755. }
  756. #endif
  757. }
  758. /*
  759. * Do minimum CPU detection early.
  760. * Fields really needed: vendor, cpuid_level, family, model, mask,
  761. * cache alignment.
  762. * The others are not touched to avoid unwanted side effects.
  763. *
  764. * WARNING: this function is only called on the BP. Don't add code here
  765. * that is supposed to run on all CPUs.
  766. */
  767. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  768. {
  769. #ifdef CONFIG_X86_64
  770. c->x86_clflush_size = 64;
  771. c->x86_phys_bits = 36;
  772. c->x86_virt_bits = 48;
  773. #else
  774. c->x86_clflush_size = 32;
  775. c->x86_phys_bits = 32;
  776. c->x86_virt_bits = 32;
  777. #endif
  778. c->x86_cache_alignment = c->x86_clflush_size;
  779. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  780. c->extended_cpuid_level = 0;
  781. /* cyrix could have cpuid enabled via c_identify()*/
  782. if (have_cpuid_p()) {
  783. cpu_detect(c);
  784. get_cpu_vendor(c);
  785. get_cpu_cap(c);
  786. setup_force_cpu_cap(X86_FEATURE_CPUID);
  787. if (this_cpu->c_early_init)
  788. this_cpu->c_early_init(c);
  789. c->cpu_index = 0;
  790. filter_cpuid_features(c, false);
  791. if (this_cpu->c_bsp_init)
  792. this_cpu->c_bsp_init(c);
  793. } else {
  794. identify_cpu_without_cpuid(c);
  795. setup_clear_cpu_cap(X86_FEATURE_CPUID);
  796. }
  797. setup_force_cpu_cap(X86_FEATURE_ALWAYS);
  798. fpu__init_system(c);
  799. #ifdef CONFIG_X86_32
  800. /*
  801. * Regardless of whether PCID is enumerated, the SDM says
  802. * that it can't be enabled in 32-bit mode.
  803. */
  804. setup_clear_cpu_cap(X86_FEATURE_PCID);
  805. #endif
  806. }
  807. void __init early_cpu_init(void)
  808. {
  809. const struct cpu_dev *const *cdev;
  810. int count = 0;
  811. #ifdef CONFIG_PROCESSOR_SELECT
  812. pr_info("KERNEL supported cpus:\n");
  813. #endif
  814. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  815. const struct cpu_dev *cpudev = *cdev;
  816. if (count >= X86_VENDOR_NUM)
  817. break;
  818. cpu_devs[count] = cpudev;
  819. count++;
  820. #ifdef CONFIG_PROCESSOR_SELECT
  821. {
  822. unsigned int j;
  823. for (j = 0; j < 2; j++) {
  824. if (!cpudev->c_ident[j])
  825. continue;
  826. pr_info(" %s %s\n", cpudev->c_vendor,
  827. cpudev->c_ident[j]);
  828. }
  829. }
  830. #endif
  831. }
  832. early_identify_cpu(&boot_cpu_data);
  833. }
  834. /*
  835. * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
  836. * unfortunately, that's not true in practice because of early VIA
  837. * chips and (more importantly) broken virtualizers that are not easy
  838. * to detect. In the latter case it doesn't even *fail* reliably, so
  839. * probing for it doesn't even work. Disable it completely on 32-bit
  840. * unless we can find a reliable way to detect all the broken cases.
  841. * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
  842. */
  843. static void detect_nopl(struct cpuinfo_x86 *c)
  844. {
  845. #ifdef CONFIG_X86_32
  846. clear_cpu_cap(c, X86_FEATURE_NOPL);
  847. #else
  848. set_cpu_cap(c, X86_FEATURE_NOPL);
  849. #endif
  850. }
  851. static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
  852. {
  853. #ifdef CONFIG_X86_64
  854. /*
  855. * Empirically, writing zero to a segment selector on AMD does
  856. * not clear the base, whereas writing zero to a segment
  857. * selector on Intel does clear the base. Intel's behavior
  858. * allows slightly faster context switches in the common case
  859. * where GS is unused by the prev and next threads.
  860. *
  861. * Since neither vendor documents this anywhere that I can see,
  862. * detect it directly instead of hardcoding the choice by
  863. * vendor.
  864. *
  865. * I've designated AMD's behavior as the "bug" because it's
  866. * counterintuitive and less friendly.
  867. */
  868. unsigned long old_base, tmp;
  869. rdmsrl(MSR_FS_BASE, old_base);
  870. wrmsrl(MSR_FS_BASE, 1);
  871. loadsegment(fs, 0);
  872. rdmsrl(MSR_FS_BASE, tmp);
  873. if (tmp != 0)
  874. set_cpu_bug(c, X86_BUG_NULL_SEG);
  875. wrmsrl(MSR_FS_BASE, old_base);
  876. #endif
  877. }
  878. static void generic_identify(struct cpuinfo_x86 *c)
  879. {
  880. c->extended_cpuid_level = 0;
  881. if (!have_cpuid_p())
  882. identify_cpu_without_cpuid(c);
  883. /* cyrix could have cpuid enabled via c_identify()*/
  884. if (!have_cpuid_p())
  885. return;
  886. cpu_detect(c);
  887. get_cpu_vendor(c);
  888. get_cpu_cap(c);
  889. if (c->cpuid_level >= 0x00000001) {
  890. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  891. #ifdef CONFIG_X86_32
  892. # ifdef CONFIG_SMP
  893. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  894. # else
  895. c->apicid = c->initial_apicid;
  896. # endif
  897. #endif
  898. c->phys_proc_id = c->initial_apicid;
  899. }
  900. get_model_name(c); /* Default name */
  901. detect_nopl(c);
  902. detect_null_seg_behavior(c);
  903. /*
  904. * ESPFIX is a strange bug. All real CPUs have it. Paravirt
  905. * systems that run Linux at CPL > 0 may or may not have the
  906. * issue, but, even if they have the issue, there's absolutely
  907. * nothing we can do about it because we can't use the real IRET
  908. * instruction.
  909. *
  910. * NB: For the time being, only 32-bit kernels support
  911. * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
  912. * whether to apply espfix using paravirt hooks. If any
  913. * non-paravirt system ever shows up that does *not* have the
  914. * ESPFIX issue, we can change this.
  915. */
  916. #ifdef CONFIG_X86_32
  917. # ifdef CONFIG_PARAVIRT
  918. do {
  919. extern void native_iret(void);
  920. if (pv_cpu_ops.iret == native_iret)
  921. set_cpu_bug(c, X86_BUG_ESPFIX);
  922. } while (0);
  923. # else
  924. set_cpu_bug(c, X86_BUG_ESPFIX);
  925. # endif
  926. #endif
  927. }
  928. static void x86_init_cache_qos(struct cpuinfo_x86 *c)
  929. {
  930. /*
  931. * The heavy lifting of max_rmid and cache_occ_scale are handled
  932. * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
  933. * in case CQM bits really aren't there in this CPU.
  934. */
  935. if (c != &boot_cpu_data) {
  936. boot_cpu_data.x86_cache_max_rmid =
  937. min(boot_cpu_data.x86_cache_max_rmid,
  938. c->x86_cache_max_rmid);
  939. }
  940. }
  941. /*
  942. * Validate that ACPI/mptables have the same information about the
  943. * effective APIC id and update the package map.
  944. */
  945. static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
  946. {
  947. #ifdef CONFIG_SMP
  948. unsigned int apicid, cpu = smp_processor_id();
  949. apicid = apic->cpu_present_to_apicid(cpu);
  950. if (apicid != c->apicid) {
  951. pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
  952. cpu, apicid, c->initial_apicid);
  953. }
  954. BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
  955. #else
  956. c->logical_proc_id = 0;
  957. #endif
  958. }
  959. /*
  960. * This does the hard work of actually picking apart the CPU stuff...
  961. */
  962. static void identify_cpu(struct cpuinfo_x86 *c)
  963. {
  964. int i;
  965. c->loops_per_jiffy = loops_per_jiffy;
  966. c->x86_cache_size = -1;
  967. c->x86_vendor = X86_VENDOR_UNKNOWN;
  968. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  969. c->x86_vendor_id[0] = '\0'; /* Unset */
  970. c->x86_model_id[0] = '\0'; /* Unset */
  971. c->x86_max_cores = 1;
  972. c->x86_coreid_bits = 0;
  973. c->cu_id = 0xff;
  974. #ifdef CONFIG_X86_64
  975. c->x86_clflush_size = 64;
  976. c->x86_phys_bits = 36;
  977. c->x86_virt_bits = 48;
  978. #else
  979. c->cpuid_level = -1; /* CPUID not detected */
  980. c->x86_clflush_size = 32;
  981. c->x86_phys_bits = 32;
  982. c->x86_virt_bits = 32;
  983. #endif
  984. c->x86_cache_alignment = c->x86_clflush_size;
  985. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  986. generic_identify(c);
  987. if (this_cpu->c_identify)
  988. this_cpu->c_identify(c);
  989. /* Clear/Set all flags overridden by options, after probe */
  990. apply_forced_caps(c);
  991. #ifdef CONFIG_X86_64
  992. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  993. #endif
  994. /*
  995. * Vendor-specific initialization. In this section we
  996. * canonicalize the feature flags, meaning if there are
  997. * features a certain CPU supports which CPUID doesn't
  998. * tell us, CPUID claiming incorrect flags, or other bugs,
  999. * we handle them here.
  1000. *
  1001. * At the end of this section, c->x86_capability better
  1002. * indicate the features this CPU genuinely supports!
  1003. */
  1004. if (this_cpu->c_init)
  1005. this_cpu->c_init(c);
  1006. /* Disable the PN if appropriate */
  1007. squash_the_stupid_serial_number(c);
  1008. /* Set up SMEP/SMAP */
  1009. setup_smep(c);
  1010. setup_smap(c);
  1011. /*
  1012. * The vendor-specific functions might have changed features.
  1013. * Now we do "generic changes."
  1014. */
  1015. /* Filter out anything that depends on CPUID levels we don't have */
  1016. filter_cpuid_features(c, true);
  1017. /* If the model name is still unset, do table lookup. */
  1018. if (!c->x86_model_id[0]) {
  1019. const char *p;
  1020. p = table_lookup_model(c);
  1021. if (p)
  1022. strcpy(c->x86_model_id, p);
  1023. else
  1024. /* Last resort... */
  1025. sprintf(c->x86_model_id, "%02x/%02x",
  1026. c->x86, c->x86_model);
  1027. }
  1028. #ifdef CONFIG_X86_64
  1029. detect_ht(c);
  1030. #endif
  1031. x86_init_rdrand(c);
  1032. x86_init_cache_qos(c);
  1033. setup_pku(c);
  1034. /*
  1035. * Clear/Set all flags overridden by options, need do it
  1036. * before following smp all cpus cap AND.
  1037. */
  1038. apply_forced_caps(c);
  1039. /*
  1040. * On SMP, boot_cpu_data holds the common feature set between
  1041. * all CPUs; so make sure that we indicate which features are
  1042. * common between the CPUs. The first time this routine gets
  1043. * executed, c == &boot_cpu_data.
  1044. */
  1045. if (c != &boot_cpu_data) {
  1046. /* AND the already accumulated flags with these */
  1047. for (i = 0; i < NCAPINTS; i++)
  1048. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  1049. /* OR, i.e. replicate the bug flags */
  1050. for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
  1051. c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
  1052. }
  1053. /* Init Machine Check Exception if available. */
  1054. mcheck_cpu_init(c);
  1055. select_idle_routine(c);
  1056. #ifdef CONFIG_NUMA
  1057. numa_add_cpu(smp_processor_id());
  1058. #endif
  1059. }
  1060. /*
  1061. * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
  1062. * on 32-bit kernels:
  1063. */
  1064. #ifdef CONFIG_X86_32
  1065. void enable_sep_cpu(void)
  1066. {
  1067. struct tss_struct *tss;
  1068. int cpu;
  1069. if (!boot_cpu_has(X86_FEATURE_SEP))
  1070. return;
  1071. cpu = get_cpu();
  1072. tss = &per_cpu(cpu_tss, cpu);
  1073. /*
  1074. * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
  1075. * see the big comment in struct x86_hw_tss's definition.
  1076. */
  1077. tss->x86_tss.ss1 = __KERNEL_CS;
  1078. wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
  1079. wrmsr(MSR_IA32_SYSENTER_ESP,
  1080. (unsigned long)&get_cpu_entry_area(cpu)->tss +
  1081. offsetofend(struct tss_struct, SYSENTER_stack),
  1082. 0);
  1083. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
  1084. put_cpu();
  1085. }
  1086. #endif
  1087. void __init identify_boot_cpu(void)
  1088. {
  1089. identify_cpu(&boot_cpu_data);
  1090. #ifdef CONFIG_X86_32
  1091. sysenter_setup();
  1092. enable_sep_cpu();
  1093. #endif
  1094. cpu_detect_tlb(&boot_cpu_data);
  1095. }
  1096. void identify_secondary_cpu(struct cpuinfo_x86 *c)
  1097. {
  1098. BUG_ON(c == &boot_cpu_data);
  1099. identify_cpu(c);
  1100. #ifdef CONFIG_X86_32
  1101. enable_sep_cpu();
  1102. #endif
  1103. mtrr_ap_init();
  1104. validate_apic_and_package_id(c);
  1105. }
  1106. static __init int setup_noclflush(char *arg)
  1107. {
  1108. setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
  1109. setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
  1110. return 1;
  1111. }
  1112. __setup("noclflush", setup_noclflush);
  1113. void print_cpu_info(struct cpuinfo_x86 *c)
  1114. {
  1115. const char *vendor = NULL;
  1116. if (c->x86_vendor < X86_VENDOR_NUM) {
  1117. vendor = this_cpu->c_vendor;
  1118. } else {
  1119. if (c->cpuid_level >= 0)
  1120. vendor = c->x86_vendor_id;
  1121. }
  1122. if (vendor && !strstr(c->x86_model_id, vendor))
  1123. pr_cont("%s ", vendor);
  1124. if (c->x86_model_id[0])
  1125. pr_cont("%s", c->x86_model_id);
  1126. else
  1127. pr_cont("%d86", c->x86);
  1128. pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
  1129. if (c->x86_mask || c->cpuid_level >= 0)
  1130. pr_cont(", stepping: 0x%x)\n", c->x86_mask);
  1131. else
  1132. pr_cont(")\n");
  1133. }
  1134. /*
  1135. * clearcpuid= was already parsed in fpu__init_parse_early_param.
  1136. * But we need to keep a dummy __setup around otherwise it would
  1137. * show up as an environment variable for init.
  1138. */
  1139. static __init int setup_clearcpuid(char *arg)
  1140. {
  1141. return 1;
  1142. }
  1143. __setup("clearcpuid=", setup_clearcpuid);
  1144. #ifdef CONFIG_X86_64
  1145. DEFINE_PER_CPU_FIRST(union irq_stack_union,
  1146. irq_stack_union) __aligned(PAGE_SIZE) __visible;
  1147. /*
  1148. * The following percpu variables are hot. Align current_task to
  1149. * cacheline size such that they fall in the same cacheline.
  1150. */
  1151. DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
  1152. &init_task;
  1153. EXPORT_PER_CPU_SYMBOL(current_task);
  1154. DEFINE_PER_CPU(char *, irq_stack_ptr) =
  1155. init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
  1156. DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
  1157. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  1158. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  1159. /*
  1160. * Special IST stacks which the CPU switches to when it calls
  1161. * an IST-marked descriptor entry. Up to 7 stacks (hardware
  1162. * limit), all of them are 4K, except the debug stack which
  1163. * is 8K.
  1164. */
  1165. static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
  1166. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  1167. [DEBUG_STACK - 1] = DEBUG_STKSZ
  1168. };
  1169. static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
  1170. [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
  1171. /* May not be marked __init: used by software suspend */
  1172. void syscall_init(void)
  1173. {
  1174. int cpu = smp_processor_id();
  1175. wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
  1176. wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
  1177. #ifdef CONFIG_IA32_EMULATION
  1178. wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
  1179. /*
  1180. * This only works on Intel CPUs.
  1181. * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
  1182. * This does not cause SYSENTER to jump to the wrong location, because
  1183. * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
  1184. */
  1185. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  1186. wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
  1187. (unsigned long)&get_cpu_entry_area(cpu)->tss +
  1188. offsetofend(struct tss_struct, SYSENTER_stack));
  1189. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
  1190. #else
  1191. wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
  1192. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
  1193. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  1194. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
  1195. #endif
  1196. /* Flags to clear on syscall */
  1197. wrmsrl(MSR_SYSCALL_MASK,
  1198. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
  1199. X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
  1200. }
  1201. /*
  1202. * Copies of the original ist values from the tss are only accessed during
  1203. * debugging, no special alignment required.
  1204. */
  1205. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  1206. static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
  1207. DEFINE_PER_CPU(int, debug_stack_usage);
  1208. int is_debug_stack(unsigned long addr)
  1209. {
  1210. return __this_cpu_read(debug_stack_usage) ||
  1211. (addr <= __this_cpu_read(debug_stack_addr) &&
  1212. addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
  1213. }
  1214. NOKPROBE_SYMBOL(is_debug_stack);
  1215. DEFINE_PER_CPU(u32, debug_idt_ctr);
  1216. void debug_stack_set_zero(void)
  1217. {
  1218. this_cpu_inc(debug_idt_ctr);
  1219. load_current_idt();
  1220. }
  1221. NOKPROBE_SYMBOL(debug_stack_set_zero);
  1222. void debug_stack_reset(void)
  1223. {
  1224. if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
  1225. return;
  1226. if (this_cpu_dec_return(debug_idt_ctr) == 0)
  1227. load_current_idt();
  1228. }
  1229. NOKPROBE_SYMBOL(debug_stack_reset);
  1230. #else /* CONFIG_X86_64 */
  1231. DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
  1232. EXPORT_PER_CPU_SYMBOL(current_task);
  1233. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  1234. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  1235. /*
  1236. * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
  1237. * the top of the kernel stack. Use an extra percpu variable to track the
  1238. * top of the kernel stack directly.
  1239. */
  1240. DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
  1241. (unsigned long)&init_thread_union + THREAD_SIZE;
  1242. EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
  1243. #ifdef CONFIG_CC_STACKPROTECTOR
  1244. DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
  1245. #endif
  1246. #endif /* CONFIG_X86_64 */
  1247. /*
  1248. * Clear all 6 debug registers:
  1249. */
  1250. static void clear_all_debug_regs(void)
  1251. {
  1252. int i;
  1253. for (i = 0; i < 8; i++) {
  1254. /* Ignore db4, db5 */
  1255. if ((i == 4) || (i == 5))
  1256. continue;
  1257. set_debugreg(0, i);
  1258. }
  1259. }
  1260. #ifdef CONFIG_KGDB
  1261. /*
  1262. * Restore debug regs if using kgdbwait and you have a kernel debugger
  1263. * connection established.
  1264. */
  1265. static void dbg_restore_debug_regs(void)
  1266. {
  1267. if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
  1268. arch_kgdb_ops.correct_hw_break();
  1269. }
  1270. #else /* ! CONFIG_KGDB */
  1271. #define dbg_restore_debug_regs()
  1272. #endif /* ! CONFIG_KGDB */
  1273. static void wait_for_master_cpu(int cpu)
  1274. {
  1275. #ifdef CONFIG_SMP
  1276. /*
  1277. * wait for ACK from master CPU before continuing
  1278. * with AP initialization
  1279. */
  1280. WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
  1281. while (!cpumask_test_cpu(cpu, cpu_callout_mask))
  1282. cpu_relax();
  1283. #endif
  1284. }
  1285. /*
  1286. * cpu_init() initializes state that is per-CPU. Some data is already
  1287. * initialized (naturally) in the bootstrap process, such as the GDT
  1288. * and IDT. We reload them nevertheless, this function acts as a
  1289. * 'CPU state barrier', nothing should get across.
  1290. * A lot of state is already set up in PDA init for 64 bit
  1291. */
  1292. #ifdef CONFIG_X86_64
  1293. void cpu_init(void)
  1294. {
  1295. struct orig_ist *oist;
  1296. struct task_struct *me;
  1297. struct tss_struct *t;
  1298. unsigned long v;
  1299. int cpu = raw_smp_processor_id();
  1300. int i;
  1301. wait_for_master_cpu(cpu);
  1302. /*
  1303. * Initialize the CR4 shadow before doing anything that could
  1304. * try to read it.
  1305. */
  1306. cr4_init_shadow();
  1307. if (cpu)
  1308. load_ucode_ap();
  1309. t = &per_cpu(cpu_tss, cpu);
  1310. oist = &per_cpu(orig_ist, cpu);
  1311. #ifdef CONFIG_NUMA
  1312. if (this_cpu_read(numa_node) == 0 &&
  1313. early_cpu_to_node(cpu) != NUMA_NO_NODE)
  1314. set_numa_node(early_cpu_to_node(cpu));
  1315. #endif
  1316. me = current;
  1317. pr_debug("Initializing CPU#%d\n", cpu);
  1318. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1319. /*
  1320. * Initialize the per-CPU GDT with the boot GDT,
  1321. * and set up the GDT descriptor:
  1322. */
  1323. switch_to_new_gdt(cpu);
  1324. loadsegment(fs, 0);
  1325. load_current_idt();
  1326. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  1327. syscall_init();
  1328. wrmsrl(MSR_FS_BASE, 0);
  1329. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  1330. barrier();
  1331. x86_configure_nx();
  1332. x2apic_setup();
  1333. /*
  1334. * set up and load the per-CPU TSS
  1335. */
  1336. if (!oist->ist[0]) {
  1337. char *estacks = per_cpu(exception_stacks, cpu);
  1338. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  1339. estacks += exception_stack_sizes[v];
  1340. oist->ist[v] = t->x86_tss.ist[v] =
  1341. (unsigned long)estacks;
  1342. if (v == DEBUG_STACK-1)
  1343. per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
  1344. }
  1345. }
  1346. t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
  1347. /*
  1348. * <= is required because the CPU will access up to
  1349. * 8 bits beyond the end of the IO permission bitmap.
  1350. */
  1351. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  1352. t->io_bitmap[i] = ~0UL;
  1353. mmgrab(&init_mm);
  1354. me->active_mm = &init_mm;
  1355. BUG_ON(me->mm);
  1356. initialize_tlbstate_and_flush();
  1357. enter_lazy_tlb(&init_mm, me);
  1358. setup_cpu_entry_area(cpu);
  1359. /*
  1360. * Initialize the TSS. Don't bother initializing sp0, as the initial
  1361. * task never enters user mode.
  1362. */
  1363. set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
  1364. load_TR_desc();
  1365. load_mm_ldt(&init_mm);
  1366. clear_all_debug_regs();
  1367. dbg_restore_debug_regs();
  1368. fpu__init_cpu();
  1369. if (is_uv_system())
  1370. uv_cpu_init();
  1371. load_fixmap_gdt(cpu);
  1372. }
  1373. #else
  1374. void cpu_init(void)
  1375. {
  1376. int cpu = smp_processor_id();
  1377. struct task_struct *curr = current;
  1378. struct tss_struct *t = &per_cpu(cpu_tss, cpu);
  1379. wait_for_master_cpu(cpu);
  1380. /*
  1381. * Initialize the CR4 shadow before doing anything that could
  1382. * try to read it.
  1383. */
  1384. cr4_init_shadow();
  1385. show_ucode_info_early();
  1386. pr_info("Initializing CPU#%d\n", cpu);
  1387. if (cpu_feature_enabled(X86_FEATURE_VME) ||
  1388. boot_cpu_has(X86_FEATURE_TSC) ||
  1389. boot_cpu_has(X86_FEATURE_DE))
  1390. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1391. load_current_idt();
  1392. switch_to_new_gdt(cpu);
  1393. /*
  1394. * Set up and load the per-CPU TSS and LDT
  1395. */
  1396. mmgrab(&init_mm);
  1397. curr->active_mm = &init_mm;
  1398. BUG_ON(curr->mm);
  1399. initialize_tlbstate_and_flush();
  1400. enter_lazy_tlb(&init_mm, curr);
  1401. setup_cpu_entry_area(cpu);
  1402. /*
  1403. * Initialize the TSS. Don't bother initializing sp0, as the initial
  1404. * task never enters user mode.
  1405. */
  1406. set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
  1407. load_TR_desc();
  1408. load_mm_ldt(&init_mm);
  1409. t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
  1410. #ifdef CONFIG_DOUBLEFAULT
  1411. /* Set up doublefault TSS pointer in the GDT */
  1412. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  1413. #endif
  1414. clear_all_debug_regs();
  1415. dbg_restore_debug_regs();
  1416. fpu__init_cpu();
  1417. load_fixmap_gdt(cpu);
  1418. }
  1419. #endif
  1420. static void bsp_resume(void)
  1421. {
  1422. if (this_cpu->c_bsp_resume)
  1423. this_cpu->c_bsp_resume(&boot_cpu_data);
  1424. }
  1425. static struct syscore_ops cpu_syscore_ops = {
  1426. .resume = bsp_resume,
  1427. };
  1428. static int __init init_cpu_syscore(void)
  1429. {
  1430. register_syscore_ops(&cpu_syscore_ops);
  1431. return 0;
  1432. }
  1433. core_initcall(init_cpu_syscore);