cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-target-arm-kvm-Implement-virtual-time-adjustment.patch

902636
From 5388ea3fc0737d1a659256ff3663057bef484c19 Mon Sep 17 00:00:00 2001
902636
From: Andrew Jones <drjones@redhat.com>
902636
Date: Fri, 31 Jan 2020 14:23:13 +0000
902636
Subject: [PATCH 11/15] target/arm/kvm: Implement virtual time adjustment
902636
MIME-Version: 1.0
902636
Content-Type: text/plain; charset=UTF-8
902636
Content-Transfer-Encoding: 8bit
902636
902636
RH-Author: Andrew Jones <drjones@redhat.com>
902636
Message-id: <20200131142314.13175-5-drjones@redhat.com>
902636
Patchwork-id: 93622
902636
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH 4/5] target/arm/kvm: Implement virtual time adjustment
902636
Bugzilla: 1647366
902636
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
902636
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
902636
RH-Acked-by: Gavin Shan <gshan@redhat.com>
902636
902636
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1647366
902636
902636
Author: Andrew Jones <drjones@redhat.com>
902636
Date:   Thu, 30 Jan 2020 16:02:06 +0000
902636
902636
    target/arm/kvm: Implement virtual time adjustment
902636
902636
    When a VM is stopped (such as when it's paused) guest virtual time
902636
    should stop counting. Otherwise, when the VM is resumed it will
902636
    experience time jumps and its kernel may report soft lockups. Not
902636
    counting virtual time while the VM is stopped has the side effect
902636
    of making the guest's time appear to lag when compared with real
902636
    time, and even with time derived from the physical counter. For
902636
    this reason, this change, which is enabled by default, comes with
902636
    a KVM CPU feature allowing it to be disabled, restoring legacy
902636
    behavior.
902636
902636
    This patch only provides the implementation of the virtual time
902636
    adjustment. A subsequent patch will provide the CPU property
902636
    allowing the change to be enabled and disabled.
902636
902636
    Reported-by: Bijan Mottahedeh <bijan.mottahedeh@oracle.com>
902636
    Signed-off-by: Andrew Jones <drjones@redhat.com>
902636
    Message-id: 20200120101023.16030-6-drjones@redhat.com
902636
    Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
902636
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
902636
902636
(cherry picked from commit e5ac4200b4cddf44df9adbef677af0d1f1c579c6)
902636
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
902636
---
902636
 target/arm/cpu.h     |  7 ++++
902636
 target/arm/kvm.c     | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++
902636
 target/arm/kvm32.c   |  3 ++
902636
 target/arm/kvm64.c   |  3 ++
902636
 target/arm/kvm_arm.h | 38 ++++++++++++++++++++++
902636
 target/arm/machine.c |  7 ++++
902636
 6 files changed, 150 insertions(+)
902636
902636
diff --git a/target/arm/cpu.h b/target/arm/cpu.h
902636
index 82dd3cc..fbd8ea0 100644
902636
--- a/target/arm/cpu.h
902636
+++ b/target/arm/cpu.h
902636
@@ -821,6 +821,13 @@ struct ARMCPU {
902636
     /* KVM init features for this CPU */
902636
     uint32_t kvm_init_features[7];
902636
 
902636
+    /* KVM CPU state */
902636
+
902636
+    /* KVM virtual time adjustment */
902636
+    bool kvm_adjvtime;
902636
+    bool kvm_vtime_dirty;
902636
+    uint64_t kvm_vtime;
902636
+
902636
     /* Uniprocessor system with MP extensions */
902636
     bool mp_is_up;
902636
 
902636
diff --git a/target/arm/kvm.c b/target/arm/kvm.c
902636
index 5b82cef..26d7f8b 100644
902636
--- a/target/arm/kvm.c
902636
+++ b/target/arm/kvm.c
902636
@@ -359,6 +359,22 @@ static int compare_u64(const void *a, const void *b)
902636
     return 0;
902636
 }
902636
 
902636
+/*
902636
+ * cpreg_values are sorted in ascending order by KVM register ID
902636
+ * (see kvm_arm_init_cpreg_list). This allows us to cheaply find
902636
+ * the storage for a KVM register by ID with a binary search.
902636
+ */
902636
+static uint64_t *kvm_arm_get_cpreg_ptr(ARMCPU *cpu, uint64_t regidx)
902636
+{
902636
+    uint64_t *res;
902636
+
902636
+    res = bsearch(&regidx, cpu->cpreg_indexes, cpu->cpreg_array_len,
902636
+                  sizeof(uint64_t), compare_u64);
902636
+    assert(res);
902636
+
902636
+    return &cpu->cpreg_values[res - cpu->cpreg_indexes];
902636
+}
902636
+
902636
 /* Initialize the ARMCPU cpreg list according to the kernel's
902636
  * definition of what CPU registers it knows about (and throw away
902636
  * the previous TCG-created cpreg list).
902636
@@ -512,6 +528,23 @@ bool write_list_to_kvmstate(ARMCPU *cpu, int level)
902636
     return ok;
902636
 }
902636
 
902636
+void kvm_arm_cpu_pre_save(ARMCPU *cpu)
902636
+{
902636
+    /* KVM virtual time adjustment */
902636
+    if (cpu->kvm_vtime_dirty) {
902636
+        *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT) = cpu->kvm_vtime;
902636
+    }
902636
+}
902636
+
902636
+void kvm_arm_cpu_post_load(ARMCPU *cpu)
902636
+{
902636
+    /* KVM virtual time adjustment */
902636
+    if (cpu->kvm_adjvtime) {
902636
+        cpu->kvm_vtime = *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT);
902636
+        cpu->kvm_vtime_dirty = true;
902636
+    }
902636
+}
902636
+
902636
 void kvm_arm_reset_vcpu(ARMCPU *cpu)
