qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/3] target/riscv: Fix tb->flags FS status
@ 2020-01-14 10:13 shihpo.hung
  2020-01-14 10:13 ` [PATCH v2 2/3] target/riscv: fsd/fsw doesn't dirty FP state shihpo.hung
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: shihpo.hung @ 2020-01-14 10:13 UTC (permalink / raw)
  To: qemu-riscv
  Cc: Sagar Karandikar, Bastian Koppelmann, Richard Henderson,
	qemu-devel, Alistair Francis, Palmer Dabbelt, ShihPo Hung

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.

Suggested-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: ShihPo Hung <shihpo.hung@sifive.com>
---
 target/riscv/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
index e59343e..f0ff57e 100644
--- a/target/riscv/cpu.h
+++ b/target/riscv/cpu.h
@@ -295,7 +295,7 @@ static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
 #else
     *flags = cpu_mmu_index(env, 0);
     if (riscv_cpu_fp_enabled(env)) {
-        *flags |= TB_FLAGS_MSTATUS_FS;
+        *flags |= env->mstatus & MSTATUS_FS;
     }
 #endif
 }
-- 
2.7.4



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/3] target/riscv: fsd/fsw doesn't dirty FP state
  2020-01-14 10:13 [PATCH v2 1/3] target/riscv: Fix tb->flags FS status shihpo.hung
@ 2020-01-14 10:13 ` shihpo.hung
  2020-01-14 15:48   ` Richard Henderson
  2020-01-14 10:14 ` [PATCH v2 3/3] target/riscv: update mstatus.SD when FS is set dirty shihpo.hung
  2020-01-14 15:29 ` [PATCH v2 1/3] target/riscv: Fix tb->flags FS status Richard Henderson
  2 siblings, 1 reply; 7+ messages in thread
From: shihpo.hung @ 2020-01-14 10:13 UTC (permalink / raw)
  To: qemu-riscv
  Cc: Sagar Karandikar, Bastian Koppelmann, Richard Henderson,
	qemu-devel, Alistair Francis, Palmer Dabbelt, ShihPo Hung

Signed-off-by: ShihPo Hung <shihpo.hung@sifive.com>
---
 target/riscv/insn_trans/trans_rvd.inc.c | 1 -
 target/riscv/insn_trans/trans_rvf.inc.c | 1 -
 2 files changed, 2 deletions(-)

diff --git a/target/riscv/insn_trans/trans_rvd.inc.c b/target/riscv/insn_trans/trans_rvd.inc.c
index 393fa02..ea1044f 100644
--- a/target/riscv/insn_trans/trans_rvd.inc.c
+++ b/target/riscv/insn_trans/trans_rvd.inc.c
@@ -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;
 }
diff --git a/target/riscv/insn_trans/trans_rvf.inc.c b/target/riscv/insn_trans/trans_rvf.inc.c
index 172dbfa..e23cd63 100644
--- a/target/riscv/insn_trans/trans_rvf.inc.c
+++ b/target/riscv/insn_trans/trans_rvf.inc.c
@@ -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



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/3] target/riscv: update mstatus.SD when FS is set dirty
  2020-01-14 10:13 [PATCH v2 1/3] target/riscv: Fix tb->flags FS status shihpo.hung
  2020-01-14 10:13 ` [PATCH v2 2/3] target/riscv: fsd/fsw doesn't dirty FP state shihpo.hung
@ 2020-01-14 10:14 ` shihpo.hung
  2020-01-14 15:47   ` Richard Henderson
  2020-01-14 15:29 ` [PATCH v2 1/3] target/riscv: Fix tb->flags FS status Richard Henderson
  2 siblings, 1 reply; 7+ messages in thread
From: shihpo.hung @ 2020-01-14 10:14 UTC (permalink / raw)
  To: qemu-riscv
  Cc: Sagar Karandikar, Bastian Koppelmann, Richard Henderson,
	qemu-devel, Alistair Francis, Palmer Dabbelt, ShihPo Hung

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

Signed-off-by: ShihPo Hung <shihpo.hung@sifive.com>
---
 target/riscv/csr.c       | 3 +--
 target/riscv/translate.c | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/target/riscv/csr.c b/target/riscv/csr.c
index da02f9f..0e34c29 100644
--- a/target/riscv/csr.c
+++ b/target/riscv/csr.c
@@ -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;
diff --git a/target/riscv/translate.c b/target/riscv/translate.c
index ab6a891..e825ee6 100644
--- a/target/riscv/translate.c
+++ b/target/riscv/translate.c
@@ -395,6 +395,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_SD);
     tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
     tcg_temp_free(tmp);
 }
-- 
2.7.4



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/3] target/riscv: Fix tb->flags FS status
  2020-01-14 10:13 [PATCH v2 1/3] target/riscv: Fix tb->flags FS status shihpo.hung
  2020-01-14 10:13 ` [PATCH v2 2/3] target/riscv: fsd/fsw doesn't dirty FP state shihpo.hung
  2020-01-14 10:14 ` [PATCH v2 3/3] target/riscv: update mstatus.SD when FS is set dirty shihpo.hung
@ 2020-01-14 15:29 ` Richard Henderson
  2020-01-15  6:16   ` ShihPo Hung
  2 siblings, 1 reply; 7+ messages in thread
From: Richard Henderson @ 2020-01-14 15:29 UTC (permalink / raw)
  To: shihpo.hung, qemu-riscv
  Cc: Alistair Francis, Palmer Dabbelt, qemu-devel, Sagar Karandikar,
	Bastian Koppelmann

On 1/14/20 12:13 AM, shihpo.hung@sifive.com wrote:
>      if (riscv_cpu_fp_enabled(env)) {
> -        *flags |= TB_FLAGS_MSTATUS_FS;
> +        *flags |= env->mstatus & MSTATUS_FS;
>      }

Note that riscv_cpu_fp_enabled is *also* testing env->mstatus, and that this
assignment can be made unconditional.

Otherwise,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/3] target/riscv: update mstatus.SD when FS is set dirty
  2020-01-14 10:14 ` [PATCH v2 3/3] target/riscv: update mstatus.SD when FS is set dirty shihpo.hung
