Skip to content

Commit c98842b

Browse files
vittyvksean-jc
authored andcommitted
KVM: nVMX: Introduce accessor to get Hyper-V eVMCS pointer
There's a number of 'vmx->nested.hv_evmcs' accesses in nested.c, introduce 'nested_vmx_evmcs()' accessor to hide them all in !CONFIG_KVM_HYPERV case. No functional change intended. Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com> Tested-by: Jeremi Piotrowski <jpiotrowski@linux.microsoft.com> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Link: https://lore.kernel.org/r/20231205103630.1391318-15-vkuznets@redhat.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 453e42b commit c98842b

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

arch/x86/kvm/vmx/hyperv.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ static inline bool nested_vmx_is_evmptr12_set(struct vcpu_vmx *vmx)
3737
return evmptr_is_set(vmx->nested.hv_evmcs_vmptr);
3838
}
3939

40+
static inline struct hv_enlightened_vmcs *nested_vmx_evmcs(struct vcpu_vmx *vmx)
41+
{
42+
return vmx->nested.hv_evmcs;
43+
}
44+
4045
static inline bool guest_cpuid_has_evmcs(struct kvm_vcpu *vcpu)
4146
{
4247
/*
@@ -75,6 +80,11 @@ static inline bool nested_vmx_is_evmptr12_set(struct vcpu_vmx *vmx)
7580
{
7681
return false;
7782
}
83+
84+
static inline struct hv_enlightened_vmcs *nested_vmx_evmcs(struct vcpu_vmx *vmx)
85+
{
86+
return NULL;
87+
}
7888
#endif
7989

8090
#endif /* __KVM_X86_VMX_HYPERV_H */

arch/x86/kvm/vmx/nested.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ static bool nested_evmcs_handle_vmclear(struct kvm_vcpu *vcpu, gpa_t vmptr)
263263
!evmptr_is_valid(nested_get_evmptr(vcpu)))
264264
return false;
265265

266-
if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr)
266+
if (nested_vmx_evmcs(vmx) && vmptr == vmx->nested.hv_evmcs_vmptr)
267267
nested_release_evmcs(vcpu);
268268

269269
return true;
@@ -601,7 +601,6 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
601601
int msr;
602602
unsigned long *msr_bitmap_l1;
603603
unsigned long *msr_bitmap_l0 = vmx->nested.vmcs02.msr_bitmap;
604-
struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
605604
struct kvm_host_map *map = &vmx->nested.msr_bitmap_map;
606605

607606
/* Nothing to do if the MSR bitmap is not in use. */
@@ -617,10 +616,13 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
617616
* - Nested hypervisor (L1) has enabled 'Enlightened MSR Bitmap' feature
618617
* and tells KVM (L0) there were no changes in MSR bitmap for L2.
619618
*/
620-
if (!vmx->nested.force_msr_bitmap_recalc && evmcs &&
621-
evmcs->hv_enlightenments_control.msr_bitmap &&
622-
evmcs->hv_clean_fields & HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP)
623-
return true;
619+
if (!vmx->nested.force_msr_bitmap_recalc) {
620+
struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx);
621+
622+
if (evmcs && evmcs->hv_enlightenments_control.msr_bitmap &&
623+
evmcs->hv_clean_fields & HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP)
624+
return true;
625+
}
624626

625627
if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
626628
return false;
@@ -1603,7 +1605,7 @@ static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields
16031605
{
16041606
#ifdef CONFIG_KVM_HYPERV
16051607
struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1606-
struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1608+
struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx);
16071609
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(&vmx->vcpu);
16081610

16091611
/* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
@@ -1851,7 +1853,7 @@ static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
18511853
{
18521854
#ifdef CONFIG_KVM_HYPERV
18531855
struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1854-
struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1856+
struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx);
18551857

18561858
/*
18571859
* Should not be changed by KVM:
@@ -2438,7 +2440,7 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0
24382440

24392441
static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
24402442
{
2441-
struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2443+
struct hv_enlightened_vmcs *hv_evmcs = nested_vmx_evmcs(vmx);
24422444

24432445
if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
24442446
HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
@@ -2570,15 +2572,15 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
25702572
enum vm_entry_failure_code *entry_failure_code)
25712573
{
25722574
struct vcpu_vmx *vmx = to_vmx(vcpu);
2575+
struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx);
25732576
bool load_guest_pdptrs_vmcs12 = false;
25742577

25752578
if (vmx->nested.dirty_vmcs12 || nested_vmx_is_evmptr12_valid(vmx)) {
25762579
prepare_vmcs02_rare(vmx, vmcs12);
25772580
vmx->nested.dirty_vmcs12 = false;
25782581

25792582
load_guest_pdptrs_vmcs12 = !nested_vmx_is_evmptr12_valid(vmx) ||
2580-
!(vmx->nested.hv_evmcs->hv_clean_fields &
2581-
HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
2583+
!(evmcs->hv_clean_fields & HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
25822584
}
25832585

25842586
if (vmx->nested.nested_run_pending &&
@@ -2700,8 +2702,7 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
27002702
* here.
27012703
*/
27022704
if (nested_vmx_is_evmptr12_valid(vmx))
2703-
vmx->nested.hv_evmcs->hv_clean_fields |=
2704-
HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2705+
evmcs->hv_clean_fields |= HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
27052706

27062707
return 0;
27072708
}
@@ -3626,7 +3627,9 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
36263627
return nested_vmx_failInvalid(vcpu);
36273628

36283629
if (nested_vmx_is_evmptr12_valid(vmx)) {
3629-
copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields);
3630+
struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx);
3631+
3632+
copy_enlightened_to_vmcs12(vmx, evmcs->hv_clean_fields);
36303633
/* Enlightened VMCS doesn't have launch state */
36313634
vmcs12->launch_state = !launch;
36323635
} else if (enable_shadow_vmcs) {
@@ -5428,7 +5431,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
54285431
return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
54295432

54305433
/* Read the field, zero-extended to a u64 value */
5431-
value = evmcs_read_any(vmx->nested.hv_evmcs, field, offset);
5434+
value = evmcs_read_any(nested_vmx_evmcs(vmx), field, offset);
54325435
}
54335436

54345437
/*

0 commit comments

Comments
 (0)