Skip to contentSkip to conversation

target/riscv: Fix tb->flags FS status

PATCH In review [email protected] 3 participants 9 messages Jan 15, 2020 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/3 target/riscv: Fix tb->flags FS status

It was found that running libquantum on riscv-linux qemu produced an incorrect result. After investigation, FP registers are not saved during context switch due to incorrect mstatus.FS.

In current implementation tb->flags merges all non-disabled state to dirty. This means the code in mark_fs_dirty in translate.c that handles initial and clean states is unreachable.

This patch fixes it and is successfully tested with:
libquantum

Thanks to Richard for pointing out the actual bug.

v3: remove the redundant condition
v2: root cause FS problem

---
 target/riscv/cpu.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
target/riscv/cpu.h +1 -4
index e59343e..de0a8d8 100644
@@ -293,10 +293,7 @@ static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
 #ifdef CONFIG_USER_ONLY
     *flags = TB_FLAGS_MSTATUS_FS;
 #else
-    *flags = cpu_mmu_index(env, 0);
-    if (riscv_cpu_fp_enabled(env)) {
-        *flags |= TB_FLAGS_MSTATUS_FS;
-    }
+    *flags = cpu_mmu_index(env, 0) | (env->mstatus & MSTATUS_FS);
 #endif
 }
 
Signature · 1 line
2.7.4
#1 lore raw

PATCH v3 2/3 target/riscv: fsd/fsw doesn't dirty FP state

---
 target/riscv/insn_trans/trans_rvd.inc.c | 1 -
 target/riscv/insn_trans/trans_rvf.inc.c | 1 -
 2 files changed, 2 deletions(-)
target/riscv/insn_trans/trans_rvd.inc.c +0 -1
index 393fa02..ea1044f 100644
@@ -43,7 +43,6 @@ static bool trans_fsd(DisasContext *ctx, arg_fsd *a)
 
     tcg_gen_qemu_st_i64(cpu_fpr[a->rs2], t0, ctx->mem_idx, MO_TEQ);
 
-    mark_fs_dirty(ctx);
     tcg_temp_free(t0);
     return true;
 }
target/riscv/insn_trans/trans_rvf.inc.c +0 -1
index 172dbfa..e23cd63 100644
@@ -52,7 +52,6 @@ static bool trans_fsw(DisasContext *ctx, arg_fsw *a)
     tcg_gen_qemu_st_i64(cpu_fpr[a->rs2], t0, ctx->mem_idx, MO_TEUL);
 
     tcg_temp_free(t0);
-    mark_fs_dirty(ctx);
     return true;
 }
 
Signature · 1 line
2.7.4
Reviewed-by on v3 2/3
#2 lore raw

Re: [PATCH v3 2/3] target/riscv: fsd/fsw doesn't dirty FP state

Quoted 3 lines · On Wed, Jan 15, 2020 at 4:18 PM <[email protected]> wrote:

Alistair

Quoted 33 lines
---
 target/riscv/insn_trans/trans_rvd.inc.c | 1 -
 target/riscv/insn_trans/trans_rvf.inc.c | 1 -
 2 files changed, 2 deletions(-)
target/riscv/insn_trans/trans_rvd.inc.c +0 -1
index 393fa02..ea1044f 100644
@@ -43,7 +43,6 @@ static bool trans_fsd(DisasContext *ctx, arg_fsd *a)
 
     tcg_gen_qemu_st_i64(cpu_fpr[a->rs2], t0, ctx->mem_idx, MO_TEQ);
 
-    mark_fs_dirty(ctx);
     tcg_temp_free(t0);
     return true;
 }
target/riscv/insn_trans/trans_rvf.inc.c +0 -2
index 172dbfa..e23cd63 100644
@@ -52,7 +52,6 @@ static bool trans_fsw(DisasContext *ctx, arg_fsw *a)
     tcg_gen_qemu_st_i64(cpu_fpr[a->rs2], t0, ctx->mem_idx, MO_TEUL);
 
     tcg_temp_free(t0);
-    mark_fs_dirty(ctx);
     return true;
 }
 
--

2.7.4

#3 lore raw

PATCH v3 3/3 target/riscv: update mstatus.SD when FS is set dirty

In reply to [email protected]

remove the check becuase SD bit should summarize FS and XS fields unconditionally.

---
 target/riscv/csr.c       | 3 +--
 target/riscv/translate.c | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
target/riscv/csr.c +1 -2
index da02f9f..0e34c29 100644
@@ -341,8 +341,7 @@ static int write_mstatus(CPURISCVState *env, int csrno, target_ulong val)
 
     mstatus = (mstatus & ~mask) | (val & mask);
 
-    dirty = (riscv_cpu_fp_enabled(env) &&
-             ((mstatus & MSTATUS_FS) == MSTATUS_FS)) |
+    dirty = ((mstatus & MSTATUS_FS) == MSTATUS_FS) |
             ((mstatus & MSTATUS_XS) == MSTATUS_XS);
     mstatus = set_field(mstatus, MSTATUS_SD, dirty);
     env->mstatus = mstatus;
target/riscv/translate.c +1 -1
index ab6a891..8e40ed3 100644
@@ -394,7 +394,7 @@ static void mark_fs_dirty(DisasContext *ctx)
 
     tmp = tcg_temp_new();
     tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
-    tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
+    tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | MSTATUS_SD);
     tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
     tcg_temp_free(tmp);
 }
Signature · 1 line
2.7.4
Reviewed-by on v3 3/3
#4 lore raw

Re: [PATCH v3 3/3] target/riscv: update mstatus.SD when FS is set dirty

Quoted 6 lines · On Wed, Jan 15, 2020 at 4:19 PM <[email protected]> wrote:

remove the check becuase SD bit should summarize FS and XS fields unconditionally.

Alistair

Quoted 36 lines
---
 target/riscv/csr.c       | 3 +--
 target/riscv/translate.c | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
target/riscv/csr.c +1 -2
index da02f9f..0e34c29 100644
@@ -341,8 +341,7 @@ static int write_mstatus(CPURISCVState *env, int csrno, target_ulong val)
 
     mstatus = (mstatus & ~mask) | (val & mask);
 
-    dirty = (riscv_cpu_fp_enabled(env) &&
-             ((mstatus & MSTATUS_FS) == MSTATUS_FS)) |
+    dirty = ((mstatus & MSTATUS_FS) == MSTATUS_FS) |
             ((mstatus & MSTATUS_XS) == MSTATUS_XS);
     mstatus = set_field(mstatus, MSTATUS_SD, dirty);
     env->mstatus = mstatus;
target/riscv/translate.c +1 -2
index ab6a891..8e40ed3 100644
@@ -394,7 +394,7 @@ static void mark_fs_dirty(DisasContext *ctx)
 
     tmp = tcg_temp_new();
     tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
-    tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
+    tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | MSTATUS_SD);
     tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
     tcg_temp_free(tmp);
 }
--

2.7.4

#5 lore raw
In reply to [email protected]
Quoted 29 lines · On Wed, Jan 15, 2020 at 4:18 PM <[email protected]> wrote:

It was found that running libquantum on riscv-linux qemu produced an incorrect result. After investigation, FP registers are not saved during context switch due to incorrect mstatus.FS.

In current implementation tb->flags merges all non-disabled state to dirty. This means the code in mark_fs_dirty in translate.c that handles initial and clean states is unreachable.

This patch fixes it and is successfully tested with:
libquantum

Thanks to Richard for pointing out the actual bug.

v3: remove the redundant condition
v2: root cause FS problem

---
 target/riscv/cpu.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
target/riscv/cpu.h +0 -0
index e59343e..de0a8d8 100644

target/riscv/cpu.h static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 296

Quoted 8 lines
target/riscv/cpu.h +1 -4
@@ -293,10 +293,7 @@ static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
 #ifdef CONFIG_USER_ONLY
     *flags = TB_FLAGS_MSTATUS_FS;
 #else
-    *flags = cpu_mmu_index(env, 0);
-    if (riscv_cpu_fp_enabled(env)) {
-        *flags |= TB_FLAGS_MSTATUS_FS;
-    }
+    *flags = cpu_mmu_index(env, 0) | (env->mstatus & MSTATUS_FS);

I don't think this is right, you should use the riscv_cpu_fp_enabled() function.

Right now it's the same as env->mstatus & MSTATUS_FS but when the Hypervisor extension goes in riscv_cpu_fp_enabled() will be more complex.

Alistair

Quoted 7 lines
target/riscv/cpu.h +0 -1
 #endif
 }
 
--

2.7.4

#6 lore raw
Quoted 9 lines · Alistair Francis

target/riscv/cpu.h static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 296

Quoted 5 lines
target/riscv/cpu.h +1 -4
-    *flags = cpu_mmu_index(env, 0);
-    if (riscv_cpu_fp_enabled(env)) {
-        *flags |= TB_FLAGS_MSTATUS_FS;
-    }
+    *flags = cpu_mmu_index(env, 0) | (env->mstatus & MSTATUS_FS);

I don't think this is right, you should use the riscv_cpu_fp_enabled() function.

Quoted 7 lines

Right now it's the same as env->mstatus & MSTATUS_FS but when the Hypervisor extension goes in riscv_cpu_fp_enabled() will be more complex.

Alistair

