All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [PULL 10/53] KVM: add support for AMD nested live migration
Date: Mon,  6 Jul 2020 12:41:12 -0400	[thread overview]
Message-ID: <20200706164155.24696-11-pbonzini@redhat.com> (raw)
In-Reply-To: <20200706164155.24696-1-pbonzini@redhat.com>

Support for nested guest live migration is part of Linux 5.8, add the
corresponding code to QEMU.  The migration format consists of a few
flags, is an opaque 4k blob.

The blob is in VMCB format (the control area represents the L1 VMCB
control fields, the save area represents the pre-vmentry state; KVM does
not use the host save area since the AMD manual allows that) but QEMU
does not really care about that.  However, the flags need to be
copied to hflags/hflags2 and back.

In addition, support for retrieving and setting the AMD nested virtualization
states allows the L1 guest to be reset while running a nested guest, but
a small bug in CPU reset needs to be fixed for that to work.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 target/i386/cpu.c     |  1 +
 target/i386/cpu.h     |  5 +++++
 target/i386/kvm.c     | 42 ++++++++++++++++++++++++++++++++++--------
 target/i386/machine.c | 31 ++++++++++++++++++++++++++++++-
 4 files changed, 70 insertions(+), 9 deletions(-)

diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index 36cbd3d027..f1cbac2fb5 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -5987,6 +5987,7 @@ static void x86_cpu_reset(DeviceState *dev)
     /* init to reset state */
 
     env->hflags2 |= HF2_GIF_MASK;
+    env->hflags &= ~HF_GUEST_MASK;
 
     cpu_x86_update_cr0(env, 0x60000010);
     env->a20_mask = ~0x0;
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index 06b2e3a5c6..9284f96896 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -2118,6 +2118,11 @@ static inline bool cpu_has_vmx(CPUX86State *env)
     return env->features[FEAT_1_ECX] & CPUID_EXT_VMX;
 }
 
+static inline bool cpu_has_svm(CPUX86State *env)
+{
+    return env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM;
+}
+
 /*
  * In order for a vCPU to enter VMX operation it must have CR4.VMXE set.
  * Since it was set, CR4.VMXE must remain set as long as vCPU is in
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
index 6adbff3d74..2b6b7443d2 100644
--- a/target/i386/kvm.c
+++ b/target/i386/kvm.c
@@ -1840,16 +1840,18 @@ int kvm_arch_init_vcpu(CPUState *cs)
     if (max_nested_state_len > 0) {
         assert(max_nested_state_len >= offsetof(struct kvm_nested_state, data));
 
-        if (cpu_has_vmx(env)) {
+        if (cpu_has_vmx(env) || cpu_has_svm(env)) {
             struct kvm_vmx_nested_state_hdr *vmx_hdr;
 
             env->nested_state = g_malloc0(max_nested_state_len);
             env->nested_state->size = max_nested_state_len;
             env->nested_state->format = KVM_STATE_NESTED_FORMAT_VMX;
 
-            vmx_hdr = &env->nested_state->hdr.vmx;
-            vmx_hdr->vmxon_pa = -1ull;
-            vmx_hdr->vmcs12_pa = -1ull;
+            if (cpu_has_vmx(env)) {
+                    vmx_hdr = &env->nested_state->hdr.vmx;
+                    vmx_hdr->vmxon_pa = -1ull;
+                    vmx_hdr->vmcs12_pa = -1ull;
+            }
         }
     }
 
@@ -3873,6 +3875,20 @@ static int kvm_put_nested_state(X86CPU *cpu)
         return 0;
     }
 
+    /*
+     * Copy flags that are affected by reset from env->hflags and env->hflags2.
+     */
+    if (env->hflags & HF_GUEST_MASK) {
+        env->nested_state->flags |= KVM_STATE_NESTED_GUEST_MODE;
+    } else {
+        env->nested_state->flags &= ~KVM_STATE_NESTED_GUEST_MODE;
+    }
+    if (env->hflags2 & HF2_GIF_MASK) {
+        env->nested_state->flags |= KVM_STATE_NESTED_GIF_SET;
+    } else {
+        env->nested_state->flags &= ~KVM_STATE_NESTED_GIF_SET;
+    }
+
     assert(env->nested_state->size <= max_nested_state_len);
     return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_NESTED_STATE, env->nested_state);
 }
