Skip to contentSkip to conversation

target/riscv/kvm: reset time changes

PATCH Applied Daniel Henrique Barboza 4 participants 12 messages Feb 24, 2025 – Mar 20, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

v3 cover target/riscv/kvm: reset time changes

Hi,

In this version I rolled back on the riscv_cpu_reset_hold() changes made in patch 1. Peter made an argument about keeping the design the same across architectures and I agreed. Patches 2 and 3 are already taking care of everything that KVM needs during reset, thus this doesn't incur in any functional changes.

Drew said it was ok to keep his ack on patch 1 so his ack is kept.

Patches based on alistair/riscv-to-apply.next. All patches acked.

Changes from v2:
- patch 1:
- do not do an early exit in riscv_cpu_reset_hold() if kvm_enabled()
- v2 link: https://lore.kernel.org/qemu-riscv/[email protected]/

Daniel Henrique Barboza (3):
target/riscv/cpu: remove unneeded !kvm_enabled() check target/riscv/kvm: add kvm_riscv_reset_regs_csr() target/riscv/kvm: add missing KVM CSRs

 target/riscv/kvm/kvm-cpu.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/3 target/riscv/cpu: remove unneeded !kvm_enabled() check

Remove the !kvm_enabled() check in kvm_riscv_reset_vcpu() since the function is already being gated by kvm_enabled() in riscv_cpu_reset_hold().

---
 target/riscv/kvm/kvm-cpu.c | 3 ---
 1 file changed, 3 deletions(-)
target/riscv/kvm/kvm-cpu.c +0 -3
index 471fd554b3..19bb87515b 100644
@@ -1606,9 +1606,6 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)
     CPURISCVState *env = &cpu->env;
     int i;
 
-    if (!kvm_enabled()) {
-        return;
-    }
     for (i = 0; i < 32; i++) {
         env->gpr[i] = 0;
     }
Signature · 1 line
2.48.1
Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] target/riscv/cpu: remove unneeded !kvm_enabled() check

Quoted 7 lines · Daniel Henrique Barboza

Remove the !kvm_enabled() check in kvm_riscv_reset_vcpu() since the function is already being gated by kvm_enabled() in riscv_cpu_reset_hold().

Alistair

Quoted 22 lines
---
 target/riscv/kvm/kvm-cpu.c | 3 ---
 1 file changed, 3 deletions(-)
target/riscv/kvm/kvm-cpu.c +0 -4
index 471fd554b3..19bb87515b 100644
@@ -1606,9 +1606,6 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)
     CPURISCVState *env = &cpu->env;
     int i;
 
-    if (!kvm_enabled()) {
-        return;
-    }
     for (i = 0; i < 32; i++) {
         env->gpr[i] = 0;
     }
--

2.48.1

#3 lore raw

PATCH v3 2/3 target/riscv/kvm: add kvm_riscv_reset_regs_csr()

In reply to Daniel Henrique Barboza

We're setting reset vals for KVM csrs during kvm_riscv_reset_vcpu(), but in no particular order and missing some of them (like env->mstatus).

Create a helper to do that, unclogging reset_vcpu(), and initialize env->mstatus as well. Keep the regs in the same order they appear in struct kvm_riscv_csr from the KVM UAPI, similar to what kvm_riscv_(get|put)_regs_csr are doing. This will make a bit easier to add new KVM CSRs and to verify which values we're writing back to KVM during vcpu reset.

---
 target/riscv/kvm/kvm-cpu.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
target/riscv/kvm/kvm-cpu.c +15 -8
index 19bb87515b..cabc34b6a2 100644
@@ -608,6 +608,19 @@ static int kvm_riscv_put_regs_core(CPUState *cs)
     return ret;
 }
 
+static void kvm_riscv_reset_regs_csr(CPURISCVState *env)
+{
+    env->mstatus = 0;
+    env->mie = 0;
+    env->stvec = 0;
+    env->sscratch = 0;
+    env->sepc = 0;
+    env->scause = 0;
+    env->stval = 0;
+    env->mip = 0;
+    env->satp = 0;
+}
+
 static int kvm_riscv_get_regs_csr(CPUState *cs)
 {
     CPURISCVState *env = &RISCV_CPU(cs)->env;
@@ -1612,14 +1625,8 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)
     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;