902636
 {
902636
     int ret;
902636
@@ -579,6 +612,50 @@ int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu)
902636
     return 0;
902636
 }
902636
 
902636
+void kvm_arm_get_virtual_time(CPUState *cs)
902636
+{
902636
+    ARMCPU *cpu = ARM_CPU(cs);
902636
+    struct kvm_one_reg reg = {
902636
+        .id = KVM_REG_ARM_TIMER_CNT,
902636
+        .addr = (uintptr_t)&cpu->kvm_vtime,
902636
+    };
902636
+    int ret;
902636
+
902636
+    if (cpu->kvm_vtime_dirty) {
902636
+        return;
902636
+    }
902636
+
902636
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®);
902636
+    if (ret) {
902636
+        error_report("Failed to get KVM_REG_ARM_TIMER_CNT");
902636
+        abort();
902636
+    }
902636
+
902636
+    cpu->kvm_vtime_dirty = true;
902636
+}
902636
+
902636
+void kvm_arm_put_virtual_time(CPUState *cs)
902636
+{
902636
+    ARMCPU *cpu = ARM_CPU(cs);
902636
+    struct kvm_one_reg reg = {
902636
+        .id = KVM_REG_ARM_TIMER_CNT,
902636
+        .addr = (uintptr_t)&cpu->kvm_vtime,
902636
+    };
902636
+    int ret;
902636
+
902636
+    if (!cpu->kvm_vtime_dirty) {
902636
+        return;
902636
+    }
902636
+
902636
+    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®);
902636
+    if (ret) {
902636
+        error_report("Failed to set KVM_REG_ARM_TIMER_CNT");
902636
+        abort();
902636
+    }
902636
+
902636
+    cpu->kvm_vtime_dirty = false;
902636
+}
902636
+
902636
 int kvm_put_vcpu_events(ARMCPU *cpu)
902636
 {
902636
     CPUARMState *env = &cpu->env;
902636
@@ -690,6 +767,21 @@ MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
902636
     return MEMTXATTRS_UNSPECIFIED;
902636
 }
902636
 
902636
+void kvm_arm_vm_state_change(void *opaque, int running, RunState state)
902636
+{
902636
+    CPUState *cs = opaque;
902636
+    ARMCPU *cpu = ARM_CPU(cs);
902636
+
902636
+    if (running) {
902636
+        if (cpu->kvm_adjvtime) {
902636
+            kvm_arm_put_virtual_time(cs);
902636
+        }
902636
+    } else {
902636
+        if (cpu->kvm_adjvtime) {
902636
+            kvm_arm_get_virtual_time(cs);
902636
+        }
902636
+    }
902636
+}
902636
 
902636
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
902636
 {
902636
diff --git a/target/arm/kvm32.c b/target/arm/kvm32.c
902636
index 32bf8d6..3a8b437 100644
902636
--- a/target/arm/kvm32.c
902636
+++ b/target/arm/kvm32.c
902636
@@ -16,6 +16,7 @@
902636
 #include "qemu-common.h"
902636
 #include "cpu.h"
902636
 #include "qemu/timer.h"
902636
+#include "sysemu/runstate.h"
902636
 #include "sysemu/kvm.h"
902636
 #include "kvm_arm.h"
902636
 #include "internals.h"
902636
@@ -198,6 +199,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
902636
         return -EINVAL;
902636
     }
902636
 
902636
+    qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cs);
902636
+
902636
     /* Determine init features for this CPU */
902636
     memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
