tsx.c 7.4 KB

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