thebeanogamer / rpms / qemu-kvm

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