0024-KVM-x86-SVM-use-smram-structs.patch 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Maxim Levitsky <[email protected]>
  3. Date: Wed, 3 Aug 2022 18:50:09 +0300
  4. Subject: [PATCH] KVM: x86: SVM: use smram structs
  5. This removes the last user of put_smstate/GET_SMSTATE so
  6. remove these functions as well.
  7. Also add a sanity check that we don't attempt to enter the SMM
  8. on non long mode capable guest CPU with a running nested guest.
  9. Signed-off-by: Maxim Levitsky <[email protected]>
  10. Signed-off-by: Thomas Lamprecht <[email protected]>
  11. ---
  12. arch/x86/include/asm/kvm_host.h | 6 ------
  13. arch/x86/kvm/svm/svm.c | 21 ++++++---------------
  14. 2 files changed, 6 insertions(+), 21 deletions(-)
  15. diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
  16. index d752fabde94ad2..d570ec522ebb55 100644
  17. --- a/arch/x86/include/asm/kvm_host.h
  18. +++ b/arch/x86/include/asm/kvm_host.h
  19. @@ -2077,12 +2077,6 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)
  20. #endif
  21. }
  22. -#define put_smstate(type, buf, offset, val) \
  23. - *(type *)((buf) + (offset) - 0x7e00) = val
  24. -
  25. -#define GET_SMSTATE(type, buf, offset) \
  26. - (*(type *)((buf) + (offset) - 0x7e00))
  27. -
  28. int kvm_cpu_dirty_log_size(void);
  29. int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages);
  30. diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
  31. index 688315d1dfabd1..7ca5e06878e19a 100644
  32. --- a/arch/x86/kvm/svm/svm.c
  33. +++ b/arch/x86/kvm/svm/svm.c
  34. @@ -4439,15 +4439,11 @@ static int svm_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram)
  35. struct kvm_host_map map_save;
  36. int ret;
  37. - char *smstate = (char *)smram;
  38. -
  39. if (!is_guest_mode(vcpu))
  40. return 0;
  41. - /* FED8h - SVM Guest */
  42. - put_smstate(u64, smstate, 0x7ed8, 1);
  43. - /* FEE0h - SVM Guest VMCB Physical Address */
  44. - put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
  45. + smram->smram64.svm_guest_flag = 1;
  46. + smram->smram64.svm_guest_vmcb_gpa = svm->nested.vmcb12_gpa;
  47. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  48. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  49. @@ -4486,28 +4482,23 @@ static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram)
  50. {
  51. struct vcpu_svm *svm = to_svm(vcpu);
  52. struct kvm_host_map map, map_save;
  53. - u64 saved_efer, vmcb12_gpa;
  54. struct vmcb *vmcb12;
  55. int ret;
  56. - const char *smstate = (const char *)smram;
  57. -
  58. if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
  59. return 0;
  60. /* Non-zero if SMI arrived while vCPU was in guest mode. */
  61. - if (!GET_SMSTATE(u64, smstate, 0x7ed8))
  62. + if (!smram->smram64.svm_guest_flag)
  63. return 0;
  64. if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
  65. return 1;
  66. - saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
  67. - if (!(saved_efer & EFER_SVME))
  68. + if (!(smram->smram64.efer & EFER_SVME))
  69. return 1;
  70. - vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
  71. - if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
  72. + if (kvm_vcpu_map(vcpu, gpa_to_gfn(smram->smram64.svm_guest_vmcb_gpa), &map) == -EINVAL)
  73. return 1;
  74. ret = 1;
  75. @@ -4533,7 +4524,7 @@ static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram)
  76. vmcb12 = map.hva;
  77. nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
  78. nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
  79. - ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, false);
  80. + ret = enter_svm_guest_mode(vcpu, smram->smram64.svm_guest_vmcb_gpa, vmcb12, false);
  81. if (ret)
  82. goto unmap_save;