Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5aead0

Browse files
tlendackybonzini
authored andcommitted
KVM: x86: Assume a 64-bit hypercall for guests with protected state
When processing a hypercall for a guest with protected state, currently SEV-ES guests, the guest CS segment register can't be checked to determine if the guest is in 64-bit mode. For an SEV-ES guest, it is expected that communication between the guest and the hypervisor is performed to shared memory using the GHCB. In order to use the GHCB, the guest must have been in long mode, otherwise writes by the guest to the GHCB would be encrypted and not be able to be comprehended by the hypervisor. Create a new helper function, is_64_bit_hypercall(), that assumes the guest is in 64-bit mode when the guest has protected state, and returns true, otherwise invoking is_64_bit_mode() to determine the mode. Update the hypercall related routines to use is_64_bit_hypercall() instead of is_64_bit_mode(). Add a WARN_ON_ONCE() to is_64_bit_mode() to catch occurences of calls to this helper function for a guest running with protected state. Fixes: f1c6366 ("KVM: SVM: Add required changes to support intercepts under SEV-ES") Reported-by: Sean Christopherson <[email protected]> Signed-off-by: Tom Lendacky <[email protected]> Message-Id: <e0b20c770c9d0d1403f23d83e785385104211f74.1621878537.git.thomas.lendacky@amd.com> Cc: [email protected] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent b768f60 commit b5aead0

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

arch/x86/kvm/hyperv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,7 @@ static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
20222022
{
20232023
bool longmode;
20242024

2025-
longmode = is_64_bit_mode(vcpu);
2025+
longmode = is_64_bit_hypercall(vcpu);
20262026
if (longmode)
20272027
kvm_rax_write(vcpu, result);
20282028
else {
@@ -2171,7 +2171,7 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
21712171
}
21722172

21732173
#ifdef CONFIG_X86_64
2174-
if (is_64_bit_mode(vcpu)) {
2174+
if (is_64_bit_hypercall(vcpu)) {
21752175
hc.param = kvm_rcx_read(vcpu);
21762176
hc.ingpa = kvm_rdx_read(vcpu);
21772177
hc.outgpa = kvm_r8_read(vcpu);

arch/x86/kvm/x86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8848,7 +8848,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
88488848

88498849
trace_kvm_hypercall(nr, a0, a1, a2, a3);
88508850

8851-
op_64_bit = is_64_bit_mode(vcpu);
8851+
op_64_bit = is_64_bit_hypercall(vcpu);
88528852
if (!op_64_bit) {
88538853
nr &= 0xFFFFFFFF;
88548854
a0 &= 0xFFFFFFFF;

arch/x86/kvm/x86.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,24 @@ static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
153153
{
154154
int cs_db, cs_l;
155155

156+
WARN_ON_ONCE(vcpu->arch.guest_state_protected);
157+
156158
if (!is_long_mode(vcpu))
157159
return false;
158160
static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
159161
return cs_l;
160162
}
161163

164+
static inline bool is_64_bit_hypercall(struct kvm_vcpu *vcpu)
165+
{
166+
/*
167+
* If running with protected guest state, the CS register is not
168+
* accessible. The hypercall register values will have had to been
169+
* provided in 64-bit mode, so assume the guest is in 64-bit.
170+
*/
171+
return vcpu->arch.guest_state_protected || is_64_bit_mode(vcpu);
172+
}
173+
162174
static inline bool x86_exception_has_error_code(unsigned int vector)
163175
{
164176
static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |

arch/x86/kvm/xen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
698698
kvm_hv_hypercall_enabled(vcpu))
699699
return kvm_hv_hypercall(vcpu);
700700

701-
longmode = is_64_bit_mode(vcpu);
701+
longmode = is_64_bit_hypercall(vcpu);
702702
if (!longmode) {
703703
params[0] = (u32)kvm_rbx_read(vcpu);
704704
params[1] = (u32)kvm_rcx_read(vcpu);

0 commit comments

Comments
 (0)