Skip to contentSkip to conversation

RISC-V: Populate mtval and stval

PATCH Superseded Alistair Francis 3 participants 7 messages Sep 2, 2021 – Sep 4, 2021 RISC-V

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover RISC-V: Populate mtval and stval

From: Alistair Francis <[email protected]>

Populate mtval and stval when taking an illegal instruction exception if the features are set for the CPU.

Alistair Francis (2):
target/riscv: Implement the stval/mtval illegal instruction target/riscv: Set mtval and stval support

 target/riscv/cpu.h        |  6 +++++-
 target/riscv/cpu.c        |  6 +++++-
 target/riscv/cpu_helper.c |  9 +++++++++
 target/riscv/translate.c  | 33 +++++++++++++++++++--------------
 4 files changed, 38 insertions(+), 16 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 target/riscv: Implement the stval/mtval illegal instruction

From: Alistair Francis <[email protected]>

The stval and mtval registers can optionally contain the faulting instruction on an illegal instruction exception. This patch adds support for setting the stval and mtval registers based on the CPU feature.

---
 target/riscv/cpu.h        |  5 ++++-
 target/riscv/cpu_helper.c |  9 +++++++++
 target/riscv/translate.c  | 33 +++++++++++++++++++--------------
 3 files changed, 32 insertions(+), 15 deletions(-)
target/riscv/cpu.h +4 -1
index bf1c899c00..6d41a16ae3 100644
@@ -77,7 +77,8 @@ enum {
     RISCV_FEATURE_MMU,
     RISCV_FEATURE_PMP,
     RISCV_FEATURE_EPMP,
-    RISCV_FEATURE_MISA
+    RISCV_FEATURE_MISA,
+    RISCV_FEATURE_MTVAL_INST,
 };
 
 #define PRIV_VERSION_1_10_0 0x00011000
@@ -130,6 +131,8 @@ struct CPURISCVState {
     target_ulong frm;
 
     target_ulong badaddr;
+    target_ulong bins;
+
     target_ulong guest_phys_fault_addr;
 
     target_ulong priv_ver;
target/riscv/cpu_helper.c +9 -0
index 968cb8046f..42edd71c1e 100644
@@ -967,6 +967,15 @@ void riscv_cpu_do_interrupt(CPUState *cs)
             write_tval  = true;
             tval = env->badaddr;
             break;
+        case RISCV_EXCP_ILLEGAL_INST:
+            if (riscv_feature(env, RISCV_FEATURE_MTVAL_INST)) {
+                /* The stval/mtval register can optionally also be used to
+                 * return the faulting instruction bits on an illegal
+                 * instruction exception.
+                 */
+                tval = env->bins;
+            }
+            break;
         default:
             break;
         }
target/riscv/translate.c +19 -14
index e356fc6c46..4221d8e2d5 100644
@@ -173,8 +173,27 @@ static void lookup_and_goto_ptr(DisasContext *ctx)
     }
 }
 
+/*
+ * Wrappers for getting reg values.
+ *
+ * The $zero register does not have cpu_gpr[0] allocated -- we supply the
+ * constant zero as a source, and an uninitialized sink as destination.
+ *
+ * Further, we may provide an extension for word operations.
+ */
+static TCGv temp_new(DisasContext *ctx)
+{
+    assert(ctx->ntemp < ARRAY_SIZE(ctx->temp));
+    return ctx->temp[ctx->ntemp++] = tcg_temp_new();
+}
+
 static void gen_exception_illegal(DisasContext *ctx)
 {
+    TCGv tmp = temp_new(ctx);
+
+    tcg_gen_movi_tl(tmp, ctx->opcode);
+    tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, bins));
+
     generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST);
 }
 
@@ -195,20 +214,6 @@ static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
     }
 }
 
