diff mbox series

[RFC,v2,6/9] target/loongarch: Implement kvm_arch_init_vcpu

Message ID 20230427072645.3368102-7-zhaotianrui@loongson.cn (mailing list archive)
State New, archived
Headers show
Series Add loongarch kvm accel support | expand

Commit Message

zhaotianrui April 27, 2023, 7:26 a.m. UTC
Implement kvm_arch_init_vcpu interface for loongarch,
in this function, we register VM change state handler.
And when VM state changes to running, the counter value
should be put into kvm to keep consistent with kvm,
and when state change to stop, counter value should be
refreshed from kvm.

Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
---
 target/loongarch/cpu.h        |  1 +
 target/loongarch/kvm.c        | 23 +++++++++++++++++++++++
 target/loongarch/trace-events |  2 ++
 3 files changed, 26 insertions(+)

Comments

Richard Henderson May 2, 2023, 11:32 a.m. UTC | #1
On 4/27/23 08:26, Tianrui Zhao wrote:
> +static void kvm_loongarch_vm_stage_change(void *opaque, bool running,

Typo:                           state

> +    uint64_t counter_value;

I know naming is hard, but this is so generic it is difficult to determine what it does. 
Perhaps kvm_state_counter?  A comment would also be helpful, even with a renaming.


r~
zhaotianrui May 4, 2023, 3:01 a.m. UTC | #2
在 2023年05月02日 19:32, Richard Henderson 写道:
> On 4/27/23 08:26, Tianrui Zhao wrote:
>> +static void kvm_loongarch_vm_stage_change(void *opaque, bool running,
>
> Typo:                           state
>
>> +    uint64_t counter_value;
>
> I know naming is hard, but this is so generic it is difficult to 
> determine what it does. Perhaps kvm_state_counter?  A comment would 
> also be helpful, even with a renaming.
>
>
> r~
Thanks, I will rename this variable like kvm_state_counter.

Thanks
Tianrui Zhao
diff mbox series

Patch

diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h
index 86b9f26d60..473d9986f3 100644
--- a/target/loongarch/cpu.h
+++ b/target/loongarch/cpu.h
@@ -350,6 +350,7 @@  struct ArchCPU {
 
     /* 'compatible' string for this CPU for Linux device trees */
     const char *dtb_compatible;
+    uint64_t counter_value;
 };
 
 #define TYPE_LOONGARCH_CPU "loongarch-cpu"
diff --git a/target/loongarch/kvm.c b/target/loongarch/kvm.c
index 8dea294930..59d7e35490 100644
--- a/target/loongarch/kvm.c
+++ b/target/loongarch/kvm.c
@@ -393,8 +393,31 @@  int kvm_arch_put_registers(CPUState *cs, int level)
     return ret;
 }
 
+static void kvm_loongarch_vm_stage_change(void *opaque, bool running,
+                                          RunState state)
+{
+    int ret;
+    CPUState *cs = opaque;
+    LoongArchCPU *cpu = LOONGARCH_CPU(cs);
+
+    if (running) {
+        ret = kvm_larch_putq(cs, KVM_REG_LOONGARCH_COUNTER,
+                             &cpu->counter_value);
+        if (ret < 0) {
+            trace_kvm_failed_put_counter(strerror(errno));
+        }
+    } else {
+        ret = kvm_larch_getq(cs, KVM_REG_LOONGARCH_COUNTER,
+                             &cpu->counter_value);
+        if (ret < 0) {
+            trace_kvm_failed_get_counter(strerror(errno));
+        }
+    }
+}
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
+    qemu_add_vm_change_state_handler(kvm_loongarch_vm_stage_change, cs);
     return 0;
 }
 
diff --git a/target/loongarch/trace-events b/target/loongarch/trace-events
index 67817fee67..6926bbe399 100644
--- a/target/loongarch/trace-events
+++ b/target/loongarch/trace-events
@@ -9,3 +9,5 @@  kvm_failed_get_fpu(const char *msg) "Failed to get fpu from KVM: %s"
 kvm_failed_put_fpu(const char *msg) "Failed to put fpu into KVM: %s"
 kvm_failed_get_mpstate(const char *msg) "Failed to get mp_state from KVM: %s"
 kvm_failed_put_mpstate(const char *msg) "Failed to put mp_state into KVM: %s"
+kvm_failed_get_counter(const char *msg) "Failed to get counter from KVM: %s"
+kvm_failed_put_counter(const char *msg) "Failed to put counter into KVM: %s"