902636
     if (cpu->start_powered_off) {
902636
diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
902636
index 666a81a..d368189 100644
902636
--- a/target/arm/kvm64.c
902636
+++ b/target/arm/kvm64.c
902636
@@ -23,6 +23,7 @@
902636
 #include "qemu/host-utils.h"
902636
 #include "qemu/main-loop.h"
902636
 #include "exec/gdbstub.h"
902636
+#include "sysemu/runstate.h"
902636
 #include "sysemu/kvm.h"
902636
 #include "sysemu/kvm_int.h"
902636
 #include "kvm_arm.h"
902636
@@ -735,6 +736,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
902636
         return -EINVAL;
902636
     }
902636
 
902636
+    qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cs);
902636
+
902636
     /* Determine init features for this CPU */
902636
     memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
902636
     if (cpu->start_powered_off) {
902636
diff --git a/target/arm/kvm_arm.h b/target/arm/kvm_arm.h
902636
index b48a9c9..01a9a18 100644
902636
--- a/target/arm/kvm_arm.h
902636
+++ b/target/arm/kvm_arm.h
902636
@@ -128,6 +128,23 @@ bool write_list_to_kvmstate(ARMCPU *cpu, int level);
902636
 bool write_kvmstate_to_list(ARMCPU *cpu);
902636
 
902636
 /**
902636
+ * kvm_arm_cpu_pre_save:
902636
+ * @cpu: ARMCPU
902636
+ *
902636
+ * Called after write_kvmstate_to_list() from cpu_pre_save() to update
902636
+ * the cpreg list with KVM CPU state.
902636
+ */
902636
+void kvm_arm_cpu_pre_save(ARMCPU *cpu);
902636
+
902636
+/**
902636
+ * kvm_arm_cpu_post_load:
902636
+ * @cpu: ARMCPU
902636
+ *
902636
+ * Called from cpu_post_load() to update KVM CPU state from the cpreg list.
902636
+ */
902636
+void kvm_arm_cpu_post_load(ARMCPU *cpu);
902636
+
902636
+/**
902636
  * kvm_arm_reset_vcpu:
902636
  * @cpu: ARMCPU
902636
  *
902636
@@ -292,6 +309,24 @@ int kvm_arm_sync_mpstate_to_kvm(ARMCPU *cpu);
902636
  */
902636
 int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu);
902636
 
902636
+/**
902636
+ * kvm_arm_get_virtual_time:
902636
+ * @cs: CPUState
902636
+ *
902636
+ * Gets the VCPU's virtual counter and stores it in the KVM CPU state.
902636
+ */
902636
+void kvm_arm_get_virtual_time(CPUState *cs);
902636
+
902636
+/**
902636
+ * kvm_arm_put_virtual_time:
902636
+ * @cs: CPUState
902636
+ *
902636
+ * Sets the VCPU's virtual counter to the value stored in the KVM CPU state.
902636
+ */
902636
+void kvm_arm_put_virtual_time(CPUState *cs);
902636
+
902636
+void kvm_arm_vm_state_change(void *opaque, int running, RunState state);
902636
+
902636
 int kvm_arm_vgic_probe(void);
902636
 
902636
 void kvm_arm_pmu_set_irq(CPUState *cs, int irq);
902636
@@ -339,6 +374,9 @@ static inline void kvm_arm_pmu_set_irq(CPUState *cs, int irq) {}
902636
 static inline void kvm_arm_pmu_init(CPUState *cs) {}
902636
 
902636
 static inline void kvm_arm_sve_get_vls(CPUState *cs, unsigned long *map) {}
902636
+
902636
+static inline void kvm_arm_get_virtual_time(CPUState *cs) {}
902636
+static inline void kvm_arm_put_virtual_time(CPUState *cs) {}
902636
 #endif
902636
 
902636
 static inline const char *gic_class_name(void)
902636
diff --git a/target/arm/machine.c b/target/arm/machine.c
902636
index eb28b23..241890a 100644
902636
--- a/target/arm/machine.c
902636
+++ b/target/arm/machine.c
902636
@@ -642,6 +642,12 @@ static int cpu_pre_save(void *opaque)
902636
             /* This should never fail */
902636
             abort();
902636
         }
902636
+
902636
+        /*
902636
+         * kvm_arm_cpu_pre_save() must be called after
902636
+         * write_kvmstate_to_list()
902636
+         */
902636
+        kvm_arm_cpu_pre_save(cpu);
902636
     } else {
902636
         if (!write_cpustate_to_list(cpu, false)) {
902636
             /* This should never fail. */
902636
@@ -744,6 +750,7 @@ static int cpu_post_load(void *opaque, int version_id)
902636
          * we're using it.
902636
          */
902636
         write_list_to_cpustate(cpu);
902636
+        kvm_arm_cpu_post_load(cpu);
902636
     } else {
902636
         if (!write_list_to_cpustate(cpu)) {
902636
             return -1;
902636
-- 
902636
1.8.3.1
902636