thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
586cba
From b84bb71165c97b475548edc1c07decccca53cf16 Mon Sep 17 00:00:00 2001
586cba
From: Vitaly Kuznetsov <vkuznets@redhat.com>
586cba
Date: Thu, 18 Aug 2022 17:01:12 +0200
586cba
Subject: [PATCH 1/2] i386: reset KVM nested state upon CPU reset
586cba
586cba
RH-Author: Vitaly Kuznetsov <vkuznets@redhat.com>
586cba
RH-MergeRequest: 217: i386: fix 'system_reset' when the VM is in VMX root operation
586cba
RH-Bugzilla: 2117546
586cba
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
586cba
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
586cba
RH-Acked-by: Peter Xu <peterx@redhat.com>
586cba
RH-Commit: [1/2] b329f053a027761f50187e4ca7fd6b50ac32d2ad
586cba
586cba
Make sure env->nested_state is cleaned up when a vCPU is reset, it may
586cba
be stale after an incoming migration, kvm_arch_put_registers() may
586cba
end up failing or putting vCPU in a weird state.
586cba
586cba
Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
586cba
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
586cba
Message-Id: <20220818150113.479917-2-vkuznets@redhat.com>
586cba
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
586cba
(cherry picked from commit 3cafdb67504a34a0305260f0c86a73d5a3fb000b)
586cba
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
586cba
---
586cba
 target/i386/kvm/kvm.c | 37 +++++++++++++++++++++++++++----------
586cba
 1 file changed, 27 insertions(+), 10 deletions(-)
586cba
586cba
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
586cba
index 6d1e009443..9feb98fe0b 100644
586cba
--- a/target/i386/kvm/kvm.c
586cba
+++ b/target/i386/kvm/kvm.c
586cba
@@ -1617,6 +1617,30 @@ static void kvm_init_xsave(CPUX86State *env)
586cba
            env->xsave_buf_len);
586cba
 }
586cba
 
586cba
+static void kvm_init_nested_state(CPUX86State *env)
586cba
+{
586cba
+    struct kvm_vmx_nested_state_hdr *vmx_hdr;
586cba
+    uint32_t size;
586cba
+
586cba
+    if (!env->nested_state) {
586cba
+        return;
586cba
+    }
586cba
+
586cba
+    size = env->nested_state->size;
586cba
+
586cba
+    memset(env->nested_state, 0, size);
586cba
+    env->nested_state->size = size;
586cba
+
586cba
+    if (cpu_has_vmx(env)) {
586cba
+        env->nested_state->format = KVM_STATE_NESTED_FORMAT_VMX;
586cba
+        vmx_hdr = &env->nested_state->hdr.vmx;
586cba
+        vmx_hdr->vmxon_pa = -1ull;
586cba
+        vmx_hdr->vmcs12_pa = -1ull;
586cba
+    } else if (cpu_has_svm(env)) {
586cba
+        env->nested_state->format = KVM_STATE_NESTED_FORMAT_SVM;
586cba
+    }
586cba
+}
586cba
+
586cba
 int kvm_arch_init_vcpu(CPUState *cs)
586cba
 {
586cba
     struct {
586cba
@@ -2044,19 +2068,10 @@ int kvm_arch_init_vcpu(CPUState *cs)
586cba
         assert(max_nested_state_len >= offsetof(struct kvm_nested_state, data));
586cba
 
586cba
         if (cpu_has_vmx(env) || cpu_has_svm(env)) {
586cba
-            struct kvm_vmx_nested_state_hdr *vmx_hdr;
586cba
-
586cba
             env->nested_state = g_malloc0(max_nested_state_len);
586cba
             env->nested_state->size = max_nested_state_len;
586cba
 
586cba
-            if (cpu_has_vmx(env)) {
586cba
-                env->nested_state->format = KVM_STATE_NESTED_FORMAT_VMX;
586cba
-                vmx_hdr = &env->nested_state->hdr.vmx;
586cba
-                vmx_hdr->vmxon_pa = -1ull;
586cba
-                vmx_hdr->vmcs12_pa = -1ull;
586cba
-            } else {
586cba
-                env->nested_state->format = KVM_STATE_NESTED_FORMAT_SVM;
586cba
-            }
586cba
+            kvm_init_nested_state(env);
586cba
         }
586cba
     }
586cba
 
586cba
@@ -2121,6 +2136,8 @@ void kvm_arch_reset_vcpu(X86CPU *cpu)
586cba
     /* enabled by default */
586cba
     env->poll_control_msr = 1;
586cba
 
586cba
+    kvm_init_nested_state(env);
586cba
+
586cba
     sev_es_set_reset_vector(CPU(cpu));
586cba
 }
586cba
 
586cba
-- 
586cba
2.31.1
586cba