target/riscv/kvm: Sync vCPU mp_state for migration
PATCH 1/1 target/riscv/kvm: Sync vCPU mp_state for migration
Fix secondary vCPUs(id > 0) stall after migration.
Signed-off-by BillXiang <[email protected]>
--- target/riscv/cpu.h | 3 +++ target/riscv/kvm/kvm-cpu.c | 34 ++++++++++++++++++++++++++-------- target/riscv/kvm/kvm_riscv.h | 3 ++- target/riscv/machine.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 9 deletions(-)
target/riscv/cpu.h +3 -0
@@ -20,6 +20,7 @@#ifndef RISCV_CPU_H #define RISCV_CPU_H +#include <linux/kvm.h> #include "hw/core/cpu.h" #include "hw/registerfields.h" #include "hw/qdev-properties.h"
@@ -546,6 +547,8 @@ struct ArchCPU {RISCVCPUConfig cfg;RISCVSATPModes satp_modes; +struct kvm_mp_state mp_state; +QEMUTimer *pmu_timer;/* A bitmask of Available programmable counters */ uint32_t pmu_avail_ctrs;
target/riscv/kvm/kvm-cpu.c +26 -8
@@ -1348,17 +1348,16 @@ int kvm_arch_get_registers(CPUState *cs, Error **errp)return ret; } +RISCVCPU *cpu =RISCV_CPU (cs); + ret =kvm_riscv_sync_mpstate_to_qemu (cpu); +return ret; } -int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state) +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu) {if (cap_has_mp_state) { -struct kvm_mp_state mp_state = { - .mp_state = state - }; - -int ret =kvm_vcpu_ioctl (CPU (cpu),KVM_SET_MP_STATE , &mp_state); +int ret =kvm_vcpu_ioctl (CPU (cpu),KVM_SET_MP_STATE , &cpu->mp_state);if (ret) {fprintf (stderr,"%s: failed to sync MP_STATE %d/%s\n" , __func__, ret,strerror (-ret));
@@ -1369,6 +1368,17 @@ int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state)return 0 ; } +int kvm_riscv_sync_mpstate_to_qemu (RISCVCPU *cpu) +{ +if (cap_has_mp_state) { +int ret =kvm_vcpu_ioctl (CPU (cpu),KVM_GET_MP_STATE , &cpu->mp_state); +if (ret) { +return ret; + } + } +return 0 ; +} +int kvm_arch_put_registers (CPUState *cs,int level,Error **errp) {int ret =0 ;
@@ -1396,13 +1406,21 @@ int kvm_arch_put_registers(CPUState *cs, int level, Error **errp)if (KVM_PUT_RESET_STATE == level) {RISCVCPU *cpu =RISCV_CPU (cs);if (cs->cpu_index ==0 ) { - ret =kvm_riscv_sync_mpstate_to_kvm (cpu,KVM_MP_STATE_RUNNABLE ); + cpu->mp_state.mp_state =KVM_MP_STATE_RUNNABLE ; + ret =kvm_riscv_sync_mpstate_to_kvm (cpu); }else { - ret =kvm_riscv_sync_mpstate_to_kvm (cpu,KVM_MP_STATE_STOPPED ); + cpu->mp_state.mp_state =KVM_MP_STATE_STOPPED ; + ret =kvm_riscv_sync_mpstate_to_kvm (cpu); }if (ret) {return ret; } + }else if (KVM_PUT_FULL_STATE == level) { +RISCVCPU *cpu =RISCV_CPU (cs); + ret =kvm_riscv_sync_mpstate_to_kvm (cpu); +if (ret) { +return ret; + } }return ret;
target/riscv/kvm/kvm_riscv.h +2 -1
@@ -28,7 +28,8 @@ void kvm_riscv_aia_create(MachineState *machine, uint64_t group_shift, uint64_t aplic_base, uint64_t imsic_base, uint64_t guest_num);void riscv_kvm_aplic_request (void *opaque,int irq,int level); -int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state); +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu); +int kvm_riscv_sync_mpstate_to_qemu (RISCVCPU *cpu);void riscv_kvm_cpu_finalize_features (RISCVCPU *cpu,Error **errp);uint64_t kvm_riscv_get_timebase_frequency (RISCVCPU *cpu);
target/riscv/machine.c +30 -0
@@ -251,6 +251,28 @@ static const VMStateDescription vmstate_debug = { } }; +static int get_mp_state (QEMUFile *f,void *opaque,size_t size, +const VMStateField *field) +{ +RISCVCPU *cpu = opaque; + cpu->mp_state.mp_state =qemu_get_be32 (f); +return 0 ; +} + +static int put_mp_state (QEMUFile *f,void *opaque,size_t size, +const VMStateField *field,JSONWriter *vmdesc) +{ +RISCVCPU *cpu = opaque; +qemu_put_be32 (f, cpu->mp_state.mp_state); +return 0 ; +} + +static const VMStateInfo vmstate_mp_state = { + .name ="mp_state" , + .get = get_mp_state, + .put = put_mp_state, +}; +static int riscv_cpu_post_load (void *opaque,int version_id) {RISCVCPU *cpu = opaque;
@@ -457,6 +479,14 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.stimecmp, RISCVCPU), + { + .name ="mp_state" , + .version_id = 0, + .size = sizeof(bool), + .info = &vmstate_mp_state, + .flags = VMS_SINGLE, + .offset = 0, + },VMSTATE_END_OF_LIST () },
Signature · 1 line
2.46.2.windows.1
Hi all, sorry for bothering. I just noticed that Xie Bo <[email protected]> is already working on this.
Quoted 162 lines · BillXiang
Fix secondary vCPUs(id > 0) stall after migration.
Signed-off-by BillXiang <[email protected]>--- target/riscv/cpu.h | 3 +++ target/riscv/kvm/kvm-cpu.c | 34 ++++++++++++++++++++++++++-------- target/riscv/kvm/kvm_riscv.h | 3 ++- target/riscv/machine.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 9 deletions(-)target/riscv/cpu.h +3
-0@@ -20,6 +20,7 @@#ifndef RISCV_CPU_H #define RISCV_CPU_H +#include <linux/kvm.h> #include "hw/core/cpu.h" #include "hw/registerfields.h" #include "hw/qdev-properties.h" @@ -546,6 +547,8 @@ struct ArchCPU {RISCVCPUConfig cfg;RISCVSATPModes satp_modes; +struct kvm_mp_state mp_state; +QEMUTimer *pmu_timer;/* A bitmask of Available programmable counters */ uint32_t pmu_avail_ctrs;target/riscv/kvm/kvm-cpu.c +26
-8@@ -1348,17 +1348,16 @@ int kvm_arch_get_registers(CPUState *cs, Error **errp)return ret; } +RISCVCPU *cpu =RISCV_CPU (cs); + ret =kvm_riscv_sync_mpstate_to_qemu (cpu); +return ret; } -int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state) +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu) {if (cap_has_mp_state) { -struct kvm_mp_state mp_state = { - .mp_state = state - }; - -int ret =kvm_vcpu_ioctl (CPU (cpu),KVM_SET_MP_STATE , &mp_state); +int ret =kvm_vcpu_ioctl (CPU (cpu),KVM_SET_MP_STATE , &cpu->mp_state);if (ret) {fprintf (stderr,"%s: failed to sync MP_STATE %d/%s\n" , __func__, ret,strerror (-ret));@@ -1369,6 +1368,17 @@ int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state)return 0 ; } +int kvm_riscv_sync_mpstate_to_qemu (RISCVCPU *cpu) +{ +if (cap_has_mp_state) { +int ret =kvm_vcpu_ioctl (CPU (cpu),KVM_GET_MP_STATE , &cpu->mp_state); +if (ret) { +return ret; + } + } +return 0 ; +} +int kvm_arch_put_registers (CPUState *cs,int level,Error **errp) {int ret =0 ;@@ -1396,13 +1406,21 @@ int kvm_arch_put_registers(CPUState *cs, int level, Error **errp)if (KVM_PUT_RESET_STATE == level) {RISCVCPU *cpu =RISCV_CPU (cs);if (cs->cpu_index ==0 ) { - ret =kvm_riscv_sync_mpstate_to_kvm (cpu,KVM_MP_STATE_RUNNABLE ); + cpu->mp_state.mp_state =KVM_MP_STATE_RUNNABLE ; + ret =kvm_riscv_sync_mpstate_to_kvm (cpu); }else { - ret =kvm_riscv_sync_mpstate_to_kvm (cpu,KVM_MP_STATE_STOPPED ); + cpu->mp_state.mp_state =KVM_MP_STATE_STOPPED ; + ret =kvm_riscv_sync_mpstate_to_kvm (cpu); }if (ret) {return ret; } + }else if (KVM_PUT_FULL_STATE == level) { +RISCVCPU *cpu =RISCV_CPU (cs); + ret =kvm_riscv_sync_mpstate_to_kvm (cpu); +if (ret) { +return ret; + } }return ret;target/riscv/kvm/kvm_riscv.h +2
-1@@ -28,7 +28,8 @@ void kvm_riscv_aia_create(MachineState *machine, uint64_t group_shift, uint64_t aplic_base, uint64_t imsic_base, uint64_t guest_num);void riscv_kvm_aplic_request (void *opaque,int irq,int level); -int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state); +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu); +int kvm_riscv_sync_mpstate_to_qemu (RISCVCPU *cpu);void riscv_kvm_cpu_finalize_features (RISCVCPU *cpu,Error **errp);uint64_t kvm_riscv_get_timebase_frequency (RISCVCPU *cpu);target/riscv/machine.c +30
-0@@ -251,6 +251,28 @@ static const VMStateDescription vmstate_debug = { } }; +static int get_mp_state (QEMUFile *f,void *opaque,size_t size, +const VMStateField *field) +{ +RISCVCPU *cpu = opaque; + cpu->mp_state.mp_state =qemu_get_be32 (f); +return 0 ; +} + +static int put_mp_state (QEMUFile *f,void *opaque,size_t size, +const VMStateField *field,JSONWriter *vmdesc) +{ +RISCVCPU *cpu = opaque; +qemu_put_be32 (f, cpu->mp_state.mp_state); +return 0 ; +} + +static const VMStateInfo vmstate_mp_state = { + .name ="mp_state" , + .get = get_mp_state, + .put = put_mp_state, +}; +static int riscv_cpu_post_load (void *opaque,int version_id) {RISCVCPU *cpu = opaque;@@ -457,6 +479,14 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.stimecmp, RISCVCPU), + { + .name ="mp_state" , + .version_id = 0, + .size = sizeof(bool), + .info = &vmstate_mp_state, + .flags = VMS_SINGLE, + .offset = 0, + },VMSTATE_END_OF_LIST () },