common.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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. void detect_ht(struct cpuinfo_x86 *c)
  562. {
  563. #ifdef CONFIG_SMP
  564. u32 eax, ebx, ecx, edx;
  565. int index_msb, core_bits;
  566. static bool printed;
  567. if (!cpu_has(c, X86_FEATURE_HT))
  568. return;
  569. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  570. goto out;
  571. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  572. return;
  573. cpuid(1, &eax, &ebx, &ecx, &edx);
  574. smp_num_siblings = (ebx & 0xff0000) >> 16;
  575. if (smp_num_siblings == 1) {
  576. pr_info_once("CPU0: Hyper-Threading is disabled\n");
  577. goto out;
  578. }
  579. if (smp_num_siblings <= 1)
  580. goto out;
  581. index_msb = get_count_order(smp_num_siblings);
  582. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
  583. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  584. index_msb = get_count_order(smp_num_siblings);
  585. core_bits = get_count_order(c->x86_max_cores);
  586. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
  587. ((1 << core_bits) - 1);
  588. out:
  589. if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
  590. pr_info("CPU: Physical Processor ID: %d\n",
  591. c->phys_proc_id);
  592. pr_info("CPU: Processor Core ID: %d\n",
  593. c->cpu_core_id);
  594. printed = 1;
  595. }
  596. #endif
  597. }
  598. static void get_cpu_vendor(struct cpuinfo_x86 *c)
  599. {
  600. char *v = c->x86_vendor_id;
  601. int i;
  602. for (i = 0; i < X86_VENDOR_NUM; i++) {
  603. if (!cpu_devs[i])
  604. break;
  605. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  606. (cpu_devs[i]->c_ident[1] &&
  607. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  608. this_cpu = cpu_devs[i];
  609. c->x86_vendor = this_cpu->c_x86_vendor;
  610. return;
  611. }
  612. }
  613. pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
  614. "CPU: Your system may be unstable.\n", v);
  615. c->x86_vendor = X86_VENDOR_UNKNOWN;
  616. this_cpu = &default_cpu;
  617. }
  618. void cpu_detect(struct cpuinfo_x86 *c)
  619. {
  620. /* Get vendor name */
  621. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  622. (unsigned int *)&c->x86_vendor_id[0],
  623. (unsigned int *)&c->x86_vendor_id[8],
  624. (unsigned int *)&c->x86_vendor_id[4]);
  625. c->x86 = 4;
  626. /* Intel-defined flags: level 0x00000001 */
  627. if (c->cpuid_level >= 0x00000001) {
  628. u32 junk, tfms, cap0, misc;
  629. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  630. c->x86 = x86_family(tfms);
  631. c->x86_model = x86_model(tfms);
  632. c->x86_stepping = x86_stepping(tfms);
  633. if (cap0 & (1<<19)) {
  634. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  635. c->x86_cache_alignment = c->x86_clflush_size;
  636. }
  637. }
  638. }
  639. static void apply_forced_caps(struct cpuinfo_x86 *c)
  640. {
  641. int i;
  642. for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
  643. c->x86_capability[i] &= ~cpu_caps_cleared[i];
  644. c->x86_capability[i] |= cpu_caps_set[i];
  645. }
  646. }
  647. static void init_speculation_control(struct cpuinfo_x86 *c)
  648. {
  649. /*
  650. * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
  651. * and they also have a different bit for STIBP support. Also,
  652. * a hypervisor might have set the individual AMD bits even on
  653. * Intel CPUs, for finer-grained selection of what's available.
  654. */
  655. if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
  656. set_cpu_cap(c, X86_FEATURE_IBRS);
  657. set_cpu_cap(c, X86_FEATURE_IBPB);
  658. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  659. }
  660. if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
  661. set_cpu_cap(c, X86_FEATURE_STIBP);
  662. if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
  663. cpu_has(c, X86_FEATURE_VIRT_SSBD))
  664. set_cpu_cap(c, X86_FEATURE_SSBD);
  665. if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
  666. set_cpu_cap(c, X86_FEATURE_IBRS);
  667. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  668. }
  669. if (cpu_has(c, X86_FEATURE_AMD_IBPB))
  670. set_cpu_cap(c, X86_FEATURE_IBPB);
  671. if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
  672. set_cpu_cap(c, X86_FEATURE_STIBP);
  673. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  674. }
  675. if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
  676. set_cpu_cap(c, X86_FEATURE_SSBD);
  677. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  678. clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
  679. }
  680. }
  681. void get_cpu_cap(struct cpuinfo_x86 *c)
  682. {
  683. u32 eax, ebx, ecx, edx;
  684. /* Intel-defined flags: level 0x00000001 */
  685. if (c->cpuid_level >= 0x00000001) {
  686. cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
  687. c->x86_capability[CPUID_1_ECX] = ecx;
  688. c->x86_capability[CPUID_1_EDX] = edx;
  689. }
  690. /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
  691. if (c->cpuid_level >= 0x00000006)
  692. c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
  693. /* Additional Intel-defined flags: level 0x00000007 */
  694. if (c->cpuid_level >= 0x00000007) {
  695. cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
  696. c->x86_capability[CPUID_7_0_EBX] = ebx;
  697. c->x86_capability[CPUID_7_ECX] = ecx;
  698. c->x86_capability[CPUID_7_EDX] = edx;
  699. }
  700. /* Extended state features: level 0x0000000d */
  701. if (c->cpuid_level >= 0x0000000d) {
  702. cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
  703. c->x86_capability[CPUID_D_1_EAX] = eax;
  704. }
  705. /* Additional Intel-defined flags: level 0x0000000F */
  706. if (c->cpuid_level >= 0x0000000F) {
  707. /* QoS sub-leaf, EAX=0Fh, ECX=0 */
  708. cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
  709. c->x86_capability[CPUID_F_0_EDX] = edx;
  710. if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
  711. /* will be overridden if occupancy monitoring exists */
  712. c->x86_cache_max_rmid = ebx;
  713. /* QoS sub-leaf, EAX=0Fh, ECX=1 */
  714. cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
  715. c->x86_capability[CPUID_F_1_EDX] = edx;
  716. if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
  717. ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
  718. (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
  719. c->x86_cache_max_rmid = ecx;
  720. c->x86_cache_occ_scale = ebx;
  721. }
  722. } else {
  723. c->x86_cache_max_rmid = -1;
  724. c->x86_cache_occ_scale = -1;
  725. }
  726. }
  727. /* AMD-defined flags: level 0x80000001 */
  728. eax = cpuid_eax(0x80000000);
  729. c->extended_cpuid_level = eax;
  730. if ((eax & 0xffff0000) == 0x80000000) {
  731. if (eax >= 0x80000001) {
  732. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  733. c->x86_capability[CPUID_8000_0001_ECX] = ecx;
  734. c->x86_capability[CPUID_8000_0001_EDX] = edx;
  735. }
  736. }
  737. if (c->extended_cpuid_level >= 0x80000007) {
  738. cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
  739. c->x86_capability[CPUID_8000_0007_EBX] = ebx;
  740. c->x86_power = edx;
  741. }
  742. if (c->extended_cpuid_level >= 0x80000008) {
  743. cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
  744. c->x86_capability[CPUID_8000_0008_EBX] = ebx;
  745. }
  746. if (c->extended_cpuid_level >= 0x8000000a)
  747. c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
  748. init_scattered_cpuid_features(c);
  749. init_speculation_control(c);
  750. /*
  751. * Clear/Set all flags overridden by options, after probe.
  752. * This needs to happen each time we re-probe, which may happen
  753. * several times during CPU initialization.
  754. */
  755. apply_forced_caps(c);
  756. }
  757. static void get_cpu_address_sizes(struct cpuinfo_x86 *c)
  758. {
  759. u32 eax, ebx, ecx, edx;
  760. if (c->extended_cpuid_level >= 0x80000008) {
  761. cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
  762. c->x86_virt_bits = (eax >> 8) & 0xff;
  763. c->x86_phys_bits = eax & 0xff;
  764. }
  765. #ifdef CONFIG_X86_32
  766. else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
  767. c->x86_phys_bits = 36;
  768. #endif
  769. }
  770. static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  771. {
  772. #ifdef CONFIG_X86_32
  773. int i;
  774. /*
  775. * First of all, decide if this is a 486 or higher
  776. * It's a 486 if we can modify the AC flag
  777. */
  778. if (flag_is_changeable_p(X86_EFLAGS_AC))
  779. c->x86 = 4;
  780. else
  781. c->x86 = 3;
  782. for (i = 0; i < X86_VENDOR_NUM; i++)
  783. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  784. c->x86_vendor_id[0] = 0;
  785. cpu_devs[i]->c_identify(c);
  786. if (c->x86_vendor_id[0]) {
  787. get_cpu_vendor(c);
  788. break;
  789. }
  790. }
  791. #endif
  792. }
  793. static const __initconst struct x86_cpu_id cpu_no_speculation[] = {
  794. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CEDARVIEW, X86_FEATURE_ANY },
  795. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CLOVERVIEW, X86_FEATURE_ANY },
  796. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_LINCROFT, X86_FEATURE_ANY },
  797. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PENWELL, X86_FEATURE_ANY },
  798. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PINEVIEW, X86_FEATURE_ANY },
  799. { X86_VENDOR_CENTAUR, 5 },
  800. { X86_VENDOR_INTEL, 5 },
  801. { X86_VENDOR_NSC, 5 },
  802. { X86_VENDOR_ANY, 4 },
  803. {}
  804. };
  805. static const __initconst struct x86_cpu_id cpu_no_meltdown[] = {
  806. { X86_VENDOR_AMD },
  807. {}
  808. };
  809. /* Only list CPUs which speculate but are non susceptible to SSB */
  810. static const __initconst struct x86_cpu_id cpu_no_spec_store_bypass[] = {
  811. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
  812. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
  813. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT2 },
  814. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_MERRIFIELD },
  815. { X86_VENDOR_INTEL, 6, INTEL_FAM6_CORE_YONAH },
  816. { X86_VENDOR_INTEL, 6, INTEL_FAM6_XEON_PHI_KNL },
  817. { X86_VENDOR_INTEL, 6, INTEL_FAM6_XEON_PHI_KNM },
  818. { X86_VENDOR_AMD, 0x12, },
  819. { X86_VENDOR_AMD, 0x11, },
  820. { X86_VENDOR_AMD, 0x10, },
  821. { X86_VENDOR_AMD, 0xf, },
  822. {}
  823. };
  824. static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
  825. {
  826. u64 ia32_cap = 0;
  827. if (x86_match_cpu(cpu_no_speculation))
  828. return;
  829. setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
  830. setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
  831. if (cpu_has(c, X86_FEATURE_ARCH_CAPABILITIES))
  832. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
  833. if (!x86_match_cpu(cpu_no_spec_store_bypass) &&
  834. !(ia32_cap & ARCH_CAP_SSB_NO) &&
  835. !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
  836. setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
  837. if (x86_match_cpu(cpu_no_meltdown))
  838. return;
  839. /* Rogue Data Cache Load? No! */
  840. if (ia32_cap & ARCH_CAP_RDCL_NO)
  841. return;
  842. setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
  843. }
  844. /*
  845. * Do minimum CPU detection early.
  846. * Fields really needed: vendor, cpuid_level, family, model, mask,
  847. * cache alignment.
  848. * The others are not touched to avoid unwanted side effects.
  849. *
  850. * WARNING: this function is only called on the boot CPU. Don't add code
  851. * here that is supposed to run on all CPUs.
  852. */
  853. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  854. {
  855. #ifdef CONFIG_X86_64
  856. c->x86_clflush_size = 64;
  857. c->x86_phys_bits = 36;
  858. c->x86_virt_bits = 48;
  859. #else
  860. c->x86_clflush_size = 32;
  861. c->x86_phys_bits = 32;
  862. c->x86_virt_bits = 32;
  863. #endif
  864. c->x86_cache_alignment = c->x86_clflush_size;
  865. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  866. c->extended_cpuid_level = 0;
  867. /* cyrix could have cpuid enabled via c_identify()*/
  868. if (have_cpuid_p()) {
  869. cpu_detect(c);
  870. get_cpu_vendor(c);
  871. get_cpu_cap(c);
  872. get_cpu_address_sizes(c);
  873. setup_force_cpu_cap(X86_FEATURE_CPUID);
  874. if (this_cpu->c_early_init)
  875. this_cpu->c_early_init(c);
  876. c->cpu_index = 0;
  877. filter_cpuid_features(c, false);
  878. if (this_cpu->c_bsp_init)
  879. this_cpu->c_bsp_init(c);
  880. } else {
  881. identify_cpu_without_cpuid(c);
  882. setup_clear_cpu_cap(X86_FEATURE_CPUID);
  883. }
  884. setup_force_cpu_cap(X86_FEATURE_ALWAYS);
  885. cpu_set_bug_bits(c);
  886. fpu__init_system(c);
  887. #ifdef CONFIG_X86_32
  888. /*
  889. * Regardless of whether PCID is enumerated, the SDM says
  890. * that it can't be enabled in 32-bit mode.
  891. */
  892. setup_clear_cpu_cap(X86_FEATURE_PCID);
  893. #endif
  894. /*
  895. * Later in the boot process pgtable_l5_enabled() relies on
  896. * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
  897. * enabled by this point we need to clear the feature bit to avoid
  898. * false-positives at the later stage.
  899. *
  900. * pgtable_l5_enabled() can be false here for several reasons:
  901. * - 5-level paging is disabled compile-time;
  902. * - it's 32-bit kernel;
  903. * - machine doesn't support 5-level paging;
  904. * - user specified 'no5lvl' in kernel command line.
  905. */
  906. if (!pgtable_l5_enabled())
  907. setup_clear_cpu_cap(X86_FEATURE_LA57);
  908. }
  909. void __init early_cpu_init(void)
  910. {
  911. const struct cpu_dev *const *cdev;
  912. int count = 0;
  913. #ifdef CONFIG_PROCESSOR_SELECT
  914. pr_info("KERNEL supported cpus:\n");
  915. #endif
  916. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  917. const struct cpu_dev *cpudev = *cdev;
  918. if (count >= X86_VENDOR_NUM)
  919. break;
  920. cpu_devs[count] = cpudev;
  921. count++;
  922. #ifdef CONFIG_PROCESSOR_SELECT
  923. {
  924. unsigned int j;
  925. for (j = 0; j < 2; j++) {
  926. if (!cpudev->c_ident[j])
  927. continue;
  928. pr_info(" %s %s\n", cpudev->c_vendor,
  929. cpudev->c_ident[j]);
  930. }
  931. }
  932. #endif
  933. }
  934. early_identify_cpu(&boot_cpu_data);
  935. }
  936. /*
  937. * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
  938. * unfortunately, that's not true in practice because of early VIA
  939. * chips and (more importantly) broken virtualizers that are not easy
  940. * to detect. In the latter case it doesn't even *fail* reliably, so
  941. * probing for it doesn't even work. Disable it completely on 32-bit
  942. * unless we can find a reliable way to detect all the broken cases.
  943. * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
  944. */
  945. static void detect_nopl(struct cpuinfo_x86 *c)
  946. {
  947. #ifdef CONFIG_X86_32
  948. clear_cpu_cap(c, X86_FEATURE_NOPL);
  949. #else
  950. set_cpu_cap(c, X86_FEATURE_NOPL);
  951. #endif
  952. }
  953. static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
  954. {
  955. #ifdef CONFIG_X86_64
  956. /*
  957. * Empirically, writing zero to a segment selector on AMD does
  958. * not clear the base, whereas writing zero to a segment
  959. * selector on Intel does clear the base. Intel's behavior
  960. * allows slightly faster context switches in the common case
  961. * where GS is unused by the prev and next threads.
  962. *
  963. * Since neither vendor documents this anywhere that I can see,
  964. * detect it directly instead of hardcoding the choice by
  965. * vendor.
  966. *
  967. * I've designated AMD's behavior as the "bug" because it's
  968. * counterintuitive and less friendly.
  969. */
  970. unsigned long old_base, tmp;
  971. rdmsrl(MSR_FS_BASE, old_base);
  972. wrmsrl(MSR_FS_BASE, 1);
  973. loadsegment(fs, 0);
  974. rdmsrl(MSR_FS_BASE, tmp);
  975. if (tmp != 0)
  976. set_cpu_bug(c, X86_BUG_NULL_SEG);
  977. wrmsrl(MSR_FS_BASE, old_base);
  978. #endif
  979. }
  980. static void generic_identify(struct cpuinfo_x86 *c)
  981. {
  982. c->extended_cpuid_level = 0;
  983. if (!have_cpuid_p())
  984. identify_cpu_without_cpuid(c);
  985. /* cyrix could have cpuid enabled via c_identify()*/
  986. if (!have_cpuid_p())
  987. return;
  988. cpu_detect(c);
  989. get_cpu_vendor(c);
  990. get_cpu_cap(c);
  991. get_cpu_address_sizes(c);
  992. if (c->cpuid_level >= 0x00000001) {
  993. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  994. #ifdef CONFIG_X86_32
  995. # ifdef CONFIG_SMP
  996. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  997. # else
  998. c->apicid = c->initial_apicid;
  999. # endif
  1000. #endif
  1001. c->phys_proc_id = c->initial_apicid;
  1002. }
  1003. get_model_name(c); /* Default name */
  1004. detect_nopl(c);
  1005. detect_null_seg_behavior(c);
  1006. /*
  1007. * ESPFIX is a strange bug. All real CPUs have it. Paravirt
  1008. * systems that run Linux at CPL > 0 may or may not have the
  1009. * issue, but, even if they have the issue, there's absolutely
  1010. * nothing we can do about it because we can't use the real IRET
  1011. * instruction.
  1012. *
  1013. * NB: For the time being, only 32-bit kernels support
  1014. * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
  1015. * whether to apply espfix using paravirt hooks. If any
  1016. * non-paravirt system ever shows up that does *not* have the
  1017. * ESPFIX issue, we can change this.
  1018. */
  1019. #ifdef CONFIG_X86_32
  1020. # ifdef CONFIG_PARAVIRT
  1021. do {
  1022. extern void native_iret(void);
  1023. if (pv_cpu_ops.iret == native_iret)
  1024. set_cpu_bug(c, X86_BUG_ESPFIX);
  1025. } while (0);
  1026. # else
  1027. set_cpu_bug(c, X86_BUG_ESPFIX);
  1028. # endif
  1029. #endif
  1030. }
  1031. static void x86_init_cache_qos(struct cpuinfo_x86 *c)
  1032. {
  1033. /*
  1034. * The heavy lifting of max_rmid and cache_occ_scale are handled
  1035. * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
  1036. * in case CQM bits really aren't there in this CPU.
  1037. */
  1038. if (c != &boot_cpu_data) {
  1039. boot_cpu_data.x86_cache_max_rmid =
  1040. min(boot_cpu_data.x86_cache_max_rmid,
  1041. c->x86_cache_max_rmid);
  1042. }
  1043. }
  1044. /*
  1045. * Validate that ACPI/mptables have the same information about the
  1046. * effective APIC id and update the package map.
  1047. */
  1048. static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
  1049. {
  1050. #ifdef CONFIG_SMP
  1051. unsigned int apicid, cpu = smp_processor_id();
  1052. apicid = apic->cpu_present_to_apicid(cpu);
  1053. if (apicid != c->apicid) {
  1054. pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
  1055. cpu, apicid, c->initial_apicid);
  1056. }
  1057. BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
  1058. #else
  1059. c->logical_proc_id = 0;
  1060. #endif
  1061. }
  1062. /*
  1063. * This does the hard work of actually picking apart the CPU stuff...
  1064. */
  1065. static void identify_cpu(struct cpuinfo_x86 *c)
  1066. {
  1067. int i;
  1068. c->loops_per_jiffy = loops_per_jiffy;
  1069. c->x86_cache_size = 0;
  1070. c->x86_vendor = X86_VENDOR_UNKNOWN;
  1071. c->x86_model = c->x86_stepping = 0; /* So far unknown... */
  1072. c->x86_vendor_id[0] = '\0'; /* Unset */
  1073. c->x86_model_id[0] = '\0'; /* Unset */
  1074. c->x86_max_cores = 1;
  1075. c->x86_coreid_bits = 0;
  1076. c->cu_id = 0xff;
  1077. #ifdef CONFIG_X86_64
  1078. c->x86_clflush_size = 64;
  1079. c->x86_phys_bits = 36;
  1080. c->x86_virt_bits = 48;
  1081. #else
  1082. c->cpuid_level = -1; /* CPUID not detected */
  1083. c->x86_clflush_size = 32;
  1084. c->x86_phys_bits = 32;
  1085. c->x86_virt_bits = 32;
  1086. #endif
  1087. c->x86_cache_alignment = c->x86_clflush_size;
  1088. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  1089. generic_identify(c);
  1090. if (this_cpu->c_identify)
  1091. this_cpu->c_identify(c);
  1092. /* Clear/Set all flags overridden by options, after probe */
  1093. apply_forced_caps(c);
  1094. #ifdef CONFIG_X86_64
  1095. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  1096. #endif
  1097. /*
  1098. * Vendor-specific initialization. In this section we
  1099. * canonicalize the feature flags, meaning if there are
  1100. * features a certain CPU supports which CPUID doesn't
  1101. * tell us, CPUID claiming incorrect flags, or other bugs,
  1102. * we handle them here.
  1103. *
  1104. * At the end of this section, c->x86_capability better
  1105. * indicate the features this CPU genuinely supports!
  1106. */
  1107. if (this_cpu->c_init)
  1108. this_cpu->c_init(c);
  1109. /* Disable the PN if appropriate */
  1110. squash_the_stupid_serial_number(c);
  1111. /* Set up SMEP/SMAP/UMIP */
  1112. setup_smep(c);
  1113. setup_smap(c);
  1114. setup_umip(c);
  1115. /*
  1116. * The vendor-specific functions might have changed features.
  1117. * Now we do "generic changes."
  1118. */
  1119. /* Filter out anything that depends on CPUID levels we don't have */
  1120. filter_cpuid_features(c, true);
  1121. /* If the model name is still unset, do table lookup. */
  1122. if (!c->x86_model_id[0]) {
  1123. const char *p;
  1124. p = table_lookup_model(c);
  1125. if (p)
  1126. strcpy(c->x86_model_id, p);
  1127. else
  1128. /* Last resort... */
  1129. sprintf(c->x86_model_id, "%02x/%02x",
  1130. c->x86, c->x86_model);
  1131. }
  1132. #ifdef CONFIG_X86_64
  1133. detect_ht(c);
  1134. #endif
  1135. x86_init_rdrand(c);
  1136. x86_init_cache_qos(c);
  1137. setup_pku(c);
  1138. /*
  1139. * Clear/Set all flags overridden by options, need do it
  1140. * before following smp all cpus cap AND.
  1141. */
  1142. apply_forced_caps(c);
  1143. /*
  1144. * On SMP, boot_cpu_data holds the common feature set between
  1145. * all CPUs; so make sure that we indicate which features are
  1146. * common between the CPUs. The first time this routine gets
  1147. * executed, c == &boot_cpu_data.
  1148. */
  1149. if (c != &boot_cpu_data) {
  1150. /* AND the already accumulated flags with these */
  1151. for (i = 0; i < NCAPINTS; i++)
  1152. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  1153. /* OR, i.e. replicate the bug flags */
  1154. for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
  1155. c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
  1156. }
  1157. /* Init Machine Check Exception if available. */
  1158. mcheck_cpu_init(c);
  1159. select_idle_routine(c);
  1160. #ifdef CONFIG_NUMA
  1161. numa_add_cpu(smp_processor_id());
  1162. #endif
  1163. }
  1164. /*
  1165. * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
  1166. * on 32-bit kernels:
  1167. */
  1168. #ifdef CONFIG_X86_32
  1169. void enable_sep_cpu(void)
  1170. {
  1171. struct tss_struct *tss;
  1172. int cpu;
  1173. if (!boot_cpu_has(X86_FEATURE_SEP))
  1174. return;
  1175. cpu = get_cpu();
  1176. tss = &per_cpu(cpu_tss_rw, cpu);
  1177. /*
  1178. * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
  1179. * see the big comment in struct x86_hw_tss's definition.
  1180. */
  1181. tss->x86_tss.ss1 = __KERNEL_CS;
  1182. wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
  1183. wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
  1184. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
  1185. put_cpu();
  1186. }
  1187. #endif
  1188. void __init identify_boot_cpu(void)
  1189. {
  1190. identify_cpu(&boot_cpu_data);
  1191. #ifdef CONFIG_X86_32
  1192. sysenter_setup();
  1193. enable_sep_cpu();
  1194. #endif
  1195. cpu_detect_tlb(&boot_cpu_data);
  1196. }
  1197. void identify_secondary_cpu(struct cpuinfo_x86 *c)
  1198. {
  1199. BUG_ON(c == &boot_cpu_data);
  1200. identify_cpu(c);
  1201. #ifdef CONFIG_X86_32
  1202. enable_sep_cpu();
  1203. #endif
  1204. mtrr_ap_init();
  1205. validate_apic_and_package_id(c);
  1206. x86_spec_ctrl_setup_ap();
  1207. }
  1208. static __init int setup_noclflush(char *arg)
  1209. {
  1210. setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
  1211. setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
  1212. return 1;
  1213. }
  1214. __setup("noclflush", setup_noclflush);
  1215. void print_cpu_info(struct cpuinfo_x86 *c)
  1216. {
  1217. const char *vendor = NULL;
  1218. if (c->x86_vendor < X86_VENDOR_NUM) {
  1219. vendor = this_cpu->c_vendor;
  1220. } else {
  1221. if (c->cpuid_level >= 0)
  1222. vendor = c->x86_vendor_id;
  1223. }
  1224. if (vendor && !strstr(c->x86_model_id, vendor))
  1225. pr_cont("%s ", vendor);
  1226. if (c->x86_model_id[0])
  1227. pr_cont("%s", c->x86_model_id);
  1228. else
  1229. pr_cont("%d86", c->x86);
  1230. pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
  1231. if (c->x86_stepping || c->cpuid_level >= 0)
  1232. pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
  1233. else
  1234. pr_cont(")\n");
  1235. }
  1236. /*
  1237. * clearcpuid= was already parsed in fpu__init_parse_early_param.
  1238. * But we need to keep a dummy __setup around otherwise it would
  1239. * show up as an environment variable for init.
  1240. */
  1241. static __init int setup_clearcpuid(char *arg)
  1242. {
  1243. return 1;
  1244. }
  1245. __setup("clearcpuid=", setup_clearcpuid);
  1246. #ifdef CONFIG_X86_64
  1247. DEFINE_PER_CPU_FIRST(union irq_stack_union,
  1248. irq_stack_union) __aligned(PAGE_SIZE) __visible;
  1249. EXPORT_PER_CPU_SYMBOL_GPL(irq_stack_union);
  1250. /*
  1251. * The following percpu variables are hot. Align current_task to
  1252. * cacheline size such that they fall in the same cacheline.
  1253. */
  1254. DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
  1255. &init_task;
  1256. EXPORT_PER_CPU_SYMBOL(current_task);
  1257. DEFINE_PER_CPU(char *, irq_stack_ptr) =
  1258. init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
  1259. DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
  1260. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  1261. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  1262. /* May not be marked __init: used by software suspend */
  1263. void syscall_init(void)
  1264. {
  1265. extern char _entry_trampoline[];
  1266. extern char entry_SYSCALL_64_trampoline[];
  1267. int cpu = smp_processor_id();
  1268. unsigned long SYSCALL64_entry_trampoline =
  1269. (unsigned long)get_cpu_entry_area(cpu)->entry_trampoline +
  1270. (entry_SYSCALL_64_trampoline - _entry_trampoline);
  1271. wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
  1272. if (static_cpu_has(X86_FEATURE_PTI))
  1273. wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline);
  1274. else
  1275. wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
  1276. #ifdef CONFIG_IA32_EMULATION
  1277. wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
  1278. /*
  1279. * This only works on Intel CPUs.
  1280. * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
  1281. * This does not cause SYSENTER to jump to the wrong location, because
  1282. * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
  1283. */
  1284. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  1285. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1));
  1286. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
  1287. #else
  1288. wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
  1289. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
  1290. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  1291. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
  1292. #endif
  1293. /* Flags to clear on syscall */
  1294. wrmsrl(MSR_SYSCALL_MASK,
  1295. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
  1296. X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
  1297. }
  1298. /*
  1299. * Copies of the original ist values from the tss are only accessed during
  1300. * debugging, no special alignment required.
  1301. */
  1302. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  1303. static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
  1304. DEFINE_PER_CPU(int, debug_stack_usage);
  1305. int is_debug_stack(unsigned long addr)
  1306. {
  1307. return __this_cpu_read(debug_stack_usage) ||
  1308. (addr <= __this_cpu_read(debug_stack_addr) &&
  1309. addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
  1310. }
  1311. NOKPROBE_SYMBOL(is_debug_stack);
  1312. DEFINE_PER_CPU(u32, debug_idt_ctr);
  1313. void debug_stack_set_zero(void)
  1314. {
  1315. this_cpu_inc(debug_idt_ctr);
  1316. load_current_idt();
  1317. }
  1318. NOKPROBE_SYMBOL(debug_stack_set_zero);
  1319. void debug_stack_reset(void)
  1320. {
  1321. if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
  1322. return;
  1323. if (this_cpu_dec_return(debug_idt_ctr) == 0)
  1324. load_current_idt();
  1325. }
  1326. NOKPROBE_SYMBOL(debug_stack_reset);
  1327. #else /* CONFIG_X86_64 */
  1328. DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
  1329. EXPORT_PER_CPU_SYMBOL(current_task);
  1330. DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
  1331. EXPORT_PER_CPU_SYMBOL(__preempt_count);
  1332. /*
  1333. * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
  1334. * the top of the kernel stack. Use an extra percpu variable to track the
  1335. * top of the kernel stack directly.
  1336. */
  1337. DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
  1338. (unsigned long)&init_thread_union + THREAD_SIZE;
  1339. EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
  1340. #ifdef CONFIG_STACKPROTECTOR
  1341. DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
  1342. #endif
  1343. #endif /* CONFIG_X86_64 */
  1344. /*
  1345. * Clear all 6 debug registers:
  1346. */
  1347. static void clear_all_debug_regs(void)
  1348. {
  1349. int i;
  1350. for (i = 0; i < 8; i++) {
  1351. /* Ignore db4, db5 */
  1352. if ((i == 4) || (i == 5))
  1353. continue;
  1354. set_debugreg(0, i);
  1355. }
  1356. }
  1357. #ifdef CONFIG_KGDB
  1358. /*
  1359. * Restore debug regs if using kgdbwait and you have a kernel debugger
  1360. * connection established.
  1361. */
  1362. static void dbg_restore_debug_regs(void)
  1363. {
  1364. if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
  1365. arch_kgdb_ops.correct_hw_break();
  1366. }
  1367. #else /* ! CONFIG_KGDB */
  1368. #define dbg_restore_debug_regs()
  1369. #endif /* ! CONFIG_KGDB */
  1370. static void wait_for_master_cpu(int cpu)
  1371. {
  1372. #ifdef CONFIG_SMP
  1373. /*
  1374. * wait for ACK from master CPU before continuing
  1375. * with AP initialization
  1376. */
  1377. WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
  1378. while (!cpumask_test_cpu(cpu, cpu_callout_mask))
  1379. cpu_relax();
  1380. #endif
  1381. }
  1382. /*
  1383. * cpu_init() initializes state that is per-CPU. Some data is already
  1384. * initialized (naturally) in the bootstrap process, such as the GDT
  1385. * and IDT. We reload them nevertheless, this function acts as a
  1386. * 'CPU state barrier', nothing should get across.
  1387. * A lot of state is already set up in PDA init for 64 bit
  1388. */
  1389. #ifdef CONFIG_X86_64
  1390. void cpu_init(void)
  1391. {
  1392. struct orig_ist *oist;
  1393. struct task_struct *me;
  1394. struct tss_struct *t;
  1395. unsigned long v;
  1396. int cpu = raw_smp_processor_id();
  1397. int i;
  1398. wait_for_master_cpu(cpu);
  1399. /*
  1400. * Initialize the CR4 shadow before doing anything that could
  1401. * try to read it.
  1402. */
  1403. cr4_init_shadow();
  1404. if (cpu)
  1405. load_ucode_ap();
  1406. t = &per_cpu(cpu_tss_rw, cpu);
  1407. oist = &per_cpu(orig_ist, cpu);
  1408. #ifdef CONFIG_NUMA
  1409. if (this_cpu_read(numa_node) == 0 &&
  1410. early_cpu_to_node(cpu) != NUMA_NO_NODE)
  1411. set_numa_node(early_cpu_to_node(cpu));
  1412. #endif
  1413. me = current;
  1414. pr_debug("Initializing CPU#%d\n", cpu);
  1415. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1416. /*
  1417. * Initialize the per-CPU GDT with the boot GDT,
  1418. * and set up the GDT descriptor:
  1419. */
  1420. switch_to_new_gdt(cpu);
  1421. loadsegment(fs, 0);
  1422. load_current_idt();
  1423. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  1424. syscall_init();
  1425. wrmsrl(MSR_FS_BASE, 0);
  1426. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  1427. barrier();
  1428. x86_configure_nx();
  1429. x2apic_setup();
  1430. /*
  1431. * set up and load the per-CPU TSS
  1432. */
  1433. if (!oist->ist[0]) {
  1434. char *estacks = get_cpu_entry_area(cpu)->exception_stacks;
  1435. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  1436. estacks += exception_stack_sizes[v];
  1437. oist->ist[v] = t->x86_tss.ist[v] =
  1438. (unsigned long)estacks;
  1439. if (v == DEBUG_STACK-1)
  1440. per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
  1441. }
  1442. }
  1443. t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
  1444. /*
  1445. * <= is required because the CPU will access up to
  1446. * 8 bits beyond the end of the IO permission bitmap.
  1447. */
  1448. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  1449. t->io_bitmap[i] = ~0UL;
  1450. mmgrab(&init_mm);
  1451. me->active_mm = &init_mm;
  1452. BUG_ON(me->mm);
  1453. initialize_tlbstate_and_flush();
  1454. enter_lazy_tlb(&init_mm, me);
  1455. /*
  1456. * Initialize the TSS. sp0 points to the entry trampoline stack
  1457. * regardless of what task is running.
  1458. */
  1459. set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
  1460. load_TR_desc();
  1461. load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
  1462. load_mm_ldt(&init_mm);
  1463. clear_all_debug_regs();
  1464. dbg_restore_debug_regs();
  1465. fpu__init_cpu();
  1466. if (is_uv_system())
  1467. uv_cpu_init();
  1468. load_fixmap_gdt(cpu);
  1469. }
  1470. #else
  1471. void cpu_init(void)
  1472. {
  1473. int cpu = smp_processor_id();
  1474. struct task_struct *curr = current;
  1475. struct tss_struct *t = &per_cpu(cpu_tss_rw, cpu);
  1476. wait_for_master_cpu(cpu);
  1477. /*
  1478. * Initialize the CR4 shadow before doing anything that could
  1479. * try to read it.
  1480. */
  1481. cr4_init_shadow();
  1482. show_ucode_info_early();
  1483. pr_info("Initializing CPU#%d\n", cpu);
  1484. if (cpu_feature_enabled(X86_FEATURE_VME) ||
  1485. boot_cpu_has(X86_FEATURE_TSC) ||
  1486. boot_cpu_has(X86_FEATURE_DE))
  1487. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1488. load_current_idt();
  1489. switch_to_new_gdt(cpu);
  1490. /*
  1491. * Set up and load the per-CPU TSS and LDT
  1492. */
  1493. mmgrab(&init_mm);
  1494. curr->active_mm = &init_mm;
  1495. BUG_ON(curr->mm);
  1496. initialize_tlbstate_and_flush();
  1497. enter_lazy_tlb(&init_mm, curr);
  1498. /*
  1499. * Initialize the TSS. Don't bother initializing sp0, as the initial
  1500. * task never enters user mode.
  1501. */
  1502. set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
  1503. load_TR_desc();
  1504. load_mm_ldt(&init_mm);
  1505. t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
  1506. #ifdef CONFIG_DOUBLEFAULT
  1507. /* Set up doublefault TSS pointer in the GDT */
  1508. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  1509. #endif
  1510. clear_all_debug_regs();
  1511. dbg_restore_debug_regs();
  1512. fpu__init_cpu();
  1513. load_fixmap_gdt(cpu);
  1514. }
  1515. #endif
  1516. static void bsp_resume(void)
  1517. {
  1518. if (this_cpu->c_bsp_resume)
  1519. this_cpu->c_bsp_resume(&boot_cpu_data);
  1520. }
  1521. static struct syscore_ops cpu_syscore_ops = {
  1522. .resume = bsp_resume,
  1523. };
  1524. static int __init init_cpu_syscore(void)
  1525. {
  1526. register_syscore_ops(&cpu_syscore_ops);
  1527. return 0;
  1528. }
  1529. core_initcall(init_cpu_syscore);
  1530. /*
  1531. * The microcode loader calls this upon late microcode load to recheck features,
  1532. * only when microcode has been updated. Caller holds microcode_mutex and CPU
  1533. * hotplug lock.
  1534. */
  1535. void microcode_check(void)
  1536. {
  1537. struct cpuinfo_x86 info;
  1538. perf_check_microcode();
  1539. /* Reload CPUID max function as it might've changed. */
  1540. info.cpuid_level = cpuid_eax(0);
  1541. /*
  1542. * Copy all capability leafs to pick up the synthetic ones so that
  1543. * memcmp() below doesn't fail on that. The ones coming from CPUID will
  1544. * get overwritten in get_cpu_cap().
  1545. */
  1546. memcpy(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability));
  1547. get_cpu_cap(&info);
  1548. if (!memcmp(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability)))
  1549. return;
  1550. pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
  1551. pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
  1552. }