@ 2020-01-14 15:47   ` Richard Henderson
  0 siblings, 0 replies; 7+ messages in thread
From: Richard Henderson @ 2020-01-14 15:47 UTC (permalink / raw)
  To: shihpo.hung, qemu-riscv
  Cc: Alistair Francis, Palmer Dabbelt, qemu-devel, Sagar Karandikar,
	Bastian Koppelmann

On 1/14/20 12:14 AM, shihpo.hung@sifive.com wrote:
>      tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
> +    tcg_gen_ori_tl(tmp, tmp, MSTATUS_SD);

Merge these:

  tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | MSTATUS_SD);

The TCG optimizer doesn't try to handle this sort of thing.

With that,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/3] target/riscv: fsd/fsw doesn't dirty FP state
  2020-01-14 10:13 ` [PATCH v2 2/3] target/riscv: fsd/fsw doesn't dirty FP state shihpo.hung
@ 2020-01-14 15:48   ` Richard Henderson
  0 siblings, 0 replies; 7+ messages in thread
From: Richard Henderson @ 2020-01-14 15:48 UTC (permalink / raw)
  To: shihpo.hung, qemu-riscv
  Cc: Alistair Francis, Palmer Dabbelt, qemu-devel, Sagar Karandikar,
	Bastian Koppelmann

On 1/14/20 12:13 AM, shihpo.hung@sifive.com wrote:
> Signed-off-by: ShihPo Hung <shihpo.hung@sifive.com>
> ---
>  target/riscv/insn_trans/trans_rvd.inc.c | 1 -
>  target/riscv/insn_trans/trans_rvf.inc.c | 1 -
>  2 files changed, 2 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/3] target/riscv: Fix tb->flags FS status
  2020-01-14 15:29 ` [PATCH v2 1/3] target/riscv: Fix tb->flags FS status Richard Henderson
@ 2020-01-15  6:16   ` ShihPo Hung
  0 siblings, 0 replies; 7+ messages in thread
From: ShihPo Hung @ 2020-01-15  6:16 UTC (permalink / raw)
  To: Richard Henderson
  Cc: qemu-riscv, Sagar Karandikar, Bastian Koppelmann, qemu-devel,
	Palmer Dabbelt, Alistair Francis

[-- Attachment #1: Type: text/plain, Size: 570 bytes --]

On Tue, Jan 14, 2020 at 11:29 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 1/14/20 12:13 AM, shihpo.hung@sifive.com wrote:
> >      if (riscv_cpu_fp_enabled(env)) {
> > -        *flags |= TB_FLAGS_MSTATUS_FS;
> > +        *flags |= env->mstatus & MSTATUS_FS;
> >      }
>
> Note that riscv_cpu_fp_enabled is *also* testing env->mstatus, and that
> this
> assignment can be made unconditional.
>
> Otherwise,
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>

I'll send v3 that cleans up the patch accordingly.
Thanks for the review!

[-- Attachment #2: Type: text/html, Size: 1119 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-01-15  6:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-14 10:13 [PATCH v2 1/3] target/riscv: Fix tb->flags FS status shihpo.hung
2020-01-14 10:13 ` [PATCH v2 2/3] target/riscv: fsd/fsw doesn't dirty FP state shihpo.hung
2020-01-14 15:48   ` Richard Henderson
2020-01-14 10:14 ` [PATCH v2 3/3] target/riscv: update mstatus.SD when FS is set dirty shihpo.hung
2020-01-14 15:47   ` Richard Henderson
2020-01-14 15:29 ` [PATCH v2 1/3] target/riscv: Fix tb->flags FS status Richard Henderson
2020-01-15  6:16   ` ShihPo Hung

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).