tsx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel Transactional Synchronization Extensions (TSX) control.
  4. *
  5. * Copyright (C) 2019-2021 Intel Corporation
  6. *
  7. * Author:
  8. * Pawan Gupta <[email protected]>
  9. */
  10. #include <linux/cpufeature.h>
  11. #include <asm/cmdline.h>
  12. #include "cpu.h"
  13. #undef pr_fmt
  14. #define pr_fmt(fmt) "tsx: " fmt
  15. enum tsx_ctrl_states tsx_ctrl_state __ro_after_init = TSX_CTRL_NOT_SUPPORTED;
  16. static void tsx_disable(void)
  17. {
  18. u64 tsx;
  19. rdmsrl(MSR_IA32_TSX_CTRL, tsx);
  20. /* Force all transactions to immediately abort */
  21. tsx |= TSX_CTRL_RTM_DISABLE;
  22. /*
  23. * Ensure TSX support is not enumerated in CPUID.
  24. * This is visible to userspace and will ensure they
  25. * do not waste resources trying TSX transactions that
  26. * will always abort.
  27. */
  28. tsx |= TSX_CTRL_CPUID_CLEAR;
  29. wrmsrl(MSR_IA32_TSX_CTRL, tsx);
  30. }
  31. static void tsx_enable(void)
  32. {
  33. u64 tsx;
  34. rdmsrl(MSR_IA32_TSX_CTRL, tsx);
  35. /* Enable the RTM feature in the cpu */
  36. tsx &= ~TSX_CTRL_RTM_DISABLE;
  37. /*
  38. * Ensure TSX support is enumerated in CPUID.
  39. * This is visible to userspace and will ensure they
  40. * can enumerate and use the TSX feature.
  41. */
  42. tsx &= ~TSX_CTRL_CPUID_CLEAR;
  43. wrmsrl(MSR_IA32_TSX_CTRL, tsx);
  44. }
  45. static enum tsx_ctrl_states x86_get_tsx_auto_mode(void)
  46. {
  47. if (boot_cpu_has_bug(X86_BUG_TAA))
  48. return TSX_CTRL_DISABLE;
  49. return TSX_CTRL_ENABLE;
  50. }
  51. /*
  52. * Disabling TSX is not a trivial business.
  53. *
  54. * First of all, there's a CPUID bit: X86_FEATURE_RTM_ALWAYS_ABORT
  55. * which says that TSX is practically disabled (all transactions are
  56. * aborted by default). When that bit is set, the kernel unconditionally
  57. * disables TSX.
  58. *
  59. * In order to do that, however, it needs to dance a bit:
  60. *
  61. * 1. The first method to disable it is through MSR_TSX_FORCE_ABORT and
  62. * the MSR is present only when *two* CPUID bits are set:
  63. *
  64. * - X86_FEATURE_RTM_ALWAYS_ABORT
  65. * - X86_FEATURE_TSX_FORCE_ABORT
  66. *
  67. * 2. The second method is for CPUs which do not have the above-mentioned
  68. * MSR: those use a different MSR - MSR_IA32_TSX_CTRL and disable TSX
  69. * through that one. Those CPUs can also have the initially mentioned
  70. * CPUID bit X86_FEATURE_RTM_ALWAYS_ABORT set and for those the same strategy
  71. * applies: TSX gets disabled unconditionally.
  72. *
  73. * When either of the two methods are present, the kernel disables TSX and
  74. * clears the respective RTM and HLE feature flags.
  75. *
  76. * An additional twist in the whole thing presents late microcode loading
  77. * which, when done, may cause for the X86_FEATURE_RTM_ALWAYS_ABORT CPUID
  78. * bit to be set after the update.
  79. *
  80. * A subsequent hotplug operation on any logical CPU except the BSP will
  81. * cause for the supported CPUID feature bits to get re-detected and, if
  82. * RTM and HLE get cleared all of a sudden, but, userspace did consult
  83. * them before the update, then funny explosions will happen. Long story
  84. * short: the kernel doesn't modify CPUID feature bits after booting.
  85. *
  86. * That's why, this function's call in init_intel() doesn't clear the
  87. * feature flags.
  88. */
  89. static void tsx_clear_cpuid(void)
  90. {
  91. u64 msr;
  92. /*
  93. * MSR_TFA_TSX_CPUID_CLEAR bit is only present when both CPUID
  94. * bits RTM_ALWAYS_ABORT and TSX_FORCE_ABORT are present.
  95. */
  96. if (boot_cpu_has(X86_FEATURE_RTM_ALWAYS_ABORT) &&
  97. boot_cpu_has(X86_FEATURE_TSX_FORCE_ABORT)) {
  98. rdmsrl(MSR_TSX_FORCE_ABORT, msr);
  99. msr |= MSR_TFA_TSX_CPUID_CLEAR;
  100. wrmsrl(MSR_TSX_FORCE_ABORT, msr);
  101. } else if (cpu_feature_enabled(X86_FEATURE_MSR_TSX_CTRL)) {
  102. rdmsrl(MSR_IA32_TSX_CTRL, msr);
  103. msr |= TSX_CTRL_CPUID_CLEAR;
  104. wrmsrl(MSR_IA32_TSX_CTRL, msr);
  105. }
  106. }
  107. /*
  108. * Disable TSX development mode
  109. *
  110. * When the microcode released in Feb 2022 is applied, TSX will be disabled by
  111. * default on some processors. MSR 0x122 (TSX_CTRL) and MSR 0x123
  112. * (IA32_MCU_OPT_CTRL) can be used to re-enable TSX for development, doing so is
  113. * not recommended for production deployments. In particular, applying MD_CLEAR
  114. * flows for mitigation of the Intel TSX Asynchronous Abort (TAA) transient
  115. * execution attack may not be effective on these processors when Intel TSX is
  116. * enabled with updated microcode.
  117. */
  118. static void tsx_dev_mode_disable(void)
  119. {
  120. u64 mcu_opt_ctrl;
  121. /* Check if RTM_ALLOW exists */
  122. if (!boot_cpu_has_bug(X86_BUG_TAA) ||
  123. !cpu_feature_enabled(X86_FEATURE_MSR_TSX_CTRL) ||
  124. !cpu_feature_enabled(X86_FEATURE_SRBDS_CTRL))
  125. return;
  126. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_opt_ctrl);
  127. if (mcu_opt_ctrl & RTM_ALLOW) {
  128. mcu_opt_ctrl &= ~RTM_ALLOW;
  129. wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_opt_ctrl);
  130. setup_force_cpu_cap(X86_FEATURE_RTM_ALWAYS_ABORT);
  131. }
  132. }
  133. void __init tsx_init(void)
  134. {
  135. char arg[5] = {};
  136. int ret;
  137. tsx_dev_mode_disable();
  138. /*
  139. * Hardware will always abort a TSX transaction when the CPUID bit
  140. * RTM_ALWAYS_ABORT is set. In this case, it is better not to enumerate
  141. * CPUID.RTM and CPUID.HLE bits. Clear them here.
  142. */
  143. if (boot_cpu_has(X86_FEATURE_RTM_ALWAYS_ABORT)) {
  144. tsx_ctrl_state = TSX_CTRL_RTM_ALWAYS_ABORT;
  145. tsx_clear_cpuid();
  146. setup_clear_cpu_cap(X86_FEATURE_RTM);
  147. setup_clear_cpu_cap(X86_FEATURE_HLE);
  148. return;
  149. }
  150. /*
  151. * TSX is controlled via MSR_IA32_TSX_CTRL. However, support for this
  152. * MSR is enumerated by ARCH_CAP_TSX_MSR bit in MSR_IA32_ARCH_CAPABILITIES.
  153. *
  154. * TSX control (aka MSR_IA32_TSX_CTRL) is only available after a
  155. * microcode update on CPUs that have their MSR_IA32_ARCH_CAPABILITIES
  156. * bit MDS_NO=1. CPUs with MDS_NO=0 are not planned to get
  157. * MSR_IA32_TSX_CTRL support even after a microcode update. Thus,
  158. * tsx= cmdline requests will do nothing on CPUs without
  159. * MSR_IA32_TSX_CTRL support.
  160. */
  161. if (x86_read_arch_cap_msr() & ARCH_CAP_TSX_CTRL_MSR) {
  162. setup_force_cpu_cap(X86_FEATURE_MSR_TSX_CTRL);
  163. } else {
  164. tsx_ctrl_state = TSX_CTRL_NOT_SUPPORTED;
  165. return;
  166. }
  167. ret = cmdline_find_option(boot_command_line, "tsx", arg, sizeof(arg));
  168. if (ret >= 0) {
  169. if (!strcmp(arg, "on")) {
  170. tsx_ctrl_state = TSX_CTRL_ENABLE;
  171. } else if (!strcmp(arg, "off")) {
  172. tsx_ctrl_state = TSX_CTRL_DISABLE;
  173. } else if (!strcmp(arg, "auto")) {
  174. tsx_ctrl_state = x86_get_tsx_auto_mode();
  175. } else {
  176. tsx_ctrl_state = TSX_CTRL_DISABLE;
  177. pr_err("invalid option, defaulting to off\n");
  178. }
  179. } else {
  180. /* tsx= not provided */
  181. if (IS_ENABLED(CONFIG_X86_INTEL_TSX_MODE_AUTO))
  182. tsx_ctrl_state = x86_get_tsx_auto_mode();
  183. else if (IS_ENABLED(CONFIG_X86_INTEL_TSX_MODE_OFF))
  184. tsx_ctrl_state = TSX_CTRL_DISABLE;
  185. else
  186. tsx_ctrl_state = TSX_CTRL_ENABLE;
  187. }
  188. if (tsx_ctrl_state == TSX_CTRL_DISABLE) {
  189. tsx_disable();
  190. /*
  191. * tsx_disable() will change the state of the RTM and HLE CPUID
  192. * bits. Clear them here since they are now expected to be not
  193. * set.
  194. */
  195. setup_clear_cpu_cap(X86_FEATURE_RTM);
  196. setup_clear_cpu_cap(X86_FEATURE_HLE);
  197. } else if (tsx_ctrl_state == TSX_CTRL_ENABLE) {
  198. /*
  199. * HW defaults TSX to be enabled at bootup.
  200. * We may still need the TSX enable support
  201. * during init for special cases like
  202. * kexec after TSX is disabled.
  203. */
  204. tsx_enable();
  205. /*
  206. * tsx_enable() will change the state of the RTM and HLE CPUID
  207. * bits. Force them here since they are now expected to be set.
  208. */
  209. setup_force_cpu_cap(X86_FEATURE_RTM);
  210. setup_force_cpu_cap(X86_FEATURE_HLE);
  211. }
  212. }
  213. void tsx_ap_init(void)
  214. {
  215. tsx_dev_mode_disable();
  216. if (tsx_ctrl_state == TSX_CTRL_ENABLE)
  217. tsx_enable();
  218. else if (tsx_ctrl_state == TSX_CTRL_DISABLE)
  219. tsx_disable();
  220. else if (tsx_ctrl_state == TSX_CTRL_RTM_ALWAYS_ABORT)
  221. /* See comment over that function for more details. */
  222. tsx_clear_cpuid();
  223. }