common.c 65 KB

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