bf143f
From 46e54544c3480658111d6f111d6c265dcea2e19b Mon Sep 17 00:00:00 2001
bf143f
From: Vitaly Kuznetsov <vkuznets@redhat.com>
bf143f
Date: Thu, 18 Aug 2022 17:01:12 +0200
bf143f
Subject: [PATCH 2/3] i386: reset KVM nested state upon CPU reset
bf143f
bf143f
RH-Author: Miroslav Rezanina <mrezanin@redhat.com>
bf143f
RH-MergeRequest: 219: Synchronize qemu-6.2.0-20.el8.1 build from RHEL 8.7 to RHEL 8.8
bf143f
RH-Bugzilla: 2125271
bf143f
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
bf143f
RH-Acked-by: Jon Maloy <jmaloy@redhat.com>
bf143f
RH-Commit: [1/2] de4db7bceb6baaf69aec8b0ae9aa8887aa869e15
bf143f
bf143f
Make sure env->nested_state is cleaned up when a vCPU is reset, it may
bf143f
be stale after an incoming migration, kvm_arch_put_registers() may
bf143f
end up failing or putting vCPU in a weird state.
bf143f
bf143f
Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
bf143f
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
bf143f
Message-Id: <20220818150113.479917-2-vkuznets@redhat.com>
bf143f
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
bf143f
(cherry picked from commit 3cafdb67504a34a0305260f0c86a73d5a3fb000b)
bf143f
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
bf143f
---
bf143f
 target/i386/kvm/kvm.c | 37 +++++++++++++++++++++++++++----------
bf143f
 1 file changed, 27 insertions(+), 10 deletions(-)
bf143f
bf143f
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
bf143f
index bd439e56ad..81d729dc40 100644
bf143f
--- a/target/i386/kvm/kvm.c
bf143f
+++ b/target/i386/kvm/kvm.c
bf143f
@@ -1615,6 +1615,30 @@ static void kvm_init_xsave(CPUX86State *env)
bf143f
            env->xsave_buf_len);
bf143f
 }
bf143f
 
bf143f
+static void kvm_init_nested_state(CPUX86State *env)
bf143f
+{
bf143f
+    struct kvm_vmx_nested_state_hdr *vmx_hdr;
bf143f
+    uint32_t size;
bf143f
+
bf143f
+    if (!env->nested_state) {
bf143f
+        return;
bf143f
+    }
bf143f
+
bf143f
+    size = env->nested_state->size;
bf143f
+
bf143f
+    memset(env->nested_state, 0, size);
bf143f
+    env->nested_state->size = size;
bf143f
+
bf143f
+    if (cpu_has_vmx(env)) {
bf143f
+        env->nested_state->format = KVM_STATE_NESTED_FORMAT_VMX;
bf143f
+        vmx_hdr = &env->nested_state->hdr.vmx;
bf143f
+        vmx_hdr->vmxon_pa = -1ull;
bf143f
+        vmx_hdr->vmcs12_pa = -1ull;
bf143f
+    } else if (cpu_has_svm(env)) {
bf143f
+        env->nested_state->format = KVM_STATE_NESTED_FORMAT_SVM;
bf143f
+    }
bf143f
+}
bf143f
+
bf143f
 int kvm_arch_init_vcpu(CPUState *cs)
bf143f
 {
bf143f
     struct {
bf143f
@@ -2042,19 +2066,10 @@ int kvm_arch_init_vcpu(CPUState *cs)
bf143f
         assert(max_nested_state_len >= offsetof(struct kvm_nested_state, data));
bf143f
 
bf143f
         if (cpu_has_vmx(env) || cpu_has_svm(env)) {
bf143f
-            struct kvm_vmx_nested_state_hdr *vmx_hdr;
bf143f
-
bf143f
             env->nested_state = g_malloc0(max_nested_state_len);
bf143f
             env->nested_state->size = max_nested_state_len;
bf143f
 
bf143f
-            if (cpu_has_vmx(env)) {
bf143f
-                env->nested_state->format = KVM_STATE_NESTED_FORMAT_VMX;
bf143f
-                vmx_hdr = &env->nested_state->hdr.vmx;
bf143f
-                vmx_hdr->vmxon_pa = -1ull;
bf143f
-                vmx_hdr->vmcs12_pa = -1ull;
bf143f
-            } else {
bf143f
-                env->nested_state->format = KVM_STATE_NESTED_FORMAT_SVM;
bf143f
-            }
bf143f
+            kvm_init_nested_state(env);
bf143f
         }
bf143f
     }
bf143f
 
bf143f
@@ -2117,6 +2132,8 @@ void kvm_arch_reset_vcpu(X86CPU *cpu)
bf143f
     /* enabled by default */
bf143f
     env->poll_control_msr = 1;
bf143f
 
bf143f
+    kvm_init_nested_state(env);
bf143f
+
bf143f
     sev_es_set_reset_vector(CPU(cpu));
bf143f
 }
bf143f
 
bf143f
-- 
bf143f
2.35.3
bf143f