+
+    kvm_riscv_reset_regs_csr(env);
 }
 
 void kvm_riscv_set_irq(RISCVCPU *cpu, int irq, int level)
Signature · 1 line
2.48.1
Reviewed-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] target/riscv/kvm: add kvm_riscv_reset_regs_csr()

Quoted 11 lines · Daniel Henrique Barboza

We're setting reset vals for KVM csrs during kvm_riscv_reset_vcpu(), but in no particular order and missing some of them (like env->mstatus).

Create a helper to do that, unclogging reset_vcpu(), and initialize env->mstatus as well. Keep the regs in the same order they appear in struct kvm_riscv_csr from the KVM UAPI, similar to what kvm_riscv_(get|put)_regs_csr are doing. This will make a bit easier to add new KVM CSRs and to verify which values we're writing back to KVM during vcpu reset.

Quoted 2 lines

Alistair

Quoted 49 lines
---
 target/riscv/kvm/kvm-cpu.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
target/riscv/kvm/kvm-cpu.c +15 -9
index 19bb87515b..cabc34b6a2 100644
@@ -608,6 +608,19 @@ static int kvm_riscv_put_regs_core(CPUState *cs)
     return ret;
 }
 
+static void kvm_riscv_reset_regs_csr(CPURISCVState *env)
+{
+    env->mstatus = 0;
+    env->mie = 0;
+    env->stvec = 0;
+    env->sscratch = 0;
+    env->sepc = 0;
+    env->scause = 0;
+    env->stval = 0;
+    env->mip = 0;
+    env->satp = 0;
+}
+
 static int kvm_riscv_get_regs_csr(CPUState *cs)
 {
     CPURISCVState *env = &RISCV_CPU(cs)->env;
@@ -1612,14 +1625,8 @@ void kvm_riscv_reset_vcpu(RISCVCPU *cpu)
     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;
+
+    kvm_riscv_reset_regs_csr(env);
 }
 
 void kvm_riscv_set_irq(RISCVCPU *cpu, int irq, int level)
--

2.48.1

#5 lore raw

PATCH v3 3/3 target/riscv/kvm: add missing KVM CSRs

In reply to Daniel Henrique Barboza

We're missing scounteren and senvcfg CSRs, both already present in the KVM UAPI.

---
 target/riscv/kvm/kvm-cpu.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/riscv/kvm/kvm-cpu.c +6 -0
index cabc34b6a2..e8c31a32d4 100644
@@ -619,6 +619,8 @@ static void kvm_riscv_reset_regs_csr(CPURISCVState *env)
     env->stval = 0;
     env->mip = 0;
     env->satp = 0;
+    env->scounteren = 0;
+    env->senvcfg = 0;
 }
 
 static int kvm_riscv_get_regs_csr(CPUState *cs)
@@ -634,6 +636,8 @@ static int kvm_riscv_get_regs_csr(CPUState *cs)
     KVM_RISCV_GET_CSR(cs, env, stval, env->stval);
     KVM_RISCV_GET_CSR(cs, env, sip, env->mip);
     KVM_RISCV_GET_CSR(cs, env, satp, env->satp);
+    KVM_RISCV_GET_CSR(cs, env, scounteren, env->scounteren);
+    KVM_RISCV_GET_CSR(cs, env, senvcfg, env->senvcfg);
 
     return 0;
 }
@@ -651,6 +655,8 @@ static int kvm_riscv_put_regs_csr(CPUState *cs)
     KVM_RISCV_SET_CSR(cs, env, stval, env->stval);
     KVM_RISCV_SET_CSR(cs, env, sip, env->mip);
     KVM_RISCV_SET_CSR(cs, env, satp, env->satp);
+    KVM_RISCV_SET_CSR(cs, env, scounteren, env->scounteren);
+    KVM_RISCV_SET_CSR(cs, env, senvcfg, env->senvcfg);
 
     return 0;
 }
Signature · 1 line
2.48.1
Acked-by on v3 3/3
#6 lore raw