I agree using riscv_cpu_fp_enabled() to hide the complexity when checking

FP,
but here I only duplicate the FP status (disabled/initial/clean/dirty) to tb->flags
no matter FP is enabled or not.

Is it still necessary to check this before duplicating it?

I think it is not as long as TB_FLAGS_MSTATUS_FS is equivalent to MSTATUS_FS.
But I don't know what changes hypervisor extension brings, please correct me if I am wrong.

ShihPo

#7 lore raw
In reply to Alistair Francis
Quoted 41 lines · Alistair Francis
Quoted 29 lines · On Wed, Jan 15, 2020 at 4:18 PM <[email protected]> wrote:

It was found that running libquantum on riscv-linux qemu produced an incorrect result. After investigation, FP registers are not saved during context switch due to incorrect mstatus.FS.

In current implementation tb->flags merges all non-disabled state to dirty. This means the code in mark_fs_dirty in translate.c that handles initial and clean states is unreachable.

This patch fixes it and is successfully tested with:
libquantum

Thanks to Richard for pointing out the actual bug.

v3: remove the redundant condition
v2: root cause FS problem

---
 target/riscv/cpu.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
target/riscv/cpu.h +0 -0
index e59343e..de0a8d8 100644

target/riscv/cpu.h static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 296

Quoted 8 lines
target/riscv/cpu.h +1 -4
@@ -293,10 +293,7 @@ static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
 #ifdef CONFIG_USER_ONLY
     *flags = TB_FLAGS_MSTATUS_FS;
 #else
-    *flags = cpu_mmu_index(env, 0);
-    if (riscv_cpu_fp_enabled(env)) {
-        *flags |= TB_FLAGS_MSTATUS_FS;
-    }
+    *flags = cpu_mmu_index(env, 0) | (env->mstatus & MSTATUS_FS);

I don't think this is right, you should use the riscv_cpu_fp_enabled() function.

Quoted 3 lines

Right now it's the same as env->mstatus & MSTATUS_FS but when the Hypervisor extension goes in riscv_cpu_fp_enabled() will be more complex.

Hmm. Are you sure something like

  flags |= riscv_cpu_effective_mstatus(env) & MSTATUS_FS;

wouldn't be more appropriate for the hypervisor extension?

I guess I should have another browse through your hv patchset, but I worry now about bare uses of env->mstatus, if they no longer mean what they appear to mean.

r~

#8 lore raw
Quoted 47 lines · Richard Henderson
Quoted 41 lines · Alistair Francis
Quoted 29 lines · On Wed, Jan 15, 2020 at 4:18 PM <[email protected]> wrote:

It was found that running libquantum on riscv-linux qemu produced an incorrect result. After investigation, FP registers are not saved during context switch due to incorrect mstatus.FS.

In current implementation tb->flags merges all non-disabled state to dirty. This means the code in mark_fs_dirty in translate.c that handles initial and clean states is unreachable.

This patch fixes it and is successfully tested with:
libquantum

Thanks to Richard for pointing out the actual bug.

v3: remove the redundant condition
v2: root cause FS problem

---
 target/riscv/cpu.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
target/riscv/cpu.h +0 -0
index e59343e..de0a8d8 100644

target/riscv/cpu.h static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 296

Quoted 8 lines
target/riscv/cpu.h +1 -4
@@ -293,10 +293,7 @@ static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
 #ifdef CONFIG_USER_ONLY
     *flags = TB_FLAGS_MSTATUS_FS;
 #else
-    *flags = cpu_mmu_index(env, 0);
-    if (riscv_cpu_fp_enabled(env)) {
-        *flags |= TB_FLAGS_MSTATUS_FS;
-    }
+    *flags = cpu_mmu_index(env, 0) | (env->mstatus & MSTATUS_FS);

I don't think this is right, you should use the riscv_cpu_fp_enabled() function.

Quoted 3 lines

Right now it's the same as env->mstatus & MSTATUS_FS but when the Hypervisor extension goes in riscv_cpu_fp_enabled() will be more complex.

Quoted 5 lines

Hmm. Are you sure something like

  flags |= riscv_cpu_effective_mstatus(env) & MSTATUS_FS;

wouldn't be more appropriate for the hypervisor extension?

I was more thinking:

    if (riscv_cpu_fp_enabled(env)) {
        *flags |= env->mstatus & MSTATUS_FS;
    }

as floating point can be disabled from multiple places when we have the H extension.

Quoted 3 lines

I guess I should have another browse through your hv patchset, but I worry now about bare uses of env->mstatus, if they no longer mean what they appear to mean.

That was why this was all refacted in the first place as we now need to check against env->vsstatus as well (depending on virt status).

Alistair

Quoted 3 lines

r~