@@ -3901,11 +3917,19 @@ static int kvm_get_nested_state(X86CPU *cpu)
         return ret;
     }
 
+    /*
+     * Copy flags that are affected by reset to env->hflags and env->hflags2.
+     */
     if (env->nested_state->flags & KVM_STATE_NESTED_GUEST_MODE) {
         env->hflags |= HF_GUEST_MASK;
     } else {
         env->hflags &= ~HF_GUEST_MASK;
     }
+    if (env->nested_state->flags & KVM_STATE_NESTED_GIF_SET) {
+        env->hflags2 |= HF2_GIF_MASK;
+    } else {
+        env->hflags2 &= ~HF2_GIF_MASK;
+    }
 
     return ret;
 }
@@ -3917,6 +3941,12 @@ int kvm_arch_put_registers(CPUState *cpu, int level)
 
     assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu));
 
+    /* must be before kvm_put_nested_state so that EFER.SVME is set */
+    ret = kvm_put_sregs(x86_cpu);
+    if (ret < 0) {
+        return ret;
+    }
+
     if (level >= KVM_PUT_RESET_STATE) {
         ret = kvm_put_nested_state(x86_cpu);
         if (ret < 0) {
@@ -3950,10 +3980,6 @@ int kvm_arch_put_registers(CPUState *cpu, int level)
     if (ret < 0) {
         return ret;
     }
-    ret = kvm_put_sregs(x86_cpu);
-    if (ret < 0) {
-        return ret;
-    }
     /* must be before kvm_put_msrs */
     ret = kvm_inject_mce_oldstyle(x86_cpu);
     if (ret < 0) {
diff --git a/target/i386/machine.c b/target/i386/machine.c
index 0c96531a56..b1acf7d0ef 100644
--- a/target/i386/machine.c
+++ b/target/i386/machine.c
@@ -1071,13 +1071,41 @@ static const VMStateDescription vmstate_vmx_nested_state = {
     }
 };
 
+static bool svm_nested_state_needed(void *opaque)
+{
+    struct kvm_nested_state *nested_state = opaque;
+
+    /*
+     * HF_GUEST_MASK and HF2_GIF_MASK are already serialized
+     * via hflags and hflags2, all that's left is the opaque
+     * nested state blob.
+     */
+    return (nested_state->format == KVM_STATE_NESTED_FORMAT_SVM &&
+            nested_state->size > offsetof(struct kvm_nested_state, data));
+}
+
+static const VMStateDescription vmstate_svm_nested_state = {
+    .name = "cpu/kvm_nested_state/svm",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = svm_nested_state_needed,
+    .fields = (VMStateField[]) {
+        VMSTATE_U64(hdr.svm.vmcb_pa, struct kvm_nested_state),
+        VMSTATE_UINT8_ARRAY(data.svm[0].vmcb12,
+                            struct kvm_nested_state,
+                            KVM_STATE_NESTED_SVM_VMCB_SIZE),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 static bool nested_state_needed(void *opaque)
 {
     X86CPU *cpu = opaque;
     CPUX86State *env = &cpu->env;
 
     return (env->nested_state &&
-            vmx_nested_state_needed(env->nested_state));
+            (vmx_nested_state_needed(env->nested_state) ||
+             svm_nested_state_needed(env->nested_state)));
 }
 
 static int nested_state_post_load(void *opaque, int version_id)
@@ -1139,6 +1167,7 @@ static const VMStateDescription vmstate_kvm_nested_state = {
     },
     .subsections = (const VMStateDescription*[]) {
         &vmstate_vmx_nested_state,
+        &vmstate_svm_nested_state,
         NULL
     }
 };
-- 
2.26.2




  parent reply	other threads:[~2020-07-06 16:47 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-06 16:41 [PULL 00/53] Misc patches for QEMU 5.1 soft freeze Paolo Bonzini
2020-07-06 16:41 ` [PULL 01/53] tcg/svm: use host cr4 during NPT page table walk Paolo Bonzini
2020-07-06 16:41 ` [PULL 02/53] tests: Inject test name also when the test fails Paolo Bonzini
2020-07-06 16:41 ` [PULL 03/53] util/qemu-error: prepend guest name to error message to identify affected VM owner Paolo Bonzini
2020-07-06 16:41 ` [PULL 04/53] qom: Introduce object_property_try_add_child() Paolo Bonzini
2020-07-06 16:41 ` [PULL 05/53] tests/qmp-cmd-test: Add qmp/object-add-duplicate-id Paolo Bonzini
2020-07-06 16:41 ` [PULL 06/53] tests/qmp-cmd-test: Add qmp/object-add-failure-modes Paolo Bonzini
2020-07-06 16:41 ` [PULL 07/53] hw/core/null-machine: Do not initialize unused chardev backends Paolo Bonzini
2020-07-06 16:41 ` [PULL 08/53] target/i386: set SSE FTZ in correct floating-point state Paolo Bonzini
2020-07-06 16:41 ` [PULL 09/53] target/i386: fix IEEE SSE floating-point exception raising Paolo Bonzini
2020-07-06 16:41 ` Paolo Bonzini [this message]
2020-07-06 16:41 ` [PULL 11/53] coverity: provide Coverity-friendly MIN_CONST and MAX_CONST Paolo Bonzini
2020-07-06 16:41 ` [PULL 12/53] i386: hvf: Set env->eip in macvm_set_rip() Paolo Bonzini
2020-07-06 16:41 ` [PULL 13/53] i386: hvf: Move synchronize functions to sysemu Paolo Bonzini
2020-07-06 16:41 ` [PULL 14/53] i386: hvf: Add hvf_cpu_synchronize_pre_loadvm() Paolo Bonzini
2020-07-06 16:41 ` [PULL 15/53] i386: hvf: Make long mode enter and exit clearer Paolo Bonzini
2020-07-06 16:41 ` [PULL 16/53] i386: hvf: Move Guest LMA reset to macvm_set_cr0() Paolo Bonzini
2020-07-06 16:41 ` [PULL 17/53] i386: hvf: Don't duplicate register reset Paolo Bonzini
2020-07-06 16:41 ` [PULL 18/53] i386: hvf: Clean up synchronize functions Paolo Bonzini
2020-07-06 16:41 ` [PULL 19/53] MAINTAINERS: Add Cameron as HVF co-maintainer Paolo Bonzini
2020-07-06 16:41 ` [PULL 20/53] MAINTAINERS: Fix KVM path expansion glob Paolo Bonzini
2020-07-06 16:41 ` [PULL 21/53] MAINTAINERS: Add an 'overall' entry for accelerators Paolo Bonzini
2020-07-06 16:41 ` [PULL 22/53] MAINTAINERS: Cover the HAX accelerator stub Paolo Bonzini
2020-07-06 16:41 ` [PULL 23/53] Makefile: Remove dangerous EOL trailing backslash Paolo Bonzini
2020-07-06 16:41 ` [PULL 24/53] Makefile: Write MINIKCONF variables as one entry per line Paolo Bonzini
2020-07-06 16:41 ` [PULL 25/53] accel/Kconfig: Extract accel selectors into their own config Paolo Bonzini
2020-07-06 16:41 ` [PULL 26/53] accel/Kconfig: Add the TCG selector Paolo Bonzini
2020-07-06 16:41 ` [PULL 27/53] accel/tcg: Add stub for probe_access() Paolo Bonzini
2020-07-06 16:41 ` [PULL 28/53] Makefile: simplify MINIKCONF rules Paolo Bonzini
2020-07-17 11:02   ` Peter Maydell
2020-07-17 11:20     ` Paolo Bonzini
2020-07-17 13:01       ` Peter Maydell
2020-07-17 13:33         ` Paolo Bonzini
2020-07-06 16:41 ` [PULL 29/53] target/i386: remove gen_io_end Paolo Bonzini
2020-07-06 16:41 ` [PULL 30/53] target/i386: implement undocumented "smsw r32" behavior Paolo Bonzini
2020-07-06 16:41 ` [PULL 31/53] KVM: x86: believe what KVM says about WAITPKG Paolo Bonzini
2020-07-07 11:42   ` Maxim Levitsky
2020-07-07 11:58     ` Paolo Bonzini
2021-12-22  9:35   ` Chenyi Qiang
2020-07-06 16:41 ` [PULL 32/53] target/i386: sev: provide proper error reporting for query-sev-capabilities Paolo Bonzini
2020-07-06 16:41 ` [PULL 33/53] target/i386: sev: fail query-sev-capabilities if QEMU cannot use SEV Paolo Bonzini
2020-07-06 16:41 ` [PULL 34/53] iscsi: handle check condition status in retry loop Paolo Bonzini
2020-07-06 16:41 ` [PULL 35/53] iscsi: return -EIO when sense fields are meaningless Paolo Bonzini
2020-07-06 16:41 ` [PULL 36/53] chardev/tcp: fix error message double free error Paolo Bonzini
2020-07-06 16:41 ` [PULL 37/53] checkpatch: Change occurences of 'kernel' to 'qemu' in user messages Paolo Bonzini
2020-07-06 16:41 ` [PULL 38/53] target/i386: Correct the warning message of Intel PT Paolo Bonzini
2020-07-06 16:41 ` [PULL 39/53] cpus: Move CPU code from exec.c to cpus-common.c Paolo Bonzini
2020-07-06 16:41 ` [PULL 40/53] pc: fix leak in pc_system_flash_cleanup_unused Paolo Bonzini
2020-07-06 16:41 ` [PULL 41/53] softmmu: move softmmu only files from root Paolo Bonzini
2020-07-06 16:41 ` [PULL 42/53] cpu-throttle: new module, extracted from cpus.c Paolo Bonzini
2020-07-06 16:41 ` [PULL 43/53] cpu-timers, icount: new modules Paolo Bonzini
2020-07-06 16:41 ` [PULL 44/53] softmmu/vl: Remove the check for colons in -accel parameters Paolo Bonzini
2020-07-06 16:41 ` [PULL 45/53] accel/kvm: Let kvm_check_extension use global KVM state Paolo Bonzini
2020-07-06 16:41 ` [PULL 46/53] accel/kvm: Simplify kvm_check_extension() Paolo Bonzini
2020-07-06 16:41 ` [PULL 47/53] accel/kvm: Simplify kvm_check_extension_list() Paolo Bonzini
2020-07-06 16:41 ` [PULL 48/53] target/i386/kvm: Simplify get_para_features() Paolo Bonzini
2020-07-06 16:41 ` [PULL 49/53] target/i386/kvm: Simplify kvm_get_mce_cap_supported() Paolo Bonzini
2020-07-06 16:41 ` [PULL 50/53] target/i386/kvm: Simplify kvm_get_supported_[feature]_msrs() Paolo Bonzini
2020-07-06 16:41 ` [PULL 51/53] target/i386: Add SERIALIZE cpu feature Paolo Bonzini
2020-07-06 16:41 ` [PULL 52/53] target/i386: Enable TSX Suspend Load Address Tracking feature Paolo Bonzini
2020-07-06 16:41 ` [PULL 53/53] scripts: improve message when TAP based tests fail Paolo Bonzini
2020-07-06 17:19 ` [PULL 00/53] Misc patches for QEMU 5.1 soft freeze no-reply
2020-07-07 18:37 ` Peter Maydell
2020-07-07 18:42   ` Peter Maydell
2020-07-07 18:48     ` Paolo Bonzini
2020-07-08  8:25   ` Philippe Mathieu-Daudé
2020-07-08 16:13   ` Claudio Fontana
2020-07-08 16:16     ` Paolo Bonzini
2020-07-08 16:45       ` Claudio Fontana
2020-07-08 16:55         ` Paolo Bonzini
2020-07-08 17:03           ` Claudio Fontana
2020-07-08 18:25             ` Claudio Fontana
2020-07-08 18:34               ` Claudio Fontana
2020-07-08 18:41               ` Paolo Bonzini
2020-07-09  6:59                 ` Claudio Fontana
2020-07-09  9:57                   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200706164155.24696-11-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.