target/riscv: Fix tb->flags FS status
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
@@ -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
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
@@ -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
@@ -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
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:
Signed-off-by ShihPo Hung <[email protected]>Reviewed-by Richard Henderson <[email protected]>
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@@ -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@@ -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
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
@@ -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
@@ -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
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.
Signed-off-by ShihPo Hung <[email protected]>Reviewed-by Richard Henderson <[email protected]>
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@@ -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@@ -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
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->flagsmerges all non-disabled state to dirty. This means the code inmark_fs_dirtyintranslate.cthat handles initial and clean states is unreachable.This patch fixes it and is successfully tested with:
libquantumThanks to Richard for pointing out the actual bug.
v3: remove the redundant condition
v2: root cause FS problemSuggested-by Richard Henderson <[email protected]>Signed-off-by ShihPo Hung <[email protected]>Reviewed-by Richard Henderson <[email protected]>--- target/riscv/cpu.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)target/riscv/cpu.h +0
-0
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
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_FSbut when the Hypervisor extension goes inriscv_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
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->flagsmerges all non-disabled state to dirty. This means the code inmark_fs_dirtyintranslate.cthat handles initial and clean states is unreachable.This patch fixes it and is successfully tested with:
libquantumThanks to Richard for pointing out the actual bug.
v3: remove the redundant condition
v2: root cause FS problemSuggested-by Richard Henderson <[email protected]>Signed-off-by ShihPo Hung <[email protected]>Reviewed-by Richard Henderson <[email protected]>--- target/riscv/cpu.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)target/riscv/cpu.h +0
-0target/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_FSbut when the Hypervisor extension goes inriscv_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~
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->flagsmerges all non-disabled state to dirty. This means the code inmark_fs_dirtyintranslate.cthat handles initial and clean states is unreachable.This patch fixes it and is successfully tested with:
libquantumThanks to Richard for pointing out the actual bug.
v3: remove the redundant condition
v2: root cause FS problemSuggested-by Richard Henderson <[email protected]>Signed-off-by ShihPo Hung <[email protected]>Reviewed-by Richard Henderson <[email protected]>--- target/riscv/cpu.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)target/riscv/cpu.h +0
-0target/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_FSbut when the Hypervisor extension goes inriscv_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~