svm_onhyperv.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * KVM L1 hypervisor optimizations on Hyper-V for SVM.
  4. */
  5. #ifndef __ARCH_X86_KVM_SVM_ONHYPERV_H__
  6. #define __ARCH_X86_KVM_SVM_ONHYPERV_H__
  7. #include <asm/mshyperv.h>
  8. #if IS_ENABLED(CONFIG_HYPERV)
  9. #include "kvm_onhyperv.h"
  10. #include "svm/hyperv.h"
  11. static struct kvm_x86_ops svm_x86_ops;
  12. int svm_hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu);
  13. static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu)
  14. {
  15. struct hv_vmcb_enlightenments *hve = &to_svm(vcpu)->vmcb->control.hv_enlightenments;
  16. return ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB &&
  17. !!hve->hv_enlightenments_control.enlightened_npt_tlb;
  18. }
  19. static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
  20. {
  21. struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
  22. BUILD_BUG_ON(sizeof(vmcb->control.hv_enlightenments) !=
  23. sizeof(vmcb->control.reserved_sw));
  24. if (npt_enabled &&
  25. ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB)
  26. hve->hv_enlightenments_control.enlightened_npt_tlb = 1;
  27. if (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)
  28. hve->hv_enlightenments_control.msr_bitmap = 1;
  29. }
  30. static inline __init void svm_hv_hardware_setup(void)
  31. {
  32. if (npt_enabled &&
  33. ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB) {
  34. pr_info("kvm: Hyper-V enlightened NPT TLB flush enabled\n");
  35. svm_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
  36. svm_x86_ops.tlb_remote_flush_with_range =
  37. hv_remote_flush_tlb_with_range;
  38. }
  39. if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) {
  40. int cpu;
  41. pr_info("kvm: Hyper-V Direct TLB Flush enabled\n");
  42. for_each_online_cpu(cpu) {
  43. struct hv_vp_assist_page *vp_ap =
  44. hv_get_vp_assist_page(cpu);
  45. if (!vp_ap)
  46. continue;
  47. vp_ap->nested_control.features.directhypercall = 1;
  48. }
  49. svm_x86_ops.enable_direct_tlbflush =
  50. svm_hv_enable_direct_tlbflush;
  51. }
  52. }
  53. static inline void svm_hv_vmcb_dirty_nested_enlightenments(
  54. struct kvm_vcpu *vcpu)
  55. {
  56. struct vmcb *vmcb = to_svm(vcpu)->vmcb;
  57. struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
  58. if (hve->hv_enlightenments_control.msr_bitmap)
  59. vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
  60. }
  61. static inline void svm_hv_update_vp_id(struct vmcb *vmcb, struct kvm_vcpu *vcpu)
  62. {
  63. struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
  64. u32 vp_index = kvm_hv_get_vpindex(vcpu);
  65. if (hve->hv_vp_id != vp_index) {
  66. hve->hv_vp_id = vp_index;
  67. vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
  68. }
  69. }
  70. #else
  71. static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu)
  72. {
  73. return false;
  74. }
  75. static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
  76. {
  77. }
  78. static inline __init void svm_hv_hardware_setup(void)
  79. {
  80. }
  81. static inline void svm_hv_vmcb_dirty_nested_enlightenments(
  82. struct kvm_vcpu *vcpu)
  83. {
  84. }
  85. static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
  86. struct kvm_vcpu *vcpu)
  87. {
  88. }
  89. #endif /* CONFIG_HYPERV */
  90. #endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */