Skip to content

Commit 14c2bf8

Browse files
weihuang-amdbonzini
authored andcommitted
KVM: SVM: Fix #GP handling for doubly-nested virtualization
Under the case of nested on nested (L0, L1, L2 are all hypervisors), we do not support emulation of the vVMLOAD/VMSAVE feature, the L0 hypervisor can inject the proper #VMEXIT to inform L1 of what is happening and L1 can avoid invoking the #GP workaround. For this reason we turns on guest VM's X86_FEATURE_SVME_ADDR_CHK bit for KVM running inside VM to receive the notification and change behavior. Similarly we check if vcpu is under guest mode before emulating the vmware-backdoor instructions. For the case of nested on nested, we let the guest handle it. Co-developed-by: Bandan Das <bsd@redhat.com> Signed-off-by: Bandan Das <bsd@redhat.com> Signed-off-by: Wei Huang <wei.huang2@amd.com> Tested-by: Maxim Levitsky <mlevitsk@redhat.com> Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com> Message-Id: <20210126081831.570253-5-wei.huang2@amd.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 3b9c723 commit 14c2bf8

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

‎arch/x86/kvm/svm/svm.c‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,9 @@ static __init void svm_set_cpu_caps(void)
933933

934934
if (npt_enabled)
935935
kvm_cpu_cap_set(X86_FEATURE_NPT);
936+
937+
/* Nested VM can receive #VMEXIT instead of triggering #GP */
938+
kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
936939
}
937940

938941
/* CPUID 0x80000008 */
@@ -2202,14 +2205,26 @@ static int svm_instr_opcode(struct kvm_vcpu *vcpu)
22022205

22032206
static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
22042207
{
2208+
const int guest_mode_exit_codes[] = {
2209+
[SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
2210+
[SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
2211+
[SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
2212+
};
22052213
int (*const svm_instr_handlers[])(struct vcpu_svm *svm) = {
22062214
[SVM_INSTR_VMRUN] = vmrun_interception,
22072215
[SVM_INSTR_VMLOAD] = vmload_interception,
22082216
[SVM_INSTR_VMSAVE] = vmsave_interception,
22092217
};
22102218
struct vcpu_svm *svm = to_svm(vcpu);
22112219

2212-
return svm_instr_handlers[opcode](svm);
2220+
if (is_guest_mode(vcpu)) {
2221+
svm->vmcb->control.exit_code = guest_mode_exit_codes[opcode];
2222+
svm->vmcb->control.exit_info_1 = 0;
2223+
svm->vmcb->control.exit_info_2 = 0;
2224+
2225+
return nested_svm_vmexit(svm);
2226+
} else
2227+
return svm_instr_handlers[opcode](svm);
22132228
}
22142229

22152230
/*
@@ -2244,7 +2259,8 @@ static int gp_interception(struct vcpu_svm *svm)
22442259
* VMware backdoor emulation on #GP interception only handles
22452260
* IN{S}, OUT{S}, and RDPMC.
22462261
*/
2247-
return kvm_emulate_instruction(vcpu,
2262+
if (!is_guest_mode(vcpu))
2263+
return kvm_emulate_instruction(vcpu,
22482264
EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
22492265
} else
22502266
return emulate_svm_instr(vcpu, opcode);

0 commit comments

Comments
 (0)