Re: [PATCH v3 3/3] target/riscv/kvm: add missing KVM CSRs

Quoted 6 lines · Daniel Henrique Barboza

We're missing scounteren and senvcfg CSRs, both already present in the KVM UAPI.

Alistair

Quoted 39 lines
---
 target/riscv/kvm/kvm-cpu.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/riscv/kvm/kvm-cpu.c +6 -1
index cabc34b6a2..e8c31a32d4 100644
@@ -619,6 +619,8 @@ static void kvm_riscv_reset_regs_csr(CPURISCVState *env)
     env->stval = 0;
     env->mip = 0;
     env->satp = 0;
+    env->scounteren = 0;
+    env->senvcfg = 0;
 }
 
 static int kvm_riscv_get_regs_csr(CPUState *cs)
@@ -634,6 +636,8 @@ static int kvm_riscv_get_regs_csr(CPUState *cs)
     KVM_RISCV_GET_CSR(cs, env, stval, env->stval);
     KVM_RISCV_GET_CSR(cs, env, sip, env->mip);
     KVM_RISCV_GET_CSR(cs, env, satp, env->satp);
+    KVM_RISCV_GET_CSR(cs, env, scounteren, env->scounteren);
+    KVM_RISCV_GET_CSR(cs, env, senvcfg, env->senvcfg);
 
     return 0;
 }
@@ -651,6 +655,8 @@ static int kvm_riscv_put_regs_csr(CPUState *cs)
     KVM_RISCV_SET_CSR(cs, env, stval, env->stval);
     KVM_RISCV_SET_CSR(cs, env, sip, env->mip);
     KVM_RISCV_SET_CSR(cs, env, satp, env->satp);
+    KVM_RISCV_SET_CSR(cs, env, scounteren, env->scounteren);
+    KVM_RISCV_SET_CSR(cs, env, senvcfg, env->senvcfg);
 
     return 0;
 }
--

2.48.1

#7 lore raw

Re: [PATCH v3 3/3] target/riscv/kvm: add missing KVM CSRs

Quoted 8 lines · Alistair Francis
Quoted 5 lines · Daniel Henrique Barboza

We're missing scounteren and senvcfg CSRs, both already present in the KVM UAPI.

This patch seems to have broken KVM acceleration for me:

  $ ./build/qemu-system-riscv64 -display none -M virt,accel=kvm -cpu host
  qemu-system-riscv64: Failed to put registers after init: No such
file or directory

Reverting it makes QEMU work again.

My host is a SiFive HiFive Premier P550 board running Fedora 41. Note that, since the upstreaming effort for this SoC has just recently started, I'm using the 6.6-based vendor kernel.

Perhaps the KVM UAPI additions mentioned in the commit message are more recent than that, and we need to make QEMU's use of them conditional rather than unconditional?

Signature · 1 line
Andrea Bolognani / Red Hat / Virtualization
#8 lore raw

Re: [PATCH v3 3/3] target/riscv/kvm: add missing KVM CSRs

Quoted 18 lines · Andrea Bolognani
Quoted 8 lines · Alistair Francis
Quoted 5 lines · Daniel Henrique Barboza

We're missing scounteren and senvcfg CSRs, both already present in the KVM UAPI.

This patch seems to have broken KVM acceleration for me:

   $ ./build/qemu-system-riscv64 -display none -M virt,accel=kvm -cpu host
   qemu-system-riscv64: Failed to put registers after init: No such
file or directory

Reverting it makes QEMU work again.

Quoted 7 lines

My host is a SiFive HiFive Premier P550 board running Fedora 41. Note that, since the upstreaming effort for this SoC has just recently started, I'm using the 6.6-based vendor kernel.

Perhaps the KVM UAPI additions mentioned in the commit message are more recent than that, and we need to make QEMU's use of them conditional rather than unconditional?

Yes, we can't assume that CSRs will be always present.

I'll work on a fix. Thanks for reporting it!

Daniel

Quoted 1 line
#9 lore raw

Re: [PATCH v3 3/3] target/riscv/kvm: add missing KVM CSRs

