common.c 51 KB

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