Skip to content

Commit c50be1c

Browse files
committed
KVM: x86: Refactor __kvm_emulate_hypercall() into a macro
Rework __kvm_emulate_hypercall() into a macro so that completion of hypercalls that don't exit to userspace use direct function calls to the completion helper, i.e. don't trigger a retpoline when RETPOLINE=y. Opportunistically take the names of the input registers, as opposed to taking the input values, to preemptively dedup more of the calling code (TDX needs to use different registers). Use the direct GPR accessors to read values to avoid the pointless marking of the registers as available (KVM requires GPRs to always be available). Signed-off-by: Sean Christopherson <[email protected]> Reviewed-by: Binbin Wu <[email protected]> Reviewed-by: Kai Huang <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent d9eb86a commit c50be1c

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

arch/x86/kvm/x86.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9982,11 +9982,11 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)
99829982
return kvm_skip_emulated_instruction(vcpu);
99839983
}
99849984

9985-
int __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
9986-
unsigned long a0, unsigned long a1,
9987-
unsigned long a2, unsigned long a3,
9988-
int op_64_bit, int cpl,
9989-
int (*complete_hypercall)(struct kvm_vcpu *))
9985+
int ____kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
9986+
unsigned long a0, unsigned long a1,
9987+
unsigned long a2, unsigned long a3,
9988+
int op_64_bit, int cpl,
9989+
int (*complete_hypercall)(struct kvm_vcpu *))
99909990
{
99919991
unsigned long ret;
99929992

@@ -10079,31 +10079,21 @@ int __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
1007910079

1008010080
out:
1008110081
vcpu->run->hypercall.ret = ret;
10082-
return complete_hypercall(vcpu);
10082+
return 1;
1008310083
}
10084-
EXPORT_SYMBOL_GPL(__kvm_emulate_hypercall);
10084+
EXPORT_SYMBOL_GPL(____kvm_emulate_hypercall);
1008510085

1008610086
int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
1008710087
{
10088-
unsigned long nr, a0, a1, a2, a3;
10089-
int op_64_bit;
10090-
int cpl;
10091-
1009210088
if (kvm_xen_hypercall_enabled(vcpu->kvm))
1009310089
return kvm_xen_hypercall(vcpu);
1009410090

1009510091
if (kvm_hv_hypercall_enabled(vcpu))
1009610092
return kvm_hv_hypercall(vcpu);
1009710093

10098-
nr = kvm_rax_read(vcpu);
10099-
a0 = kvm_rbx_read(vcpu);
10100-
a1 = kvm_rcx_read(vcpu);
10101-
a2 = kvm_rdx_read(vcpu);
10102-
a3 = kvm_rsi_read(vcpu);
10103-
op_64_bit = is_64_bit_hypercall(vcpu);
10104-
cpl = kvm_x86_call(get_cpl)(vcpu);
10105-
10106-
return __kvm_emulate_hypercall(vcpu, nr, a0, a1, a2, a3, op_64_bit, cpl,
10094+
return __kvm_emulate_hypercall(vcpu, rax, rbx, rcx, rdx, rsi,
10095+
is_64_bit_hypercall(vcpu),
10096+
kvm_x86_call(get_cpl)(vcpu),
1010710097
complete_hypercall_exit);
1010810098
}
1010910099
EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);

arch/x86/kvm/x86.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,26 @@ static inline bool user_exit_on_hypercall(struct kvm *kvm, unsigned long hc_nr)
617617
return kvm->arch.hypercall_exit_enabled & BIT(hc_nr);
618618
}
619619

620-
int __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
621-
unsigned long a0, unsigned long a1,
622-
unsigned long a2, unsigned long a3,
623-
int op_64_bit, int cpl,
624-
int (*complete_hypercall)(struct kvm_vcpu *));
620+
int ____kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
621+
unsigned long a0, unsigned long a1,
622+
unsigned long a2, unsigned long a3,
623+
int op_64_bit, int cpl,
624+
int (*complete_hypercall)(struct kvm_vcpu *));
625+
626+
#define __kvm_emulate_hypercall(_vcpu, nr, a0, a1, a2, a3, op_64_bit, cpl, complete_hypercall) \
627+
({ \
628+
int __ret; \
629+
\
630+
__ret = ____kvm_emulate_hypercall(_vcpu, \
631+
kvm_##nr##_read(_vcpu), kvm_##a0##_read(_vcpu), \
632+
kvm_##a1##_read(_vcpu), kvm_##a2##_read(_vcpu), \
633+
kvm_##a3##_read(_vcpu), op_64_bit, cpl, \
634+
complete_hypercall); \
635+
\
636+
if (__ret > 0) \
637+
__ret = complete_hypercall(_vcpu); \
638+
__ret; \
639+
})
625640

626641
int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
627642

0 commit comments

Comments
 (0)