From: Jiri Denemark <[email protected]>

The arch-capabilities MSR is not defined on AMD CPUs, but KVM has always
been emulating them. Unfortunately, this may cause Windows to crash so
QEMU (since 10.1, commit d3a24134e37d57abd3e7445842cda2717f49e96d)
decided to mask the MSR by default with some additional compatibility
code for older machine types.

This is all mostly transparent except for probing when we run QEMU
without a machine type and expand the "host" CPU model. With QEMU 10.1
and newer none of the arch-capabilities features will be shown as
enabled, which may cause unexpected issues for users (such as KubeVirt)
that get the list of all supported features from the host-model CPU
definition in domain capabilities to select possible target nodes for
migration. As a result of the change, no AMD host with new QEMU will be
shown as available for incoming migration from older hosts.

While we don't want to add the features in host-model definition by
default, we can show them when a user wants to see all features
supported on the host, i.e., when a recently introduced
VIR_CONNECT_GET_DOMAIN_CAPABILITIES_EXPAND_CPU_FEATURES flag is passed.

Signed-off-by: Jiri Denemark <[email protected]>
---
 src/qemu/qemu_capabilities.c | 39 ++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 8295a9ae9d..b0406b348a 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -4093,6 +4093,41 @@ virQEMUCapsInitCPUModel(virQEMUCaps *qemuCaps,
 }
 
 
+static virCPUDef *
+virQEMUCapsInitExpandedHostCPUModel(virQEMUCaps *qemuCaps,
+                                    virCPUDef *hostModel)
+{
+    g_autoptr(virCPUDef) cpu = virCPUDefCopy(hostModel);
+    virArch arch = qemuCaps->arch;
+
+    if (ARCH_IS_X86(arch) &&
+        STREQ_NULLABLE(cpu->vendor, "AMD") &&
+        virCPUCheckFeature(arch, cpu, "arch-capabilities") == 0) {
+        uint32_t index = 0x10a;
+        uint64_t msr = 0;
+        /* While the arch-capabilities MSR is not defined on AMD CPUs, KVM has
+         * always been emulating them. Unfortunately, this may cause some
+         * Windows version to crash so QEMU decided to mask the MSR by default.
+         * Let's just add the emulated features back to the expanded host-model
+         * definition used for reporting all CPU features supported on a host.
+         */
+        if (virHostCPUGetMSRFromKVM(index, &msr) == 0) {
+            g_autoptr(virCPUData) data = virCPUDataNew(arch);
+            virCPUFeaturePolicy policy = VIR_CPU_FEATURE_REQUIRE;
+
+            virCPUx86DataAddMSR(data, index, msr);
+            virCPUUpdateFeatures(arch, cpu, data, policy);
+            virCPUDefUpdateFeature(cpu, "arch-capabilities", policy);
+        }
+    }
+
+    if (virCPUExpandFeatures(arch, cpu) < 0)
+        return NULL;
+
+    return g_steal_pointer(&cpu);
+}
+
+
 static virCPUDef *
 virQEMUCapsNewHostCPUModel(void)
 {
@@ -4150,8 +4185,8 @@ virQEMUCapsInitHostCPUModel(virQEMUCaps *qemuCaps,
                                  &qemuCaps->arch);
     }
 
-    cpuExpanded = virCPUDefCopy(cpu);
-    if (virCPUExpandFeatures(qemuCaps->arch, cpuExpanded) < 0)
+    cpuExpanded = virQEMUCapsInitExpandedHostCPUModel(qemuCaps, cpu);
+    if (!cpuExpanded)
         goto error;
 
     if (rc == 0 &&
-- 
2.54.0

Reply via email to