Skip to contentSkip to conversation

target/riscv/kvm: reset time changes

PATCH Superseded Daniel Henrique Barboza 2 participants 7 messages Feb 21, 2025 – Feb 28, 2025 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover target/riscv/kvm: reset time changes

Hi,

In this version the following changes were made, based on feedback from Drew in v1:

- patch 2: reworked. Instead of using a different existing 'env' field to get/put the 'sie' CSR, we're adding a new helper that contains all reset vals for the KVM CSRs we support
- patch 3: reworked. Add two KVM CSRs from the KVM UAPI that we're not using.

Link to v1: https://lore.kernel.org/qemu-riscv/[email protected]/

Patches based on alistair/riscv-to-apply.next.

Daniel Henrique Barboza (3):
target/riscv/cpu: ignore TCG init for KVM CPUs in reset_hold target/riscv/kvm: add kvm_riscv_reset_regs_csr() target/riscv/kvm: add missing KVM CSRs

 target/riscv/cpu.c         |  9 +++++----
 target/riscv/kvm/kvm-cpu.c | 32 +++++++++++++++++++++-----------
 2 files changed, 26 insertions(+), 15 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v2 1/3 target/riscv/cpu: ignore TCG init for KVM CPUs in reset_hold

riscv_cpu_reset_hold() does a lot of TCG-related initializations that aren't relevant for KVM, but nevertheless are impacting the reset state of KVM vcpus.

When running a KVM guest, kvm_riscv_reset_vcpu() is called at the end of reset_hold(). At that point env->mstatus is initialized to a non-zero value, and it will be use to write 'sstatus' in the vcpu (kvm_arch_put_registers() then kvm_riscv_put_regs_csr()).

Do an early exit in riscv_cpu_reset_hold() if we're running KVM. All the KVM reset procedure will be centered in kvm_riscv_reset_vcpu().

While we're at it, remove the kvm_enabled() check in kvm_riscv_reset_vcpu() since it's already being gated in riscv_cpu_reset_hold().

---
 target/riscv/cpu.c         | 9 +++++----
 target/riscv/kvm/kvm-cpu.c | 3 ---
 2 files changed, 5 insertions(+), 7 deletions(-)
target/riscv/cpu.c +5 -4
index 522d6584e4..8e6e629ec4 100644
@@ -1050,6 +1050,11 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)
         mcc->parent_phases.hold(obj, type);
     }
 #ifndef CONFIG_USER_ONLY
+    if (kvm_enabled()) {
+        kvm_riscv_reset_vcpu(cpu);
+        return;
+    }
+
     env->misa_mxl = mcc->misa_mxl_max;
     env->priv = PRV_M;
     env->mstatus &= ~(MSTATUS_MIE | MSTATUS_MPRV);
@@ -1146,10 +1151,6 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)
         env->rnmip = 0;
         env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, false);
     }
-
-    if (kvm_enabled()) {
-        kvm_riscv_reset_vcpu(cpu);
-    }
 #endif
 }
 
target/riscv/kvm/kvm-cpu.c +0 -3
index 23ce779359..484b6afe7c 100644
@@ -1603,9 +1603,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
#2 lore raw

PATCH v2 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 484b6afe7c..f14fcc58bb 100644
@@ -605,6 +605,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;
@@ -1609,14 +1622,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 v2 2/3
#3 lore raw

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

Quoted 59 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.

---
 target/riscv/kvm/kvm-cpu.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
target/riscv/kvm/kvm-cpu.c +15 -8
index 484b6afe7c..f14fcc58bb 100644
@@ -605,6 +605,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;
@@ -1609,14 +1622,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
#4 lore raw

PATCH v2 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 f14fcc58bb..017ca82226 100644
@@ -616,6 +616,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)
@@ -631,6 +633,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;
 }
@@ -648,6 +652,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
Reviewed-by on v2 3/3
#5 lore raw

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

Quoted 42 lines · 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 f14fcc58bb..017ca82226 100644
@@ -616,6 +616,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)
@@ -631,6 +633,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;
 }
@@ -648,6 +652,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
#6 lore raw
In reply to Daniel Henrique Barboza

Hi Alistair,

Can you please take a look in this series? This will collide with another series that are changing KVM CSRs that will require a rebase. Might as well do a rebase on top of this set too since it's fully acked already.

Thanks,

Daniel

Quoted 24 lines · Daniel Henrique Barboza

Hi,

In this version the following changes were made, based on feedback from Drew in v1:

- patch 2: reworked. Instead of using a different existing 'env' field to get/put the 'sie' CSR, we're adding a new helper that contains all reset vals for the KVM CSRs we support
- patch 3: reworked. Add two KVM CSRs from the KVM UAPI that we're not using.

Link to v1: https://lore.kernel.org/qemu-riscv/[email protected]/

Patches based on alistair/riscv-to-apply.next.

Daniel Henrique Barboza (3):
target/riscv/cpu: ignore TCG init for KVM CPUs in reset_hold target/riscv/kvm: add kvm_riscv_reset_regs_csr() target/riscv/kvm: add missing KVM CSRs

  target/riscv/cpu.c         |  9 +++++----
  target/riscv/kvm/kvm-cpu.c | 32 +++++++++++++++++++++-----------
  2 files changed, 26 insertions(+), 15 deletions(-)