feat_ctl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /* Synthetic APIC features that are aggregates of multiple features. */
  63. if ((c->vmx_capability[PRIMARY_CTLS] & VMX_F(VIRTUAL_TPR)) &&
  64. (c->vmx_capability[SECONDARY_CTLS] & VMX_F(VIRT_APIC_ACCESSES)))
  65. c->vmx_capability[MISC_FEATURES] |= VMX_F(FLEXPRIORITY);
  66. if ((c->vmx_capability[PRIMARY_CTLS] & VMX_F(VIRTUAL_TPR)) &&
  67. (c->vmx_capability[SECONDARY_CTLS] & VMX_F(APIC_REGISTER_VIRT)) &&
  68. (c->vmx_capability[SECONDARY_CTLS] & VMX_F(VIRT_INTR_DELIVERY)) &&
  69. (c->vmx_capability[MISC_FEATURES] & VMX_F(POSTED_INTR)))
  70. c->vmx_capability[MISC_FEATURES] |= VMX_F(APICV);
  71. /* Set the synthetic cpufeatures to preserve /proc/cpuinfo's ABI. */
  72. if (c->vmx_capability[PRIMARY_CTLS] & VMX_F(VIRTUAL_TPR))
  73. set_cpu_cap(c, X86_FEATURE_TPR_SHADOW);
  74. if (c->vmx_capability[MISC_FEATURES] & VMX_F(FLEXPRIORITY))
  75. set_cpu_cap(c, X86_FEATURE_FLEXPRIORITY);
  76. if (c->vmx_capability[MISC_FEATURES] & VMX_F(VIRTUAL_NMIS))
  77. set_cpu_cap(c, X86_FEATURE_VNMI);
  78. if (c->vmx_capability[SECONDARY_CTLS] & VMX_F(EPT))
  79. set_cpu_cap(c, X86_FEATURE_EPT);
  80. if (c->vmx_capability[MISC_FEATURES] & VMX_F(EPT_AD))
  81. set_cpu_cap(c, X86_FEATURE_EPT_AD);
  82. if (c->vmx_capability[MISC_FEATURES] & VMX_F(VPID))
  83. set_cpu_cap(c, X86_FEATURE_VPID);
  84. }
  85. #endif /* CONFIG_X86_VMX_FEATURE_NAMES */
  86. static int __init nosgx(char *str)
  87. {
  88. setup_clear_cpu_cap(X86_FEATURE_SGX);
  89. return 0;
  90. }
  91. early_param("nosgx", nosgx);
  92. void init_ia32_feat_ctl(struct cpuinfo_x86 *c)
  93. {
  94. bool enable_sgx_kvm = false, enable_sgx_driver = false;
  95. bool tboot = tboot_enabled();
  96. bool enable_vmx;
  97. u64 msr;
  98. if (rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr)) {
  99. clear_cpu_cap(c, X86_FEATURE_VMX);
  100. clear_cpu_cap(c, X86_FEATURE_SGX);
  101. return;
  102. }
  103. enable_vmx = cpu_has(c, X86_FEATURE_VMX) &&
  104. IS_ENABLED(CONFIG_KVM_INTEL);
  105. if (cpu_has(c, X86_FEATURE_SGX) && IS_ENABLED(CONFIG_X86_SGX)) {
  106. /*
  107. * Separate out SGX driver enabling from KVM. This allows KVM
  108. * guests to use SGX even if the kernel SGX driver refuses to
  109. * use it. This happens if flexible Launch Control is not
  110. * available.
  111. */
  112. enable_sgx_driver = cpu_has(c, X86_FEATURE_SGX_LC);
  113. enable_sgx_kvm = enable_vmx && IS_ENABLED(CONFIG_X86_SGX_KVM);
  114. }
  115. if (msr & FEAT_CTL_LOCKED)
  116. goto update_caps;
  117. /*
  118. * Ignore whatever value BIOS left in the MSR to avoid enabling random
  119. * features or faulting on the WRMSR.
  120. */
  121. msr = FEAT_CTL_LOCKED;
  122. /*
  123. * Enable VMX if and only if the kernel may do VMXON at some point,
  124. * i.e. KVM is enabled, to avoid unnecessarily adding an attack vector
  125. * for the kernel, e.g. using VMX to hide malicious code.
  126. */
  127. if (enable_vmx) {
  128. msr |= FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
  129. if (tboot)
  130. msr |= FEAT_CTL_VMX_ENABLED_INSIDE_SMX;
  131. }
  132. if (enable_sgx_kvm || enable_sgx_driver) {
  133. msr |= FEAT_CTL_SGX_ENABLED;
  134. if (enable_sgx_driver)
  135. msr |= FEAT_CTL_SGX_LC_ENABLED;
  136. }
  137. wrmsrl(MSR_IA32_FEAT_CTL, msr);
  138. update_caps:
  139. set_cpu_cap(c, X86_FEATURE_MSR_IA32_FEAT_CTL);
  140. if (!cpu_has(c, X86_FEATURE_VMX))
  141. goto update_sgx;
  142. if ( (tboot && !(msr & FEAT_CTL_VMX_ENABLED_INSIDE_SMX)) ||
  143. (!tboot && !(msr & FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX))) {
  144. if (IS_ENABLED(CONFIG_KVM_INTEL))
  145. pr_err_once("VMX (%s TXT) disabled by BIOS\n",
  146. tboot ? "inside" : "outside");
  147. clear_cpu_cap(c, X86_FEATURE_VMX);
  148. } else {
  149. #ifdef CONFIG_X86_VMX_FEATURE_NAMES
  150. init_vmx_capabilities(c);
  151. #endif
  152. }
  153. update_sgx:
  154. if (!(msr & FEAT_CTL_SGX_ENABLED)) {
  155. if (enable_sgx_kvm || enable_sgx_driver)
  156. pr_err_once("SGX disabled by BIOS.\n");
  157. clear_cpu_cap(c, X86_FEATURE_SGX);
  158. return;
  159. }
  160. /*
  161. * VMX feature bit may be cleared due to being disabled in BIOS,
  162. * in which case SGX virtualization cannot be supported either.
  163. */
  164. if (!cpu_has(c, X86_FEATURE_VMX) && enable_sgx_kvm) {
  165. pr_err_once("SGX virtualization disabled due to lack of VMX.\n");
  166. enable_sgx_kvm = 0;
  167. }
  168. if (!(msr & FEAT_CTL_SGX_LC_ENABLED) && enable_sgx_driver) {
  169. if (!enable_sgx_kvm) {
  170. pr_err_once("SGX Launch Control is locked. Disable SGX.\n");
  171. clear_cpu_cap(c, X86_FEATURE_SGX);
  172. } else {
  173. pr_err_once("SGX Launch Control is locked. Support SGX virtualization only.\n");
  174. clear_cpu_cap(c, X86_FEATURE_SGX_LC);
  175. }
  176. }
  177. }