Skip to content

Commit c4c083d

Browse files
Binbin Wubonzini
authored andcommitted
KVM: x86: Add a helper to check for user interception of KVM hypercalls
Add and use user_exit_on_hypercall() to check if userspace wants to handle a KVM hypercall instead of open-coding the logic everywhere. No functional change intended. Signed-off-by: Binbin Wu <[email protected]> Reviewed-by: Isaku Yamahata <[email protected]> Reviewed-by: Kai Huang <[email protected]> Reviewed-by: Xiaoyao Li <[email protected]> [sean: squash into one patch, keep explicit KVM_HC_MAP_GPA_RANGE check] Signed-off-by: Sean Christopherson <[email protected]> Reviewed-by: Tom Lendacky <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 9a1dfef commit c4c083d

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,7 +3627,7 @@ static int snp_begin_psc_msr(struct vcpu_svm *svm, u64 ghcb_msr)
36273627
return 1; /* resume guest */
36283628
}
36293629

3630-
if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) {
3630+
if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) {
36313631
set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR);
36323632
return 1; /* resume guest */
36333633
}
@@ -3717,7 +3717,7 @@ static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc)
37173717
bool huge;
37183718
u64 gfn;
37193719

3720-
if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) {
3720+
if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) {
37213721
snp_complete_psc(svm, VMGEXIT_PSC_ERROR_GENERIC);
37223722
return 1;
37233723
}

arch/x86/kvm/x86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10041,7 +10041,7 @@ unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
1004110041
u64 gpa = a0, npages = a1, attrs = a2;
1004210042

1004310043
ret = -KVM_ENOSYS;
10044-
if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE)))
10044+
if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE))
1004510045
break;
1004610046

1004710047
if (!PAGE_ALIGNED(gpa) || !npages ||

arch/x86/kvm/x86.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,4 +612,9 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
612612
unsigned int port, void *data, unsigned int count,
613613
int in);
614614

615+
static inline bool user_exit_on_hypercall(struct kvm *kvm, unsigned long hc_nr)
616+
{
617+
return kvm->arch.hypercall_exit_enabled & BIT(hc_nr);
618+
}
619+
615620
#endif

0 commit comments

Comments
 (0)