common.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* cpu_feature_enabled() cannot be used this early */
  3. #define USE_EARLY_PGTABLE_L5
  4. #include <linux/memblock.h>
  5. #include <linux/linkage.h>
  6. #include <linux/bitops.h>
  7. #include <linux/kernel.h>
  8. #include <linux/export.h>
  9. #include <linux/percpu.h>
  10. #include <linux/string.h>
  11. #include <linux/ctype.h>
  12. #include <linux/delay.h>
  13. #include <linux/sched/mm.h>
  14. #include <linux/sched/clock.h>
  15. #include <linux/sched/task.h>
  16. #include <linux/sched/smt.h>
  17. #include <linux/init.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/kgdb.h>
  20. #include <linux/mem_encrypt.h>
  21. #include <linux/smp.h>
  22. #include <linux/cpu.h>
  23. #include <linux/io.h>
  24. #include <linux/syscore_ops.h>
  25. #include <linux/pgtable.h>
  26. #include <linux/stackprotector.h>
  27. #include <linux/utsname.h>
  28. #include <asm/alternative.h>
  29. #include <asm/cmdline.h>
  30. #include <asm/perf_event.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/doublefault.h>
  33. #include <asm/archrandom.h>
  34. #include <asm/hypervisor.h>
  35. #include <asm/processor.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/debugreg.h>
  38. #include <asm/sections.h>
  39. #include <asm/vsyscall.h>
  40. #include <linux/topology.h>
  41. #include <linux/cpumask.h>
  42. #include <linux/atomic.h>
  43. #include <asm/proto.h>
  44. #include <asm/setup.h>
  45. #include <asm/apic.h>
  46. #include <asm/desc.h>
  47. #include <asm/fpu/api.h>
  48. #include <asm/mtrr.h>
  49. #include <asm/hwcap2.h>
  50. #include <linux/numa.h>
  51. #include <asm/numa.h>
  52. #include <asm/asm.h>
  53. #include <asm/bugs.h>
  54. #include <asm/cpu.h>
  55. #include <asm/mce.h>
  56. #include <asm/msr.h>
  57. #include <asm/cacheinfo.h>
  58. #include <asm/memtype.h>
  59. #include <asm/microcode.h>
  60. #include <asm/intel-family.h>
  61. #include <asm/cpu_device_id.h>
  62. #include <asm/fred.h>
  63. #include <asm/uv/uv.h>
  64. #include <asm/ia32.h>
  65. #include <asm/set_memory.h>
  66. #include <asm/traps.h>
  67. #include <asm/sev.h>
  68. #include <asm/tdx.h>
  69. #include <asm/posted_intr.h>
  70. #include <asm/runtime-const.h>
  71. #include "cpu.h"
  72. DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
  73. EXPORT_PER_CPU_SYMBOL(cpu_info);
  74. u32 elf_hwcap2 __read_mostly;
  75. /* Number of siblings per CPU package */
  76. unsigned int __max_threads_per_core __ro_after_init = 1;
  77. EXPORT_SYMBOL(__max_threads_per_core);
  78. unsigned int __max_dies_per_package __ro_after_init = 1;
  79. EXPORT_SYMBOL(__max_dies_per_package);
  80. unsigned int __max_logical_packages __ro_after_init = 1;
  81. EXPORT_SYMBOL(__max_logical_packages);
  82. unsigned int __num_cores_per_package __ro_after_init = 1;
  83. EXPORT_SYMBOL(__num_cores_per_package);
  84. unsigned int __num_threads_per_package __ro_after_init = 1;
  85. EXPORT_SYMBOL(__num_threads_per_package);
  86. static struct ppin_info {
  87. int feature;
  88. int msr_ppin_ctl;
  89. int msr_ppin;
  90. } ppin_info[] = {
  91. [X86_VENDOR_INTEL] = {
  92. .feature = X86_FEATURE_INTEL_PPIN,
  93. .msr_ppin_ctl = MSR_PPIN_CTL,
  94. .msr_ppin = MSR_PPIN
  95. },
  96. [X86_VENDOR_AMD] = {
  97. .feature = X86_FEATURE_AMD_PPIN,
  98. .msr_ppin_ctl = MSR_AMD_PPIN_CTL,
  99. .msr_ppin = MSR_AMD_PPIN
  100. },
  101. };
  102. static const struct x86_cpu_id ppin_cpuids[] = {
  103. X86_MATCH_FEATURE(X86_FEATURE_AMD_PPIN, &ppin_info[X86_VENDOR_AMD]),
  104. X86_MATCH_FEATURE(X86_FEATURE_INTEL_PPIN, &ppin_info[X86_VENDOR_INTEL]),
  105. /* Legacy models without CPUID enumeration */
  106. X86_MATCH_VFM(INTEL_IVYBRIDGE_X, &ppin_info[X86_VENDOR_INTEL]),
  107. X86_MATCH_VFM(INTEL_HASWELL_X, &ppin_info[X86_VENDOR_INTEL]),
  108. X86_MATCH_VFM(INTEL_BROADWELL_D, &ppin_info[X86_VENDOR_INTEL]),
  109. X86_MATCH_VFM(INTEL_BROADWELL_X, &ppin_info[X86_VENDOR_INTEL]),
  110. X86_MATCH_VFM(INTEL_SKYLAKE_X, &ppin_info[X86_VENDOR_INTEL]),
  111. X86_MATCH_VFM(INTEL_ICELAKE_X, &ppin_info[X86_VENDOR_INTEL]),
  112. X86_MATCH_VFM(INTEL_ICELAKE_D, &ppin_info[X86_VENDOR_INTEL]),
  113. X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, &ppin_info[X86_VENDOR_INTEL]),
  114. X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, &ppin_info[X86_VENDOR_INTEL]),
  115. X86_MATCH_VFM(INTEL_XEON_PHI_KNL, &ppin_info[X86_VENDOR_INTEL]),
  116. X86_MATCH_VFM(INTEL_XEON_PHI_KNM, &ppin_info[X86_VENDOR_INTEL]),
  117. {}
  118. };
  119. static void ppin_init(struct cpuinfo_x86 *c)
  120. {
  121. const struct x86_cpu_id *id;
  122. unsigned long long val;
  123. struct ppin_info *info;
  124. id = x86_match_cpu(ppin_cpuids);
  125. if (!id)
  126. return;
  127. /*
  128. * Testing the presence of the MSR is not enough. Need to check
  129. * that the PPIN_CTL allows reading of the PPIN.
  130. */
  131. info = (struct ppin_info *)id->driver_data;
  132. if (rdmsrl_safe(info->msr_ppin_ctl, &val))
  133. goto clear_ppin;
  134. if ((val & 3UL) == 1UL) {
  135. /* PPIN locked in disabled mode */
  136. goto clear_ppin;
  137. }
  138. /* If PPIN is disabled, try to enable */
  139. if (!(val & 2UL)) {
  140. wrmsrl_safe(info->msr_ppin_ctl, val | 2UL);
  141. rdmsrl_safe(info->msr_ppin_ctl, &val);
  142. }
  143. /* Is the enable bit set? */
  144. if (val & 2UL) {
  145. c->ppin = __rdmsr(info->msr_ppin);
  146. set_cpu_cap(c, info->feature);
  147. return;
  148. }
  149. clear_ppin:
  150. clear_cpu_cap(c, info->feature);
  151. }
  152. static void default_init(struct cpuinfo_x86 *c)
  153. {
  154. #ifdef CONFIG_X86_64
  155. cpu_detect_cache_sizes(c);
  156. #else
  157. /* Not much we can do here... */
  158. /* Check if at least it has cpuid */
  159. if (c->cpuid_level == -1) {
  160. /* No cpuid. It must be an ancient CPU */
  161. if (c->x86 == 4)
  162. strcpy(c->x86_model_id, "486");
  163. else if (c->x86 == 3)
  164. strcpy(c->x86_model_id, "386");
  165. }
  166. #endif
  167. }
  168. static const struct cpu_dev default_cpu = {
  169. .c_init = default_init,
  170. .c_vendor = "Unknown",
  171. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  172. };
  173. static const struct cpu_dev *this_cpu = &default_cpu;
  174. DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
  175. #ifdef CONFIG_X86_64
  176. /*
  177. * We need valid kernel segments for data and code in long mode too
  178. * IRET will check the segment types kkeil 2000/10/28
  179. * Also sysret mandates a special GDT layout
  180. *
  181. * TLS descriptors are currently at a different place compared to i386.
  182. * Hopefully nobody expects them at a fixed place (Wine?)
  183. */
  184. [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
  185. [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(DESC_CODE64, 0, 0xfffff),
  186. [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(DESC_DATA64, 0, 0xfffff),
  187. [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff),
  188. [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(DESC_DATA64 | DESC_USER, 0, 0xfffff),
  189. [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(DESC_CODE64 | DESC_USER, 0, 0xfffff),
  190. #else
  191. [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
  192. [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
  193. [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff),
  194. [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(DESC_DATA32 | DESC_USER, 0, 0xfffff),
  195. /*
  196. * Segments used for calling PnP BIOS have byte granularity.
  197. * They code segments and data segments have fixed 64k limits,
  198. * the transfer segment sizes are set at run time.
  199. */
  200. [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff),
  201. [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff),
  202. [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(DESC_DATA16, 0, 0xffff),
  203. [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(DESC_DATA16, 0, 0),
  204. [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(DESC_DATA16, 0, 0),
  205. /*
  206. * The APM segments have byte granularity and their bases
  207. * are set at run time. All have 64k limits.
  208. */
  209. [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff),
  210. [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff),
  211. [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(DESC_DATA32_BIOS, 0, 0xffff),
  212. [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
  213. [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
  214. #endif
  215. } };
  216. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  217. #ifdef CONFIG_X86_64
  218. static int __init x86_nopcid_setup(char *s)
  219. {
  220. /* nopcid doesn't accept parameters */
  221. if (s)
  222. return -EINVAL;
  223. /* do not emit a message if the feature is not present */
  224. if (!boot_cpu_has(X86_FEATURE_PCID))
  225. return 0;
  226. setup_clear_cpu_cap(X86_FEATURE_PCID);
  227. pr_info("nopcid: PCID feature disabled\n");
  228. return 0;
  229. }
  230. early_param("nopcid", x86_nopcid_setup);
  231. #endif
  232. static int __init x86_noinvpcid_setup(char *s)
  233. {
  234. /* noinvpcid doesn't accept parameters */
  235. if (s)
  236. return -EINVAL;
  237. /* do not emit a message if the feature is not present */
  238. if (!boot_cpu_has(X86_FEATURE_INVPCID))
  239. return 0;
  240. setup_clear_cpu_cap(X86_FEATURE_INVPCID);
  241. pr_info("noinvpcid: INVPCID feature disabled\n");
  242. return 0;
  243. }
  244. early_param("noinvpcid", x86_noinvpcid_setup);
  245. #ifdef CONFIG_X86_32
  246. static int cachesize_override = -1;
  247. static int disable_x86_serial_nr = 1;
  248. static int __init cachesize_setup(char *str)
  249. {
  250. get_option(&str, &cachesize_override);
  251. return 1;
  252. }
  253. __setup("cachesize=", cachesize_setup);
  254. /* Standard macro to see if a specific flag is changeable */
  255. static inline int flag_is_changeable_p(u32 flag)
  256. {
  257. u32 f1, f2;
  258. /*
  259. * Cyrix and IDT cpus allow disabling of CPUID
  260. * so the code below may return different results
  261. * when it is executed before and after enabling
  262. * the CPUID. Add "volatile" to not allow gcc to
  263. * optimize the subsequent calls to this function.
  264. */
  265. asm volatile ("pushfl \n\t"
  266. "pushfl \n\t"
  267. "popl %0 \n\t"
  268. "movl %0, %1 \n\t"
  269. "xorl %2, %0 \n\t"
  270. "pushl %0 \n\t"
  271. "popfl \n\t"
  272. "pushfl \n\t"
  273. "popl %0 \n\t"
  274. "popfl \n\t"
  275. : "=&r" (f1), "=&r" (f2)
  276. : "ir" (flag));
  277. return ((f1^f2) & flag) != 0;
  278. }
  279. /* Probe for the CPUID instruction */
  280. int have_cpuid_p(void)
  281. {
  282. return flag_is_changeable_p(X86_EFLAGS_ID);
  283. }
  284. static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  285. {
  286. unsigned long lo, hi;
  287. if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
  288. return;
  289. /* Disable processor serial number: */
  290. rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  291. lo |= 0x200000;
  292. wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  293. pr_notice("CPU serial number disabled.\n");
  294. clear_cpu_cap(c, X86_FEATURE_PN);
  295. /* Disabling the serial number may affect the cpuid level */
  296. c->cpuid_level = cpuid_eax(0);
  297. }
  298. static int __init x86_serial_nr_setup(char *s)
  299. {
  300. disable_x86_serial_nr = 0;
  301. return 1;
  302. }
  303. __setup("serialnumber", x86_serial_nr_setup);
  304. #else
  305. static inline int flag_is_changeable_p(u32 flag)
  306. {
  307. return 1;
  308. }
  309. static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  310. {
  311. }
  312. #endif
  313. static __always_inline void setup_smep(struct cpuinfo_x86 *c)
  314. {
  315. if (cpu_has(c, X86_FEATURE_SMEP))
  316. cr4_set_bits(X86_CR4_SMEP);
  317. }
  318. static __always_inline void setup_smap(struct cpuinfo_x86 *c)
  319. {
  320. unsigned long eflags = native_save_fl();
  321. /* This should have been cleared long ago */
  322. BUG_ON(eflags & X86_EFLAGS_AC);
  323. if (cpu_has(c, X86_FEATURE_SMAP))
  324. cr4_set_bits(X86_CR4_SMAP);
  325. }
  326. static __always_inline void setup_umip(struct cpuinfo_x86 *c)
  327. {
  328. /* Check the boot processor, plus build option for UMIP. */
  329. if (!cpu_feature_enabled(X86_FEATURE_UMIP))
  330. goto out;
  331. /* Check the current processor's cpuid bits. */
  332. if (!cpu_has(c, X86_FEATURE_UMIP))
  333. goto out;
  334. cr4_set_bits(X86_CR4_UMIP);
  335. pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n");
  336. return;
  337. out:
  338. /*
  339. * Make sure UMIP is disabled in case it was enabled in a
  340. * previous boot (e.g., via kexec).
  341. */
  342. cr4_clear_bits(X86_CR4_UMIP);
  343. }
  344. /* These bits should not change their value after CPU init is finished. */
  345. static const unsigned long cr4_pinned_mask = X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP |
  346. X86_CR4_FSGSBASE | X86_CR4_CET | X86_CR4_FRED;
  347. static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
  348. static unsigned long cr4_pinned_bits __ro_after_init;
  349. void native_write_cr0(unsigned long val)
  350. {
  351. unsigned long bits_missing = 0;
  352. set_register:
  353. asm volatile("mov %0,%%cr0": "+r" (val) : : "memory");
  354. if (static_branch_likely(&cr_pinning)) {
  355. if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
  356. bits_missing = X86_CR0_WP;
  357. val |= bits_missing;
  358. goto set_register;
  359. }
  360. /* Warn after we've set the missing bits. */
  361. WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
  362. }
  363. }
  364. EXPORT_SYMBOL(native_write_cr0);
  365. void __no_profile native_write_cr4(unsigned long val)
  366. {
  367. unsigned long bits_changed = 0;
  368. set_register:
  369. asm volatile("mov %0,%%cr4": "+r" (val) : : "memory");
  370. if (static_branch_likely(&cr_pinning)) {
  371. if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) {
  372. bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits;
  373. val = (val & ~cr4_pinned_mask) | cr4_pinned_bits;
  374. goto set_register;
  375. }
  376. /* Warn after we've corrected the changed bits. */
  377. WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n",
  378. bits_changed);
  379. }
  380. }
  381. #if IS_MODULE(CONFIG_LKDTM)
  382. EXPORT_SYMBOL_GPL(native_write_cr4);
  383. #endif
  384. void cr4_update_irqsoff(unsigned long set, unsigned long clear)
  385. {
  386. unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
  387. lockdep_assert_irqs_disabled();
  388. newval = (cr4 & ~clear) | set;
  389. if (newval != cr4) {
  390. this_cpu_write(cpu_tlbstate.cr4, newval);
  391. __write_cr4(newval);
  392. }
  393. }
  394. EXPORT_SYMBOL(cr4_update_irqsoff);
  395. /* Read the CR4 shadow. */
  396. unsigned long cr4_read_shadow(void)
  397. {
  398. return this_cpu_read(cpu_tlbstate.cr4);
  399. }
  400. EXPORT_SYMBOL_GPL(cr4_read_shadow);
  401. void cr4_init(void)
  402. {
  403. unsigned long cr4 = __read_cr4();
  404. if (boot_cpu_has(X86_FEATURE_PCID))
  405. cr4 |= X86_CR4_PCIDE;
  406. if (static_branch_likely(&cr_pinning))
  407. cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits;
  408. __write_cr4(cr4);
  409. /* Initialize cr4 shadow for this CPU. */
  410. this_cpu_write(cpu_tlbstate.cr4, cr4);
  411. }
  412. /*
  413. * Once CPU feature detection is finished (and boot params have been
  414. * parsed), record any of the sensitive CR bits that are set, and
  415. * enable CR pinning.
  416. */
  417. static void __init setup_cr_pinning(void)
  418. {
  419. cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask;
  420. static_key_enable(&cr_pinning.key);
  421. }
  422. static __init int x86_nofsgsbase_setup(char *arg)
  423. {
  424. /* Require an exact match without trailing characters. */
  425. if (strlen(arg))
  426. return 0;
  427. /* Do not emit a message if the feature is not present. */
  428. if (!boot_cpu_has(X86_FEATURE_FSGSBASE))
  429. return 1;
  430. setup_clear_cpu_cap(X86_FEATURE_FSGSBASE);
  431. pr_info("FSGSBASE disabled via kernel command line\n");
  432. return 1;
  433. }
  434. __setup("nofsgsbase", x86_nofsgsbase_setup);
  435. /*
  436. * Protection Keys are not available in 32-bit mode.
  437. */
  438. static bool pku_disabled;
  439. static __always_inline void setup_pku(struct cpuinfo_x86 *c)
  440. {
  441. if (c == &boot_cpu_data) {
  442. if (pku_disabled || !cpu_feature_enabled(X86_FEATURE_PKU))
  443. return;
  444. /*
  445. * Setting CR4.PKE will cause the X86_FEATURE_OSPKE cpuid
  446. * bit to be set. Enforce it.
  447. */
  448. setup_force_cpu_cap(X86_FEATURE_OSPKE);
  449. } else if (!cpu_feature_enabled(X86_FEATURE_OSPKE)) {
  450. return;
  451. }
  452. cr4_set_bits(X86_CR4_PKE);
  453. /* Load the default PKRU value */
  454. pkru_write_default();
  455. }
  456. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  457. static __init int setup_disable_pku(char *arg)
  458. {
  459. /*
  460. * Do not clear the X86_FEATURE_PKU bit. All of the
  461. * runtime checks are against OSPKE so clearing the
  462. * bit does nothing.
  463. *
  464. * This way, we will see "pku" in cpuinfo, but not
  465. * "ospke", which is exactly what we want. It shows
  466. * that the CPU has PKU, but the OS has not enabled it.
  467. * This happens to be exactly how a system would look
  468. * if we disabled the config option.
  469. */
  470. pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
  471. pku_disabled = true;
  472. return 1;
  473. }
  474. __setup("nopku", setup_disable_pku);
  475. #endif
  476. #ifdef CONFIG_X86_KERNEL_IBT
  477. __noendbr u64 ibt_save(bool disable)
  478. {
  479. u64 msr = 0;
  480. if (cpu_feature_enabled(X86_FEATURE_IBT)) {
  481. rdmsrl(MSR_IA32_S_CET, msr);
  482. if (disable)
  483. wrmsrl(MSR_IA32_S_CET, msr & ~CET_ENDBR_EN);
  484. }
  485. return msr;
  486. }
  487. __noendbr void ibt_restore(u64 save)
  488. {
  489. u64 msr;
  490. if (cpu_feature_enabled(X86_FEATURE_IBT)) {
  491. rdmsrl(MSR_IA32_S_CET, msr);
  492. msr &= ~CET_ENDBR_EN;
  493. msr |= (save & CET_ENDBR_EN);
  494. wrmsrl(MSR_IA32_S_CET, msr);
  495. }
  496. }
  497. #endif
  498. static __always_inline void setup_cet(struct cpuinfo_x86 *c)
  499. {
  500. bool user_shstk, kernel_ibt;
  501. if (!IS_ENABLED(CONFIG_X86_CET))
  502. return;
  503. kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT);
  504. user_shstk = cpu_feature_enabled(X86_FEATURE_SHSTK) &&
  505. IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK);
  506. if (!kernel_ibt && !user_shstk)
  507. return;
  508. if (user_shstk)
  509. set_cpu_cap(c, X86_FEATURE_USER_SHSTK);
  510. if (kernel_ibt)
  511. wrmsrl(MSR_IA32_S_CET, CET_ENDBR_EN);
  512. else
  513. wrmsrl(MSR_IA32_S_CET, 0);
  514. cr4_set_bits(X86_CR4_CET);
  515. if (kernel_ibt && ibt_selftest()) {
  516. pr_err("IBT selftest: Failed!\n");
  517. wrmsrl(MSR_IA32_S_CET, 0);
  518. setup_clear_cpu_cap(X86_FEATURE_IBT);
  519. }
  520. }
  521. __noendbr void cet_disable(void)
  522. {
  523. if (!(cpu_feature_enabled(X86_FEATURE_IBT) ||
  524. cpu_feature_enabled(X86_FEATURE_SHSTK)))
  525. return;
  526. wrmsrl(MSR_IA32_S_CET, 0);
  527. wrmsrl(MSR_IA32_U_CET, 0);
  528. }
  529. /*
  530. * Some CPU features depend on higher CPUID levels, which may not always
  531. * be available due to CPUID level capping or broken virtualization
  532. * software. Add those features to this table to auto-disable them.
  533. */
  534. struct cpuid_dependent_feature {
  535. u32 feature;
  536. u32 level;
  537. };
  538. static const struct cpuid_dependent_feature
  539. cpuid_dependent_features[] = {
  540. { X86_FEATURE_MWAIT, 0x00000005 },
  541. { X86_FEATURE_DCA, 0x00000009 },
  542. { X86_FEATURE_XSAVE, 0x0000000d },
  543. { 0, 0 }
  544. };
  545. static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
  546. {
  547. const struct cpuid_dependent_feature *df;
  548. for (df = cpuid_dependent_features; df->feature; df++) {
  549. if (!cpu_has(c, df->feature))
  550. continue;
  551. /*
  552. * Note: cpuid_level is set to -1 if unavailable, but
  553. * extended_extended_level is set to 0 if unavailable
  554. * and the legitimate extended levels are all negative
  555. * when signed; hence the weird messing around with
  556. * signs here...
  557. */
  558. if (!((s32)df->level < 0 ?
  559. (u32)df->level > (u32)c->extended_cpuid_level :
  560. (s32)df->level > (s32)c->cpuid_level))
  561. continue;
  562. clear_cpu_cap(c, df->feature);
  563. if (!warn)
  564. continue;
  565. pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
  566. x86_cap_flag(df->feature), df->level);
  567. }
  568. }
  569. /*
  570. * Naming convention should be: <Name> [(<Codename>)]
  571. * This table only is used unless init_<vendor>() below doesn't set it;
  572. * in particular, if CPUID levels 0x80000002..4 are supported, this
  573. * isn't used
  574. */
  575. /* Look up CPU names by table lookup. */
  576. static const char *table_lookup_model(struct cpuinfo_x86 *c)
  577. {
  578. #ifdef CONFIG_X86_32
  579. const struct legacy_cpu_model_info *info;
  580. if (c->x86_model >= 16)
  581. return NULL; /* Range check */
  582. if (!this_cpu)
  583. return NULL;
  584. info = this_cpu->legacy_models;
  585. while (info->family) {
  586. if (info->family == c->x86)
  587. return info->model_names[c->x86_model];
  588. info++;
  589. }
  590. #endif
  591. return NULL; /* Not found */
  592. }
  593. /* Aligned to unsigned long to avoid split lock in atomic bitmap ops */
  594. __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
  595. __u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
  596. #ifdef CONFIG_X86_32
  597. /* The 32-bit entry code needs to find cpu_entry_area. */
  598. DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
  599. #endif
  600. /* Load the original GDT from the per-cpu structure */
  601. void load_direct_gdt(int cpu)
  602. {
  603. struct desc_ptr gdt_descr;
  604. gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
  605. gdt_descr.size = GDT_SIZE - 1;
  606. load_gdt(&gdt_descr);
  607. }
  608. EXPORT_SYMBOL_GPL(load_direct_gdt);
  609. /* Load a fixmap remapping of the per-cpu GDT */
  610. void load_fixmap_gdt(int cpu)
  611. {
  612. struct desc_ptr gdt_descr;
  613. gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
  614. gdt_descr.size = GDT_SIZE - 1;
  615. load_gdt(&gdt_descr);
  616. }
  617. EXPORT_SYMBOL_GPL(load_fixmap_gdt);
  618. /**
  619. * switch_gdt_and_percpu_base - Switch to direct GDT and runtime per CPU base
  620. * @cpu: The CPU number for which this is invoked
  621. *
  622. * Invoked during early boot to switch from early GDT and early per CPU to
  623. * the direct GDT and the runtime per CPU area. On 32-bit the percpu base
  624. * switch is implicit by loading the direct GDT. On 64bit this requires
  625. * to update GSBASE.
  626. */
  627. void __init switch_gdt_and_percpu_base(int cpu)
  628. {
  629. load_direct_gdt(cpu);
  630. #ifdef CONFIG_X86_64
  631. /*
  632. * No need to load %gs. It is already correct.
  633. *
  634. * Writing %gs on 64bit would zero GSBASE which would make any per
  635. * CPU operation up to the point of the wrmsrl() fault.
  636. *
  637. * Set GSBASE to the new offset. Until the wrmsrl() happens the
  638. * early mapping is still valid. That means the GSBASE update will
  639. * lose any prior per CPU data which was not copied over in
  640. * setup_per_cpu_areas().
  641. *
  642. * This works even with stackprotector enabled because the
  643. * per CPU stack canary is 0 in both per CPU areas.
  644. */
  645. wrmsrl(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
  646. #else
  647. /*
  648. * %fs is already set to __KERNEL_PERCPU, but after switching GDT
  649. * it is required to load FS again so that the 'hidden' part is
  650. * updated from the new GDT. Up to this point the early per CPU
  651. * translation is active. Any content of the early per CPU data
  652. * which was not copied over in setup_per_cpu_areas() is lost.
  653. */
  654. loadsegment(fs, __KERNEL_PERCPU);
  655. #endif
  656. }
  657. static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  658. static void get_model_name(struct cpuinfo_x86 *c)
  659. {
  660. unsigned int *v;
  661. char *p, *q, *s;
  662. if (c->extended_cpuid_level < 0x80000004)
  663. return;
  664. v = (unsigned int *)c->x86_model_id;
  665. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  666. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  667. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  668. c->x86_model_id[48] = 0;
  669. /* Trim whitespace */
  670. p = q = s = &c->x86_model_id[0];
  671. while (*p == ' ')
  672. p++;
  673. while (*p) {
  674. /* Note the last non-whitespace index */
  675. if (!isspace(*p))
  676. s = q;
  677. *q++ = *p++;
  678. }
  679. *(s + 1) = '\0';
  680. }
  681. void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
  682. {
  683. unsigned int n, dummy, ebx, ecx, edx, l2size;
  684. n = c->extended_cpuid_level;
  685. if (n >= 0x80000005) {
  686. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  687. c->x86_cache_size = (ecx>>24) + (edx>>24);
  688. #ifdef CONFIG_X86_64
  689. /* On K8 L1 TLB is inclusive, so don't count it */
  690. c->x86_tlbsize = 0;
  691. #endif
  692. }
  693. if (n < 0x80000006) /* Some chips just has a large L1. */
  694. return;
  695. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  696. l2size = ecx >> 16;
  697. #ifdef CONFIG_X86_64
  698. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  699. #else
  700. /* do processor-specific cache resizing */
  701. if (this_cpu->legacy_cache_size)
  702. l2size = this_cpu->legacy_cache_size(c, l2size);
  703. /* Allow user to override all this if necessary. */
  704. if (cachesize_override != -1)
  705. l2size = cachesize_override;
  706. if (l2size == 0)
  707. return; /* Again, no L2 cache is possible */
  708. #endif
  709. c->x86_cache_size = l2size;
  710. }
  711. u16 __read_mostly tlb_lli_4k[NR_INFO];
  712. u16 __read_mostly tlb_lli_2m[NR_INFO];
  713. u16 __read_mostly tlb_lli_4m[NR_INFO];
  714. u16 __read_mostly tlb_lld_4k[NR_INFO];
  715. u16 __read_mostly tlb_lld_2m[NR_INFO];
  716. u16 __read_mostly tlb_lld_4m[NR_INFO];
  717. u16 __read_mostly tlb_lld_1g[NR_INFO];
  718. static void cpu_detect_tlb(struct cpuinfo_x86 *c)
  719. {
  720. if (this_cpu->c_detect_tlb)
  721. this_cpu->c_detect_tlb(c);
  722. pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
  723. tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
  724. tlb_lli_4m[ENTRIES]);
  725. pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
  726. tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
  727. tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
  728. }
  729. void get_cpu_vendor(struct cpuinfo_x86 *c)
  730. {
  731. char *v = c->x86_vendor_id;
  732. int i;
  733. for (i = 0; i < X86_VENDOR_NUM; i++) {
  734. if (!cpu_devs[i])
  735. break;
  736. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  737. (cpu_devs[i]->c_ident[1] &&
  738. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  739. this_cpu = cpu_devs[i];
  740. c->x86_vendor = this_cpu->c_x86_vendor;
  741. return;
  742. }
  743. }
  744. pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
  745. "CPU: Your system may be unstable.\n", v);
  746. c->x86_vendor = X86_VENDOR_UNKNOWN;
  747. this_cpu = &default_cpu;
  748. }
  749. void cpu_detect(struct cpuinfo_x86 *c)
  750. {
  751. /* Get vendor name */
  752. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  753. (unsigned int *)&c->x86_vendor_id[0],
  754. (unsigned int *)&c->x86_vendor_id[8],
  755. (unsigned int *)&c->x86_vendor_id[4]);
  756. c->x86 = 4;
  757. /* Intel-defined flags: level 0x00000001 */
  758. if (c->cpuid_level >= 0x00000001) {
  759. u32 junk, tfms, cap0, misc;
  760. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  761. c->x86 = x86_family(tfms);
  762. c->x86_model = x86_model(tfms);
  763. c->x86_stepping = x86_stepping(tfms);
  764. if (cap0 & (1<<19)) {
  765. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  766. c->x86_cache_alignment = c->x86_clflush_size;
  767. }
  768. }
  769. }
  770. static void apply_forced_caps(struct cpuinfo_x86 *c)
  771. {
  772. int i;
  773. for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
  774. c->x86_capability[i] &= ~cpu_caps_cleared[i];
  775. c->x86_capability[i] |= cpu_caps_set[i];
  776. }
  777. }
  778. static void init_speculation_control(struct cpuinfo_x86 *c)
  779. {
  780. /*
  781. * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
  782. * and they also have a different bit for STIBP support. Also,
  783. * a hypervisor might have set the individual AMD bits even on
  784. * Intel CPUs, for finer-grained selection of what's available.
  785. */
  786. if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
  787. set_cpu_cap(c, X86_FEATURE_IBRS);
  788. set_cpu_cap(c, X86_FEATURE_IBPB);
  789. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  790. }
  791. if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
  792. set_cpu_cap(c, X86_FEATURE_STIBP);
  793. if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
  794. cpu_has(c, X86_FEATURE_VIRT_SSBD))
  795. set_cpu_cap(c, X86_FEATURE_SSBD);
  796. if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
  797. set_cpu_cap(c, X86_FEATURE_IBRS);
  798. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  799. }
  800. if (cpu_has(c, X86_FEATURE_AMD_IBPB))
  801. set_cpu_cap(c, X86_FEATURE_IBPB);
  802. if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
  803. set_cpu_cap(c, X86_FEATURE_STIBP);
  804. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  805. }
  806. if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
  807. set_cpu_cap(c, X86_FEATURE_SSBD);
  808. set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
  809. clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
  810. }
  811. }
  812. void get_cpu_cap(struct cpuinfo_x86 *c)
  813. {
  814. u32 eax, ebx, ecx, edx;
  815. /* Intel-defined flags: level 0x00000001 */
  816. if (c->cpuid_level >= 0x00000001) {
  817. cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
  818. c->x86_capability[CPUID_1_ECX] = ecx;
  819. c->x86_capability[CPUID_1_EDX] = edx;
  820. }
  821. /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
  822. if (c->cpuid_level >= 0x00000006)
  823. c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
  824. /* Additional Intel-defined flags: level 0x00000007 */
  825. if (c->cpuid_level >= 0x00000007) {
  826. cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
  827. c->x86_capability[CPUID_7_0_EBX] = ebx;
  828. c->x86_capability[CPUID_7_ECX] = ecx;
  829. c->x86_capability[CPUID_7_EDX] = edx;
  830. /* Check valid sub-leaf index before accessing it */
  831. if (eax >= 1) {
  832. cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx);
  833. c->x86_capability[CPUID_7_1_EAX] = eax;
  834. }
  835. }
  836. /* Extended state features: level 0x0000000d */
  837. if (c->cpuid_level >= 0x0000000d) {
  838. cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
  839. c->x86_capability[CPUID_D_1_EAX] = eax;
  840. }
  841. /*
  842. * Check if extended CPUID leaves are implemented: Max extended
  843. * CPUID leaf must be in the 0x80000001-0x8000ffff range.
  844. */
  845. eax = cpuid_eax(0x80000000);
  846. c->extended_cpuid_level = ((eax & 0xffff0000) == 0x80000000) ? eax : 0;
  847. if (c->extended_cpuid_level >= 0x80000001) {
  848. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  849. c->x86_capability[CPUID_8000_0001_ECX] = ecx;
  850. c->x86_capability[CPUID_8000_0001_EDX] = edx;
  851. }
  852. if (c->extended_cpuid_level >= 0x80000007) {
  853. cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
  854. c->x86_capability[CPUID_8000_0007_EBX] = ebx;
  855. c->x86_power = edx;
  856. }
  857. if (c->extended_cpuid_level >= 0x80000008) {
  858. cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
  859. c->x86_capability[CPUID_8000_0008_EBX] = ebx;
  860. }
  861. if (c->extended_cpuid_level >= 0x8000000a)
  862. c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
  863. if (c->extended_cpuid_level >= 0x8000001f)
  864. c->x86_capability[CPUID_8000_001F_EAX] = cpuid_eax(0x8000001f);
  865. if (c->extended_cpuid_level >= 0x80000021)
  866. c->x86_capability[CPUID_8000_0021_EAX] = cpuid_eax(0x80000021);
  867. init_scattered_cpuid_features(c);
  868. init_speculation_control(c);
  869. /*
  870. * Clear/Set all flags overridden by options, after probe.
  871. * This needs to happen each time we re-probe, which may happen
  872. * several times during CPU initialization.
  873. */
  874. apply_forced_caps(c);
  875. }
  876. void get_cpu_address_sizes(struct cpuinfo_x86 *c)
  877. {
  878. u32 eax, ebx, ecx, edx;
  879. if (!cpu_has(c, X86_FEATURE_CPUID) ||
  880. (c->extended_cpuid_level < 0x80000008)) {
  881. if (IS_ENABLED(CONFIG_X86_64)) {
  882. c->x86_clflush_size = 64;
  883. c->x86_phys_bits = 36;
  884. c->x86_virt_bits = 48;
  885. } else {
  886. c->x86_clflush_size = 32;
  887. c->x86_virt_bits = 32;
  888. c->x86_phys_bits = 32;
  889. if (cpu_has(c, X86_FEATURE_PAE) ||
  890. cpu_has(c, X86_FEATURE_PSE36))
  891. c->x86_phys_bits = 36;
  892. }
  893. } else {
  894. cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
  895. c->x86_virt_bits = (eax >> 8) & 0xff;
  896. c->x86_phys_bits = eax & 0xff;
  897. /* Provide a sane default if not enumerated: */
  898. if (!c->x86_clflush_size)
  899. c->x86_clflush_size = 32;
  900. }
  901. c->x86_cache_bits = c->x86_phys_bits;
  902. c->x86_cache_alignment = c->x86_clflush_size;
  903. }
  904. static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  905. {
  906. #ifdef CONFIG_X86_32
  907. int i;
  908. /*
  909. * First of all, decide if this is a 486 or higher
  910. * It's a 486 if we can modify the AC flag
  911. */
  912. if (flag_is_changeable_p(X86_EFLAGS_AC))
  913. c->x86 = 4;
  914. else
  915. c->x86 = 3;
  916. for (i = 0; i < X86_VENDOR_NUM; i++)
  917. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  918. c->x86_vendor_id[0] = 0;
  919. cpu_devs[i]->c_identify(c);
  920. if (c->x86_vendor_id[0]) {
  921. get_cpu_vendor(c);
  922. break;
  923. }
  924. }
  925. #endif
  926. }
  927. #define NO_SPECULATION BIT(0)
  928. #define NO_MELTDOWN BIT(1)
  929. #define NO_SSB BIT(2)
  930. #define NO_L1TF BIT(3)
  931. #define NO_MDS BIT(4)
  932. #define MSBDS_ONLY BIT(5)
  933. #define NO_SWAPGS BIT(6)
  934. #define NO_ITLB_MULTIHIT BIT(7)
  935. #define NO_SPECTRE_V2 BIT(8)
  936. #define NO_MMIO BIT(9)
  937. #define NO_EIBRS_PBRSB BIT(10)
  938. #define NO_BHI BIT(11)
  939. #define VULNWL(vendor, family, model, whitelist) \
  940. X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, whitelist)
  941. #define VULNWL_INTEL(vfm, whitelist) \
  942. X86_MATCH_VFM(vfm, whitelist)
  943. #define VULNWL_AMD(family, whitelist) \
  944. VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
  945. #define VULNWL_HYGON(family, whitelist) \
  946. VULNWL(HYGON, family, X86_MODEL_ANY, whitelist)
  947. static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
  948. VULNWL(ANY, 4, X86_MODEL_ANY, NO_SPECULATION),
  949. VULNWL(CENTAUR, 5, X86_MODEL_ANY, NO_SPECULATION),
  950. VULNWL(INTEL, 5, X86_MODEL_ANY, NO_SPECULATION),
  951. VULNWL(NSC, 5, X86_MODEL_ANY, NO_SPECULATION),
  952. VULNWL(VORTEX, 5, X86_MODEL_ANY, NO_SPECULATION),
  953. VULNWL(VORTEX, 6, X86_MODEL_ANY, NO_SPECULATION),
  954. /* Intel Family 6 */
  955. VULNWL_INTEL(INTEL_TIGERLAKE, NO_MMIO),
  956. VULNWL_INTEL(INTEL_TIGERLAKE_L, NO_MMIO),
  957. VULNWL_INTEL(INTEL_ALDERLAKE, NO_MMIO),
  958. VULNWL_INTEL(INTEL_ALDERLAKE_L, NO_MMIO),
  959. VULNWL_INTEL(INTEL_ATOM_SALTWELL, NO_SPECULATION | NO_ITLB_MULTIHIT),
  960. VULNWL_INTEL(INTEL_ATOM_SALTWELL_TABLET, NO_SPECULATION | NO_ITLB_MULTIHIT),
  961. VULNWL_INTEL(INTEL_ATOM_SALTWELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT),
  962. VULNWL_INTEL(INTEL_ATOM_BONNELL, NO_SPECULATION | NO_ITLB_MULTIHIT),
  963. VULNWL_INTEL(INTEL_ATOM_BONNELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT),
  964. VULNWL_INTEL(INTEL_ATOM_SILVERMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
  965. VULNWL_INTEL(INTEL_ATOM_SILVERMONT_D, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
  966. VULNWL_INTEL(INTEL_ATOM_SILVERMONT_MID, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
  967. VULNWL_INTEL(INTEL_ATOM_AIRMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
  968. VULNWL_INTEL(INTEL_XEON_PHI_KNL, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
  969. VULNWL_INTEL(INTEL_XEON_PHI_KNM, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
  970. VULNWL_INTEL(INTEL_CORE_YONAH, NO_SSB),
  971. VULNWL_INTEL(INTEL_ATOM_AIRMONT_MID, NO_SSB | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | MSBDS_ONLY),
  972. VULNWL_INTEL(INTEL_ATOM_AIRMONT_NP, NO_SSB | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
  973. VULNWL_INTEL(INTEL_ATOM_GOLDMONT, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
  974. VULNWL_INTEL(INTEL_ATOM_GOLDMONT_D, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
  975. VULNWL_INTEL(INTEL_ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
  976. /*
  977. * Technically, swapgs isn't serializing on AMD (despite it previously
  978. * being documented as such in the APM). But according to AMD, %gs is
  979. * updated non-speculatively, and the issuing of %gs-relative memory
  980. * operands will be blocked until the %gs update completes, which is
  981. * good enough for our purposes.
  982. */
  983. VULNWL_INTEL(INTEL_ATOM_TREMONT, NO_EIBRS_PBRSB),
  984. VULNWL_INTEL(INTEL_ATOM_TREMONT_L, NO_EIBRS_PBRSB),
  985. VULNWL_INTEL(INTEL_ATOM_TREMONT_D, NO_ITLB_MULTIHIT | NO_EIBRS_PBRSB),
  986. /* AMD Family 0xf - 0x12 */
  987. VULNWL_AMD(0x0f, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
  988. VULNWL_AMD(0x10, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
  989. VULNWL_AMD(0x11, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
  990. VULNWL_AMD(0x12, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
  991. /* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
  992. VULNWL_AMD(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB | NO_BHI),
  993. VULNWL_HYGON(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB | NO_BHI),
  994. /* Zhaoxin Family 7 */
  995. VULNWL(CENTAUR, 7, X86_MODEL_ANY, NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO | NO_BHI),
  996. VULNWL(ZHAOXIN, 7, X86_MODEL_ANY, NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO | NO_BHI),
  997. {}
  998. };
  999. #define VULNBL(vendor, family, model, blacklist) \
  1000. X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, blacklist)
  1001. #define VULNBL_INTEL_STEPPINGS(vfm, steppings, issues) \
  1002. X86_MATCH_VFM_STEPPINGS(vfm, steppings, issues)
  1003. #define VULNBL_AMD(family, blacklist) \
  1004. VULNBL(AMD, family, X86_MODEL_ANY, blacklist)
  1005. #define VULNBL_HYGON(family, blacklist) \
  1006. VULNBL(HYGON, family, X86_MODEL_ANY, blacklist)
  1007. #define SRBDS BIT(0)
  1008. /* CPU is affected by X86_BUG_MMIO_STALE_DATA */
  1009. #define MMIO BIT(1)
  1010. /* CPU is affected by Shared Buffers Data Sampling (SBDS), a variant of X86_BUG_MMIO_STALE_DATA */
  1011. #define MMIO_SBDS BIT(2)
  1012. /* CPU is affected by RETbleed, speculating where you would not expect it */
  1013. #define RETBLEED BIT(3)
  1014. /* CPU is affected by SMT (cross-thread) return predictions */
  1015. #define SMT_RSB BIT(4)
  1016. /* CPU is affected by SRSO */
  1017. #define SRSO BIT(5)
  1018. /* CPU is affected by GDS */
  1019. #define GDS BIT(6)
  1020. /* CPU is affected by Register File Data Sampling */
  1021. #define RFDS BIT(7)
  1022. /* CPU is affected by Indirect Target Selection */
  1023. #define ITS BIT(8)
  1024. /* CPU is affected by Indirect Target Selection, but guest-host isolation is not affected */
  1025. #define ITS_NATIVE_ONLY BIT(9)
  1026. /* CPU is affected by Transient Scheduler Attacks */
  1027. #define TSA BIT(10)
  1028. /* CPU is affected by VMSCAPE */
  1029. #define VMSCAPE BIT(11)
  1030. static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
  1031. VULNBL_INTEL_STEPPINGS(INTEL_SANDYBRIDGE_X, X86_STEPPING_ANY, VMSCAPE),
  1032. VULNBL_INTEL_STEPPINGS(INTEL_SANDYBRIDGE, X86_STEPPING_ANY, VMSCAPE),
  1033. VULNBL_INTEL_STEPPINGS(INTEL_IVYBRIDGE_X, X86_STEPPING_ANY, VMSCAPE),
  1034. VULNBL_INTEL_STEPPINGS(INTEL_IVYBRIDGE, X86_STEPPING_ANY, SRBDS | VMSCAPE),
  1035. VULNBL_INTEL_STEPPINGS(INTEL_HASWELL, X86_STEPPING_ANY, SRBDS | VMSCAPE),
  1036. VULNBL_INTEL_STEPPINGS(INTEL_HASWELL_L, X86_STEPPING_ANY, SRBDS | VMSCAPE),
  1037. VULNBL_INTEL_STEPPINGS(INTEL_HASWELL_G, X86_STEPPING_ANY, SRBDS | VMSCAPE),
  1038. VULNBL_INTEL_STEPPINGS(INTEL_HASWELL_X, X86_STEPPING_ANY, MMIO | VMSCAPE),
  1039. VULNBL_INTEL_STEPPINGS(INTEL_BROADWELL_D, X86_STEPPING_ANY, MMIO | VMSCAPE),
  1040. VULNBL_INTEL_STEPPINGS(INTEL_BROADWELL_X, X86_STEPPING_ANY, MMIO | VMSCAPE),
  1041. VULNBL_INTEL_STEPPINGS(INTEL_BROADWELL_G, X86_STEPPING_ANY, SRBDS | VMSCAPE),
  1042. VULNBL_INTEL_STEPPINGS(INTEL_BROADWELL, X86_STEPPING_ANY, SRBDS | VMSCAPE),
  1043. VULNBL_INTEL_STEPPINGS(INTEL_SKYLAKE_X, X86_STEPPINGS(0x0, 0x5), MMIO | RETBLEED | GDS | VMSCAPE),
  1044. VULNBL_INTEL_STEPPINGS(INTEL_SKYLAKE_X, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | ITS | VMSCAPE),
  1045. VULNBL_INTEL_STEPPINGS(INTEL_SKYLAKE_L, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
  1046. VULNBL_INTEL_STEPPINGS(INTEL_SKYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
  1047. VULNBL_INTEL_STEPPINGS(INTEL_KABYLAKE_L, X86_STEPPINGS(0x0, 0xb), MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
  1048. VULNBL_INTEL_STEPPINGS(INTEL_KABYLAKE_L, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS | ITS | VMSCAPE),
  1049. VULNBL_INTEL_STEPPINGS(INTEL_KABYLAKE, X86_STEPPINGS(0x0, 0xc), MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
  1050. VULNBL_INTEL_STEPPINGS(INTEL_KABYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS | ITS | VMSCAPE),
  1051. VULNBL_INTEL_STEPPINGS(INTEL_CANNONLAKE_L, X86_STEPPING_ANY, RETBLEED | VMSCAPE),
  1052. VULNBL_INTEL_STEPPINGS(INTEL_ICELAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
  1053. VULNBL_INTEL_STEPPINGS(INTEL_ICELAKE_D, X86_STEPPING_ANY, MMIO | GDS | ITS | ITS_NATIVE_ONLY),
  1054. VULNBL_INTEL_STEPPINGS(INTEL_ICELAKE_X, X86_STEPPING_ANY, MMIO | GDS | ITS | ITS_NATIVE_ONLY),
  1055. VULNBL_INTEL_STEPPINGS(INTEL_COMETLAKE, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | VMSCAPE),
  1056. VULNBL_INTEL_STEPPINGS(INTEL_COMETLAKE_L, X86_STEPPINGS(0x0, 0x0), MMIO | RETBLEED | ITS | VMSCAPE),
  1057. VULNBL_INTEL_STEPPINGS(INTEL_COMETLAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | VMSCAPE),
  1058. VULNBL_INTEL_STEPPINGS(INTEL_TIGERLAKE_L, X86_STEPPING_ANY, GDS | ITS | ITS_NATIVE_ONLY),
  1059. VULNBL_INTEL_STEPPINGS(INTEL_TIGERLAKE, X86_STEPPING_ANY, GDS | ITS | ITS_NATIVE_ONLY),
  1060. VULNBL_INTEL_STEPPINGS(INTEL_LAKEFIELD, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED),
  1061. VULNBL_INTEL_STEPPINGS(INTEL_ROCKETLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
  1062. VULNBL_INTEL_STEPPINGS(INTEL_ALDERLAKE, X86_STEPPING_ANY, RFDS | VMSCAPE),
  1063. VULNBL_INTEL_STEPPINGS(INTEL_ALDERLAKE_L, X86_STEPPING_ANY, RFDS | VMSCAPE),
  1064. VULNBL_INTEL_STEPPINGS(INTEL_RAPTORLAKE, X86_STEPPING_ANY, RFDS | VMSCAPE),
  1065. VULNBL_INTEL_STEPPINGS(INTEL_RAPTORLAKE_P, X86_STEPPING_ANY, RFDS | VMSCAPE),
  1066. VULNBL_INTEL_STEPPINGS(INTEL_RAPTORLAKE_S, X86_STEPPING_ANY, RFDS | VMSCAPE),
  1067. VULNBL_INTEL_STEPPINGS(INTEL_METEORLAKE_L, X86_STEPPING_ANY, VMSCAPE),
  1068. VULNBL_INTEL_STEPPINGS(INTEL_ARROWLAKE_H, X86_STEPPING_ANY, VMSCAPE),
  1069. VULNBL_INTEL_STEPPINGS(INTEL_ARROWLAKE, X86_STEPPING_ANY, VMSCAPE),
  1070. VULNBL_INTEL_STEPPINGS(INTEL_ARROWLAKE_U, X86_STEPPING_ANY, VMSCAPE),
  1071. VULNBL_INTEL_STEPPINGS(INTEL_LUNARLAKE_M, X86_STEPPING_ANY, VMSCAPE),
  1072. VULNBL_INTEL_STEPPINGS(INTEL_SAPPHIRERAPIDS_X, X86_STEPPING_ANY, VMSCAPE),
  1073. VULNBL_INTEL_STEPPINGS(INTEL_GRANITERAPIDS_X, X86_STEPPING_ANY, VMSCAPE),
  1074. VULNBL_INTEL_STEPPINGS(INTEL_EMERALDRAPIDS_X, X86_STEPPING_ANY, VMSCAPE),
  1075. VULNBL_INTEL_STEPPINGS(INTEL_ATOM_GRACEMONT, X86_STEPPING_ANY, RFDS | VMSCAPE),
  1076. VULNBL_INTEL_STEPPINGS(INTEL_ATOM_TREMONT, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RFDS),
  1077. VULNBL_INTEL_STEPPINGS(INTEL_ATOM_TREMONT_D, X86_STEPPING_ANY, MMIO | RFDS),
  1078. VULNBL_INTEL_STEPPINGS(INTEL_ATOM_TREMONT_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RFDS),
  1079. VULNBL_INTEL_STEPPINGS(INTEL_ATOM_GOLDMONT, X86_STEPPING_ANY, RFDS),
  1080. VULNBL_INTEL_STEPPINGS(INTEL_ATOM_GOLDMONT_D, X86_STEPPING_ANY, RFDS),
  1081. VULNBL_INTEL_STEPPINGS(INTEL_ATOM_GOLDMONT_PLUS, X86_STEPPING_ANY, RFDS),
  1082. VULNBL_INTEL_STEPPINGS(INTEL_ATOM_CRESTMONT_X, X86_STEPPING_ANY, VMSCAPE),
  1083. VULNBL_AMD(0x15, RETBLEED),
  1084. VULNBL_AMD(0x16, RETBLEED),
  1085. VULNBL_AMD(0x17, RETBLEED | SMT_RSB | SRSO | VMSCAPE),
  1086. VULNBL_HYGON(0x18, RETBLEED | SMT_RSB | SRSO | VMSCAPE),
  1087. VULNBL_AMD(0x19, SRSO | TSA | VMSCAPE),
  1088. VULNBL_AMD(0x1a, SRSO | VMSCAPE),
  1089. {}
  1090. };
  1091. static bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which)
  1092. {
  1093. const struct x86_cpu_id *m = x86_match_cpu(table);
  1094. return m && !!(m->driver_data & which);
  1095. }
  1096. u64 x86_read_arch_cap_msr(void)
  1097. {
  1098. u64 x86_arch_cap_msr = 0;
  1099. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
  1100. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, x86_arch_cap_msr);
  1101. return x86_arch_cap_msr;
  1102. }
  1103. static bool arch_cap_mmio_immune(u64 x86_arch_cap_msr)
  1104. {
  1105. return (x86_arch_cap_msr & ARCH_CAP_FBSDP_NO &&
  1106. x86_arch_cap_msr & ARCH_CAP_PSDP_NO &&
  1107. x86_arch_cap_msr & ARCH_CAP_SBDR_SSDP_NO);
  1108. }
  1109. static bool __init vulnerable_to_rfds(u64 x86_arch_cap_msr)
  1110. {
  1111. /* The "immunity" bit trumps everything else: */
  1112. if (x86_arch_cap_msr & ARCH_CAP_RFDS_NO)
  1113. return false;
  1114. /*
  1115. * VMMs set ARCH_CAP_RFDS_CLEAR for processors not in the blacklist to
  1116. * indicate that mitigation is needed because guest is running on a
  1117. * vulnerable hardware or may migrate to such hardware:
  1118. */
  1119. if (x86_arch_cap_msr & ARCH_CAP_RFDS_CLEAR)
  1120. return true;
  1121. /* Only consult the blacklist when there is no enumeration: */
  1122. return cpu_matches(cpu_vuln_blacklist, RFDS);
  1123. }
  1124. static bool __init vulnerable_to_its(u64 x86_arch_cap_msr)
  1125. {
  1126. /* The "immunity" bit trumps everything else: */
  1127. if (x86_arch_cap_msr & ARCH_CAP_ITS_NO)
  1128. return false;
  1129. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  1130. return false;
  1131. /* None of the affected CPUs have BHI_CTRL */
  1132. if (boot_cpu_has(X86_FEATURE_BHI_CTRL))
  1133. return false;
  1134. /*
  1135. * If a VMM did not expose ITS_NO, assume that a guest could
  1136. * be running on a vulnerable hardware or may migrate to such
  1137. * hardware.
  1138. */
  1139. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  1140. return true;
  1141. if (cpu_matches(cpu_vuln_blacklist, ITS))
  1142. return true;
  1143. return false;
  1144. }
  1145. static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
  1146. {
  1147. u64 x86_arch_cap_msr = x86_read_arch_cap_msr();
  1148. /* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */
  1149. if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) &&
  1150. !(x86_arch_cap_msr & ARCH_CAP_PSCHANGE_MC_NO))
  1151. setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT);
  1152. if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION))
  1153. return;
  1154. setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
  1155. if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2))
  1156. setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
  1157. if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) &&
  1158. !(x86_arch_cap_msr & ARCH_CAP_SSB_NO) &&
  1159. !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
  1160. setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
  1161. /*
  1162. * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature
  1163. * flag and protect from vendor-specific bugs via the whitelist.
  1164. *
  1165. * Don't use AutoIBRS when SNP is enabled because it degrades host
  1166. * userspace indirect branch performance.
  1167. */
  1168. if ((x86_arch_cap_msr & ARCH_CAP_IBRS_ALL) ||
  1169. (cpu_has(c, X86_FEATURE_AUTOIBRS) &&
  1170. !cpu_feature_enabled(X86_FEATURE_SEV_SNP))) {
  1171. setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
  1172. if (!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) &&
  1173. !(x86_arch_cap_msr & ARCH_CAP_PBRSB_NO))
  1174. setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB);
  1175. }
  1176. if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) &&
  1177. !(x86_arch_cap_msr & ARCH_CAP_MDS_NO)) {
  1178. setup_force_cpu_bug(X86_BUG_MDS);
  1179. if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY))
  1180. setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
  1181. }
  1182. if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS))
  1183. setup_force_cpu_bug(X86_BUG_SWAPGS);
  1184. /*
  1185. * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when:
  1186. * - TSX is supported or
  1187. * - TSX_CTRL is present
  1188. *
  1189. * TSX_CTRL check is needed for cases when TSX could be disabled before
  1190. * the kernel boot e.g. kexec.
  1191. * TSX_CTRL check alone is not sufficient for cases when the microcode
  1192. * update is not present or running as guest that don't get TSX_CTRL.
  1193. */
  1194. if (!(x86_arch_cap_msr & ARCH_CAP_TAA_NO) &&
  1195. (cpu_has(c, X86_FEATURE_RTM) ||
  1196. (x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR)))
  1197. setup_force_cpu_bug(X86_BUG_TAA);
  1198. /*
  1199. * SRBDS affects CPUs which support RDRAND or RDSEED and are listed
  1200. * in the vulnerability blacklist.
  1201. *
  1202. * Some of the implications and mitigation of Shared Buffers Data
  1203. * Sampling (SBDS) are similar to SRBDS. Give SBDS same treatment as
  1204. * SRBDS.
  1205. */
  1206. if ((cpu_has(c, X86_FEATURE_RDRAND) ||
  1207. cpu_has(c, X86_FEATURE_RDSEED)) &&
  1208. cpu_matches(cpu_vuln_blacklist, SRBDS | MMIO_SBDS))
  1209. setup_force_cpu_bug(X86_BUG_SRBDS);
  1210. /*
  1211. * Processor MMIO Stale Data bug enumeration
  1212. *
  1213. * Affected CPU list is generally enough to enumerate the vulnerability,
  1214. * but for virtualization case check for ARCH_CAP MSR bits also, VMM may
  1215. * not want the guest to enumerate the bug.
  1216. *
  1217. * Set X86_BUG_MMIO_UNKNOWN for CPUs that are neither in the blacklist,
  1218. * nor in the whitelist and also don't enumerate MSR ARCH_CAP MMIO bits.
  1219. */
  1220. if (!arch_cap_mmio_immune(x86_arch_cap_msr)) {
  1221. if (cpu_matches(cpu_vuln_blacklist, MMIO))
  1222. setup_force_cpu_bug(X86_BUG_MMIO_STALE_DATA);
  1223. else if (!cpu_matches(cpu_vuln_whitelist, NO_MMIO))
  1224. setup_force_cpu_bug(X86_BUG_MMIO_UNKNOWN);
  1225. }
  1226. if (!cpu_has(c, X86_FEATURE_BTC_NO)) {
  1227. if (cpu_matches(cpu_vuln_blacklist, RETBLEED) || (x86_arch_cap_msr & ARCH_CAP_RSBA))
  1228. setup_force_cpu_bug(X86_BUG_RETBLEED);
  1229. }
  1230. if (cpu_matches(cpu_vuln_blacklist, SMT_RSB))
  1231. setup_force_cpu_bug(X86_BUG_SMT_RSB);
  1232. if (!cpu_has(c, X86_FEATURE_SRSO_NO)) {
  1233. if (cpu_matches(cpu_vuln_blacklist, SRSO))
  1234. setup_force_cpu_bug(X86_BUG_SRSO);
  1235. }
  1236. /*
  1237. * Check if CPU is vulnerable to GDS. If running in a virtual machine on
  1238. * an affected processor, the VMM may have disabled the use of GATHER by
  1239. * disabling AVX2. The only way to do this in HW is to clear XCR0[2],
  1240. * which means that AVX will be disabled.
  1241. */
  1242. if (cpu_matches(cpu_vuln_blacklist, GDS) && !(x86_arch_cap_msr & ARCH_CAP_GDS_NO) &&
  1243. boot_cpu_has(X86_FEATURE_AVX))
  1244. setup_force_cpu_bug(X86_BUG_GDS);
  1245. if (vulnerable_to_rfds(x86_arch_cap_msr))
  1246. setup_force_cpu_bug(X86_BUG_RFDS);
  1247. /*
  1248. * Intel parts with eIBRS are vulnerable to BHI attacks. Parts with
  1249. * BHI_NO still need to use the BHI mitigation to prevent Intra-mode
  1250. * attacks. When virtualized, eIBRS could be hidden, assume vulnerable.
  1251. */
  1252. if (!cpu_matches(cpu_vuln_whitelist, NO_BHI) &&
  1253. (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED) ||
  1254. boot_cpu_has(X86_FEATURE_HYPERVISOR)))
  1255. setup_force_cpu_bug(X86_BUG_BHI);
  1256. if (cpu_has(c, X86_FEATURE_AMD_IBPB) && !cpu_has(c, X86_FEATURE_AMD_IBPB_RET))
  1257. setup_force_cpu_bug(X86_BUG_IBPB_NO_RET);
  1258. if (vulnerable_to_its(x86_arch_cap_msr)) {
  1259. setup_force_cpu_bug(X86_BUG_ITS);
  1260. if (cpu_matches(cpu_vuln_blacklist, ITS_NATIVE_ONLY))
  1261. setup_force_cpu_bug(X86_BUG_ITS_NATIVE_ONLY);
  1262. }
  1263. if (c->x86_vendor == X86_VENDOR_AMD) {
  1264. if (!cpu_has(c, X86_FEATURE_TSA_SQ_NO) ||
  1265. !cpu_has(c, X86_FEATURE_TSA_L1_NO)) {
  1266. if (cpu_matches(cpu_vuln_blacklist, TSA) ||
  1267. /* Enable bug on Zen guests to allow for live migration. */
  1268. (cpu_has(c, X86_FEATURE_HYPERVISOR) && cpu_has(c, X86_FEATURE_ZEN)))
  1269. setup_force_cpu_bug(X86_BUG_TSA);
  1270. }
  1271. }
  1272. /*
  1273. * Set the bug only on bare-metal. A nested hypervisor should already be
  1274. * deploying IBPB to isolate itself from nested guests.
  1275. */
  1276. if (cpu_matches(cpu_vuln_blacklist, VMSCAPE) &&
  1277. !boot_cpu_has(X86_FEATURE_HYPERVISOR))
  1278. setup_force_cpu_bug(X86_BUG_VMSCAPE);
  1279. if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
  1280. return;
  1281. /* Rogue Data Cache Load? No! */
  1282. if (x86_arch_cap_msr & ARCH_CAP_RDCL_NO)
  1283. return;
  1284. setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
  1285. if (cpu_matches(cpu_vuln_whitelist, NO_L1TF))
  1286. return;
  1287. setup_force_cpu_bug(X86_BUG_L1TF);
  1288. }
  1289. /*
  1290. * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
  1291. * unfortunately, that's not true in practice because of early VIA
  1292. * chips and (more importantly) broken virtualizers that are not easy
  1293. * to detect. In the latter case it doesn't even *fail* reliably, so
  1294. * probing for it doesn't even work. Disable it completely on 32-bit
  1295. * unless we can find a reliable way to detect all the broken cases.
  1296. * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
  1297. */
  1298. static void detect_nopl(void)
  1299. {
  1300. #ifdef CONFIG_X86_32
  1301. setup_clear_cpu_cap(X86_FEATURE_NOPL);
  1302. #else
  1303. setup_force_cpu_cap(X86_FEATURE_NOPL);
  1304. #endif
  1305. }
  1306. /*
  1307. * We parse cpu parameters early because fpu__init_system() is executed
  1308. * before parse_early_param().
  1309. */
  1310. static void __init cpu_parse_early_param(void)
  1311. {
  1312. char arg[128];
  1313. char *argptr = arg, *opt;
  1314. int arglen, taint = 0;
  1315. #ifdef CONFIG_X86_32
  1316. if (cmdline_find_option_bool(boot_command_line, "no387"))
  1317. #ifdef CONFIG_MATH_EMULATION
  1318. setup_clear_cpu_cap(X86_FEATURE_FPU);
  1319. #else
  1320. pr_err("Option 'no387' required CONFIG_MATH_EMULATION enabled.\n");
  1321. #endif
  1322. if (cmdline_find_option_bool(boot_command_line, "nofxsr"))
  1323. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  1324. #endif
  1325. if (cmdline_find_option_bool(boot_command_line, "noxsave"))
  1326. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  1327. if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
  1328. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  1329. if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
  1330. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  1331. if (cmdline_find_option_bool(boot_command_line, "nousershstk"))
  1332. setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK);
  1333. /* Minimize the gap between FRED is available and available but disabled. */
  1334. arglen = cmdline_find_option(boot_command_line, "fred", arg, sizeof(arg));
  1335. if (arglen != 2 || strncmp(arg, "on", 2))
  1336. setup_clear_cpu_cap(X86_FEATURE_FRED);
  1337. arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg));
  1338. if (arglen <= 0)
  1339. return;
  1340. pr_info("Clearing CPUID bits:");
  1341. while (argptr) {
  1342. bool found __maybe_unused = false;
  1343. unsigned int bit;
  1344. opt = strsep(&argptr, ",");
  1345. /*
  1346. * Handle naked numbers first for feature flags which don't
  1347. * have names.
  1348. */
  1349. if (!kstrtouint(opt, 10, &bit)) {
  1350. if (bit < NCAPINTS * 32) {
  1351. /* empty-string, i.e., ""-defined feature flags */
  1352. if (!x86_cap_flags[bit])
  1353. pr_cont(" " X86_CAP_FMT_NUM, x86_cap_flag_num(bit));
  1354. else
  1355. pr_cont(" " X86_CAP_FMT, x86_cap_flag(bit));
  1356. setup_clear_cpu_cap(bit);
  1357. taint++;
  1358. }
  1359. /*
  1360. * The assumption is that there are no feature names with only
  1361. * numbers in the name thus go to the next argument.
  1362. */
  1363. continue;
  1364. }
  1365. for (bit = 0; bit < 32 * NCAPINTS; bit++) {
  1366. if (!x86_cap_flag(bit))
  1367. continue;
  1368. if (strcmp(x86_cap_flag(bit), opt))
  1369. continue;
  1370. pr_cont(" %s", opt);
  1371. setup_clear_cpu_cap(bit);
  1372. taint++;
  1373. found = true;
  1374. break;
  1375. }
  1376. if (!found)
  1377. pr_cont(" (unknown: %s)", opt);
  1378. }
  1379. pr_cont("\n");
  1380. if (taint)
  1381. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  1382. }
  1383. /*
  1384. * Do minimum CPU detection early.
  1385. * Fields really needed: vendor, cpuid_level, family, model, mask,
  1386. * cache alignment.
  1387. * The others are not touched to avoid unwanted side effects.
  1388. *
  1389. * WARNING: this function is only called on the boot CPU. Don't add code
  1390. * here that is supposed to run on all CPUs.
  1391. */
  1392. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  1393. {
  1394. memset(&c->x86_capability, 0, sizeof(c->x86_capability));
  1395. c->extended_cpuid_level = 0;
  1396. if (!have_cpuid_p())
  1397. identify_cpu_without_cpuid(c);
  1398. /* cyrix could have cpuid enabled via c_identify()*/
  1399. if (have_cpuid_p()) {
  1400. cpu_detect(c);
  1401. get_cpu_vendor(c);
  1402. intel_unlock_cpuid_leafs(c);
  1403. get_cpu_cap(c);
  1404. setup_force_cpu_cap(X86_FEATURE_CPUID);
  1405. get_cpu_address_sizes(c);
  1406. cpu_parse_early_param();
  1407. cpu_init_topology(c);
  1408. if (this_cpu->c_early_init)
  1409. this_cpu->c_early_init(c);
  1410. c->cpu_index = 0;
  1411. filter_cpuid_features(c, false);
  1412. if (this_cpu->c_bsp_init)
  1413. this_cpu->c_bsp_init(c);
  1414. } else {
  1415. setup_clear_cpu_cap(X86_FEATURE_CPUID);
  1416. get_cpu_address_sizes(c);
  1417. cpu_init_topology(c);
  1418. }
  1419. setup_force_cpu_cap(X86_FEATURE_ALWAYS);
  1420. cpu_set_bug_bits(c);
  1421. sld_setup(c);
  1422. #ifdef CONFIG_X86_32
  1423. /*
  1424. * Regardless of whether PCID is enumerated, the SDM says
  1425. * that it can't be enabled in 32-bit mode.
  1426. */
  1427. setup_clear_cpu_cap(X86_FEATURE_PCID);
  1428. #endif
  1429. /*
  1430. * Later in the boot process pgtable_l5_enabled() relies on
  1431. * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
  1432. * enabled by this point we need to clear the feature bit to avoid
  1433. * false-positives at the later stage.
  1434. *
  1435. * pgtable_l5_enabled() can be false here for several reasons:
  1436. * - 5-level paging is disabled compile-time;
  1437. * - it's 32-bit kernel;
  1438. * - machine doesn't support 5-level paging;
  1439. * - user specified 'no5lvl' in kernel command line.
  1440. */
  1441. if (!pgtable_l5_enabled())
  1442. setup_clear_cpu_cap(X86_FEATURE_LA57);
  1443. detect_nopl();
  1444. }
  1445. void __init init_cpu_devs(void)
  1446. {
  1447. const struct cpu_dev *const *cdev;
  1448. int count = 0;
  1449. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  1450. const struct cpu_dev *cpudev = *cdev;
  1451. if (count >= X86_VENDOR_NUM)
  1452. break;
  1453. cpu_devs[count] = cpudev;
  1454. count++;
  1455. }
  1456. }
  1457. void __init early_cpu_init(void)
  1458. {
  1459. #ifdef CONFIG_PROCESSOR_SELECT
  1460. unsigned int i, j;
  1461. pr_info("KERNEL supported cpus:\n");
  1462. #endif
  1463. init_cpu_devs();
  1464. #ifdef CONFIG_PROCESSOR_SELECT
  1465. for (i = 0; i < X86_VENDOR_NUM && cpu_devs[i]; i++) {
  1466. for (j = 0; j < 2; j++) {
  1467. if (!cpu_devs[i]->c_ident[j])
  1468. continue;
  1469. pr_info(" %s %s\n", cpu_devs[i]->c_vendor,
  1470. cpu_devs[i]->c_ident[j]);
  1471. }
  1472. }
  1473. #endif
  1474. early_identify_cpu(&boot_cpu_data);
  1475. }
  1476. static bool detect_null_seg_behavior(void)
  1477. {
  1478. /*
  1479. * Empirically, writing zero to a segment selector on AMD does
  1480. * not clear the base, whereas writing zero to a segment
  1481. * selector on Intel does clear the base. Intel's behavior
  1482. * allows slightly faster context switches in the common case
  1483. * where GS is unused by the prev and next threads.
  1484. *
  1485. * Since neither vendor documents this anywhere that I can see,
  1486. * detect it directly instead of hard-coding the choice by
  1487. * vendor.
  1488. *
  1489. * I've designated AMD's behavior as the "bug" because it's
  1490. * counterintuitive and less friendly.
  1491. */
  1492. unsigned long old_base, tmp;
  1493. rdmsrl(MSR_FS_BASE, old_base);
  1494. wrmsrl(MSR_FS_BASE, 1);
  1495. loadsegment(fs, 0);
  1496. rdmsrl(MSR_FS_BASE, tmp);
  1497. wrmsrl(MSR_FS_BASE, old_base);
  1498. return tmp == 0;
  1499. }
  1500. void check_null_seg_clears_base(struct cpuinfo_x86 *c)
  1501. {
  1502. /* BUG_NULL_SEG is only relevant with 64bit userspace */
  1503. if (!IS_ENABLED(CONFIG_X86_64))
  1504. return;
  1505. if (cpu_has(c, X86_FEATURE_NULL_SEL_CLR_BASE))
  1506. return;
  1507. /*
  1508. * CPUID bit above wasn't set. If this kernel is still running
  1509. * as a HV guest, then the HV has decided not to advertize
  1510. * that CPUID bit for whatever reason. For example, one
  1511. * member of the migration pool might be vulnerable. Which
  1512. * means, the bug is present: set the BUG flag and return.
  1513. */
  1514. if (cpu_has(c, X86_FEATURE_HYPERVISOR)) {
  1515. set_cpu_bug(c, X86_BUG_NULL_SEG);
  1516. return;
  1517. }
  1518. /*
  1519. * Zen2 CPUs also have this behaviour, but no CPUID bit.
  1520. * 0x18 is the respective family for Hygon.
  1521. */
  1522. if ((c->x86 == 0x17 || c->x86 == 0x18) &&
  1523. detect_null_seg_behavior())
  1524. return;
  1525. /* All the remaining ones are affected */
  1526. set_cpu_bug(c, X86_BUG_NULL_SEG);
  1527. }
  1528. static void generic_identify(struct cpuinfo_x86 *c)
  1529. {
  1530. c->extended_cpuid_level = 0;
  1531. if (!have_cpuid_p())
  1532. identify_cpu_without_cpuid(c);
  1533. /* cyrix could have cpuid enabled via c_identify()*/
  1534. if (!have_cpuid_p())
  1535. return;
  1536. cpu_detect(c);
  1537. get_cpu_vendor(c);
  1538. intel_unlock_cpuid_leafs(c);
  1539. get_cpu_cap(c);
  1540. get_cpu_address_sizes(c);
  1541. get_model_name(c); /* Default name */
  1542. /*
  1543. * ESPFIX is a strange bug. All real CPUs have it. Paravirt
  1544. * systems that run Linux at CPL > 0 may or may not have the
  1545. * issue, but, even if they have the issue, there's absolutely
  1546. * nothing we can do about it because we can't use the real IRET
  1547. * instruction.
  1548. *
  1549. * NB: For the time being, only 32-bit kernels support
  1550. * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
  1551. * whether to apply espfix using paravirt hooks. If any
  1552. * non-paravirt system ever shows up that does *not* have the
  1553. * ESPFIX issue, we can change this.
  1554. */
  1555. #ifdef CONFIG_X86_32
  1556. set_cpu_bug(c, X86_BUG_ESPFIX);
  1557. #endif
  1558. }
  1559. /*
  1560. * This does the hard work of actually picking apart the CPU stuff...
  1561. */
  1562. static void identify_cpu(struct cpuinfo_x86 *c)
  1563. {
  1564. int i;
  1565. c->loops_per_jiffy = loops_per_jiffy;
  1566. c->x86_cache_size = 0;
  1567. c->x86_vendor = X86_VENDOR_UNKNOWN;
  1568. c->x86_model = c->x86_stepping = 0; /* So far unknown... */
  1569. c->x86_vendor_id[0] = '\0'; /* Unset */
  1570. c->x86_model_id[0] = '\0'; /* Unset */
  1571. #ifdef CONFIG_X86_64
  1572. c->x86_clflush_size = 64;
  1573. c->x86_phys_bits = 36;
  1574. c->x86_virt_bits = 48;
  1575. #else
  1576. c->cpuid_level = -1; /* CPUID not detected */
  1577. c->x86_clflush_size = 32;
  1578. c->x86_phys_bits = 32;
  1579. c->x86_virt_bits = 32;
  1580. #endif
  1581. c->x86_cache_alignment = c->x86_clflush_size;
  1582. memset(&c->x86_capability, 0, sizeof(c->x86_capability));
  1583. #ifdef CONFIG_X86_VMX_FEATURE_NAMES
  1584. memset(&c->vmx_capability, 0, sizeof(c->vmx_capability));
  1585. #endif
  1586. generic_identify(c);
  1587. cpu_parse_topology(c);
  1588. if (this_cpu->c_identify)
  1589. this_cpu->c_identify(c);
  1590. /* Clear/Set all flags overridden by options, after probe */
  1591. apply_forced_caps(c);
  1592. /*
  1593. * Set default APIC and TSC_DEADLINE MSR fencing flag. AMD and
  1594. * Hygon will clear it in ->c_init() below.
  1595. */
  1596. set_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE);
  1597. /*
  1598. * Vendor-specific initialization. In this section we
  1599. * canonicalize the feature flags, meaning if there are
  1600. * features a certain CPU supports which CPUID doesn't
  1601. * tell us, CPUID claiming incorrect flags, or other bugs,
  1602. * we handle them here.
  1603. *
  1604. * At the end of this section, c->x86_capability better
  1605. * indicate the features this CPU genuinely supports!
  1606. */
  1607. if (this_cpu->c_init)
  1608. this_cpu->c_init(c);
  1609. /* Disable the PN if appropriate */
  1610. squash_the_stupid_serial_number(c);
  1611. /* Set up SMEP/SMAP/UMIP */
  1612. setup_smep(c);
  1613. setup_smap(c);
  1614. setup_umip(c);
  1615. /* Enable FSGSBASE instructions if available. */
  1616. if (cpu_has(c, X86_FEATURE_FSGSBASE)) {
  1617. cr4_set_bits(X86_CR4_FSGSBASE);
  1618. elf_hwcap2 |= HWCAP2_FSGSBASE;
  1619. }
  1620. /*
  1621. * The vendor-specific functions might have changed features.
  1622. * Now we do "generic changes."
  1623. */
  1624. /* Filter out anything that depends on CPUID levels we don't have */
  1625. filter_cpuid_features(c, true);
  1626. /* If the model name is still unset, do table lookup. */
  1627. if (!c->x86_model_id[0]) {
  1628. const char *p;
  1629. p = table_lookup_model(c);
  1630. if (p)
  1631. strcpy(c->x86_model_id, p);
  1632. else
  1633. /* Last resort... */
  1634. sprintf(c->x86_model_id, "%02x/%02x",
  1635. c->x86, c->x86_model);
  1636. }
  1637. x86_init_rdrand(c);
  1638. setup_pku(c);
  1639. setup_cet(c);
  1640. /*
  1641. * Clear/Set all flags overridden by options, need do it
  1642. * before following smp all cpus cap AND.
  1643. */
  1644. apply_forced_caps(c);
  1645. /*
  1646. * On SMP, boot_cpu_data holds the common feature set between
  1647. * all CPUs; so make sure that we indicate which features are
  1648. * common between the CPUs. The first time this routine gets
  1649. * executed, c == &boot_cpu_data.
  1650. */
  1651. if (c != &boot_cpu_data) {
  1652. /* AND the already accumulated flags with these */
  1653. for (i = 0; i < NCAPINTS; i++)
  1654. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  1655. /* OR, i.e. replicate the bug flags */
  1656. for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
  1657. c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
  1658. }
  1659. ppin_init(c);
  1660. /* Init Machine Check Exception if available. */
  1661. mcheck_cpu_init(c);
  1662. #ifdef CONFIG_NUMA
  1663. numa_add_cpu(smp_processor_id());
  1664. #endif
  1665. }
  1666. /*
  1667. * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
  1668. * on 32-bit kernels:
  1669. */
  1670. #ifdef CONFIG_X86_32
  1671. void enable_sep_cpu(void)
  1672. {
  1673. struct tss_struct *tss;
  1674. int cpu;
  1675. if (!boot_cpu_has(X86_FEATURE_SEP))
  1676. return;
  1677. cpu = get_cpu();
  1678. tss = &per_cpu(cpu_tss_rw, cpu);
  1679. /*
  1680. * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
  1681. * see the big comment in struct x86_hw_tss's definition.
  1682. */
  1683. tss->x86_tss.ss1 = __KERNEL_CS;
  1684. wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
  1685. wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
  1686. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
  1687. put_cpu();
  1688. }
  1689. #endif
  1690. static __init void identify_boot_cpu(void)
  1691. {
  1692. identify_cpu(&boot_cpu_data);
  1693. if (HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT))
  1694. pr_info("CET detected: Indirect Branch Tracking enabled\n");
  1695. #ifdef CONFIG_X86_32
  1696. enable_sep_cpu();
  1697. #endif
  1698. cpu_detect_tlb(&boot_cpu_data);
  1699. setup_cr_pinning();
  1700. tsx_init();
  1701. tdx_init();
  1702. lkgs_init();
  1703. }
  1704. void identify_secondary_cpu(struct cpuinfo_x86 *c)
  1705. {
  1706. BUG_ON(c == &boot_cpu_data);
  1707. identify_cpu(c);
  1708. #ifdef CONFIG_X86_32
  1709. enable_sep_cpu();
  1710. #endif
  1711. x86_spec_ctrl_setup_ap();
  1712. update_srbds_msr();
  1713. if (boot_cpu_has_bug(X86_BUG_GDS))
  1714. update_gds_msr();
  1715. tsx_ap_init();
  1716. }
  1717. void print_cpu_info(struct cpuinfo_x86 *c)
  1718. {
  1719. const char *vendor = NULL;
  1720. if (c->x86_vendor < X86_VENDOR_NUM) {
  1721. vendor = this_cpu->c_vendor;
  1722. } else {
  1723. if (c->cpuid_level >= 0)
  1724. vendor = c->x86_vendor_id;
  1725. }
  1726. if (vendor && !strstr(c->x86_model_id, vendor))
  1727. pr_cont("%s ", vendor);
  1728. if (c->x86_model_id[0])
  1729. pr_cont("%s", c->x86_model_id);
  1730. else
  1731. pr_cont("%d86", c->x86);
  1732. pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
  1733. if (c->x86_stepping || c->cpuid_level >= 0)
  1734. pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
  1735. else
  1736. pr_cont(")\n");
  1737. }
  1738. /*
  1739. * clearcpuid= was already parsed in cpu_parse_early_param(). This dummy
  1740. * function prevents it from becoming an environment variable for init.
  1741. */
  1742. static __init int setup_clearcpuid(char *arg)
  1743. {
  1744. return 1;
  1745. }
  1746. __setup("clearcpuid=", setup_clearcpuid);
  1747. DEFINE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot) = {
  1748. .current_task = &init_task,
  1749. .preempt_count = INIT_PREEMPT_COUNT,
  1750. .top_of_stack = TOP_OF_INIT_STACK,
  1751. };
  1752. EXPORT_PER_CPU_SYMBOL(pcpu_hot);
  1753. EXPORT_PER_CPU_SYMBOL(const_pcpu_hot);
  1754. #ifdef CONFIG_X86_64
  1755. DEFINE_PER_CPU_FIRST(struct fixed_percpu_data,
  1756. fixed_percpu_data) __aligned(PAGE_SIZE) __visible;
  1757. EXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data);
  1758. static void wrmsrl_cstar(unsigned long val)
  1759. {
  1760. /*
  1761. * Intel CPUs do not support 32-bit SYSCALL. Writing to MSR_CSTAR
  1762. * is so far ignored by the CPU, but raises a #VE trap in a TDX
  1763. * guest. Avoid the pointless write on all Intel CPUs.
  1764. */
  1765. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  1766. wrmsrl(MSR_CSTAR, val);
  1767. }
  1768. static inline void idt_syscall_init(void)
  1769. {
  1770. wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
  1771. if (ia32_enabled()) {
  1772. wrmsrl_cstar((unsigned long)entry_SYSCALL_compat);
  1773. /*
  1774. * This only works on Intel CPUs.
  1775. * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
  1776. * This does not cause SYSENTER to jump to the wrong location, because
  1777. * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
  1778. */
  1779. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  1780. wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
  1781. (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
  1782. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
  1783. } else {
  1784. wrmsrl_cstar((unsigned long)entry_SYSCALL32_ignore);
  1785. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
  1786. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  1787. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
  1788. }
  1789. /*
  1790. * Flags to clear on syscall; clear as much as possible
  1791. * to minimize user space-kernel interference.
  1792. */
  1793. wrmsrl(MSR_SYSCALL_MASK,
  1794. X86_EFLAGS_CF|X86_EFLAGS_PF|X86_EFLAGS_AF|
  1795. X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_TF|
  1796. X86_EFLAGS_IF|X86_EFLAGS_DF|X86_EFLAGS_OF|
  1797. X86_EFLAGS_IOPL|X86_EFLAGS_NT|X86_EFLAGS_RF|
  1798. X86_EFLAGS_AC|X86_EFLAGS_ID);
  1799. }
  1800. /* May not be marked __init: used by software suspend */
  1801. void syscall_init(void)
  1802. {
  1803. /* The default user and kernel segments */
  1804. wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
  1805. /*
  1806. * Except the IA32_STAR MSR, there is NO need to setup SYSCALL and
  1807. * SYSENTER MSRs for FRED, because FRED uses the ring 3 FRED
  1808. * entrypoint for SYSCALL and SYSENTER, and ERETU is the only legit
  1809. * instruction to return to ring 3 (both sysexit and sysret cause
  1810. * #UD when FRED is enabled).
  1811. */
  1812. if (!cpu_feature_enabled(X86_FEATURE_FRED))
  1813. idt_syscall_init();
  1814. }
  1815. #else /* CONFIG_X86_64 */
  1816. #ifdef CONFIG_STACKPROTECTOR
  1817. DEFINE_PER_CPU(unsigned long, __stack_chk_guard);
  1818. #ifndef CONFIG_SMP
  1819. EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
  1820. #endif
  1821. #endif
  1822. #endif /* CONFIG_X86_64 */
  1823. static void initialize_debug_regs(void)
  1824. {
  1825. /* Control register first -- to make sure everything is disabled. */
  1826. set_debugreg(DR7_FIXED_1, 7);
  1827. set_debugreg(DR6_RESERVED, 6);
  1828. /* dr5 and dr4 don't exist */
  1829. set_debugreg(0, 3);
  1830. set_debugreg(0, 2);
  1831. set_debugreg(0, 1);
  1832. set_debugreg(0, 0);
  1833. }
  1834. #ifdef CONFIG_KGDB
  1835. /*
  1836. * Restore debug regs if using kgdbwait and you have a kernel debugger
  1837. * connection established.
  1838. */
  1839. static void dbg_restore_debug_regs(void)
  1840. {
  1841. if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
  1842. arch_kgdb_ops.correct_hw_break();
  1843. }
  1844. #else /* ! CONFIG_KGDB */
  1845. #define dbg_restore_debug_regs()
  1846. #endif /* ! CONFIG_KGDB */
  1847. static inline void setup_getcpu(int cpu)
  1848. {
  1849. unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
  1850. struct desc_struct d = { };
  1851. if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
  1852. wrmsr(MSR_TSC_AUX, cpudata, 0);
  1853. /* Store CPU and node number in limit. */
  1854. d.limit0 = cpudata;
  1855. d.limit1 = cpudata >> 16;
  1856. d.type = 5; /* RO data, expand down, accessed */
  1857. d.dpl = 3; /* Visible to user code */
  1858. d.s = 1; /* Not a system segment */
  1859. d.p = 1; /* Present */
  1860. d.d = 1; /* 32-bit */
  1861. write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
  1862. }
  1863. #ifdef CONFIG_X86_64
  1864. static inline void tss_setup_ist(struct tss_struct *tss)
  1865. {
  1866. /* Set up the per-CPU TSS IST stacks */
  1867. tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
  1868. tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
  1869. tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
  1870. tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
  1871. /* Only mapped when SEV-ES is active */
  1872. tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC);
  1873. }
  1874. #else /* CONFIG_X86_64 */
  1875. static inline void tss_setup_ist(struct tss_struct *tss) { }
  1876. #endif /* !CONFIG_X86_64 */
  1877. static inline void tss_setup_io_bitmap(struct tss_struct *tss)
  1878. {
  1879. tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
  1880. #ifdef CONFIG_X86_IOPL_IOPERM
  1881. tss->io_bitmap.prev_max = 0;
  1882. tss->io_bitmap.prev_sequence = 0;
  1883. memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap));
  1884. /*
  1885. * Invalidate the extra array entry past the end of the all
  1886. * permission bitmap as required by the hardware.
  1887. */
  1888. tss->io_bitmap.mapall[IO_BITMAP_LONGS] = ~0UL;
  1889. #endif
  1890. }
  1891. /*
  1892. * Setup everything needed to handle exceptions from the IDT, including the IST
  1893. * exceptions which use paranoid_entry().
  1894. */
  1895. void cpu_init_exception_handling(bool boot_cpu)
  1896. {
  1897. struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
  1898. int cpu = raw_smp_processor_id();
  1899. /* paranoid_entry() gets the CPU number from the GDT */
  1900. setup_getcpu(cpu);
  1901. /* For IDT mode, IST vectors need to be set in TSS. */
  1902. if (!cpu_feature_enabled(X86_FEATURE_FRED))
  1903. tss_setup_ist(tss);
  1904. tss_setup_io_bitmap(tss);
  1905. set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
  1906. load_TR_desc();
  1907. /* GHCB needs to be setup to handle #VC. */
  1908. setup_ghcb();
  1909. if (cpu_feature_enabled(X86_FEATURE_FRED)) {
  1910. /* The boot CPU has enabled FRED during early boot */
  1911. if (!boot_cpu)
  1912. cpu_init_fred_exceptions();
  1913. cpu_init_fred_rsps();
  1914. } else {
  1915. load_current_idt();
  1916. }
  1917. }
  1918. void __init cpu_init_replace_early_idt(void)
  1919. {
  1920. if (cpu_feature_enabled(X86_FEATURE_FRED))
  1921. cpu_init_fred_exceptions();
  1922. else
  1923. idt_setup_early_pf();
  1924. }
  1925. /*
  1926. * cpu_init() initializes state that is per-CPU. Some data is already
  1927. * initialized (naturally) in the bootstrap process, such as the GDT. We
  1928. * reload it nevertheless, this function acts as a 'CPU state barrier',
  1929. * nothing should get across.
  1930. */
  1931. void cpu_init(void)
  1932. {
  1933. struct task_struct *cur = current;
  1934. int cpu = raw_smp_processor_id();
  1935. #ifdef CONFIG_NUMA
  1936. if (this_cpu_read(numa_node) == 0 &&
  1937. early_cpu_to_node(cpu) != NUMA_NO_NODE)
  1938. set_numa_node(early_cpu_to_node(cpu));
  1939. #endif
  1940. pr_debug("Initializing CPU#%d\n", cpu);
  1941. if (IS_ENABLED(CONFIG_X86_64) || cpu_feature_enabled(X86_FEATURE_VME) ||
  1942. boot_cpu_has(X86_FEATURE_TSC) || boot_cpu_has(X86_FEATURE_DE))
  1943. cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  1944. if (IS_ENABLED(CONFIG_X86_64)) {
  1945. loadsegment(fs, 0);
  1946. memset(cur->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  1947. syscall_init();
  1948. wrmsrl(MSR_FS_BASE, 0);
  1949. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  1950. barrier();
  1951. x2apic_setup();
  1952. intel_posted_msi_init();
  1953. }
  1954. mmgrab(&init_mm);
  1955. cur->active_mm = &init_mm;
  1956. BUG_ON(cur->mm);
  1957. initialize_tlbstate_and_flush();
  1958. enter_lazy_tlb(&init_mm, cur);
  1959. /*
  1960. * sp0 points to the entry trampoline stack regardless of what task
  1961. * is running.
  1962. */
  1963. load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
  1964. load_mm_ldt(&init_mm);
  1965. initialize_debug_regs();
  1966. dbg_restore_debug_regs();
  1967. doublefault_init_cpu_tss();
  1968. if (is_uv_system())
  1969. uv_cpu_init();
  1970. load_fixmap_gdt(cpu);
  1971. }
  1972. #ifdef CONFIG_MICROCODE_LATE_LOADING
  1973. /**
  1974. * store_cpu_caps() - Store a snapshot of CPU capabilities
  1975. * @curr_info: Pointer where to store it
  1976. *
  1977. * Returns: None
  1978. */
  1979. void store_cpu_caps(struct cpuinfo_x86 *curr_info)
  1980. {
  1981. /* Reload CPUID max function as it might've changed. */
  1982. curr_info->cpuid_level = cpuid_eax(0);
  1983. /* Copy all capability leafs and pick up the synthetic ones. */
  1984. memcpy(&curr_info->x86_capability, &boot_cpu_data.x86_capability,
  1985. sizeof(curr_info->x86_capability));
  1986. /* Get the hardware CPUID leafs */
  1987. get_cpu_cap(curr_info);
  1988. }
  1989. /**
  1990. * microcode_check() - Check if any CPU capabilities changed after an update.
  1991. * @prev_info: CPU capabilities stored before an update.
  1992. *
  1993. * The microcode loader calls this upon late microcode load to recheck features,
  1994. * only when microcode has been updated. Caller holds and CPU hotplug lock.
  1995. *
  1996. * Return: None
  1997. */
  1998. void microcode_check(struct cpuinfo_x86 *prev_info)
  1999. {
  2000. struct cpuinfo_x86 curr_info;
  2001. perf_check_microcode();
  2002. amd_check_microcode();
  2003. store_cpu_caps(&curr_info);
  2004. if (!memcmp(&prev_info->x86_capability, &curr_info.x86_capability,
  2005. sizeof(prev_info->x86_capability)))
  2006. return;
  2007. pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
  2008. pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
  2009. }
  2010. #endif
  2011. /*
  2012. * Invoked from core CPU hotplug code after hotplug operations
  2013. */
  2014. void arch_smt_update(void)
  2015. {
  2016. /* Handle the speculative execution misfeatures */
  2017. cpu_bugs_smt_update();
  2018. /* Check whether IPI broadcasting can be enabled */
  2019. apic_smt_update();
  2020. }
  2021. void __init arch_cpu_finalize_init(void)
  2022. {
  2023. struct cpuinfo_x86 *c = this_cpu_ptr(&cpu_info);
  2024. identify_boot_cpu();
  2025. select_idle_routine();
  2026. /*
  2027. * identify_boot_cpu() initialized SMT support information, let the
  2028. * core code know.
  2029. */
  2030. cpu_smt_set_num_threads(__max_threads_per_core, __max_threads_per_core);
  2031. if (!IS_ENABLED(CONFIG_SMP)) {
  2032. pr_info("CPU: ");
  2033. print_cpu_info(&boot_cpu_data);
  2034. }
  2035. cpu_select_mitigations();
  2036. arch_smt_update();
  2037. if (IS_ENABLED(CONFIG_X86_32)) {
  2038. /*
  2039. * Check whether this is a real i386 which is not longer
  2040. * supported and fixup the utsname.
  2041. */
  2042. if (boot_cpu_data.x86 < 4)
  2043. panic("Kernel requires i486+ for 'invlpg' and other features");
  2044. init_utsname()->machine[1] =
  2045. '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
  2046. }
  2047. /*
  2048. * Must be before alternatives because it might set or clear
  2049. * feature bits.
  2050. */
  2051. fpu__init_system();
  2052. fpu__init_cpu();
  2053. /*
  2054. * Ensure that access to the per CPU representation has the initial
  2055. * boot CPU configuration.
  2056. */
  2057. *c = boot_cpu_data;
  2058. c->initialized = true;
  2059. alternative_instructions();
  2060. if (IS_ENABLED(CONFIG_X86_64)) {
  2061. unsigned long USER_PTR_MAX = TASK_SIZE_MAX;
  2062. /*
  2063. * Enable this when LAM is gated on LASS support
  2064. if (cpu_feature_enabled(X86_FEATURE_LAM))
  2065. USER_PTR_MAX = (1ul << 63) - PAGE_SIZE;
  2066. */
  2067. runtime_const_init(ptr, USER_PTR_MAX);
  2068. /*
  2069. * Make sure the first 2MB area is not mapped by huge pages
  2070. * There are typically fixed size MTRRs in there and overlapping
  2071. * MTRRs into large pages causes slow downs.
  2072. *
  2073. * Right now we don't do that with gbpages because there seems
  2074. * very little benefit for that case.
  2075. */
  2076. if (!direct_gbpages)
  2077. set_memory_4k((unsigned long)__va(0), 1);
  2078. } else {
  2079. fpu__init_check_bugs();
  2080. }
  2081. /*
  2082. * This needs to be called before any devices perform DMA
  2083. * operations that might use the SWIOTLB bounce buffers. It will
  2084. * mark the bounce buffers as decrypted so that their usage will
  2085. * not cause "plain-text" data to be decrypted when accessed. It
  2086. * must be called after late_time_init() so that Hyper-V x86/x64
  2087. * hypercalls work when the SWIOTLB bounce buffers are decrypted.
  2088. */
  2089. mem_encrypt_init();
  2090. }