common.c 47 KB

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