feat_ctl.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/tboot.h>
  3. #include <asm/cpu.h>
  4. #include <asm/cpufeature.h>
  5. #include <asm/msr-index.h>
  6. #include <asm/processor.h>
  7. #include <asm/vmx.h>
  8. #undef pr_fmt
  9. #define pr_fmt(fmt) "x86/cpu: " fmt
  10. #ifdef CONFIG_X86_VMX_FEATURE_NAMES
  11. enum vmx_feature_leafs {
  12. MISC_FEATURES = 0,
  13. PRIMARY_CTLS,
  14. SECONDARY_CTLS,
  15. TERTIARY_CTLS_LOW,
  16. TERTIARY_CTLS_HIGH,
  17. NR_VMX_FEATURE_WORDS,
  18. };
  19. #define VMX_F(x) BIT(VMX_FEATURE_##x & 0x1f)
  20. static void init_vmx_capabilities(struct cpuinfo_x86 *c)
  21. {
  22. u32 supported, funcs, ept, vpid, ign, low, high;
  23. BUILD_BUG_ON(NVMXINTS != NR_VMX_FEATURE_WORDS);
  24. /*
  25. * The high bits contain the allowed-1 settings, i.e. features that can
  26. * be turned on. The low bits contain the allowed-0 settings, i.e.
  27. * features that can be turned off. Ignore the allowed-0 settings,
  28. * if a feature can be turned on then it's supported.
  29. *
  30. * Use raw rdmsr() for primary processor controls and pin controls MSRs
  31. * as they exist on any CPU that supports VMX, i.e. we want the WARN if
  32. * the RDMSR faults.
  33. */
  34. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, ign, supported);
  35. c->vmx_capability[PRIMARY_CTLS] = supported;
  36. rdmsr_safe(MSR_IA32_VMX_PROCBASED_CTLS2, &ign, &supported);
  37. c->vmx_capability[SECONDARY_CTLS] = supported;
  38. /* All 64 bits of tertiary controls MSR are allowed-1 settings. */
  39. rdmsr_safe(MSR_IA32_VMX_PROCBASED_CTLS3, &low, &high);
  40. c->vmx_capability[TERTIARY_CTLS_LOW] = low;
  41. c->vmx_capability[TERTIARY_CTLS_HIGH] = high;
  42. rdmsr(MSR_IA32_VMX_PINBASED_CTLS, ign, supported);
  43. rdmsr_safe(MSR_IA32_VMX_VMFUNC, &ign, &funcs);
  44. /*
  45. * Except for EPT+VPID, which enumerates support for both in a single
  46. * MSR, low for EPT, high for VPID.
  47. */
  48. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, &ept, &vpid);
  49. /* Pin, EPT, VPID and VM-Func are merged into a single word. */
  50. WARN_ON_ONCE(supported >> 16);
  51. WARN_ON_ONCE(funcs >> 4);
  52. c->vmx_capability[MISC_FEATURES] = (supported & 0xffff) |
  53. ((vpid & 0x1) << 16) |
  54. ((funcs & 0xf) << 28);
  55. /* EPT bits are full on scattered and must be manually handled. */
  56. if (ept & VMX_EPT_EXECUTE_ONLY_BIT)
  57. c->vmx_capability[MISC_FEATURES] |= VMX_F(EPT_EXECUTE_ONLY);
  58. if (ept & VMX_EPT_AD_BIT)
  59. c->vmx_capability[MISC_FEATURES] |= VMX_F(EPT_AD);
  60. if (ept & VMX_EPT_1GB_PAGE_BIT)
  61. c->vmx_capability[MISC_FEATURES] |= VMX_F(EPT_1GB);
  62. if (ept & VMX_EPT_PAGE_WALK_5_BIT)
  63. c->vmx_capability[MISC_FEATURES] |= VMX_F(EPT_5LEVEL);
  64. /* Synthetic APIC features that are aggregates of multiple features. */
  65. if ((c->vmx_capability[PRIMARY_CTLS] & VMX_F(VIRTUAL_TPR)) &&
  66. (c->vmx_capability[SECONDARY_CTLS] & VMX_F(VIRT_APIC_ACCESSES)))
  67. c->vmx_capability[MISC_FEATURES] |= VMX_F(FLEXPRIORITY);
  68. if ((c->vmx_capability[PRIMARY_CTLS] & VMX_F(VIRTUAL_TPR)) &&
  69. (c->vmx_capability[SECONDARY_CTLS] & VMX_F(APIC_REGISTER_VIRT)) &&
  70. (c->vmx_capability[SECONDARY_CTLS] & VMX_F(VIRT_INTR_DELIVERY)) &&
  71. (c->vmx_capability[MISC_FEATURES] & VMX_F(POSTED_INTR)))
  72. c->vmx_capability[MISC_FEATURES] |= VMX_F(APICV);
  73. /* Set the synthetic cpufeatures to preserve /proc/cpuinfo's ABI. */
  74. if (c->vmx_capability[PRIMARY_CTLS] & VMX_F(VIRTUAL_TPR))
  75. set_cpu_cap(c, X86_FEATURE_TPR_SHADOW);
  76. if (c->vmx_capability[MISC_FEATURES] & VMX_F(FLEXPRIORITY))
  77. set_cpu_cap(c, X86_FEATURE_FLEXPRIORITY);
  78. if (c->vmx_capability[MISC_FEATURES] & VMX_F(VIRTUAL_NMIS))
  79. set_cpu_cap(c, X86_FEATURE_VNMI);
  80. if (c->vmx_capability[SECONDARY_CTLS] & VMX_F(EPT))
  81. set_cpu_cap(c, X86_FEATURE_EPT);
  82. if (c->vmx_capability[MISC_FEATURES] & VMX_F(EPT_AD))
  83. set_cpu_cap(c, X86_FEATURE_EPT_AD);
  84. if (c->vmx_capability[MISC_FEATURES] & VMX_F(VPID))
  85. set_cpu_cap(c, X86_FEATURE_VPID);
  86. }
  87. #endif /* CONFIG_X86_VMX_FEATURE_NAMES */
  88. static int __init nosgx(char *str)
  89. {
  90. setup_clear_cpu_cap(X86_FEATURE_SGX);
  91. return 0;
  92. }
  93. early_param("nosgx", nosgx);
  94. void init_ia32_feat_ctl(struct cpuinfo_x86 *c)
  95. {
  96. bool enable_sgx_kvm = false, enable_sgx_driver = false;
  97. bool tboot = tboot_enabled();
  98. bool enable_vmx;
  99. u64 msr;
  100. if (rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr)) {
  101. clear_cpu_cap(c, X86_FEATURE_VMX);
  102. clear_cpu_cap(c, X86_FEATURE_SGX);
  103. return;
  104. }
  105. enable_vmx = cpu_has(c, X86_FEATURE_VMX) &&
  106. IS_ENABLED(CONFIG_KVM_INTEL);
  107. if (cpu_has(c, X86_FEATURE_SGX) && IS_ENABLED(CONFIG_X86_SGX)) {
  108. /*
  109. * Separate out SGX driver enabling from KVM. This allows KVM
  110. * guests to use SGX even if the kernel SGX driver refuses to
  111. * use it. This happens if flexible Launch Control is not
  112. * available.
  113. */
  114. enable_sgx_driver = cpu_has(c, X86_FEATURE_SGX_LC);
  115. enable_sgx_kvm = enable_vmx && IS_ENABLED(CONFIG_X86_SGX_KVM);
  116. }
  117. if (msr & FEAT_CTL_LOCKED)
  118. goto update_caps;
  119. /*
  120. * Ignore whatever value BIOS left in the MSR to avoid enabling random
  121. * features or faulting on the WRMSR.
  122. */
  123. msr = FEAT_CTL_LOCKED;
  124. /*
  125. * Enable VMX if and only if the kernel may do VMXON at some point,
  126. * i.e. KVM is enabled, to avoid unnecessarily adding an attack vector
  127. * for the kernel, e.g. using VMX to hide malicious code.
  128. */
  129. if (enable_vmx) {
  130. msr |= FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
  131. if (tboot)
  132. msr |= FEAT_CTL_VMX_ENABLED_INSIDE_SMX;
  133. }
  134. if (enable_sgx_kvm || enable_sgx_driver) {
  135. msr |= FEAT_CTL_SGX_ENABLED;
  136. if (enable_sgx_driver)
  137. msr |= FEAT_CTL_SGX_LC_ENABLED;
  138. }
  139. wrmsrl(MSR_IA32_FEAT_CTL, msr);
  140. update_caps:
  141. set_cpu_cap(c, X86_FEATURE_MSR_IA32_FEAT_CTL);
  142. if (!cpu_has(c, X86_FEATURE_VMX))
  143. goto update_sgx;
  144. if ( (tboot && !(msr & FEAT_CTL_VMX_ENABLED_INSIDE_SMX)) ||
  145. (!tboot && !(msr & FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX))) {
  146. if (IS_ENABLED(CONFIG_KVM_INTEL))
  147. pr_err_once("VMX (%s TXT) disabled by BIOS\n",
  148. tboot ? "inside" : "outside");
  149. clear_cpu_cap(c, X86_FEATURE_VMX);
  150. } else {
  151. #ifdef CONFIG_X86_VMX_FEATURE_NAMES
  152. init_vmx_capabilities(c);
  153. #endif
  154. }
  155. update_sgx:
  156. if (!(msr & FEAT_CTL_SGX_ENABLED)) {
  157. if (enable_sgx_kvm || enable_sgx_driver)
  158. pr_err_once("SGX disabled or unsupported by BIOS.\n");
  159. clear_cpu_cap(c, X86_FEATURE_SGX);
  160. return;
  161. }
  162. /*
  163. * VMX feature bit may be cleared due to being disabled in BIOS,
  164. * in which case SGX virtualization cannot be supported either.
  165. */
  166. if (!cpu_has(c, X86_FEATURE_VMX) && enable_sgx_kvm) {
  167. pr_err_once("SGX virtualization disabled due to lack of VMX.\n");
  168. enable_sgx_kvm = 0;
  169. }
  170. if (!(msr & FEAT_CTL_SGX_LC_ENABLED) && enable_sgx_driver) {
  171. if (!enable_sgx_kvm) {
  172. pr_err_once("SGX Launch Control is locked. Disable SGX.\n");
  173. clear_cpu_cap(c, X86_FEATURE_SGX);
  174. } else {
  175. pr_err_once("SGX Launch Control is locked. Support SGX virtualization only.\n");
  176. clear_cpu_cap(c, X86_FEATURE_SGX_LC);
  177. }
  178. }
  179. }