0012-KVM-allow-compiling-out-SMM-support.patch 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. From 14fe6763b2618afb73a1109d7fda337cb06af0a2 Mon Sep 17 00:00:00 2001
  2. From: Paolo Bonzini <[email protected]>
  3. Date: Tue, 25 Oct 2022 15:47:23 +0300
  4. Subject: [PATCH] KVM: allow compiling out SMM support
  5. Some users of KVM implement the UEFI variable store through a paravirtual device
  6. that does not require the "SMM lockbox" component of edk2; allow them to
  7. compile out system management mode, which is not a full implementation
  8. especially in how it interacts with nested virtualization.
  9. Suggested-by: Sean Christopherson <[email protected]>
  10. Signed-off-by: Paolo Bonzini <[email protected]>
  11. ---
  12. arch/x86/kvm/Kconfig | 11 ++++++++++
  13. arch/x86/kvm/Makefile | 2 +-
  14. arch/x86/kvm/smm.h | 13 ++++++++++++
  15. arch/x86/kvm/svm/svm.c | 2 ++
  16. arch/x86/kvm/vmx/vmx.c | 2 ++
  17. arch/x86/kvm/x86.c | 21 +++++++++++++++++--
  18. tools/testing/selftests/kvm/x86_64/smm_test.c | 2 ++
  19. 7 files changed, 50 insertions(+), 3 deletions(-)
  20. diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig
  21. index e3cbd7706136..20d5aea868a4 100644
  22. --- a/arch/x86/kvm/Kconfig
  23. +++ b/arch/x86/kvm/Kconfig
  24. @@ -86,6 +86,17 @@ config KVM_INTEL
  25. To compile this as a module, choose M here: the module
  26. will be called kvm-intel.
  27. +config KVM_SMM
  28. + bool "System Management Mode emulation"
  29. + default y
  30. + depends on KVM
  31. + help
  32. + Provides support for KVM to emulate System Management Mode (SMM)
  33. + in virtual machines. This can be used by the virtual machine
  34. + firmware to implement UEFI secure boot.
  35. +
  36. + If unsure, say Y.
  37. +
  38. config X86_SGX_KVM
  39. bool "Software Guard eXtensions (SGX) Virtualization"
  40. depends on X86_SGX && KVM_INTEL
  41. diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
  42. index ec6f7656254b..6cf40f668277 100644
  43. --- a/arch/x86/kvm/Makefile
  44. +++ b/arch/x86/kvm/Makefile
  45. @@ -20,7 +20,7 @@ endif
  46. kvm-$(CONFIG_X86_64) += mmu/tdp_iter.o mmu/tdp_mmu.o
  47. kvm-$(CONFIG_KVM_XEN) += xen.o
  48. -kvm-y += smm.o
  49. +kvm-$(CONFIG_KVM_SMM) += smm.o
  50. kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \
  51. vmx/evmcs.o vmx/nested.o vmx/posted_intr.o
  52. diff --git a/arch/x86/kvm/smm.h b/arch/x86/kvm/smm.h
  53. index b0602a92e511..4c699fee4492 100644
  54. --- a/arch/x86/kvm/smm.h
  55. +++ b/arch/x86/kvm/smm.h
  56. @@ -8,6 +8,7 @@
  57. #define PUT_SMSTATE(type, buf, offset, val) \
  58. *(type *)((buf) + (offset) - 0x7e00) = val
  59. +#ifdef CONFIG_KVM_SMM
  60. static inline int kvm_inject_smi(struct kvm_vcpu *vcpu)
  61. {
  62. kvm_make_request(KVM_REQ_SMI, vcpu);
  63. @@ -23,5 +24,17 @@ void kvm_smm_changed(struct kvm_vcpu *vcpu, bool in_smm);
  64. void enter_smm(struct kvm_vcpu *vcpu);
  65. int emulator_leave_smm(struct x86_emulate_ctxt *ctxt);
  66. void process_smi(struct kvm_vcpu *vcpu);
  67. +#else
  68. +static inline int kvm_inject_smi(struct kvm_vcpu *vcpu) { return -ENOTTY; }
  69. +static inline bool is_smm(struct kvm_vcpu *vcpu) { return false; }
  70. +static inline void kvm_smm_changed(struct kvm_vcpu *vcpu, bool in_smm) { WARN_ON_ONCE(1); }
  71. +static inline void enter_smm(struct kvm_vcpu *vcpu) { WARN_ON_ONCE(1); }
  72. +static inline void process_smi(struct kvm_vcpu *vcpu) { WARN_ON_ONCE(1); }
  73. +
  74. +/*
  75. + * emulator_leave_smm is used as a function pointer, so the
  76. + * stub is defined in x86.c.
  77. + */
  78. +#endif
  79. #endif
  80. diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
  81. index f4ed4a02b109..a6807492bfae 100644
  82. --- a/arch/x86/kvm/svm/svm.c
  83. +++ b/arch/x86/kvm/svm/svm.c
  84. @@ -4151,6 +4151,8 @@ static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
  85. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  86. return false;
  87. case MSR_IA32_SMBASE:
  88. + if (!IS_ENABLED(CONFIG_KVM_SMM))
  89. + return false;
  90. /* SEV-ES guests do not support SMM, so report false */
  91. if (kvm && sev_es_guest(kvm))
  92. return false;
  93. diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
  94. index dc75de78ceb6..ce22860156c5 100644
  95. --- a/arch/x86/kvm/vmx/vmx.c
  96. +++ b/arch/x86/kvm/vmx/vmx.c
  97. @@ -6849,6 +6849,8 @@ static bool vmx_has_emulated_msr(struct kvm *kvm, u32 index)
  98. {
  99. switch (index) {
  100. case MSR_IA32_SMBASE:
  101. + if (!IS_ENABLED(CONFIG_KVM_SMM))
  102. + return false;
  103. /*
  104. * We cannot do SMM unless we can run the guest in big
  105. * real mode.
  106. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
  107. index 77e0ca43ee27..14ef42c6efbd 100644
  108. --- a/arch/x86/kvm/x86.c
  109. +++ b/arch/x86/kvm/x86.c
  110. @@ -3631,7 +3631,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  111. break;
  112. }
  113. case MSR_IA32_SMBASE:
  114. - if (!msr_info->host_initiated)
  115. + if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated)
  116. return 1;
  117. vcpu->arch.smbase = data;
  118. break;
  119. @@ -4047,7 +4047,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  120. msr_info->data = vcpu->arch.ia32_misc_enable_msr;
  121. break;
  122. case MSR_IA32_SMBASE:
  123. - if (!msr_info->host_initiated)
  124. + if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated)
  125. return 1;
  126. msr_info->data = vcpu->arch.smbase;
  127. break;
  128. @@ -4421,6 +4421,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  129. r |= KVM_X86_DISABLE_EXITS_MWAIT;
  130. break;
  131. case KVM_CAP_X86_SMM:
  132. + if (!IS_ENABLED(CONFIG_KVM_SMM))
  133. + break;
  134. +
  135. /* SMBASE is usually relocated above 1M on modern chipsets,
  136. * and SMM handlers might indeed rely on 4G segment limits,
  137. * so do not report SMM to be available if real mode is
  138. @@ -5146,6 +5149,12 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
  139. vcpu->arch.apic->sipi_vector = events->sipi_vector;
  140. if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
  141. + if (!IS_ENABLED(CONFIG_KVM_SMM) &&
  142. + (events->smi.smm ||
  143. + events->smi.pending ||
  144. + events->smi.smm_inside_nmi))
  145. + return -EINVAL;
  146. +
  147. if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
  148. kvm_x86_ops.nested_ops->leave_nested(vcpu);
  149. kvm_smm_changed(vcpu, events->smi.smm);
  150. @@ -8021,6 +8030,14 @@ static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
  151. return emul_to_vcpu(ctxt)->arch.hflags;
  152. }
  153. +#ifndef CONFIG_KVM_SMM
  154. +static int emulator_leave_smm(struct x86_emulate_ctxt *ctxt)
  155. +{
  156. + WARN_ON_ONCE(1);
  157. + return X86EMUL_UNHANDLEABLE;
  158. +}
  159. +#endif
  160. +
  161. static void emulator_triple_fault(struct x86_emulate_ctxt *ctxt)
  162. {
  163. kvm_make_request(KVM_REQ_TRIPLE_FAULT, emul_to_vcpu(ctxt));
  164. diff --git a/tools/testing/selftests/kvm/x86_64/smm_test.c b/tools/testing/selftests/kvm/x86_64/smm_test.c
  165. index 1f136a81858e..cb38a478e1f6 100644
  166. --- a/tools/testing/selftests/kvm/x86_64/smm_test.c
  167. +++ b/tools/testing/selftests/kvm/x86_64/smm_test.c
  168. @@ -137,6 +137,8 @@ int main(int argc, char *argv[])
  169. struct kvm_x86_state *state;
  170. int stage, stage_reported;
  171. + TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_SMM));
  172. +
  173. /* Create VM */
  174. vm = vm_create_with_one_vcpu(&vcpu, guest_code);
  175. --
  176. 2.38.1