diff mbox series

[for-9.1,5/7] target/i386/kvm: Add legacy_kvmclock cpu property

Message ID 20240329101954.3954987-6-zhao1.liu@linux.intel.com (mailing list archive)
State New
Headers show
Series target/i386/kvm: Cleanup the kvmclock feature name | expand

Commit Message

Zhao Liu March 29, 2024, 10:19 a.m. UTC
From: Zhao Liu <zhao1.liu@intel.com>

Currently, the old kvmclock (KVM_FEATURE_CLOCKSOURCE) and the new
(KVM_FEATURE_CLOCKSOURCE2) are always set/unset together since they have
the same feature name "kvmclock" since the commit 642258c6c7 ("kvm: add
kvmclock to its second bit").

Before renaming the new kvmclock, introduce legacy_kvmclock to inherit
the behavior of both old and new kvmclocks being enabled/disabled at the
same time.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 hw/i386/pc.c              |  1 +
 target/i386/cpu.c         |  1 +
 target/i386/cpu.h         |  7 +++++++
 target/i386/kvm/kvm-cpu.c | 19 +++++++++++++++++++
 4 files changed, 28 insertions(+)
diff mbox series

Patch

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index 9c4b3969cc8a..a452da0a45a1 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -80,6 +80,7 @@ 
 
 GlobalProperty pc_compat_9_0[] = {
     { TYPE_X86_CPU, "guest-phys-bits", "0" },
+    { TYPE_X86_CPU, "legacy-kvmclock", "true" },
 };
 const size_t pc_compat_9_0_len = G_N_ELEMENTS(pc_compat_9_0);
 
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index eef3d08473ed..1b6caf071a6d 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -7941,6 +7941,7 @@  static Property x86_cpu_properties[] = {
      */
     DEFINE_PROP_BOOL("legacy-cache", X86CPU, legacy_cache, true),
     DEFINE_PROP_BOOL("xen-vapic", X86CPU, xen_vapic, false),
+    DEFINE_PROP_BOOL("legacy-kvmclock", X86CPU, legacy_kvmclock, false),
 
     /*
      * From "Requirements for Implementing the Microsoft
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index b339f9ce454f..b3ee2a35f2c1 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -2070,6 +2070,13 @@  struct ArchCPU {
     int32_t hv_max_vps;
 
     bool xen_vapic;
+
+    /*
+     * Compatibility bits for old machine types.
+     * If true, always set/unset KVM_FEATURE_CLOCKSOURCE and
+     * KVM_FEATURE_CLOCKSOURCE2 at the same time.
+     */
+    bool legacy_kvmclock;
 };
 
 typedef struct X86CPUModel X86CPUModel;
diff --git a/target/i386/kvm/kvm-cpu.c b/target/i386/kvm/kvm-cpu.c
index e6b7a46743b5..ae3cb27c8aa8 100644
--- a/target/i386/kvm/kvm-cpu.c
+++ b/target/i386/kvm/kvm-cpu.c
@@ -18,6 +18,8 @@ 
 #include "kvm_i386.h"
 #include "hw/core/accel-cpu.h"
 
+#include "standard-headers/asm-x86/kvm_para.h"
+
 static void kvm_set_guest_phys_bits(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
@@ -72,6 +74,23 @@  static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
                                                    MSR_IA32_UCODE_REV);
         }
     }
+
+    if (cpu->legacy_kvmclock) {
+        /*
+         * The old and new kvmclock are both set by default from the
+         * oldest KVM supported (v4.5, see "OS requirements" section at
+         * docs/system/target-i386.rst). So when one of them is missing,
+         * it is only possible that the user is actively masking it.
+         * Then, mask both at the same time for compatibility with v9.0
+         * and older QEMU's kvmclock behavior.
+         */
+        if (!(env->features[FEAT_KVM] & CPUID_FEAT_KVM_CLOCK) ||
+            !(env->features[FEAT_KVM] & CPUID_FEAT_KVM_CLOCK2)) {
+            env->features[FEAT_KVM] &= ~(CPUID_FEAT_KVM_CLOCK |
+                                         CPUID_FEAT_KVM_CLOCK2);
+        }
+    }
+
     ret = host_cpu_realizefn(cs, errp);
     if (!ret) {
         return ret;