Yu-hsin Wang has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/42161 )

Change subject: cpu-kvm,arch-arm: correct kvm virtual time
......................................................................

cpu-kvm,arch-arm: correct kvm virtual time

According to kernel code*1, the virtual time is related to physical
timer and cntvoff. When the simulator goes from KVM to gem5, the
physical timer is still ticking. After gem5 simulating models and going
back to KVM, the virtual time also goes away. We should update cntvoff
by ourselve to get correct virtual time.

This cl adds hooks before and after KVM_RUN. The inherit class overrides
the two hooks to save and restore the virtual time.

1. https://code.woboq.org/linux/linux/virt/kvm/arm/arch_timer.c.html#826

Change-Id: Ie054104642f2a6d5a0740f39b947f5f2c29c36f3
---
M src/arch/arm/kvm/base_cpu.cc
M src/arch/arm/kvm/base_cpu.hh
M src/cpu/kvm/base.cc
M src/cpu/kvm/base.hh
4 files changed, 36 insertions(+), 1 deletion(-)



diff --git a/src/arch/arm/kvm/base_cpu.cc b/src/arch/arm/kvm/base_cpu.cc
index 2a948c7..440efcd 100644
--- a/src/arch/arm/kvm/base_cpu.cc
+++ b/src/arch/arm/kvm/base_cpu.cc
@@ -62,7 +62,7 @@
 BaseArmKvmCPU::BaseArmKvmCPU(const BaseArmKvmCPUParams &params)
     : BaseKvmCPU(params),
       irqAsserted(false), fiqAsserted(false),
-      virtTimerPin(nullptr), prevDeviceIRQLevel(0)
+      virtTimerPin(nullptr), prevDeviceIRQLevel(0), vtime(0)
 {
 }

@@ -182,6 +182,18 @@
         panic("KVM: Failed to initialize vCPU\n");
 }

+void
+BaseArmKvmCPU::beforeLoadKvmVCPU()
+{
+    setOneReg(KVM_REG_ARM_TIMER_CNT, vtime);
+}
+
+void
+BaseArmKvmCPU::afterLeaveKvmVCPU()
+{
+    getOneReg(KVM_REG_ARM_TIMER_CNT, &vtime);
+}
+
 bool
 BaseArmKvmCPU::getRegList(struct kvm_reg_list &regs) const
 {
diff --git a/src/arch/arm/kvm/base_cpu.hh b/src/arch/arm/kvm/base_cpu.hh
index 6419547..92a8817 100644
--- a/src/arch/arm/kvm/base_cpu.hh
+++ b/src/arch/arm/kvm/base_cpu.hh
@@ -79,6 +79,13 @@
      */
     uint64_t prevDeviceIRQLevel;

+    /**
+     * When the simulator returns from KVM for simulating other models, the
+     * in-kernel timer doesn't stop. We have to save the virtual time and
+     * restore before going into KVM next time.
+     */
+    uint64_t vtime;
+
   protected:
     typedef std::vector<uint64_t> RegIndexVector;

@@ -106,6 +113,10 @@
      */
     void kvmArmVCpuInit(const struct kvm_vcpu_init &init);

+    void beforeLoadKvmVCPU() override;
+
+    void afterLeaveKvmVCPU() override;
+
   private:
std::unique_ptr<struct kvm_reg_list> tryGetRegList(uint64_t nelem) const;

diff --git a/src/cpu/kvm/base.cc b/src/cpu/kvm/base.cc
index 7b523a4..4dbfc72 100644
--- a/src/cpu/kvm/base.cc
+++ b/src/cpu/kvm/base.cc
@@ -1302,11 +1302,13 @@
 void
 BaseKvmCPU::ioctlRun()
 {
+    beforeLoadKvmVCPU();
     if (ioctl(KVM_RUN) == -1) {
         if (errno != EINTR)
             panic("KVM: Failed to start virtual CPU (errno: %i)\n",
                   errno);
     }
+    afterLeaveKvmVCPU();
 }

 void
diff --git a/src/cpu/kvm/base.hh b/src/cpu/kvm/base.hh
index d97845b..e235987 100644
--- a/src/cpu/kvm/base.hh
+++ b/src/cpu/kvm/base.hh
@@ -503,6 +503,16 @@
     /** @} */

     /**
+     * A hook function before starting the virtualized CPU
+     */
+    virtual void beforeLoadKvmVCPU() {}
+
+    /**
+     * A hook function after leaving the virtualized CPU
+     */
+    virtual void afterLeaveKvmVCPU() {}
+
+    /**
      * Is the architecture specific code in a state that prevents
      * draining?
      *

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/42161
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Ie054104642f2a6d5a0740f39b947f5f2c29c36f3
Gerrit-Change-Number: 42161
Gerrit-PatchSet: 1
Gerrit-Owner: Yu-hsin Wang <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to