-/*
- * Wrappers for getting reg values.
- *
- * The $zero register does not have cpu_gpr[0] allocated -- we supply the
- * constant zero as a source, and an uninitialized sink as destination.
- *
- * Further, we may provide an extension for word operations.
- */
-static TCGv temp_new(DisasContext *ctx)
-{
-    assert(ctx->ntemp < ARRAY_SIZE(ctx->temp));
-    return ctx->temp[ctx->ntemp++] = tcg_temp_new();
-}
-
 static TCGv get_gpr(DisasContext *ctx, int reg_num, DisasExtend ext)
 {
     TCGv t;
Signature · 1 line
2.31.1
#4 lore raw

PATCH 2/2 target/riscv: Set mtval and stval support

In reply to Alistair Francis

From: Alistair Francis <[email protected]>

---
 target/riscv/cpu.h | 1 +
 target/riscv/cpu.c | 6 +++++-
 2 files changed, 6 insertions(+), 1 deletion(-)
target/riscv/cpu.h +1 -0
index 6d41a16ae3..64ebb593fb 100644
@@ -309,6 +309,7 @@ struct RISCVCPU {
         bool mmu;
         bool pmp;
         bool epmp;
+        bool mtval_inst;
         uint64_t resetvec;
     } cfg;
 };
target/riscv/cpu.c +5 -1
index 1a2b03d579..8b77526c79 100644
@@ -437,6 +437,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
         }
     }
 
+    if (cpu->cfg.mtval_inst) {
+        set_feature(env, RISCV_FEATURE_MTVAL_INST);
+    }
+
     set_resetvec(env, cpu->cfg.resetvec);
 
     /* If only XLEN is set for misa, then set misa from properties */
@@ -600,7 +604,7 @@ static Property riscv_cpu_properties[] = {
     DEFINE_PROP_BOOL("mmu", RISCVCPU, cfg.mmu, true),
     DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),
     DEFINE_PROP_BOOL("x-epmp", RISCVCPU, cfg.epmp, false),
-
+    DEFINE_PROP_BOOL("mtval_inst", RISCVCPU, cfg.mtval_inst, true),
     DEFINE_PROP_UINT64("resetvec", RISCVCPU, cfg.resetvec, DEFAULT_RSTVEC),
     DEFINE_PROP_END_OF_LIST(),
 };
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH v1 1/2] target/riscv: Implement the stval/mtval illegal instruction

In reply to Alistair Francis
Quoted 1 line · Alistair Francis

+ target_ulong bins;

Surely uint32_t, at least until 64-bit insn width is required.

target/riscv/translate.c static void lookup_and_goto_ptr(DisasContext *ctx) · line 195

Quoted 4 lines
target/riscv/translate.c +4 -0
+    TCGv tmp = temp_new(ctx);
+
+    tcg_gen_movi_tl(tmp, ctx->opcode);
+    tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, bins));
  tcg_gen_st_i32(tcg_constant_i32(ctx->opcode),
                 cpu_env, offsetof(CPURISCVState, bins));

r~

#5 lore raw

Re: [PATCH v1 2/2] target/riscv: Set mtval and stval support

In reply to Alistair Francis
Quoted 1 line · Alistair Francis

+ DEFINE_PROP_BOOL("mtval_inst", RISCVCPU, cfg.mtval_inst, true),

Dash not underscore for the prop name, I think.

r~

#6 lore raw

Re: [PATCH v1 2/2] target/riscv: Set mtval and stval support

Quoted 6 lines · Richard Henderson
Quoted 1 line · Alistair Francis

+ DEFINE_PROP_BOOL("mtval_inst", RISCVCPU, cfg.mtval_inst, true),

Dash not underscore for the prop name, I think.

But we also have "priv_spec" :)

The name "mtval_inst" sounds like only for M-mode. Maybe omitting 'm', and just "tval"?

Regards,
Bin

#3 lore raw

Re: [PATCH v1 1/2] target/riscv: Implement the stval/mtval illegal instruction

In reply to Alistair Francis
Quoted 42 lines · Alistair Francis

From: Alistair Francis <[email protected]>

The stval and mtval registers can optionally contain the faulting instruction on an illegal instruction exception. This patch adds support for setting the stval and mtval registers based on the CPU feature.

