target/riscv: fix the issue of guest reboot then no response or crash in kvm-mode
PATCH 1/1 target/riscv: fix the issue of guest reboot then no response or crash in kvm-mode
From: "liguang.zhang" <[email protected]>
There have a issue of guest reboot bug in kvm-mode:
1. in guest shell just run the reboot, guest can't reboot success, and host kvm stop the vcpu schedual.
2. for smp guest, ctrl+a+c switch to qemu command, use system_reset command to reset the guest, then vcpu crash
kernel log
```shell $reboot The system is going down NOW! Sent SIGTERM to all processes logout Sent SIGKILL to all processes Requesting system reboot ```
then no response
for qemu command:
$system_reset:
kernel log:
```shell [ 53.739556] kvm [150]: VCPU exit error -95 [ 53.739563] kvm [148]: VCPU exit error -95 [ 53.739557] kvm [149]: VCPU exit error -95 [ 53.740957] kvm [149]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.740957] kvm [148]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.741054] kvm [148]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.741058] kvm [149]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.756187] kvm [150]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.757797] kvm [150]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 ```
solution:
add reset csr and context for riscv vcpu qemu ioctl reset vcpu->arch.power_off state of kvm
tests:
qemu-system-riscv64 -M virt -bios none -kernel Image \
-smp 4 -enable-kvm \
-append "rootwait root=/dev/vda ro" \
-drive file=rootfs.ext2,format=raw,id=hd0 \
-device virtio-blk-device,drive=hd0
in guest shell:
$reboot
qemu command:
$system_reset
---
v2:
- update submit description
--- target/riscv/kvm.c | 43 ++++++++++++++++++++++++++++++++++++++++ target/riscv/kvm_riscv.h | 1 + 2 files changed, 44 insertions(+)
target/riscv/kvm.c +43 -0
@@ -42,6 +42,8 @@#include "migration/migration.h" #include "sysemu/runstate.h" +static bool cap_has_mp_state; +static uint64_t kvm_riscv_reg_id (CPURISCVState *env,uint64_t type,uint64_t idx) {
@@ -335,6 +337,25 @@ int kvm_arch_get_registers(CPUState *cs)return ret; } +int kvm_riscv_set_mpstate_to_kvm (RISCVCPU *cpu,int state) +{ +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); +if (ret) { +fprintf (stderr,"%s: failed to set MP_STATE %d/%s\n" , + __func__, ret,strerror (-ret)); +return -1 ; + } + } + +return 0 ; +} +int kvm_arch_put_registers (CPUState *cs,int level) {int ret =0 ;
@@ -354,6 +375,18 @@ int kvm_arch_put_registers(CPUState *cs, int level)return ret; } +if (KVM_PUT_RESET_STATE == level) { +RISCVCPU *cpu =RISCV_CPU (cs); +if (cs->cpu_index ==0 ) { + ret =kvm_riscv_set_mpstate_to_kvm (cpu,KVM_MP_STATE_RUNNABLE ); + }else { + ret =kvm_riscv_set_mpstate_to_kvm (cpu,KVM_MP_STATE_STOPPED ); + } +if (ret) { +return ret; + } + } +return ret; }
@@ -428,6 +461,7 @@ int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,int kvm_arch_init (MachineState *ms,KVMState *s) { + cap_has_mp_state =kvm_check_extension (s,KVM_CAP_MP_STATE );return 0 ; }
@@ -506,10 +540,19 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)if (!kvm_enabled ()) {return ; } +for (int i=0 ; i<32 ; i++) + env->gpr[i] =0 ; env->pc = cpu->env.kernel_addr; env->gpr[10 ] =kvm_arch_vcpu_id (CPU (cpu));/* a0 */ env->gpr[11 ] = cpu->env.fdt_addr;/* a1 */ env->satp =0 ; + env->mie =0 ; + env->stvec =0 ; + env->sscratch =0 ; + env->sepc =0 ; + env->scause =0 ; + env->stval =0 ; + env->mip =0 ; }void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level)
target/riscv/kvm_riscv.h +1 -0
@@ -21,5 +21,6 @@void kvm_riscv_reset_vcpu (RISCVCPU *cpu);void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level); +int kvm_riscv_set_mpstate_to_kvm (RISCVCPU *cpu,int state);#endif
Signature · 1 line
2.17.1
Quoted 2 lines · liguang.zhang
From: "liguang.zhang" <[email protected]>
Hello, thanks for the patch
Quoted 4 lines
There have a issue of guest reboot bug in kvm-mode:
1. in guest shell just run the reboot, guest can't reboot success, and host kvm stop the vcpu schedual.
2. for smp guest, ctrl+a+c switch to qemu command, usesystem_resetcommand to reset the guest, then vcpu crash
There are two issues when rebooting a guest using KVM
1. When the guest initiates a reboot the host is unable to stop the vcpu
2. When running a SMP guest the qemu monitor system_reset causes a vcpu crash
This can be fixed by clearing the CSR values at reset and syncing the MPSTATE with the host.
Quoted 73 lines
kernel log
```shell $reboot The system is going down NOW! Sent SIGTERM to all processes logout Sent SIGKILL to all processes Requesting system reboot ```then no response
for qemu command:
$system_reset:kernel log:
```shell [ 53.739556] kvm [150]: VCPU exit error -95 [ 53.739563] kvm [148]: VCPU exit error -95 [ 53.739557] kvm [149]: VCPU exit error -95 [ 53.740957] kvm [149]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.740957] kvm [148]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.741054] kvm [148]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.741058] kvm [149]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.756187] kvm [150]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.757797] kvm [150]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 ```solution:
add reset csr and context for riscv vcpu qemu ioctl reset
vcpu->arch.power_offstate of kvmtests:
qemu-system-riscv64 -M virt -bios none -kernel Image \ -smp 4 -enable-kvm \ -append "rootwait root=/dev/vda ro" \ -drive file=rootfs.ext2,format=raw,id=hd0 \ -device virtio-blk-device,drive=hd0in guest shell:
$rebootqemu command:
$system_reset---
v2:
- update submit descriptionSigned-off-by liguang.zhang <[email protected]>--- target/riscv/kvm.c | 43 ++++++++++++++++++++++++++++++++++++++++ target/riscv/kvm_riscv.h | 1 + 2 files changed, 44 insertions(+)target/riscv/kvm.c +2
-0@@ -42,6 +42,8 @@#include "migration/migration.h" #include "sysemu/runstate.h" +static bool cap_has_mp_state; +static uint64_t kvm_riscv_reg_id (CPURISCVState *env,uint64_t type,uint64_t idx) {
target/riscv/kvm.c int kvm_arch_get_registers(CPUState *cs) · line 340
Quoted 4 lines
target/riscv/kvm.c +1
-0@@ -335,6 +337,25 @@ int kvm_arch_get_registers(CPUState *cs)return ret; } +int kvm_riscv_set_mpstate_to_kvm (RISCVCPU *cpu,int state)
This should probably be called:
kvm_riscv_sync_mpstate_to_kvm()
instead
target/riscv/kvm.c int kvm_arch_get_registers(CPUState *cs) · line 343
Quoted 3 lines
target/riscv/kvm.c +3
-0+{ +if (cap_has_mp_state) { +
No newline required
Otherwise the patch looks good
Alistair
Quoted 80 lines
target/riscv/kvm.c +37
-0+struct kvm_mp_state mp_state = { + .mp_state = state + }; + +int ret =kvm_vcpu_ioctl (CPU (cpu),KVM_SET_MP_STATE , &mp_state); +if (ret) { +fprintf (stderr,"%s: failed to set MP_STATE %d/%s\n" , + __func__, ret,strerror (-ret)); +return -1 ; + } + } + +return 0 ; +} +int kvm_arch_put_registers (CPUState *cs,int level) {int ret =0 ;@@ -354,6 +375,18 @@ int kvm_arch_put_registers(CPUState *cs, int level)return ret; } +if (KVM_PUT_RESET_STATE == level) { +RISCVCPU *cpu =RISCV_CPU (cs); +if (cs->cpu_index ==0 ) { + ret =kvm_riscv_set_mpstate_to_kvm (cpu,KVM_MP_STATE_RUNNABLE ); + }else { + ret =kvm_riscv_set_mpstate_to_kvm (cpu,KVM_MP_STATE_STOPPED ); + } +if (ret) { +return ret; + } + } +return ret; }@@ -428,6 +461,7 @@ int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,int kvm_arch_init (MachineState *ms,KVMState *s) { + cap_has_mp_state =kvm_check_extension (s,KVM_CAP_MP_STATE );return 0 ; }@@ -506,10 +540,19 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)if (!kvm_enabled ()) {return ; } +for (int i=0 ; i<32 ; i++) + env->gpr[i] =0 ; env->pc = cpu->env.kernel_addr; env->gpr[10 ] =kvm_arch_vcpu_id (CPU (cpu));/* a0 */ env->gpr[11 ] = cpu->env.fdt_addr;/* a1 */ env->satp =0 ; + env->mie =0 ; + env->stvec =0 ; + env->sscratch =0 ; + env->sepc =0 ; + env->scause =0 ; + env->stval =0 ; + env->mip =0 ; }void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level)target/riscv/kvm_riscv.h +1
-1@@ -21,5 +21,6 @@void kvm_riscv_reset_vcpu (RISCVCPU *cpu);void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level); +int kvm_riscv_set_mpstate_to_kvm (RISCVCPU *cpu,int state);#endif --2.17.1
Hi Alistair:
Thanks for your reply.
I'll modify the change follow your suggestion, and push the new change, pls help to review again.
---- Replied Message ----
From Alistair Francis<[email protected]> <mailto:[email protected]> Date 6/23/2023 10:15 To liguang.zhang<[email protected]> <mailto:[email protected]> Cc [email protected]<[email protected]> ,
<mailto:[email protected]>[email protected]<[email protected]> , <mailto:[email protected]>张立广<[email protected]> <mailto:[email protected]>
Subject Re: [PATCH] target/riscv: fix the issue of guest reboot then no response or crash in kvm-mode
On Mon, Jun 12, 2023 at 11:07 PM liguang.zhang <[email protected]> wrote:
From: "liguang.zhang" <[email protected]>
Hello, thanks for the patch
There have a issue of guest reboot bug in kvm-mode:
1. in guest shell just run the reboot, guest can't reboot success, and host kvm stop the vcpu schedual.
2. for smp guest, ctrl+a+c switch to qemu command, use system_reset command to reset the guest, then vcpu crash
There are two issues when rebooting a guest using KVM
1. When the guest initiates a reboot the host is unable to stop the vcpu
2. When running a SMP guest the qemu monitor system_reset causes a vcpu crash
This can be fixed by clearing the CSR values at reset and syncing the MPSTATE with the host.
kernel log
```shell $reboot The system is going down NOW! Sent SIGTERM to all processes logout Sent SIGKILL to all processes Requesting system reboot ```
then no response
for qemu command:
$system_reset:
kernel log:
```shell [ 53.739556] kvm [150]: VCPU exit error -95 [ 53.739563] kvm [148]: VCPU exit error -95 [ 53.739557] kvm [149]: VCPU exit error -95 [ 53.740957] kvm [149]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.740957] kvm [148]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.741054] kvm [148]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.741058] kvm [149]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.756187] kvm [150]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.757797] kvm [150]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 ```
solution:
add reset csr and context for riscv vcpu qemu ioctl reset vcpu->arch.power_off state of kvm
tests:
qemu-system-riscv64 -M virt -bios none -kernel Image \
-smp 4 -enable-kvm \
-append "rootwait root=/dev/vda ro" \
-drive file=rootfs.ext2,format=raw,id=hd0 \
-device virtio-blk-device,drive=hd0
in guest shell:
$reboot
qemu command:
$system_reset
---
v2:
- update submit description
---target/riscv/kvm.c | 43 ++++++++++++++++++++++++++++++++++++++++target/riscv/kvm_riscv.h | 1 +
2 files changed, 44 insertions(+)
target/riscv/kvm.c +0 -0
@@ -42,6 +42,8 @@
#include "migration/migration.h"
#include "sysemu/runstate.h"
+static bool cap_has_mp_state;
+
static uint64_t kvm_riscv_reg_id(CPURISCVState *env, uint64_t type, uint64_t idx)
{
@@ -335,6 +337,25 @@ int kvm_arch_get_registers(CPUState *cs) return ret;
}
+int kvm_riscv_set_mpstate_to_kvm(RISCVCPU *cpu, int state)
This should probably be called:
kvm_riscv_sync_mpstate_to_kvm()
instead
+{
+ if (cap_has_mp_state) {
+
No newline required
Otherwise the patch looks good
Alistair
+ struct kvm_mp_state mp_state = {
+ .mp_state = state
+ };
+
+ int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
+ if (ret) {
+ fprintf(stderr, "%s: failed to set MP_STATE %d/%s\n",
+ __func__, ret, strerror(-ret));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
int kvm_arch_put_registers(CPUState *cs, int level)
{
int ret = 0;
@@ -354,6 +375,18 @@ int kvm_arch_put_registers(CPUState *cs, int level) return ret;
}
+ if (KVM_PUT_RESET_STATE == level) {
+ RISCVCPU *cpu = RISCV_CPU(cs);
+ if (cs->cpu_index == 0) {
+ ret = kvm_riscv_set_mpstate_to_kvm(cpu, KVM_MP_STATE_RUNNABLE);
+ } else {
+ ret = kvm_riscv_set_mpstate_to_kvm(cpu, KVM_MP_STATE_STOPPED);
+ }
+ if (ret) {
+ return ret;
+ }
+ }
+
return ret;
}
@@ -428,6 +461,7 @@ int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
int kvm_arch_init(MachineState *ms, KVMState *s)
{
+ cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE);
return 0;
}
@@ -506,10 +540,19 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu) if (!kvm_enabled()) {
return;
}
+ for (int i=0; i<32; i++)
+ env->gpr[i] = 0;env->pc = cpu->env.kernel_addr;
env->gpr[10] = kvm_arch_vcpu_id(CPU(cpu)); /* a0 */
env->gpr[11] = cpu->env.fdt_addr; /* a1 */env->satp = 0;
+ env->mie = 0; + env->stvec = 0; + env->sscratch = 0; + env->sepc = 0; + env->scause = 0; + env->stval = 0; + env->mip = 0;
}
void kvm_riscv_set_irq(RISCVCPU *cpu, int irq, int level)
target/riscv/kvm_riscv.h +0 -0
@@ -21,5 +21,6 @@
void kvm_riscv_reset_vcpu(RISCVCPU *cpu);
void kvm_riscv_set_irq(RISCVCPU *cpu, int irq, int level); +int kvm_riscv_set_mpstate_to_kvm(RISCVCPU *cpu, int state);
#endif
--
2.17.1
This message contains confidential information and is intended only for the individual(s) addressed in the message. If you aren't the named addressee, you should not disseminate, distribute, or copy this e-mail.
From: "liguang.zhang" <[email protected]>
Quoted 78 lines · liguang.zhang
Quoted 8 lines · On Sun, July 10, 2023 at 9:17 alistair <[email protected]> wrote:
From: "liguang.zhang" <[email protected]>
There are two issues when rebooting a guest using KVM
1. When the guest initiates a reboot the host is unable to stop the vcpu
2. When running a SMP guest the qemu monitorsystem_resetcauses a vcpu crashThis can be fixed by clearing the CSR values at reset and syncing the MPSTATE with the host.
Thanks for the patch.
I think we should move everything
--- from here ---
Quoted 45 lines
kernel log
```shell $reboot The system is going down NOW! Sent SIGTERM to all processes logout Sent SIGKILL to all processes Requesting system reboot ```then no response
for qemu command:
$system_reset:kernel log:
```shell [ 53.739556] kvm [150]: VCPU exit error -95 [ 53.739563] kvm [148]: VCPU exit error -95 [ 53.739557] kvm [149]: VCPU exit error -95 [ 53.740957] kvm [149]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.740957] kvm [148]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.741054] kvm [148]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.741058] kvm [149]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.756187] kvm [150]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.757797] kvm [150]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 ```solution:
add reset csr and context for riscv vcpu qemu ioctl reset
vcpu->arch.power_offstate of kvmtests:
qemu-system-riscv64 -M virt -bios none -kernel Image \
(quoted diff) +0
-4-smp 4 -enable-kvm \ -append "rootwait root=/dev/vda ro" \ -drive file=rootfs.ext2,format=raw,id=hd0 \ -device virtio-blk-device,drive=hd0in guest shell:
$rebootQuoted 9 lines
qemu command:
$system_reset---
v3:
- changekvm_riscv_set_mpstate_to_kvmtokvm_riscv_sync_mpstate_to_kvm
- remove newline afterif(cap_has_mp_state)
- update submit description--- to here ---
Quoted 2 lines
Signed-off-by liguang.zhang <[email protected]>---
Quoted 7 lines
below this line. That way it will be included in the patch submission, but won't be included in the git tree.
We never include the patch changelog in the git tree and I don't think we need to include the steps as well (they will be preserved on the mailing list).
Ok, It's no problem.
Quoted 4 lines
For the patch title (the git commit title) can you explain what the patch is doing?
How about the title use:
"target/riscv: Clearing the CSR values at reset and syncing the MPSTATE with the host"
And the reason description is
Fix the guest reboot error when using KVM
There are two issues when rebooting a guest using KVM
1. When the guest initiates a reboot the host is unable to stop the vcpu
2. When running a SMP guest the qemu monitor system_reset causes a vcpu crash
This can be fixed by clearing the CSR values at reset and syncing the MPSTATE with the host.
Quoted 4 lines
Also, do you mind rebasing on
https://github.com/alistair23/qemu/tree/riscv-to-apply.next
I don't remind rebase on it.
Thanks~
-- liguang.zhang
Quoted 116 lines
Then the patch should be good to go!
Alistair
target/riscv/kvm.c| 44 +++++++++++++++++++++++++++++++++++++++-target/riscv/kvm_riscv.h| 1 +2 files changed, 44 insertions(+), 1 deletion(-)target/riscv/kvm.c +36
-1@@ -42,6 +42,8 @@#include "migration/migration.h" #include "sysemu/runstate.h" +static bool cap_has_mp_state; +static uint64_t kvm_riscv_reg_id (CPURISCVState *env,uint64_t type,uint64_t idx) {@@ -99,7 +101,7 @@ static uint64_t kvm_riscv_reg_id(CPURISCVState *env, uint64_t type,#define KVM_RISCV_SET_TIMER (cs, env, name, reg) \ do { \ - int ret = kvm_set_one_reg(cs, RISCV_TIMER_REG(env, time), ®); \ + int ret = kvm_set_one_reg(cs, RISCV_TIMER_REG(env, name), ®); \ if (ret) { \ abort(); \ } \@@ -335,6 +337,24 @@ int kvm_arch_get_registers(CPUState *cs)return ret; } +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state) +{ +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); +if (ret) { +fprintf (stderr,"%s: failed to sync MP_STATE %d/%s\n" , + __func__, ret,strerror (-ret)); +return -1 ; + } + } + +return 0 ; +} +int kvm_arch_put_registers (CPUState *cs,int level) {int ret =0 ;@@ -354,6 +374,18 @@ int kvm_arch_put_registers(CPUState *cs, int level)return ret; } +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 ); + }else { + ret =kvm_riscv_sync_mpstate_to_kvm (cpu,KVM_MP_STATE_STOPPED ); + } +if (ret) { +return ret; + } + } +return ret; }@@ -428,6 +460,7 @@ int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,int kvm_arch_init (MachineState *ms,KVMState *s) { + cap_has_mp_state =kvm_check_extension (s,KVM_CAP_MP_STATE );return 0 ; }@@ -506,10 +539,19 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)if (!kvm_enabled ()) {return ; } +for (int i=0 ; i<32 ; i++) + env->gpr[i] =0 ; env->pc = cpu->env.kernel_addr;env->gpr[10] = kvm_arch_vcpu_id(CPU(cpu)); /* a0 */
env->gpr[11] =cpu->env.fdt_addr; /* a1 */env->satp= 0;target/riscv/kvm.c +7
-0+ env->mie =0 ; + env->stvec =0 ; + env->sscratch =0 ; + env->sepc =0 ; + env->scause =0 ; + env->stval =0 ; + env->mip =0 ;}
void
kvm_riscv_set_irq(RISCVCPU *cpu, int irq, int level)target/riscv/kvm_riscv.h +1
-1@@ -21,5 +21,6 @@void kvm_riscv_reset_vcpu (RISCVCPU *cpu);void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level); +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state);#endif --2.17.1
From: "liguang.zhang" <[email protected]>
Quoted 78 lines · liguang.zhang
Quoted 8 lines · On Sun, July 10, 2023 at 9:17 alistair <[email protected]> wrote:
From: "liguang.zhang" <[email protected]>
There are two issues when rebooting a guest using KVM
1. When the guest initiates a reboot the host is unable to stop the vcpu
2. When running a SMP guest the qemu monitorsystem_resetcauses a vcpu crashThis can be fixed by clearing the CSR values at reset and syncing the MPSTATE with the host.
Thanks for the patch.
I think we should move everything
--- from here ---
Quoted 45 lines
kernel log
```shell $reboot The system is going down NOW! Sent SIGTERM to all processes logout Sent SIGKILL to all processes Requesting system reboot ```then no response
for qemu command:
$system_reset:kernel log:
```shell [ 53.739556] kvm [150]: VCPU exit error -95 [ 53.739563] kvm [148]: VCPU exit error -95 [ 53.739557] kvm [149]: VCPU exit error -95 [ 53.740957] kvm [149]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.740957] kvm [148]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.741054] kvm [148]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.741058] kvm [149]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 [ 53.756187] kvm [150]: SEPC=0x0 SSTATUS=0x200004120 HSTATUS=0x2002001c0 [ 53.757797] kvm [150]: SCAUSE=0x14 STVAL=0x0 HTVAL=0x0 HTINST=0x0 ```solution:
add reset csr and context for riscv vcpu qemu ioctl reset
vcpu->arch.power_offstate of kvmtests:
qemu-system-riscv64 -M virt -bios none -kernel Image \
(quoted diff) +0
-4-smp 4 -enable-kvm \ -append "rootwait root=/dev/vda ro" \ -drive file=rootfs.ext2,format=raw,id=hd0 \ -device virtio-blk-device,drive=hd0in guest shell:
$rebootQuoted 9 lines
qemu command:
$system_reset---
v3:
- changekvm_riscv_set_mpstate_to_kvmtokvm_riscv_sync_mpstate_to_kvm
- remove newline afterif(cap_has_mp_state)
- update submit description--- to here ---
Quoted 2 lines
Signed-off-by liguang.zhang <[email protected]>---
Quoted 7 lines
below this line. That way it will be included in the patch submission, but won't be included in the git tree.
We never include the patch changelog in the git tree and I don't think we need to include the steps as well (they will be preserved on the mailing list).
Ok, It's no problem.
Quoted 4 lines
For the patch title (the git commit title) can you explain what the patch is doing?
How about the title use:
"target/riscv: Clearing the CSR values at reset and syncing the MPSTATE with the host"
And the reason description is
Fix the guest reboot error when using KVM
There are two issues when rebooting a guest using KVM
1. When the guest initiates a reboot the host is unable to stop the vcpu
2. When running a SMP guest the qemu monitor system_reset causes a vcpu crash
This can be fixed by clearing the CSR values at reset and syncing the MPSTATE with the host.
Quoted 4 lines
Also, do you mind rebasing on
https://github.com/alistair23/qemu/tree/riscv-to-apply.next
I don't remind rebase on it.
Thanks~
-- liguang.zhang
Quoted 116 lines
Then the patch should be good to go!
Alistair
target/riscv/kvm.c| 44 +++++++++++++++++++++++++++++++++++++++-target/riscv/kvm_riscv.h| 1 +2 files changed, 44 insertions(+), 1 deletion(-)target/riscv/kvm.c +36
-1@@ -42,6 +42,8 @@#include "migration/migration.h" #include "sysemu/runstate.h" +static bool cap_has_mp_state; +static uint64_t kvm_riscv_reg_id (CPURISCVState *env,uint64_t type,uint64_t idx) {@@ -99,7 +101,7 @@ static uint64_t kvm_riscv_reg_id(CPURISCVState *env, uint64_t type,#define KVM_RISCV_SET_TIMER (cs, env, name, reg) \ do { \ - int ret = kvm_set_one_reg(cs, RISCV_TIMER_REG(env, time), ®); \ + int ret = kvm_set_one_reg(cs, RISCV_TIMER_REG(env, name), ®); \ if (ret) { \ abort(); \ } \@@ -335,6 +337,24 @@ int kvm_arch_get_registers(CPUState *cs)return ret; } +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state) +{ +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); +if (ret) { +fprintf (stderr,"%s: failed to sync MP_STATE %d/%s\n" , + __func__, ret,strerror (-ret)); +return -1 ; + } + } + +return 0 ; +} +int kvm_arch_put_registers (CPUState *cs,int level) {int ret =0 ;@@ -354,6 +374,18 @@ int kvm_arch_put_registers(CPUState *cs, int level)return ret; } +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 ); + }else { + ret =kvm_riscv_sync_mpstate_to_kvm (cpu,KVM_MP_STATE_STOPPED ); + } +if (ret) { +return ret; + } + } +return ret; }@@ -428,6 +460,7 @@ int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,int kvm_arch_init (MachineState *ms,KVMState *s) { + cap_has_mp_state =kvm_check_extension (s,KVM_CAP_MP_STATE );return 0 ; }@@ -506,10 +539,19 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)if (!kvm_enabled ()) {return ; } +for (int i=0 ; i<32 ; i++) + env->gpr[i] =0 ; env->pc = cpu->env.kernel_addr;env->gpr[10] = kvm_arch_vcpu_id(CPU(cpu)); /* a0 */
env->gpr[11] =cpu->env.fdt_addr; /* a1 */env->satp= 0;target/riscv/kvm.c +7
-0+ env->mie =0 ; + env->stvec =0 ; + env->sscratch =0 ; + env->sepc =0 ; + env->scause =0 ; + env->stval =0 ; + env->mip =0 ;}
void
kvm_riscv_set_irq(RISCVCPU *cpu, int irq, int level)target/riscv/kvm_riscv.h +1
-1@@ -21,5 +21,6 @@void kvm_riscv_reset_vcpu (RISCVCPU *cpu);void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level); +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state);#endif --2.17.1
PATCH 1/1 target/riscv: Clearing the CSR values at reset and syncing the MPSTATE with the host
In reply to Alistair FrancisFrom: "liguang.zhang" <[email protected]>
Fix the guest reboot error when using KVM
There are two issues when rebooting a guest using KVM
1. When the guest initiates a reboot the host is unable to stop the vcpu
2. When running a SMP guest the qemu monitor system_reset causes a vcpu crash
This can be fixed by clearing the CSR values at reset and syncing the MPSTATE with the host.
--- target/riscv/kvm.c | 42 ++++++++++++++++++++++++++++++++++++++++ target/riscv/kvm_riscv.h | 1 + 2 files changed, 43 insertions(+)
target/riscv/kvm.c +42 -0
@@ -44,6 +44,8 @@#include "migration/migration.h" #include "sysemu/runstate.h" +static bool cap_has_mp_state; +static uint64_t kvm_riscv_reg_id (CPURISCVState *env,uint64_t type,uint64_t idx) {
@@ -790,6 +792,24 @@ int kvm_arch_get_registers(CPUState *cs)return ret; } +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state) +{ +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); +if (ret) { +fprintf (stderr,"%s: failed to sync MP_STATE %d/%s\n" , + __func__, ret,strerror (-ret)); +return -1 ; + } + } + +return 0 ; +} +int kvm_arch_put_registers (CPUState *cs,int level) {int ret =0 ;
@@ -809,6 +829,18 @@ int kvm_arch_put_registers(CPUState *cs, int level)return ret; } +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 ); + }else { + ret =kvm_riscv_sync_mpstate_to_kvm (cpu,KVM_MP_STATE_STOPPED ); + } +if (ret) { +return ret; + } + } +return ret; }
@@ -909,6 +941,7 @@ int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,int kvm_arch_init (MachineState *ms,KVMState *s) { + cap_has_mp_state =kvm_check_extension (s,KVM_CAP_MP_STATE );return 0 ; }
@@ -987,10 +1020,19 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)if (!kvm_enabled ()) {return ; } +for (int i=0 ; i<32 ; i++) + env->gpr[i] =0 ; env->pc = cpu->env.kernel_addr; env->gpr[10 ] =kvm_arch_vcpu_id (CPU (cpu));/* a0 */ env->gpr[11 ] = cpu->env.fdt_addr;/* a1 */ env->satp =0 ; + env->mie =0 ; + env->stvec =0 ; + env->sscratch =0 ; + env->sepc =0 ; + env->scause =0 ; + env->stval =0 ; + env->mip =0 ; }void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level)
target/riscv/kvm_riscv.h +1 -0
@@ -22,5 +22,6 @@void kvm_riscv_init_user_properties (Object *cpu_obj);void kvm_riscv_reset_vcpu (RISCVCPU *cpu);void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level); +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state);#endif
Signature · 1 line
2.17.1
PATCH 1/1 target/riscv: Clearing the CSR values at reset and syncing the MPSTATE with the host
In reply to Alistair FrancisFrom: "liguang.zhang" <[email protected]>
Fix the guest reboot error when using KVM
There are two issues when rebooting a guest using KVM
1. When the guest initiates a reboot the host is unable to stop the vcpu
2. When running a SMP guest the qemu monitor system_reset causes a vcpu crash
This can be fixed by clearing the CSR values at reset and syncing the MPSTATE with the host.
--- target/riscv/kvm.c | 42 ++++++++++++++++++++++++++++++++++++++++ target/riscv/kvm_riscv.h | 1 + 2 files changed, 43 insertions(+)
target/riscv/kvm.c +42 -0
@@ -44,6 +44,8 @@#include "migration/migration.h" #include "sysemu/runstate.h" +static bool cap_has_mp_state; +static uint64_t kvm_riscv_reg_id (CPURISCVState *env,uint64_t type,uint64_t idx) {
@@ -790,6 +792,24 @@ int kvm_arch_get_registers(CPUState *cs)return ret; } +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state) +{ +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); +if (ret) { +fprintf (stderr,"%s: failed to sync MP_STATE %d/%s\n" , + __func__, ret,strerror (-ret)); +return -1 ; + } + } + +return 0 ; +} +int kvm_arch_put_registers (CPUState *cs,int level) {int ret =0 ;
@@ -809,6 +829,18 @@ int kvm_arch_put_registers(CPUState *cs, int level)return ret; } +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 ); + }else { + ret =kvm_riscv_sync_mpstate_to_kvm (cpu,KVM_MP_STATE_STOPPED ); + } +if (ret) { +return ret; + } + } +return ret; }
@@ -909,6 +941,7 @@ int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,int kvm_arch_init (MachineState *ms,KVMState *s) { + cap_has_mp_state =kvm_check_extension (s,KVM_CAP_MP_STATE );return 0 ; }
@@ -987,10 +1020,19 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)if (!kvm_enabled ()) {return ; } +for (int i=0 ; i<32 ; i++) + env->gpr[i] =0 ; env->pc = cpu->env.kernel_addr; env->gpr[10 ] =kvm_arch_vcpu_id (CPU (cpu));/* a0 */ env->gpr[11 ] = cpu->env.fdt_addr;/* a1 */ env->satp =0 ; + env->mie =0 ; + env->stvec =0 ; + env->sscratch =0 ; + env->sepc =0 ; + env->scause =0 ; + env->stval =0 ; + env->mip =0 ; }void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level)
target/riscv/kvm_riscv.h +1 -0
@@ -22,5 +22,6 @@void kvm_riscv_init_user_properties (Object *cpu_obj);void kvm_riscv_reset_vcpu (RISCVCPU *cpu);void kvm_riscv_set_irq (RISCVCPU *cpu,int irq,int level); +int kvm_riscv_sync_mpstate_to_kvm (RISCVCPU *cpu,int state);#endif
Signature · 1 line
2.17.1