Skip to contentSkip to conversation

target/riscv/kvm: fix env->priv setting in reset_regs_csr()

PATCH Applied Daniel Henrique Barboza 2 participants 3 messages Oct 22, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv/kvm: fix env->priv setting in reset_regs_csr()

This patch was originally made by Gitlab user Bo Gan (@ganboing) 4 months ago in the context of issue [1]. I asked the author to send a patch to the mailing list ~3 months ago and got no reply. I'm sending the patch myself because we already missed 10.1 without this fix.

I'll also just post verbatim Bo Gan comment in the commit msg:

"In RISCV Linux with KVM enabled, gdbstub is broken. The get_physical_address isn't able to page-walk correctly and resolve the physical page. This is due to that the vcpu is being treated as starting in M mode even if KVM enabled. However, with KVM, the vcpu is actually started in S mode. The mmu_idx will give 3 (M), instead of 1 (S), resulting in Guest PA == VA (wrong)!"

Set env->priv to PRV_S in kvm_riscv_reset_regs_csr() since the VCPU is always started in S-mode for KVM.

[1] https://gitlab.com/qemu-project/qemu/-/issues/2991

---
 target/riscv/kvm/kvm-cpu.c | 1 +
 1 file changed, 1 insertion(+)
target/riscv/kvm/kvm-cpu.c +1 -0
index 0dd0d59d41..000e44b2b7 100644
@@ -705,6 +705,7 @@ static void kvm_riscv_reset_regs_csr(CPURISCVState *env)
     env->satp = 0;
     env->scounteren = 0;
     env->senvcfg = 0;
+    env->priv = PRV_S;
 }
 
 static int kvm_riscv_get_regs_fp(CPUState *cs)
Signature · 1 line
2.51.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Daniel Henrique Barboza

This patch was originally made by Gitlab user Bo Gan (@ganboing) 4 months ago in the context of issue [1]. I asked the author to send a patch to the mailing list ~3 months ago and got no reply. I'm sending the patch myself because we already missed 10.1 without this fix.

I'll also just post verbatim Bo Gan comment in the commit msg:

"In RISCV Linux with KVM enabled, gdbstub is broken. The get_physical_address isn't able to page-walk correctly and resolve the physical page. This is due to that the vcpu is being treated as starting in M mode even if KVM enabled. However, with KVM, the vcpu is actually started in S mode. The mmu_idx will give 3 (M), instead of 1 (S), resulting in Guest PA == VA (wrong)!"

Set env->priv to PRV_S in kvm_riscv_reset_regs_csr() since the VCPU is always started in S-mode for KVM.

Quoted 6 lines

[1] https://gitlab.com/qemu-project/qemu/-/issues/2991

Alistair

Quoted 20 lines
---
 target/riscv/kvm/kvm-cpu.c | 1 +
 1 file changed, 1 insertion(+)
target/riscv/kvm/kvm-cpu.c +1 -1
index 0dd0d59d41..000e44b2b7 100644
@@ -705,6 +705,7 @@ static void kvm_riscv_reset_regs_csr(CPURISCVState *env)
     env->satp = 0;
     env->scounteren = 0;
     env->senvcfg = 0;
+    env->priv = PRV_S;
 }
 
 static int kvm_riscv_get_regs_fp(CPUState *cs)
--

2.51.0

#2 lore raw
In reply to Daniel Henrique Barboza
Quoted 18 lines · Daniel Henrique Barboza

This patch was originally made by Gitlab user Bo Gan (@ganboing) 4 months ago in the context of issue [1]. I asked the author to send a patch to the mailing list ~3 months ago and got no reply. I'm sending the patch myself because we already missed 10.1 without this fix.

I'll also just post verbatim Bo Gan comment in the commit msg:

"In RISCV Linux with KVM enabled, gdbstub is broken. The get_physical_address isn't able to page-walk correctly and resolve the physical page. This is due to that the vcpu is being treated as starting in M mode even if KVM enabled. However, with KVM, the vcpu is actually started in S mode. The mmu_idx will give 3 (M), instead of 1 (S), resulting in Guest PA == VA (wrong)!"

Set env->priv to PRV_S in kvm_riscv_reset_regs_csr() since the VCPU is always started in S-mode for KVM.

Quoted 6 lines

[1] https://gitlab.com/qemu-project/qemu/-/issues/2991

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 20 lines
---
 target/riscv/kvm/kvm-cpu.c | 1 +
 1 file changed, 1 insertion(+)
target/riscv/kvm/kvm-cpu.c +1 -1
index 0dd0d59d41..000e44b2b7 100644
@@ -705,6 +705,7 @@ static void kvm_riscv_reset_regs_csr(CPURISCVState *env)
     env->satp = 0;
     env->scounteren = 0;
     env->senvcfg = 0;
+    env->priv = PRV_S;
 }
 
 static int kvm_riscv_get_regs_fp(CPUState *cs)
--

2.51.0