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