Skip to contentSkip to conversation

target/riscv: avoid abort when reading vtype before env->xl is set

PATCH Applied ZhengXiang Qin 3 participants 4 messages Jun 15 – Jun 22 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: avoid abort when reading vtype before env->xl is set

TCG plugins may read registers from the vcpu_init_cb() callback. For vtype, this reaches read_vtype() before env->xl has been initialized.

In that case read_vtype() currently hits g_assert_not_reached() because env->xl is zero. Fall back to the CPU's maximum XLEN only for this early-init case.

Fixes 638181a180bd ("core/cpu-common: initialise plugin state before thread creation")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3545

---
 target/riscv/csr.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
target/riscv/csr.c +10 -1
index 5514e0f455..6ade7ccb0b 100644
@@ -936,7 +936,16 @@ static RISCVException read_vtype(CPURISCVState *env, int csrno,
                                  target_ulong *val)
 {
     uint64_t vill;
-    switch (env->xl) {
+    int xl = env->xl;
+    /*
+     * TCG plugins can read registers before env->xl is initialized.
+     * Fall back to the CPU's maximum XLEN in that early-init case.
+     */
+    if (xl == 0) {
+        xl = riscv_cpu_mxl(env);
+    }
+
+    switch (xl) {
     case MXL_RV32:
         vill = (uint32_t)env->vill << 31;
         break;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · ZhengXiang Qin

TCG plugins may read registers from the vcpu_init_cb() callback. For vtype, this reaches read_vtype() before env->xl has been initialized.

In that case read_vtype() currently hits g_assert_not_reached() because env->xl is zero. Fall back to the CPU's maximum XLEN only for this early-init case.

Fixes 638181a180bd ("core/cpu-common: initialise plugin state before thread creation")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3545

---

Quoted 25 lines
  target/riscv/csr.c | 11 ++++++++++-
  1 file changed, 10 insertions(+), 1 deletion(-)
target/riscv/csr.c +10 -1
index 5514e0f455..6ade7ccb0b 100644
@@ -936,7 +936,16 @@ static RISCVException read_vtype(CPURISCVState *env, int csrno,
                                   target_ulong *val)
  {
      uint64_t vill;
-    switch (env->xl) {
+    int xl = env->xl;
+    /*
+     * TCG plugins can read registers before env->xl is initialized.
+     * Fall back to the CPU's maximum XLEN in that early-init case.
+     */
+    if (xl == 0) {
+        xl = riscv_cpu_mxl(env);
+    }
+
+    switch (xl) {
      case MXL_RV32:
          vill = (uint32_t)env->vill << 31;
          break;
#3 lore raw
In reply to ZhengXiang Qin
Quoted 11 lines · ZhengXiang Qin

TCG plugins may read registers from the vcpu_init_cb() callback. For vtype, this reaches read_vtype() before env->xl has been initialized.

In that case read_vtype() currently hits g_assert_not_reached() because env->xl is zero. Fall back to the CPU's maximum XLEN only for this early-init case.

Fixes 638181a180bd ("core/cpu-common: initialise plugin state before thread creation")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3545

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 30 lines
---
 target/riscv/csr.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
target/riscv/csr.c +10 -2
index 5514e0f455..6ade7ccb0b 100644
@@ -936,7 +936,16 @@ static RISCVException read_vtype(CPURISCVState *env, int csrno,
                                  target_ulong *val)
 {
     uint64_t vill;
-    switch (env->xl) {
+    int xl = env->xl;
+    /*
+     * TCG plugins can read registers before env->xl is initialized.
+     * Fall back to the CPU's maximum XLEN in that early-init case.
+     */
+    if (xl == 0) {
+        xl = riscv_cpu_mxl(env);
+    }
+
+    switch (xl) {
     case MXL_RV32:
         vill = (uint32_t)env->vill << 31;
         break;
--

2.43.0