Skip to content

Commit e913ef1

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: Split core of hypercall emulation to helper function
By necessity, TDX will use a different register ABI for hypercalls. Break out the core functionality so that it may be reused for TDX. Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Isaku Yamahata <[email protected]> Message-Id: <5134caa55ac3dec33fb2addb5545b52b3b52db02.1705965635.git.isaku.yamahata@intel.com> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent f9cecb3 commit e913ef1

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,10 @@ static inline void kvm_clear_apicv_inhibit(struct kvm *kvm,
21422142
kvm_set_or_clear_apicv_inhibit(kvm, reason, false);
21432143
}
21442144

2145+
unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
2146+
unsigned long a0, unsigned long a1,
2147+
unsigned long a2, unsigned long a3,
2148+
int op_64_bit, int cpl);
21452149
int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
21462150

21472151
int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,

arch/x86/kvm/x86.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10080,26 +10080,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)
1008010080
return kvm_skip_emulated_instruction(vcpu);
1008110081
}
1008210082

10083-
int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
10083+
unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
10084+
unsigned long a0, unsigned long a1,
10085+
unsigned long a2, unsigned long a3,
10086+
int op_64_bit, int cpl)
1008410087
{
10085-
unsigned long nr, a0, a1, a2, a3, ret;
10086-
int op_64_bit;
10087-
10088-
if (kvm_xen_hypercall_enabled(vcpu->kvm))
10089-
return kvm_xen_hypercall(vcpu);
10090-
10091-
if (kvm_hv_hypercall_enabled(vcpu))
10092-
return kvm_hv_hypercall(vcpu);
10093-
10094-
nr = kvm_rax_read(vcpu);
10095-
a0 = kvm_rbx_read(vcpu);
10096-
a1 = kvm_rcx_read(vcpu);
10097-
a2 = kvm_rdx_read(vcpu);
10098-
a3 = kvm_rsi_read(vcpu);
10088+
unsigned long ret;
1009910089

1010010090
trace_kvm_hypercall(nr, a0, a1, a2, a3);
1010110091

10102-
op_64_bit = is_64_bit_hypercall(vcpu);
1010310092
if (!op_64_bit) {
1010410093
nr &= 0xFFFFFFFF;
1010510094
a0 &= 0xFFFFFFFF;
@@ -10108,7 +10097,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
1010810097
a3 &= 0xFFFFFFFF;
1010910098
}
1011010099

10111-
if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
10100+
if (cpl) {
1011210101
ret = -KVM_EPERM;
1011310102
goto out;
1011410103
}
@@ -10169,18 +10158,49 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
1016910158

1017010159
WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ);
1017110160
vcpu->arch.complete_userspace_io = complete_hypercall_exit;
10161+
/* stat is incremented on completion. */
1017210162
return 0;
1017310163
}
1017410164
default:
1017510165
ret = -KVM_ENOSYS;
1017610166
break;
1017710167
}
10168+
1017810169
out:
10170+
++vcpu->stat.hypercalls;
10171+
return ret;
10172+
}
10173+
EXPORT_SYMBOL_GPL(__kvm_emulate_hypercall);
10174+
10175+
int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
10176+
{
10177+
unsigned long nr, a0, a1, a2, a3, ret;
10178+
int op_64_bit;
10179+
int cpl;
10180+
10181+
if (kvm_xen_hypercall_enabled(vcpu->kvm))
10182+
return kvm_xen_hypercall(vcpu);
10183+
10184+
if (kvm_hv_hypercall_enabled(vcpu))
10185+
return kvm_hv_hypercall(vcpu);
10186+
10187+
nr = kvm_rax_read(vcpu);
10188+
a0 = kvm_rbx_read(vcpu);
10189+
a1 = kvm_rcx_read(vcpu);
10190+
a2 = kvm_rdx_read(vcpu);
10191+
a3 = kvm_rsi_read(vcpu);
10192+
op_64_bit = is_64_bit_hypercall(vcpu);
10193+
cpl = static_call(kvm_x86_get_cpl)(vcpu);
10194+
10195+
ret = __kvm_emulate_hypercall(vcpu, nr, a0, a1, a2, a3, op_64_bit, cpl);
10196+
if (nr == KVM_HC_MAP_GPA_RANGE && !ret)
10197+
/* MAP_GPA tosses the request to the user space. */
10198+
return 0;
10199+
1017910200
if (!op_64_bit)
1018010201
ret = (u32)ret;
1018110202
kvm_rax_write(vcpu, ret);
1018210203

10183-
++vcpu->stat.hypercalls;
1018410204
return kvm_skip_emulated_instruction(vcpu);
1018510205
}
1018610206
EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);

0 commit comments

Comments
 (0)