In reply to Andrea Bolognani
Quoted 16 lines · Andrea Bolognani
Quoted 8 lines · Alistair Francis
Quoted 5 lines · Daniel Henrique Barboza

We're missing scounteren and senvcfg CSRs, both already present in the KVM UAPI.

This patch seems to have broken KVM acceleration for me:

  $ ./build/qemu-system-riscv64 -display none -M virt,accel=kvm -cpu host
  qemu-system-riscv64: Failed to put registers after init: No such
file or directory
Quoted 5 lines

Reverting it makes QEMU work again.

My host is a SiFive HiFive Premier P550 board running Fedora 41. Note that, since the upstreaming effort for this SoC has just recently started, I'm using the 6.6-based vendor kernel.

Ancient :-)

Quoted 4 lines

Perhaps the KVM UAPI additions mentioned in the commit message are more recent than that, and we need to make QEMU's use of them conditional rather than unconditional?

scounteren has been around since the dawn of riscv kvm, but senvcfg has only been there since 6.7 (just missed your ancient cut-off).

The true fix for this is to start using get-reg-list, which should hopefully work with the 6.6 kernel too since get-reg-list support has been around since 6.6.

A quick fix for this is to just drop senvcfg for now since nobody noticed it was missing before (well, I noticed it was missing, but by inspection, not test).

Thanks,
drew

#10 lore raw

Re: [PATCH v3 3/3] target/riscv/kvm: add missing KVM CSRs

Quoted 30 lines · Andrew Jones
Quoted 16 lines · Andrea Bolognani
Quoted 8 lines · Alistair Francis
Quoted 5 lines · Daniel Henrique Barboza

We're missing scounteren and senvcfg CSRs, both already present in the KVM UAPI.

This patch seems to have broken KVM acceleration for me:

   $ ./build/qemu-system-riscv64 -display none -M virt,accel=kvm -cpu host
   qemu-system-riscv64: Failed to put registers after init: No such
file or directory
Quoted 5 lines

Reverting it makes QEMU work again.

My host is a SiFive HiFive Premier P550 board running Fedora 41. Note that, since the upstreaming effort for this SoC has just recently started, I'm using the 6.6-based vendor kernel.

Ancient :-)

Quoted 4 lines

Perhaps the KVM UAPI additions mentioned in the commit message are more recent than that, and we need to make QEMU's use of them conditional rather than unconditional?

Quoted 6 lines

scounteren has been around since the dawn of riscv kvm, but senvcfg has only been there since 6.7 (just missed your ancient cut-off).

The true fix for this is to start using get-reg-list, which should hopefully work with the 6.6 kernel too since get-reg-list support has been around since 6.6.

That's the plan. We need to make the same treatment we're already doing with the extensions with these CSRs.

Quoted 4 lines

A quick fix for this is to just drop senvcfg for now since nobody noticed it was missing before (well, I noticed it was missing, but by inspection, not test).

We can do that in case the proper fix turns out to be more complex than we've anticipated and we'll miss the cut for the release. I think we have time to do it properly, but let's see.

Thanks,

Daniel

Quoted 3 lines

Thanks,
drew

#11 lore raw
In reply to Daniel Henrique Barboza
Quoted 19 lines · Daniel Henrique Barboza

Hi,

In this version I rolled back on the riscv_cpu_reset_hold() changes made in patch 1. Peter made an argument about keeping the design the same across architectures and I agreed. Patches 2 and 3 are already taking care of everything that KVM needs during reset, thus this doesn't incur in any functional changes.

Drew said it was ok to keep his ack on patch 1 so his ack is kept.

Patches based on alistair/riscv-to-apply.next. All patches acked.

Changes from v2:
- patch 1:
- do not do an early exit in riscv_cpu_reset_hold() if kvm_enabled()
- v2 link: https://lore.kernel.org/qemu-riscv/[email protected]/

Quoted 4 lines

Daniel Henrique Barboza (3):
target/riscv/cpu: remove unneeded !kvm_enabled() check target/riscv/kvm: add kvm_riscv_reset_regs_csr() target/riscv/kvm: add missing KVM CSRs

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 8 lines
 target/riscv/kvm/kvm-cpu.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

--
2.48.1