Hi,
On 10/17/2026 11:51 AM, Wang Yechao wrote:
Save and restore the KVM SBI state as a vmstate subsection, so that
a guest using SBI STA keeps working after live migration. Only the
STA shmem address (shmem_lo and shmem_hi) is saved for now; other
SBI state can be appended to kvm_sbi_state as it is added.
Signed-off-by: Wang Yechao <[email protected]>
---
target/riscv/machine.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/target/riscv/machine.c b/target/riscv/machine.c
index 0de06e9588..b8cdca79cc 100644
--- a/target/riscv/machine.c
+++ b/target/riscv/machine.c
@@ -318,6 +318,23 @@ static const VMStateDescription vmstate_kvm_mp_state = {
VMSTATE_END_OF_LIST()
}
};
+
+static bool kvm_sbi_state_needed(void *opaque)
+{
+ return kvm_enabled();
+}
I think kvm_enabled() isn't enough here since there's no guarantee that
the current running kvm driver supports SBI STA.
If we take the existing mp_state as an example, in kvm-cpu.c we have:
bool kvm_riscv_has_mp_state(void)
{
return cap_has_mp_state;
}
And then, in machine.c:
static bool kvm_mp_state_needed(void *opaque)
{
return kvm_enabled() && kvm_riscv_has_mp_state();
}
I think we can do the same here. In patch 1 you're setting
kvm_sbi_sta.supported
to true if SBI STA is available. We can add a:
bool kvm_riscv_has_sbi_sta_state(void)
{
return kvm_sbi_sta.supported;
}
And then you can use it as another check in kvm_sbi_state_needed() too.
Thanks,
Daniel
+
+static const VMStateDescription vmstate_kvm_sbi_state = {
+ .name = "cpu/kvm-sbi-state",
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .needed = kvm_sbi_state_needed,
+ .fields = (const VMStateField[]) {
+ VMSTATE_UINT64(env.kvm_sbi_sta.shmem_lo, RISCVCPU),
+ VMSTATE_UINT64(env.kvm_sbi_sta.shmem_hi, RISCVCPU),
+ VMSTATE_END_OF_LIST()
+ }
+};
#endif
static bool debug_needed(void *opaque)
@@ -645,6 +662,7 @@ const VMStateDescription vmstate_riscv_cpu = {
&vmstate_kvm_timer_frequency,
&vmstate_kvmtimer,
&vmstate_kvm_mp_state,
+ &vmstate_kvm_sbi_state,
#endif
&vmstate_envcfg,
&vmstate_debug,