Skip to contentSkip to conversation

target/riscv: Force to set mstatus_hs.[SD|FS] bits in mark_fs_dirty()

PATCH In review [email protected] 2 participants 3 messages Sep 14, 2021 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: Force to set mstatus_hs.[SD|FS] bits in mark_fs_dirty()

From: Frank Chang <[email protected]>

When V=1, both vsstauts.FS and HS-level sstatus.FS are in effect. Modifying the floating-point state when V=1 causes both fields to be set to 3 (Dirty).

However, it's possible that HS-level sstatus.FS is Clean and VS-level vsstatus.FS is Dirty at the time mark_fs_dirty() is called when V=1. We can't early return for this case because we still need to set sstatus.FS to Dirty according to spec.

---
 target/riscv/translate.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
target/riscv/translate.c +10 -9
index e356fc6c46c..0096b098738 100644
@@ -280,26 +280,27 @@ static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
 static void mark_fs_dirty(DisasContext *ctx)
 {
     TCGv tmp;
-    target_ulong sd;
+    target_ulong sd = is_32bit(ctx) ? MSTATUS32_SD : MSTATUS64_SD;
+
+    if (ctx->virt_enabled) {
+        tmp = tcg_temp_new();
+        tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
+        tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | sd);
+        tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
+        tcg_temp_free(tmp);
+    }
 
     if (ctx->mstatus_fs == MSTATUS_FS) {
         return;
     }
+
     /* Remember the state change for the rest of the TB.  */
     ctx->mstatus_fs = MSTATUS_FS;
 
     tmp = tcg_temp_new();
-    sd = is_32bit(ctx) ? MSTATUS32_SD : MSTATUS64_SD;
-
     tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
     tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | sd);
     tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
-
-    if (ctx->virt_enabled) {
-        tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
-        tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | sd);
-        tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
-    }
     tcg_temp_free(tmp);
 }
 #else
Signature · 1 line
2.25.1
#1 lore raw
Quoted 30 lines · On 9/13/21 6:37 PM, [email protected] wrote:

From: Frank Chang <[email protected]>

When V=1, both vsstauts.FS and HS-level sstatus.FS are in effect. Modifying the floating-point state when V=1 causes both fields to be set to 3 (Dirty).

However, it's possible that HS-level sstatus.FS is Clean and VS-level vsstatus.FS is Dirty at the time mark_fs_dirty() is called when V=1. We can't early return for this case because we still need to set sstatus.FS to Dirty according to spec.

---
  target/riscv/translate.c | 19 ++++++++++---------
  1 file changed, 10 insertions(+), 9 deletions(-)
target/riscv/translate.c +3 -1
index e356fc6c46c..0096b098738 100644
@@ -280,26 +280,27 @@ static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
  static void mark_fs_dirty(DisasContext *ctx)
  {
      TCGv tmp;
-    target_ulong sd;
+    target_ulong sd = is_32bit(ctx) ? MSTATUS32_SD : MSTATUS64_SD;
+
+    if (ctx->virt_enabled) {

target/riscv/translate.c static void gen_jal(DisasContext *ctx, int rd, target_ulong imm) · line 295

Quoted 10 lines
target/riscv/translate.c +6 -0
+        tmp = tcg_temp_new();
+        tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
+        tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | sd);
+        tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
+        tcg_temp_free(tmp);
+    }
 
      if (ctx->mstatus_fs == MSTATUS_FS) {
          return;
      }

You should introduce a ctx->mstatus_hs field to track the code that you moved. Otherwise you'll be setting this dirty bit for every fp insn.

r~

#2 lore raw
Quoted 41 lines · Richard Henderson

target/riscv/translate.c static void gen_jal(DisasContext *ctx, int rd,

Quoted 23 lines · On 9/13/21 6:37 PM, [email protected] wrote:

From: Frank Chang <[email protected]>

When V=1, both vsstauts.FS and HS-level sstatus.FS are in effect. Modifying the floating-point state when V=1 causes both fields to be set to 3 (Dirty).

However, it's possible that HS-level sstatus.FS is Clean and VS-level vsstatus.FS is Dirty at the time mark_fs_dirty() is called when V=1. We can't early return for this case because we still need to set sstatus.FS to Dirty according to spec.

---
  target/riscv/translate.c | 19 ++++++++++---------
  1 file changed, 10 insertions(+), 9 deletions(-)
target/riscv/translate.c +0 -0
index e356fc6c46c..0096b098738 100644
@@ -280,26 +280,27 @@ static void gen_jal(DisasContext *ctx, int rd,

target_ulong imm)

target/riscv/translate.c static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)

Quoted 9 lines
target/riscv/translate.c +5 -1
  static void mark_fs_dirty(DisasContext *ctx)
  {
      TCGv tmp;
-    target_ulong sd;
+    target_ulong sd = is_32bit(ctx) ? MSTATUS32_SD : MSTATUS64_SD;
+
+    if (ctx->virt_enabled) {
+        tmp = tcg_temp_new();
+        tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState,

mstatus_hs));

target/riscv/translate.c static void gen_jal(DisasContext *ctx, int rd, · line 289

Quoted 2 lines
target/riscv/translate.c +2 -0
+        tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | sd);
+        tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState,

mstatus_hs));

Quoted 3 lines
target/riscv/translate.c +2 -0
+        tcg_temp_free(tmp);
+    }
 
Quoted 9 lines

target/riscv/translate.c static void gen_jal(DisasContext *ctx, int rd, target_ulong imm) · line 295

Quoted 3 lines
target/riscv/translate.c +0 -0
      if (ctx->mstatus_fs == MSTATUS_FS) {
          return;
      }

You should introduce a ctx->mstatus_hs field to track the code that you moved. Otherwise
you'll be setting this dirty bit for every fp insn.

Thanks, Richard, I was struggling with whether to introduce a new field in DisasContext.
I will update my patch.

Regards,
Frank Chang

Quoted 3 lines

r~