---
 target/riscv/cpu.h        |  5 ++++-
 target/riscv/cpu_helper.c |  9 +++++++++
 target/riscv/translate.c  | 33 +++++++++++++++++++--------------
 3 files changed, 32 insertions(+), 15 deletions(-)
target/riscv/cpu.h +4 -1
index bf1c899c00..6d41a16ae3 100644
@@ -77,7 +77,8 @@ enum {
     RISCV_FEATURE_MMU,
     RISCV_FEATURE_PMP,
     RISCV_FEATURE_EPMP,
-    RISCV_FEATURE_MISA
+    RISCV_FEATURE_MISA,
+    RISCV_FEATURE_MTVAL_INST,
 };
 
 #define PRIV_VERSION_1_10_0 0x00011000
@@ -130,6 +131,8 @@ struct CPURISCVState {
     target_ulong frm;
 
     target_ulong badaddr;
+    target_ulong bins;
+
     target_ulong guest_phys_fault_addr;
 
     target_ulong priv_ver;
target/riscv/cpu_helper.c +0 -0
index 968cb8046f..42edd71c1e 100644

target/riscv/cpu_helper.c void riscv_cpu_do_interrupt(CPUState *cs) · line 972

Quoted 6 lines
target/riscv/cpu_helper.c +3 -0
@@ -967,6 +967,15 @@ void riscv_cpu_do_interrupt(CPUState *cs)
             write_tval  = true;
             tval = env->badaddr;
             break;
+        case RISCV_EXCP_ILLEGAL_INST:
+            if (riscv_feature(env, RISCV_FEATURE_MTVAL_INST)) {
+                /* The stval/mtval register can optionally also be used to

nits: incorrect multi-line comment format

Quoted 26 lines
target/riscv/cpu_helper.c +6 -0
+                 * return the faulting instruction bits on an illegal
+                 * instruction exception.
+                 */
+                tval = env->bins;
+            }
+            break;
         default:
             break;
         }
target/riscv/translate.c +9 -0
index e356fc6c46..4221d8e2d5 100644
@@ -173,8 +173,27 @@ static void lookup_and_goto_ptr(DisasContext *ctx)
     }
 }
 
+/*
+ * Wrappers for getting reg values.
+ *
+ * The $zero register does not have cpu_gpr[0] allocated -- we supply the
+ * constant zero as a source, and an uninitialized sink as destination.
+ *
+ * Further, we may provide an extension for word operations.
+ */
+static TCGv temp_new(DisasContext *ctx)

target/riscv/translate.c static void lookup_and_goto_ptr(DisasContext *ctx) · line 194

Quoted 10 lines
target/riscv/translate.c +8 -0
+{
+    assert(ctx->ntemp < ARRAY_SIZE(ctx->temp));
+    return ctx->temp[ctx->ntemp++] = tcg_temp_new();
+}
+
 static void gen_exception_illegal(DisasContext *ctx)
 {
+    TCGv tmp = temp_new(ctx);
+
+    tcg_gen_movi_tl(tmp, ctx->opcode);

ctx->opcode is not initialized anywhere.

Quoted 17 lines
target/riscv/translate.c +2 -8
+    tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, bins));
+
     generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST);
 }
 
@@ -195,20 +214,6 @@ static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
     }
 }
 
-/*
- * Wrappers for getting reg values.
- *
- * The $zero register does not have cpu_gpr[0] allocated -- we supply the
- * constant zero as a source, and an uninitialized sink as destination.
- *
- * Further, we may provide an extension for word operations.
- */

target/riscv/translate.c static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)

Quoted 10 lines
target/riscv/translate.c +0 -7
-static TCGv temp_new(DisasContext *ctx)
-{
-    assert(ctx->ntemp < ARRAY_SIZE(ctx->temp));
-    return ctx->temp[ctx->ntemp++] = tcg_temp_new();
-}
-
 static TCGv get_gpr(DisasContext *ctx, int reg_num, DisasExtend ext)
 {
     TCGv t;
--

Regards,
Bin