intel.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/pgtable.h>
  4. #include <linux/string.h>
  5. #include <linux/bitops.h>
  6. #include <linux/smp.h>
  7. #include <linux/sched.h>
  8. #include <linux/sched/clock.h>
  9. #include <linux/semaphore.h>
  10. #include <linux/thread_info.h>
  11. #include <linux/init.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/delay.h>
  15. #include <linux/cpuhotplug.h>
  16. #include <asm/cpufeature.h>
  17. #include <asm/msr.h>
  18. #include <asm/bugs.h>
  19. #include <asm/cpu.h>
  20. #include <asm/intel-family.h>
  21. #include <asm/microcode.h>
  22. #include <asm/hwcap2.h>
  23. #include <asm/elf.h>
  24. #include <asm/cpu_device_id.h>
  25. #include <asm/cmdline.h>
  26. #include <asm/traps.h>
  27. #include <asm/resctrl.h>
  28. #include <asm/numa.h>
  29. #include <asm/thermal.h>
  30. #ifdef CONFIG_X86_64
  31. #include <linux/topology.h>
  32. #endif
  33. #include "cpu.h"
  34. #ifdef CONFIG_X86_LOCAL_APIC
  35. #include <asm/mpspec.h>
  36. #include <asm/apic.h>
  37. #endif
  38. enum split_lock_detect_state {
  39. sld_off = 0,
  40. sld_warn,
  41. sld_fatal,
  42. sld_ratelimit,
  43. };
  44. /*
  45. * Default to sld_off because most systems do not support split lock detection.
  46. * sld_state_setup() will switch this to sld_warn on systems that support
  47. * split lock/bus lock detect, unless there is a command line override.
  48. */
  49. static enum split_lock_detect_state sld_state __ro_after_init = sld_off;
  50. static u64 msr_test_ctrl_cache __ro_after_init;
  51. /*
  52. * With a name like MSR_TEST_CTL it should go without saying, but don't touch
  53. * MSR_TEST_CTL unless the CPU is one of the whitelisted models. Writing it
  54. * on CPUs that do not support SLD can cause fireworks, even when writing '0'.
  55. */
  56. static bool cpu_model_supports_sld __ro_after_init;
  57. /*
  58. * Processors which have self-snooping capability can handle conflicting
  59. * memory type across CPUs by snooping its own cache. However, there exists
  60. * CPU models in which having conflicting memory types still leads to
  61. * unpredictable behavior, machine check errors, or hangs. Clear this
  62. * feature to prevent its use on machines with known erratas.
  63. */
  64. static void check_memory_type_self_snoop_errata(struct cpuinfo_x86 *c)
  65. {
  66. switch (c->x86_vfm) {
  67. case INTEL_CORE_YONAH:
  68. case INTEL_CORE2_MEROM:
  69. case INTEL_CORE2_MEROM_L:
  70. case INTEL_CORE2_PENRYN:
  71. case INTEL_CORE2_DUNNINGTON:
  72. case INTEL_NEHALEM:
  73. case INTEL_NEHALEM_G:
  74. case INTEL_NEHALEM_EP:
  75. case INTEL_NEHALEM_EX:
  76. case INTEL_WESTMERE:
  77. case INTEL_WESTMERE_EP:
  78. case INTEL_SANDYBRIDGE:
  79. setup_clear_cpu_cap(X86_FEATURE_SELFSNOOP);
  80. }
  81. }
  82. static bool ring3mwait_disabled __read_mostly;
  83. static int __init ring3mwait_disable(char *__unused)
  84. {
  85. ring3mwait_disabled = true;
  86. return 1;
  87. }
  88. __setup("ring3mwait=disable", ring3mwait_disable);
  89. static void probe_xeon_phi_r3mwait(struct cpuinfo_x86 *c)
  90. {
  91. /*
  92. * Ring 3 MONITOR/MWAIT feature cannot be detected without
  93. * cpu model and family comparison.
  94. */
  95. if (c->x86 != 6)
  96. return;
  97. switch (c->x86_vfm) {
  98. case INTEL_XEON_PHI_KNL:
  99. case INTEL_XEON_PHI_KNM:
  100. break;
  101. default:
  102. return;
  103. }
  104. if (ring3mwait_disabled)
  105. return;
  106. set_cpu_cap(c, X86_FEATURE_RING3MWAIT);
  107. this_cpu_or(msr_misc_features_shadow,
  108. 1UL << MSR_MISC_FEATURES_ENABLES_RING3MWAIT_BIT);
  109. if (c == &boot_cpu_data)
  110. ELF_HWCAP2 |= HWCAP2_RING3MWAIT;
  111. }
  112. /*
  113. * Early microcode releases for the Spectre v2 mitigation were broken.
  114. * Information taken from;
  115. * - https://newsroom.intel.com/wp-content/uploads/sites/11/2018/03/microcode-update-guidance.pdf
  116. * - https://kb.vmware.com/s/article/52345
  117. * - Microcode revisions observed in the wild
  118. * - Release note from 20180108 microcode release
  119. */
  120. struct sku_microcode {
  121. u32 vfm;
  122. u8 stepping;
  123. u32 microcode;
  124. };
  125. static const struct sku_microcode spectre_bad_microcodes[] = {
  126. { INTEL_KABYLAKE, 0x0B, 0x80 },
  127. { INTEL_KABYLAKE, 0x0A, 0x80 },
  128. { INTEL_KABYLAKE, 0x09, 0x80 },
  129. { INTEL_KABYLAKE_L, 0x0A, 0x80 },
  130. { INTEL_KABYLAKE_L, 0x09, 0x80 },
  131. { INTEL_SKYLAKE_X, 0x03, 0x0100013e },
  132. { INTEL_SKYLAKE_X, 0x04, 0x0200003c },
  133. { INTEL_BROADWELL, 0x04, 0x28 },
  134. { INTEL_BROADWELL_G, 0x01, 0x1b },
  135. { INTEL_BROADWELL_D, 0x02, 0x14 },
  136. { INTEL_BROADWELL_D, 0x03, 0x07000011 },
  137. { INTEL_BROADWELL_X, 0x01, 0x0b000025 },
  138. { INTEL_HASWELL_L, 0x01, 0x21 },
  139. { INTEL_HASWELL_G, 0x01, 0x18 },
  140. { INTEL_HASWELL, 0x03, 0x23 },
  141. { INTEL_HASWELL_X, 0x02, 0x3b },
  142. { INTEL_HASWELL_X, 0x04, 0x10 },
  143. { INTEL_IVYBRIDGE_X, 0x04, 0x42a },
  144. /* Observed in the wild */
  145. { INTEL_SANDYBRIDGE_X, 0x06, 0x61b },
  146. { INTEL_SANDYBRIDGE_X, 0x07, 0x712 },
  147. };
  148. static bool bad_spectre_microcode(struct cpuinfo_x86 *c)
  149. {
  150. int i;
  151. /*
  152. * We know that the hypervisor lie to us on the microcode version so
  153. * we may as well hope that it is running the correct version.
  154. */
  155. if (cpu_has(c, X86_FEATURE_HYPERVISOR))
  156. return false;
  157. for (i = 0; i < ARRAY_SIZE(spectre_bad_microcodes); i++) {
  158. if (c->x86_vfm == spectre_bad_microcodes[i].vfm &&
  159. c->x86_stepping == spectre_bad_microcodes[i].stepping)
  160. return (c->microcode <= spectre_bad_microcodes[i].microcode);
  161. }
  162. return false;
  163. }
  164. #define MSR_IA32_TME_ACTIVATE 0x982
  165. /* Helpers to access TME_ACTIVATE MSR */
  166. #define TME_ACTIVATE_LOCKED(x) (x & 0x1)
  167. #define TME_ACTIVATE_ENABLED(x) (x & 0x2)
  168. #define TME_ACTIVATE_KEYID_BITS(x) ((x >> 32) & 0xf) /* Bits 35:32 */
  169. static void detect_tme_early(struct cpuinfo_x86 *c)
  170. {
  171. u64 tme_activate;
  172. int keyid_bits;
  173. rdmsrl(MSR_IA32_TME_ACTIVATE, tme_activate);
  174. if (!TME_ACTIVATE_LOCKED(tme_activate) || !TME_ACTIVATE_ENABLED(tme_activate)) {
  175. pr_info_once("x86/tme: not enabled by BIOS\n");
  176. clear_cpu_cap(c, X86_FEATURE_TME);
  177. return;
  178. }
  179. pr_info_once("x86/tme: enabled by BIOS\n");
  180. keyid_bits = TME_ACTIVATE_KEYID_BITS(tme_activate);
  181. if (!keyid_bits)
  182. return;
  183. /*
  184. * KeyID bits are set by BIOS and can be present regardless
  185. * of whether the kernel is using them. They effectively lower
  186. * the number of physical address bits.
  187. *
  188. * Update cpuinfo_x86::x86_phys_bits accordingly.
  189. */
  190. c->x86_phys_bits -= keyid_bits;
  191. pr_info_once("x86/mktme: BIOS enabled: x86_phys_bits reduced by %d\n",
  192. keyid_bits);
  193. }
  194. void intel_unlock_cpuid_leafs(struct cpuinfo_x86 *c)
  195. {
  196. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  197. return;
  198. if (c->x86 < 6 || (c->x86 == 6 && c->x86_model < 0xd))
  199. return;
  200. /*
  201. * The BIOS can have limited CPUID to leaf 2, which breaks feature
  202. * enumeration. Unlock it and update the maximum leaf info.
  203. */
  204. if (msr_clear_bit(MSR_IA32_MISC_ENABLE, MSR_IA32_MISC_ENABLE_LIMIT_CPUID_BIT) > 0)
  205. c->cpuid_level = cpuid_eax(0);
  206. }
  207. static void early_init_intel(struct cpuinfo_x86 *c)
  208. {
  209. u64 misc_enable;
  210. if ((c->x86 == 0xf && c->x86_model >= 0x03) ||
  211. (c->x86 == 0x6 && c->x86_model >= 0x0e))
  212. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  213. if (c->x86 >= 6 && !cpu_has(c, X86_FEATURE_IA64))
  214. c->microcode = intel_get_microcode_revision();
  215. /* Now if any of them are set, check the blacklist and clear the lot */
  216. if ((cpu_has(c, X86_FEATURE_SPEC_CTRL) ||
  217. cpu_has(c, X86_FEATURE_INTEL_STIBP) ||
  218. cpu_has(c, X86_FEATURE_IBRS) || cpu_has(c, X86_FEATURE_IBPB) ||
  219. cpu_has(c, X86_FEATURE_STIBP)) && bad_spectre_microcode(c)) {
  220. pr_warn("Intel Spectre v2 broken microcode detected; disabling Speculation Control\n");
  221. setup_clear_cpu_cap(X86_FEATURE_IBRS);
  222. setup_clear_cpu_cap(X86_FEATURE_IBPB);
  223. setup_clear_cpu_cap(X86_FEATURE_STIBP);
  224. setup_clear_cpu_cap(X86_FEATURE_SPEC_CTRL);
  225. setup_clear_cpu_cap(X86_FEATURE_MSR_SPEC_CTRL);
  226. setup_clear_cpu_cap(X86_FEATURE_INTEL_STIBP);
  227. setup_clear_cpu_cap(X86_FEATURE_SSBD);
  228. setup_clear_cpu_cap(X86_FEATURE_SPEC_CTRL_SSBD);
  229. }
  230. /*
  231. * Atom erratum AAE44/AAF40/AAG38/AAH41:
  232. *
  233. * A race condition between speculative fetches and invalidating
  234. * a large page. This is worked around in microcode, but we
  235. * need the microcode to have already been loaded... so if it is
  236. * not, recommend a BIOS update and disable large pages.
  237. */
  238. if (c->x86_vfm == INTEL_ATOM_BONNELL && c->x86_stepping <= 2 &&
  239. c->microcode < 0x20e) {
  240. pr_warn("Atom PSE erratum detected, BIOS microcode update recommended\n");
  241. clear_cpu_cap(c, X86_FEATURE_PSE);
  242. }
  243. #ifdef CONFIG_X86_64
  244. set_cpu_cap(c, X86_FEATURE_SYSENTER32);
  245. #else
  246. /* Netburst reports 64 bytes clflush size, but does IO in 128 bytes */
  247. if (c->x86 == 15 && c->x86_cache_alignment == 64)
  248. c->x86_cache_alignment = 128;
  249. #endif
  250. /* CPUID workaround for 0F33/0F34 CPU */
  251. if (c->x86 == 0xF && c->x86_model == 0x3
  252. && (c->x86_stepping == 0x3 || c->x86_stepping == 0x4))
  253. c->x86_phys_bits = 36;
  254. /*
  255. * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
  256. * with P/T states and does not stop in deep C-states.
  257. *
  258. * It is also reliable across cores and sockets. (but not across
  259. * cabinets - we turn it off in that case explicitly.)
  260. */
  261. if (c->x86_power & (1 << 8)) {
  262. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  263. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
  264. }
  265. /* Penwell and Cloverview have the TSC which doesn't sleep on S3 */
  266. switch (c->x86_vfm) {
  267. case INTEL_ATOM_SALTWELL_MID:
  268. case INTEL_ATOM_SALTWELL_TABLET:
  269. case INTEL_ATOM_SILVERMONT_MID:
  270. case INTEL_ATOM_AIRMONT_NP:
  271. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC_S3);
  272. break;
  273. }
  274. /*
  275. * PAT is broken on early family 6 CPUs, the last of which
  276. * is "Yonah" where the erratum is named "AN7":
  277. *
  278. * Page with PAT (Page Attribute Table) Set to USWC
  279. * (Uncacheable Speculative Write Combine) While
  280. * Associated MTRR (Memory Type Range Register) Is UC
  281. * (Uncacheable) May Consolidate to UC
  282. *
  283. * Disable PAT and fall back to MTRR on these CPUs.
  284. */
  285. if (c->x86_vfm >= INTEL_PENTIUM_PRO &&
  286. c->x86_vfm <= INTEL_CORE_YONAH)
  287. clear_cpu_cap(c, X86_FEATURE_PAT);
  288. /*
  289. * If fast string is not enabled in IA32_MISC_ENABLE for any reason,
  290. * clear the fast string and enhanced fast string CPU capabilities.
  291. */
  292. if (c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xd)) {
  293. rdmsrl(MSR_IA32_MISC_ENABLE, misc_enable);
  294. if (!(misc_enable & MSR_IA32_MISC_ENABLE_FAST_STRING)) {
  295. pr_info("Disabled fast string operations\n");
  296. setup_clear_cpu_cap(X86_FEATURE_REP_GOOD);
  297. setup_clear_cpu_cap(X86_FEATURE_ERMS);
  298. }
  299. }
  300. /*
  301. * Intel Quark Core DevMan_001.pdf section 6.4.11
  302. * "The operating system also is required to invalidate (i.e., flush)
  303. * the TLB when any changes are made to any of the page table entries.
  304. * The operating system must reload CR3 to cause the TLB to be flushed"
  305. *
  306. * As a result, boot_cpu_has(X86_FEATURE_PGE) in arch/x86/include/asm/tlbflush.h
  307. * should be false so that __flush_tlb_all() causes CR3 instead of CR4.PGE
  308. * to be modified.
  309. */
  310. if (c->x86_vfm == INTEL_QUARK_X1000) {
  311. pr_info("Disabling PGE capability bit\n");
  312. setup_clear_cpu_cap(X86_FEATURE_PGE);
  313. }
  314. check_memory_type_self_snoop_errata(c);
  315. /*
  316. * Adjust the number of physical bits early because it affects the
  317. * valid bits of the MTRR mask registers.
  318. */
  319. if (cpu_has(c, X86_FEATURE_TME))
  320. detect_tme_early(c);
  321. }
  322. static void bsp_init_intel(struct cpuinfo_x86 *c)
  323. {
  324. resctrl_cpu_detect(c);
  325. }
  326. #ifdef CONFIG_X86_32
  327. /*
  328. * Early probe support logic for ppro memory erratum #50
  329. *
  330. * This is called before we do cpu ident work
  331. */
  332. int ppro_with_ram_bug(void)
  333. {
  334. /* Uses data from early_cpu_detect now */
  335. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  336. boot_cpu_data.x86 == 6 &&
  337. boot_cpu_data.x86_model == 1 &&
  338. boot_cpu_data.x86_stepping < 8) {
  339. pr_info("Pentium Pro with Errata#50 detected. Taking evasive action.\n");
  340. return 1;
  341. }
  342. return 0;
  343. }
  344. static void intel_smp_check(struct cpuinfo_x86 *c)
  345. {
  346. /* calling is from identify_secondary_cpu() ? */
  347. if (!c->cpu_index)
  348. return;
  349. /*
  350. * Mask B, Pentium, but not Pentium MMX
  351. */
  352. if (c->x86 == 5 &&
  353. c->x86_stepping >= 1 && c->x86_stepping <= 4 &&
  354. c->x86_model <= 3) {
  355. /*
  356. * Remember we have B step Pentia with bugs
  357. */
  358. WARN_ONCE(1, "WARNING: SMP operation may be unreliable"
  359. "with B stepping processors.\n");
  360. }
  361. }
  362. static int forcepae;
  363. static int __init forcepae_setup(char *__unused)
  364. {
  365. forcepae = 1;
  366. return 1;
  367. }
  368. __setup("forcepae", forcepae_setup);
  369. static void intel_workarounds(struct cpuinfo_x86 *c)
  370. {
  371. #ifdef CONFIG_X86_F00F_BUG
  372. /*
  373. * All models of Pentium and Pentium with MMX technology CPUs
  374. * have the F0 0F bug, which lets nonprivileged users lock up the
  375. * system. Announce that the fault handler will be checking for it.
  376. * The Quark is also family 5, but does not have the same bug.
  377. */
  378. clear_cpu_bug(c, X86_BUG_F00F);
  379. if (c->x86 == 5 && c->x86_model < 9) {
  380. static int f00f_workaround_enabled;
  381. set_cpu_bug(c, X86_BUG_F00F);
  382. if (!f00f_workaround_enabled) {
  383. pr_notice("Intel Pentium with F0 0F bug - workaround enabled.\n");
  384. f00f_workaround_enabled = 1;
  385. }
  386. }
  387. #endif
  388. /*
  389. * SEP CPUID bug: Pentium Pro reports SEP but doesn't have it until
  390. * model 3 mask 3
  391. */
  392. if ((c->x86<<8 | c->x86_model<<4 | c->x86_stepping) < 0x633)
  393. clear_cpu_cap(c, X86_FEATURE_SEP);
  394. /*
  395. * PAE CPUID issue: many Pentium M report no PAE but may have a
  396. * functionally usable PAE implementation.
  397. * Forcefully enable PAE if kernel parameter "forcepae" is present.
  398. */
  399. if (forcepae) {
  400. pr_warn("PAE forced!\n");
  401. set_cpu_cap(c, X86_FEATURE_PAE);
  402. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_NOW_UNRELIABLE);
  403. }
  404. /*
  405. * P4 Xeon erratum 037 workaround.
  406. * Hardware prefetcher may cause stale data to be loaded into the cache.
  407. */
  408. if ((c->x86 == 15) && (c->x86_model == 1) && (c->x86_stepping == 1)) {
  409. if (msr_set_bit(MSR_IA32_MISC_ENABLE,
  410. MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE_BIT) > 0) {
  411. pr_info("CPU: C0 stepping P4 Xeon detected.\n");
  412. pr_info("CPU: Disabling hardware prefetching (Erratum 037)\n");
  413. }
  414. }
  415. /*
  416. * See if we have a good local APIC by checking for buggy Pentia,
  417. * i.e. all B steppings and the C2 stepping of P54C when using their
  418. * integrated APIC (see 11AP erratum in "Pentium Processor
  419. * Specification Update").
  420. */
  421. if (boot_cpu_has(X86_FEATURE_APIC) && (c->x86<<8 | c->x86_model<<4) == 0x520 &&
  422. (c->x86_stepping < 0x6 || c->x86_stepping == 0xb))
  423. set_cpu_bug(c, X86_BUG_11AP);
  424. #ifdef CONFIG_X86_INTEL_USERCOPY
  425. /*
  426. * Set up the preferred alignment for movsl bulk memory moves
  427. */
  428. switch (c->x86) {
  429. case 4: /* 486: untested */
  430. break;
  431. case 5: /* Old Pentia: untested */
  432. break;
  433. case 6: /* PII/PIII only like movsl with 8-byte alignment */
  434. movsl_mask.mask = 7;
  435. break;
  436. case 15: /* P4 is OK down to 8-byte alignment */
  437. movsl_mask.mask = 7;
  438. break;
  439. }
  440. #endif
  441. intel_smp_check(c);
  442. }
  443. #else
  444. static void intel_workarounds(struct cpuinfo_x86 *c)
  445. {
  446. }
  447. #endif
  448. static void srat_detect_node(struct cpuinfo_x86 *c)
  449. {
  450. #ifdef CONFIG_NUMA
  451. unsigned node;
  452. int cpu = smp_processor_id();
  453. /* Don't do the funky fallback heuristics the AMD version employs
  454. for now. */
  455. node = numa_cpu_node(cpu);
  456. if (node == NUMA_NO_NODE || !node_online(node)) {
  457. /* reuse the value from init_cpu_to_node() */
  458. node = cpu_to_node(cpu);
  459. }
  460. numa_set_node(cpu, node);
  461. #endif
  462. }
  463. static void init_cpuid_fault(struct cpuinfo_x86 *c)
  464. {
  465. u64 msr;
  466. if (!rdmsrl_safe(MSR_PLATFORM_INFO, &msr)) {
  467. if (msr & MSR_PLATFORM_INFO_CPUID_FAULT)
  468. set_cpu_cap(c, X86_FEATURE_CPUID_FAULT);
  469. }
  470. }
  471. static void init_intel_misc_features(struct cpuinfo_x86 *c)
  472. {
  473. u64 msr;
  474. if (rdmsrl_safe(MSR_MISC_FEATURES_ENABLES, &msr))
  475. return;
  476. /* Clear all MISC features */
  477. this_cpu_write(msr_misc_features_shadow, 0);
  478. /* Check features and update capabilities and shadow control bits */
  479. init_cpuid_fault(c);
  480. probe_xeon_phi_r3mwait(c);
  481. msr = this_cpu_read(msr_misc_features_shadow);
  482. wrmsrl(MSR_MISC_FEATURES_ENABLES, msr);
  483. }
  484. static void split_lock_init(void);
  485. static void bus_lock_init(void);
  486. static void init_intel(struct cpuinfo_x86 *c)
  487. {
  488. early_init_intel(c);
  489. intel_workarounds(c);
  490. init_intel_cacheinfo(c);
  491. if (c->cpuid_level > 9) {
  492. unsigned eax = cpuid_eax(10);
  493. /* Check for version and the number of counters */
  494. if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
  495. set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
  496. }
  497. if (cpu_has(c, X86_FEATURE_XMM2))
  498. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  499. if (boot_cpu_has(X86_FEATURE_DS)) {
  500. unsigned int l1, l2;
  501. rdmsr(MSR_IA32_MISC_ENABLE, l1, l2);
  502. if (!(l1 & MSR_IA32_MISC_ENABLE_BTS_UNAVAIL))
  503. set_cpu_cap(c, X86_FEATURE_BTS);
  504. if (!(l1 & MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL))
  505. set_cpu_cap(c, X86_FEATURE_PEBS);
  506. }
  507. if (boot_cpu_has(X86_FEATURE_CLFLUSH) &&
  508. (c->x86_vfm == INTEL_CORE2_DUNNINGTON ||
  509. c->x86_vfm == INTEL_NEHALEM_EX ||
  510. c->x86_vfm == INTEL_WESTMERE_EX))
  511. set_cpu_bug(c, X86_BUG_CLFLUSH_MONITOR);
  512. if (boot_cpu_has(X86_FEATURE_MWAIT) &&
  513. (c->x86_vfm == INTEL_ATOM_GOLDMONT ||
  514. c->x86_vfm == INTEL_LUNARLAKE_M))
  515. set_cpu_bug(c, X86_BUG_MONITOR);
  516. #ifdef CONFIG_X86_64
  517. if (c->x86 == 15)
  518. c->x86_cache_alignment = c->x86_clflush_size * 2;
  519. if (c->x86 == 6)
  520. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  521. #else
  522. /*
  523. * Names for the Pentium II/Celeron processors
  524. * detectable only by also checking the cache size.
  525. * Dixon is NOT a Celeron.
  526. */
  527. if (c->x86 == 6) {
  528. unsigned int l2 = c->x86_cache_size;
  529. char *p = NULL;
  530. switch (c->x86_model) {
  531. case 5:
  532. if (l2 == 0)
  533. p = "Celeron (Covington)";
  534. else if (l2 == 256)
  535. p = "Mobile Pentium II (Dixon)";
  536. break;
  537. case 6:
  538. if (l2 == 128)
  539. p = "Celeron (Mendocino)";
  540. else if (c->x86_stepping == 0 || c->x86_stepping == 5)
  541. p = "Celeron-A";
  542. break;
  543. case 8:
  544. if (l2 == 128)
  545. p = "Celeron (Coppermine)";
  546. break;
  547. }
  548. if (p)
  549. strcpy(c->x86_model_id, p);
  550. }
  551. if (c->x86 == 15)
  552. set_cpu_cap(c, X86_FEATURE_P4);
  553. if (c->x86 == 6)
  554. set_cpu_cap(c, X86_FEATURE_P3);
  555. #endif
  556. /* Work around errata */
  557. srat_detect_node(c);
  558. init_ia32_feat_ctl(c);
  559. init_intel_misc_features(c);
  560. split_lock_init();
  561. bus_lock_init();
  562. intel_init_thermal(c);
  563. }
  564. #ifdef CONFIG_X86_32
  565. static unsigned int intel_size_cache(struct cpuinfo_x86 *c, unsigned int size)
  566. {
  567. /*
  568. * Intel PIII Tualatin. This comes in two flavours.
  569. * One has 256kb of cache, the other 512. We have no way
  570. * to determine which, so we use a boottime override
  571. * for the 512kb model, and assume 256 otherwise.
  572. */
  573. if ((c->x86 == 6) && (c->x86_model == 11) && (size == 0))
  574. size = 256;
  575. /*
  576. * Intel Quark SoC X1000 contains a 4-way set associative
  577. * 16K cache with a 16 byte cache line and 256 lines per tag
  578. */
  579. if ((c->x86 == 5) && (c->x86_model == 9))
  580. size = 16;
  581. return size;
  582. }
  583. #endif
  584. #define TLB_INST_4K 0x01
  585. #define TLB_INST_4M 0x02
  586. #define TLB_INST_2M_4M 0x03
  587. #define TLB_INST_ALL 0x05
  588. #define TLB_INST_1G 0x06
  589. #define TLB_DATA_4K 0x11
  590. #define TLB_DATA_4M 0x12
  591. #define TLB_DATA_2M_4M 0x13
  592. #define TLB_DATA_4K_4M 0x14
  593. #define TLB_DATA_1G 0x16
  594. #define TLB_DATA_1G_2M_4M 0x17
  595. #define TLB_DATA0_4K 0x21
  596. #define TLB_DATA0_4M 0x22
  597. #define TLB_DATA0_2M_4M 0x23
  598. #define STLB_4K 0x41
  599. #define STLB_4K_2M 0x42
  600. /*
  601. * All of leaf 0x2's one-byte TLB descriptors implies the same number of
  602. * entries for their respective TLB types. The 0x63 descriptor is an
  603. * exception: it implies 4 dTLB entries for 1GB pages 32 dTLB entries
  604. * for 2MB or 4MB pages. Encode descriptor 0x63 dTLB entry count for
  605. * 2MB/4MB pages here, as its count for dTLB 1GB pages is already at the
  606. * intel_tlb_table[] mapping.
  607. */
  608. #define TLB_0x63_2M_4M_ENTRIES 32
  609. static const struct _tlb_table intel_tlb_table[] = {
  610. { 0x01, TLB_INST_4K, 32, " TLB_INST 4 KByte pages, 4-way set associative" },
  611. { 0x02, TLB_INST_4M, 2, " TLB_INST 4 MByte pages, full associative" },
  612. { 0x03, TLB_DATA_4K, 64, " TLB_DATA 4 KByte pages, 4-way set associative" },
  613. { 0x04, TLB_DATA_4M, 8, " TLB_DATA 4 MByte pages, 4-way set associative" },
  614. { 0x05, TLB_DATA_4M, 32, " TLB_DATA 4 MByte pages, 4-way set associative" },
  615. { 0x0b, TLB_INST_4M, 4, " TLB_INST 4 MByte pages, 4-way set associative" },
  616. { 0x4f, TLB_INST_4K, 32, " TLB_INST 4 KByte pages" },
  617. { 0x50, TLB_INST_ALL, 64, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
  618. { 0x51, TLB_INST_ALL, 128, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
  619. { 0x52, TLB_INST_ALL, 256, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
  620. { 0x55, TLB_INST_2M_4M, 7, " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
  621. { 0x56, TLB_DATA0_4M, 16, " TLB_DATA0 4 MByte pages, 4-way set associative" },
  622. { 0x57, TLB_DATA0_4K, 16, " TLB_DATA0 4 KByte pages, 4-way associative" },
  623. { 0x59, TLB_DATA0_4K, 16, " TLB_DATA0 4 KByte pages, fully associative" },
  624. { 0x5a, TLB_DATA0_2M_4M, 32, " TLB_DATA0 2-MByte or 4 MByte pages, 4-way set associative" },
  625. { 0x5b, TLB_DATA_4K_4M, 64, " TLB_DATA 4 KByte and 4 MByte pages" },
  626. { 0x5c, TLB_DATA_4K_4M, 128, " TLB_DATA 4 KByte and 4 MByte pages" },
  627. { 0x5d, TLB_DATA_4K_4M, 256, " TLB_DATA 4 KByte and 4 MByte pages" },
  628. { 0x61, TLB_INST_4K, 48, " TLB_INST 4 KByte pages, full associative" },
  629. { 0x63, TLB_DATA_1G_2M_4M, 4, " TLB_DATA 1 GByte pages, 4-way set associative"
  630. " (plus 32 entries TLB_DATA 2 MByte or 4 MByte pages, not encoded here)" },
  631. { 0x6b, TLB_DATA_4K, 256, " TLB_DATA 4 KByte pages, 8-way associative" },
  632. { 0x6c, TLB_DATA_2M_4M, 128, " TLB_DATA 2 MByte or 4 MByte pages, 8-way associative" },
  633. { 0x6d, TLB_DATA_1G, 16, " TLB_DATA 1 GByte pages, fully associative" },
  634. { 0x76, TLB_INST_2M_4M, 8, " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
  635. { 0xb0, TLB_INST_4K, 128, " TLB_INST 4 KByte pages, 4-way set associative" },
  636. { 0xb1, TLB_INST_2M_4M, 4, " TLB_INST 2M pages, 4-way, 8 entries or 4M pages, 4-way entries" },
  637. { 0xb2, TLB_INST_4K, 64, " TLB_INST 4KByte pages, 4-way set associative" },
  638. { 0xb3, TLB_DATA_4K, 128, " TLB_DATA 4 KByte pages, 4-way set associative" },
  639. { 0xb4, TLB_DATA_4K, 256, " TLB_DATA 4 KByte pages, 4-way associative" },
  640. { 0xb5, TLB_INST_4K, 64, " TLB_INST 4 KByte pages, 8-way set associative" },
  641. { 0xb6, TLB_INST_4K, 128, " TLB_INST 4 KByte pages, 8-way set associative" },
  642. { 0xba, TLB_DATA_4K, 64, " TLB_DATA 4 KByte pages, 4-way associative" },
  643. { 0xc0, TLB_DATA_4K_4M, 8, " TLB_DATA 4 KByte and 4 MByte pages, 4-way associative" },
  644. { 0xc1, STLB_4K_2M, 1024, " STLB 4 KByte and 2 MByte pages, 8-way associative" },
  645. { 0xc2, TLB_DATA_2M_4M, 16, " TLB_DATA 2 MByte/4MByte pages, 4-way associative" },
  646. { 0xca, STLB_4K, 512, " STLB 4 KByte pages, 4-way associative" },
  647. { 0x00, 0, 0 }
  648. };
  649. static void intel_tlb_lookup(const unsigned char desc)
  650. {
  651. unsigned char k;
  652. if (desc == 0)
  653. return;
  654. /* look up this descriptor in the table */
  655. for (k = 0; intel_tlb_table[k].descriptor != desc &&
  656. intel_tlb_table[k].descriptor != 0; k++)
  657. ;
  658. if (intel_tlb_table[k].tlb_type == 0)
  659. return;
  660. switch (intel_tlb_table[k].tlb_type) {
  661. case STLB_4K:
  662. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  663. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  664. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  665. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  666. break;
  667. case STLB_4K_2M:
  668. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  669. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  670. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  671. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  672. if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
  673. tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
  674. if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
  675. tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
  676. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  677. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  678. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  679. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  680. break;
  681. case TLB_INST_ALL:
  682. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  683. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  684. if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
  685. tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
  686. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  687. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  688. break;
  689. case TLB_INST_4K:
  690. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  691. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  692. break;
  693. case TLB_INST_4M:
  694. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  695. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  696. break;
  697. case TLB_INST_2M_4M:
  698. if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
  699. tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
  700. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  701. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  702. break;
  703. case TLB_DATA_4K:
  704. case TLB_DATA0_4K:
  705. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  706. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  707. break;
  708. case TLB_DATA_4M:
  709. case TLB_DATA0_4M:
  710. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  711. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  712. break;
  713. case TLB_DATA_2M_4M:
  714. case TLB_DATA0_2M_4M:
  715. if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
  716. tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
  717. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  718. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  719. break;
  720. case TLB_DATA_4K_4M:
  721. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  722. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  723. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  724. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  725. break;
  726. case TLB_DATA_1G_2M_4M:
  727. if (tlb_lld_2m[ENTRIES] < TLB_0x63_2M_4M_ENTRIES)
  728. tlb_lld_2m[ENTRIES] = TLB_0x63_2M_4M_ENTRIES;
  729. if (tlb_lld_4m[ENTRIES] < TLB_0x63_2M_4M_ENTRIES)
  730. tlb_lld_4m[ENTRIES] = TLB_0x63_2M_4M_ENTRIES;
  731. fallthrough;
  732. case TLB_DATA_1G:
  733. if (tlb_lld_1g[ENTRIES] < intel_tlb_table[k].entries)
  734. tlb_lld_1g[ENTRIES] = intel_tlb_table[k].entries;
  735. break;
  736. }
  737. }
  738. static void intel_detect_tlb(struct cpuinfo_x86 *c)
  739. {
  740. int i, j, n;
  741. unsigned int regs[4];
  742. unsigned char *desc = (unsigned char *)regs;
  743. if (c->cpuid_level < 2)
  744. return;
  745. /* Number of times to iterate */
  746. n = cpuid_eax(2) & 0xFF;
  747. for (i = 0 ; i < n ; i++) {
  748. cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
  749. /* If bit 31 is set, this is an unknown format */
  750. for (j = 0 ; j < 4 ; j++)
  751. if (regs[j] & (1 << 31))
  752. regs[j] = 0;
  753. /* Byte 0 is level count, not a descriptor */
  754. for (j = 1 ; j < 16 ; j++)
  755. intel_tlb_lookup(desc[j]);
  756. }
  757. }
  758. static const struct cpu_dev intel_cpu_dev = {
  759. .c_vendor = "Intel",
  760. .c_ident = { "GenuineIntel" },
  761. #ifdef CONFIG_X86_32
  762. .legacy_models = {
  763. { .family = 4, .model_names =
  764. {
  765. [0] = "486 DX-25/33",
  766. [1] = "486 DX-50",
  767. [2] = "486 SX",
  768. [3] = "486 DX/2",
  769. [4] = "486 SL",
  770. [5] = "486 SX/2",
  771. [7] = "486 DX/2-WB",
  772. [8] = "486 DX/4",
  773. [9] = "486 DX/4-WB"
  774. }
  775. },
  776. { .family = 5, .model_names =
  777. {
  778. [0] = "Pentium 60/66 A-step",
  779. [1] = "Pentium 60/66",
  780. [2] = "Pentium 75 - 200",
  781. [3] = "OverDrive PODP5V83",
  782. [4] = "Pentium MMX",
  783. [7] = "Mobile Pentium 75 - 200",
  784. [8] = "Mobile Pentium MMX",
  785. [9] = "Quark SoC X1000",
  786. }
  787. },
  788. { .family = 6, .model_names =
  789. {
  790. [0] = "Pentium Pro A-step",
  791. [1] = "Pentium Pro",
  792. [3] = "Pentium II (Klamath)",
  793. [4] = "Pentium II (Deschutes)",
  794. [5] = "Pentium II (Deschutes)",
  795. [6] = "Mobile Pentium II",
  796. [7] = "Pentium III (Katmai)",
  797. [8] = "Pentium III (Coppermine)",
  798. [10] = "Pentium III (Cascades)",
  799. [11] = "Pentium III (Tualatin)",
  800. }
  801. },
  802. { .family = 15, .model_names =
  803. {
  804. [0] = "Pentium 4 (Unknown)",
  805. [1] = "Pentium 4 (Willamette)",
  806. [2] = "Pentium 4 (Northwood)",
  807. [4] = "Pentium 4 (Foster)",
  808. [5] = "Pentium 4 (Foster)",
  809. }
  810. },
  811. },
  812. .legacy_cache_size = intel_size_cache,
  813. #endif
  814. .c_detect_tlb = intel_detect_tlb,
  815. .c_early_init = early_init_intel,
  816. .c_bsp_init = bsp_init_intel,
  817. .c_init = init_intel,
  818. .c_x86_vendor = X86_VENDOR_INTEL,
  819. };
  820. cpu_dev_register(intel_cpu_dev);
  821. #undef pr_fmt
  822. #define pr_fmt(fmt) "x86/split lock detection: " fmt
  823. static const struct {
  824. const char *option;
  825. enum split_lock_detect_state state;
  826. } sld_options[] __initconst = {
  827. { "off", sld_off },
  828. { "warn", sld_warn },
  829. { "fatal", sld_fatal },
  830. { "ratelimit:", sld_ratelimit },
  831. };
  832. static struct ratelimit_state bld_ratelimit;
  833. static unsigned int sysctl_sld_mitigate = 1;
  834. static DEFINE_SEMAPHORE(buslock_sem, 1);
  835. #ifdef CONFIG_PROC_SYSCTL
  836. static struct ctl_table sld_sysctls[] = {
  837. {
  838. .procname = "split_lock_mitigate",
  839. .data = &sysctl_sld_mitigate,
  840. .maxlen = sizeof(unsigned int),
  841. .mode = 0644,
  842. .proc_handler = proc_douintvec_minmax,
  843. .extra1 = SYSCTL_ZERO,
  844. .extra2 = SYSCTL_ONE,
  845. },
  846. };
  847. static int __init sld_mitigate_sysctl_init(void)
  848. {
  849. register_sysctl_init("kernel", sld_sysctls);
  850. return 0;
  851. }
  852. late_initcall(sld_mitigate_sysctl_init);
  853. #endif
  854. static inline bool match_option(const char *arg, int arglen, const char *opt)
  855. {
  856. int len = strlen(opt), ratelimit;
  857. if (strncmp(arg, opt, len))
  858. return false;
  859. /*
  860. * Min ratelimit is 1 bus lock/sec.
  861. * Max ratelimit is 1000 bus locks/sec.
  862. */
  863. if (sscanf(arg, "ratelimit:%d", &ratelimit) == 1 &&
  864. ratelimit > 0 && ratelimit <= 1000) {
  865. ratelimit_state_init(&bld_ratelimit, HZ, ratelimit);
  866. ratelimit_set_flags(&bld_ratelimit, RATELIMIT_MSG_ON_RELEASE);
  867. return true;
  868. }
  869. return len == arglen;
  870. }
  871. static bool split_lock_verify_msr(bool on)
  872. {
  873. u64 ctrl, tmp;
  874. if (rdmsrl_safe(MSR_TEST_CTRL, &ctrl))
  875. return false;
  876. if (on)
  877. ctrl |= MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
  878. else
  879. ctrl &= ~MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
  880. if (wrmsrl_safe(MSR_TEST_CTRL, ctrl))
  881. return false;
  882. rdmsrl(MSR_TEST_CTRL, tmp);
  883. return ctrl == tmp;
  884. }
  885. static void __init sld_state_setup(void)
  886. {
  887. enum split_lock_detect_state state = sld_warn;
  888. char arg[20];
  889. int i, ret;
  890. if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
  891. !boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
  892. return;
  893. ret = cmdline_find_option(boot_command_line, "split_lock_detect",
  894. arg, sizeof(arg));
  895. if (ret >= 0) {
  896. for (i = 0; i < ARRAY_SIZE(sld_options); i++) {
  897. if (match_option(arg, ret, sld_options[i].option)) {
  898. state = sld_options[i].state;
  899. break;
  900. }
  901. }
  902. }
  903. sld_state = state;
  904. }
  905. static void __init __split_lock_setup(void)
  906. {
  907. if (!split_lock_verify_msr(false)) {
  908. pr_info("MSR access failed: Disabled\n");
  909. return;
  910. }
  911. rdmsrl(MSR_TEST_CTRL, msr_test_ctrl_cache);
  912. if (!split_lock_verify_msr(true)) {
  913. pr_info("MSR access failed: Disabled\n");
  914. return;
  915. }
  916. /* Restore the MSR to its cached value. */
  917. wrmsrl(MSR_TEST_CTRL, msr_test_ctrl_cache);
  918. setup_force_cpu_cap(X86_FEATURE_SPLIT_LOCK_DETECT);
  919. }
  920. /*
  921. * MSR_TEST_CTRL is per core, but we treat it like a per CPU MSR. Locking
  922. * is not implemented as one thread could undo the setting of the other
  923. * thread immediately after dropping the lock anyway.
  924. */
  925. static void sld_update_msr(bool on)
  926. {
  927. u64 test_ctrl_val = msr_test_ctrl_cache;
  928. if (on)
  929. test_ctrl_val |= MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
  930. wrmsrl(MSR_TEST_CTRL, test_ctrl_val);
  931. }
  932. static void split_lock_init(void)
  933. {
  934. /*
  935. * #DB for bus lock handles ratelimit and #AC for split lock is
  936. * disabled.
  937. */
  938. if (sld_state == sld_ratelimit) {
  939. split_lock_verify_msr(false);
  940. return;
  941. }
  942. if (cpu_model_supports_sld)
  943. split_lock_verify_msr(sld_state != sld_off);
  944. }
  945. static void __split_lock_reenable_unlock(struct work_struct *work)
  946. {
  947. sld_update_msr(true);
  948. up(&buslock_sem);
  949. }
  950. static DECLARE_DELAYED_WORK(sl_reenable_unlock, __split_lock_reenable_unlock);
  951. static void __split_lock_reenable(struct work_struct *work)
  952. {
  953. sld_update_msr(true);
  954. }
  955. static DECLARE_DELAYED_WORK(sl_reenable, __split_lock_reenable);
  956. /*
  957. * If a CPU goes offline with pending delayed work to re-enable split lock
  958. * detection then the delayed work will be executed on some other CPU. That
  959. * handles releasing the buslock_sem, but because it executes on a
  960. * different CPU probably won't re-enable split lock detection. This is a
  961. * problem on HT systems since the sibling CPU on the same core may then be
  962. * left running with split lock detection disabled.
  963. *
  964. * Unconditionally re-enable detection here.
  965. */
  966. static int splitlock_cpu_offline(unsigned int cpu)
  967. {
  968. sld_update_msr(true);
  969. return 0;
  970. }
  971. static void split_lock_warn(unsigned long ip)
  972. {
  973. struct delayed_work *work;
  974. int cpu;
  975. if (!current->reported_split_lock)
  976. pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
  977. current->comm, current->pid, ip);
  978. current->reported_split_lock = 1;
  979. if (sysctl_sld_mitigate) {
  980. /*
  981. * misery factor #1:
  982. * sleep 10ms before trying to execute split lock.
  983. */
  984. if (msleep_interruptible(10) > 0)
  985. return;
  986. /*
  987. * Misery factor #2:
  988. * only allow one buslocked disabled core at a time.
  989. */
  990. if (down_interruptible(&buslock_sem) == -EINTR)
  991. return;
  992. work = &sl_reenable_unlock;
  993. } else {
  994. work = &sl_reenable;
  995. }
  996. cpu = get_cpu();
  997. schedule_delayed_work_on(cpu, work, 2);
  998. /* Disable split lock detection on this CPU to make progress */
  999. sld_update_msr(false);
  1000. put_cpu();
  1001. }
  1002. bool handle_guest_split_lock(unsigned long ip)
  1003. {
  1004. if (sld_state == sld_warn) {
  1005. split_lock_warn(ip);
  1006. return true;
  1007. }
  1008. pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
  1009. current->comm, current->pid,
  1010. sld_state == sld_fatal ? "fatal" : "bogus", ip);
  1011. current->thread.error_code = 0;
  1012. current->thread.trap_nr = X86_TRAP_AC;
  1013. force_sig_fault(SIGBUS, BUS_ADRALN, NULL);
  1014. return false;
  1015. }
  1016. EXPORT_SYMBOL_GPL(handle_guest_split_lock);
  1017. static void bus_lock_init(void)
  1018. {
  1019. u64 val;
  1020. if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
  1021. return;
  1022. rdmsrl(MSR_IA32_DEBUGCTLMSR, val);
  1023. if ((boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
  1024. (sld_state == sld_warn || sld_state == sld_fatal)) ||
  1025. sld_state == sld_off) {
  1026. /*
  1027. * Warn and fatal are handled by #AC for split lock if #AC for
  1028. * split lock is supported.
  1029. */
  1030. val &= ~DEBUGCTLMSR_BUS_LOCK_DETECT;
  1031. } else {
  1032. val |= DEBUGCTLMSR_BUS_LOCK_DETECT;
  1033. }
  1034. wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
  1035. }
  1036. bool handle_user_split_lock(struct pt_regs *regs, long error_code)
  1037. {
  1038. if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
  1039. return false;
  1040. split_lock_warn(regs->ip);
  1041. return true;
  1042. }
  1043. void handle_bus_lock(struct pt_regs *regs)
  1044. {
  1045. switch (sld_state) {
  1046. case sld_off:
  1047. break;
  1048. case sld_ratelimit:
  1049. /* Enforce no more than bld_ratelimit bus locks/sec. */
  1050. while (!__ratelimit(&bld_ratelimit))
  1051. msleep(20);
  1052. /* Warn on the bus lock. */
  1053. fallthrough;
  1054. case sld_warn:
  1055. pr_warn_ratelimited("#DB: %s/%d took a bus_lock trap at address: 0x%lx\n",
  1056. current->comm, current->pid, regs->ip);
  1057. break;
  1058. case sld_fatal:
  1059. force_sig_fault(SIGBUS, BUS_ADRALN, NULL);
  1060. break;
  1061. }
  1062. }
  1063. /*
  1064. * CPU models that are known to have the per-core split-lock detection
  1065. * feature even though they do not enumerate IA32_CORE_CAPABILITIES.
  1066. */
  1067. static const struct x86_cpu_id split_lock_cpu_ids[] __initconst = {
  1068. X86_MATCH_VFM(INTEL_ICELAKE_X, 0),
  1069. X86_MATCH_VFM(INTEL_ICELAKE_L, 0),
  1070. X86_MATCH_VFM(INTEL_ICELAKE_D, 0),
  1071. {}
  1072. };
  1073. static void __init split_lock_setup(struct cpuinfo_x86 *c)
  1074. {
  1075. const struct x86_cpu_id *m;
  1076. u64 ia32_core_caps;
  1077. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  1078. return;
  1079. /* Check for CPUs that have support but do not enumerate it: */
  1080. m = x86_match_cpu(split_lock_cpu_ids);
  1081. if (m)
  1082. goto supported;
  1083. if (!cpu_has(c, X86_FEATURE_CORE_CAPABILITIES))
  1084. return;
  1085. /*
  1086. * Not all bits in MSR_IA32_CORE_CAPS are architectural, but
  1087. * MSR_IA32_CORE_CAPS_SPLIT_LOCK_DETECT is. All CPUs that set
  1088. * it have split lock detection.
  1089. */
  1090. rdmsrl(MSR_IA32_CORE_CAPS, ia32_core_caps);
  1091. if (ia32_core_caps & MSR_IA32_CORE_CAPS_SPLIT_LOCK_DETECT)
  1092. goto supported;
  1093. /* CPU is not in the model list and does not have the MSR bit: */
  1094. return;
  1095. supported:
  1096. cpu_model_supports_sld = true;
  1097. __split_lock_setup();
  1098. }
  1099. static void sld_state_show(void)
  1100. {
  1101. if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) &&
  1102. !boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
  1103. return;
  1104. switch (sld_state) {
  1105. case sld_off:
  1106. pr_info("disabled\n");
  1107. break;
  1108. case sld_warn:
  1109. if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
  1110. pr_info("#AC: crashing the kernel on kernel split_locks and warning on user-space split_locks\n");
  1111. if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  1112. "x86/splitlock", NULL, splitlock_cpu_offline) < 0)
  1113. pr_warn("No splitlock CPU offline handler\n");
  1114. } else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
  1115. pr_info("#DB: warning on user-space bus_locks\n");
  1116. }
  1117. break;
  1118. case sld_fatal:
  1119. if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
  1120. pr_info("#AC: crashing the kernel on kernel split_locks and sending SIGBUS on user-space split_locks\n");
  1121. } else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
  1122. pr_info("#DB: sending SIGBUS on user-space bus_locks%s\n",
  1123. boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) ?
  1124. " from non-WB" : "");
  1125. }
  1126. break;
  1127. case sld_ratelimit:
  1128. if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
  1129. pr_info("#DB: setting system wide bus lock rate limit to %u/sec\n", bld_ratelimit.burst);
  1130. break;
  1131. }
  1132. }
  1133. void __init sld_setup(struct cpuinfo_x86 *c)
  1134. {
  1135. split_lock_setup(c);
  1136. sld_state_setup();
  1137. sld_state_show();
  1138. }
  1139. #define X86_HYBRID_CPU_TYPE_ID_SHIFT 24
  1140. /**
  1141. * get_this_hybrid_cpu_type() - Get the type of this hybrid CPU
  1142. *
  1143. * Returns the CPU type [31:24] (i.e., Atom or Core) of a CPU in
  1144. * a hybrid processor. If the processor is not hybrid, returns 0.
  1145. */
  1146. u8 get_this_hybrid_cpu_type(void)
  1147. {
  1148. if (!cpu_feature_enabled(X86_FEATURE_HYBRID_CPU))
  1149. return 0;
  1150. return cpuid_eax(0x0000001a) >> X86_HYBRID_CPU_TYPE_ID_SHIFT;
  1151. }