Skip to contentSkip to conversation

riscv: add initial sdext support

PATCH Superseded Daniel Henrique Barboza 3 participants 20 messages Sep 9 – Sep 28

Subsystems (2)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v9 cover riscv: add initial sdext support

Hi there!

In this new version I changed patch 7 to use the new meson tcg tests framework.

No other changes made. Patches based on master.

Changes from v8:
- patch 7:
- use the new meson infrastructure for test-sdext-sigill.S
- v8 link: https://lore.kernel.org/qemu-devel/[email protected]/

Chao Liu (6):
target/riscv: add sdext debug CSRs state target/riscv: add sdext Debug Mode helpers target/riscv: add dret instruction target/riscv: add sdext enter Debug Mode on ebreak target/riscv: add sdext single-step support target/riscv: add sdtrig trigger action=debug mode

Daniel Henrique Barboza (1):
tests/tcg/riscv64: add sdext "not on Debug mode" sigill tests

 include/exec/translation-block.h              |   4 +-
 target/riscv/cpu.c                            |   7 +
 target/riscv/cpu.h                            |   9 ++
 target/riscv/cpu_bits.h                       |  33 +++++
 target/riscv/cpu_cfg_fields.h.inc             |   1 +
 target/riscv/helper.h                         |   3 +
 target/riscv/insn32.decode                    |   1 +
 target/riscv/machine.c                        |  20 +++
 target/riscv/tcg/cpu_helper.c                 |  91 +++++++++++++
 target/riscv/tcg/csr.c                        | 126 ++++++++++++++++++
 target/riscv/tcg/debug.c                      |  59 +++++++-
 .../tcg/insn_trans/trans_privileged.c.inc     |  24 ++++
 target/riscv/tcg/op_helper.c                  |  70 ++++++++++
 target/riscv/tcg/tcg-cpu.c                    |  19 +++
 target/riscv/tcg/translate.c                  |  15 ++-
 tests/tcg/riscv64/system/meson.build          |   8 ++
 tests/tcg/riscv64/test-sdext-sigill.S         |  83 ++++++++++++
 17 files changed, 567 insertions(+), 6 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-sdext-sigill.S
Signature · 1 line
2.43.0
#1 lore raw

PATCH v9 1/7 target/riscv: add sdext debug CSRs state

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Add architectural state for Sdext Debug Mode: debug_mode, dcsr, dpc and dscratch0/1. Wire up CSR access for dcsr/dpc/dscratch and gate them to Debug Mode (or host debugger access).

The Sdext is not fully implemented, so it is disabled by default.

---
 target/riscv/cpu.c                |   7 ++
 target/riscv/cpu.h                |   4 +
 target/riscv/cpu_bits.h           |  33 ++++++++
 target/riscv/cpu_cfg_fields.h.inc |   1 +
 target/riscv/machine.c            |  20 +++++
 target/riscv/tcg/csr.c            | 126 ++++++++++++++++++++++++++++++
 6 files changed, 191 insertions(+)
target/riscv/cpu.c +7 -0
index 5fff9d745e..0fe9762022 100644
@@ -248,6 +248,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(zvkt, PRIV_VERSION_1_12_0, ext_zvkt),
     ISA_EXT_DATA_ENTRY(zhinx, PRIV_VERSION_1_12_0, ext_zhinx),
     ISA_EXT_DATA_ENTRY(zhinxmin, PRIV_VERSION_1_12_0, ext_zhinxmin),
+    ISA_EXT_DATA_ENTRY(sdext, PRIV_VERSION_1_12_0, ext_sdext),
     ISA_EXT_DATA_ENTRY(sdtrig, PRIV_VERSION_1_12_0, ext_sdtrig),
     ISA_INTERNAL_EXT_DATA_ENTRY(shcounterenw, PRIV_VERSION_1_12_0,
                                 has_priv_1_12),
@@ -1091,6 +1092,12 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)
 
 #ifndef CONFIG_USER_ONLY
 #ifdef CONFIG_TCG
+    env->debug_mode = false;
+    env->dcsr = DCSR_DEBUGVER(4);
+    env->dpc = 0;
+    env->dscratch[0] = 0;
+    env->dscratch[1] = 0;
+
     if (cpu->cfg.debug || cpu->cfg.ext_sdtrig) {
         riscv_trigger_reset_hold(env);
     }
target/riscv/cpu.h +4 -0
index c2138dbd4b..7b343d7a3e 100644
@@ -507,6 +507,10 @@ struct CPUArchState {
 
     /* True if in debugger mode.  */
     bool debugger;
+    bool debug_mode;
+    target_ulong dcsr;
+    target_ulong dpc;
+    target_ulong dscratch[2];
 
     uint64_t mstateen[SMSTATEEN_MAX_COUNT];
     uint64_t hstateen[SMSTATEEN_MAX_COUNT];
target/riscv/cpu_bits.h +33 -0
index c01050ce2b..769c945517 100644
@@ -467,6 +467,39 @@
 #define CSR_DCSR            0x7b0
 #define CSR_DPC             0x7b1
 #define CSR_DSCRATCH        0x7b2
+#define CSR_DSCRATCH1       0x7b3
+
+/* DCSR fields */
+#define DCSR_XDEBUGVER_SHIFT    28
+#define DCSR_XDEBUGVER_MASK     (0xfu << DCSR_XDEBUGVER_SHIFT)
+#define DCSR_DEBUGVER(val)      ((target_ulong)(val) << DCSR_XDEBUGVER_SHIFT)
+#define DCSR_EXTCAUSE_SHIFT     24
+#define DCSR_EXTCAUSE_MASK      (0x7u << DCSR_EXTCAUSE_SHIFT)
+#define DCSR_CETRIG             BIT(19)
+#define DCSR_PELP               BIT(18)
+#define DCSR_EBREAKVS           BIT(17)
+#define DCSR_EBREAKVU           BIT(16)
+#define DCSR_EBREAKM            BIT(15)
+#define DCSR_EBREAKS            BIT(13)
+#define DCSR_EBREAKU            BIT(12)
+#define DCSR_STEPIE             BIT(11)
+#define DCSR_STOPCOUNT          BIT(10)
+#define DCSR_STOPTIME           BIT(9)
+#define DCSR_CAUSE_SHIFT        6
+#define DCSR_CAUSE_MASK         (0x7u << DCSR_CAUSE_SHIFT)
+#define DCSR_V                  BIT(5)
+#define DCSR_MPRVEN             BIT(4)
+#define DCSR_NMIP               BIT(3)
+#define DCSR_STEP               BIT(2)
+#define DCSR_PRV_MASK           0x3u
+
+#define DCSR_CAUSE_EBREAK       1
+#define DCSR_CAUSE_TRIGGER      2
+#define DCSR_CAUSE_HALTREQ      3
+#define DCSR_CAUSE_STEP         4
+#define DCSR_CAUSE_RESET        5
+#define DCSR_CAUSE_GROUP        6
+#define DCSR_CAUSE_OTHER        7
 
 /* Performance Counters */
 #define CSR_MHPMCOUNTER3    0xb03
target/riscv/cpu_cfg_fields.h.inc +1 -0
index f91c780299..24f3606f94 100644
@@ -105,6 +105,7 @@ BOOL_FIELD(ext_zvfbfmin)
 BOOL_FIELD(ext_zvfbfwma)
 BOOL_FIELD(ext_zvfh)
 BOOL_FIELD(ext_zvfhmin)
+BOOL_FIELD(ext_sdext)
 BOOL_FIELD(ext_sdtrig)
 BOOL_FIELD(ext_smaia)
 BOOL_FIELD(ext_ssaia)
target/riscv/machine.c +20 -0
index bf203bffce..8658d43643 100644
@@ -301,6 +301,25 @@ static const VMStateDescription vmstate_debug = {
         VMSTATE_VARRAY_UINT32(env.tdata3, RISCVCPU,
                               env.num_triggers, 0,
                               vmstate_info_uint64, uint64_t),
+        VMSTATE_BOOL_V(env.debug_mode, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dcsr, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dpc, RISCVCPU, 3),
+        VMSTATE_UINTTL_ARRAY_V(env.dscratch, RISCVCPU, 2, 3),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_sdext = {
+    .name = "cpu/sdext",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = debug_needed,
+    .post_load = debug_post_load,
+    .fields = (const VMStateField[]) {
+        VMSTATE_BOOL_V(env.debug_mode, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dcsr, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dpc, RISCVCPU, 3),
+        VMSTATE_UINTTL_ARRAY_V(env.dscratch, RISCVCPU, 2, 3),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -576,6 +595,7 @@ const VMStateDescription vmstate_riscv_cpu = {
         &vmstate_ctr,
         &vmstate_sstc,
         &vmstate_mseccfg,
+        &vmstate_sdext,
         NULL
     }
 };
target/riscv/tcg/csr.c +126 -0

138 lines not on this page · raw message

Signature · 1 line
2.43.0
Acked-by on v9 1/7
#2 lore raw

Re: [PATCH v9 1/7] target/riscv: add sdext debug CSRs state

Quoted 9 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Add architectural state for Sdext Debug Mode: debug_mode, dcsr, dpc and dscratch0/1. Wire up CSR access for dcsr/dpc/dscratch and gate them to Debug Mode (or host debugger access).

Quoted 8 lines

The Sdext is not fully implemented, so it is disabled by default.

Reviewed-by Daniel Henrique Barboza

<[email protected]>

Signed-off-by Daniel Henrique Barboza

<[email protected]>

Alistair

Quoted 304 lines

---
 target/riscv/cpu.c                |   7 ++
 target/riscv/cpu.h                |   4 +
 target/riscv/cpu_bits.h           |  33 ++++++++
 target/riscv/cpu_cfg_fields.h.inc |   1 +
 target/riscv/machine.c            |  20 +++++
 target/riscv/tcg/csr.c            | 126
++++++++++++++++++++++++++++++

 6 files changed, 191 insertions(+)
target/riscv/cpu.c +1 -0
index 5fff9d745e..0fe9762022 100644
@@ -248,6 +248,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
      ISA_EXT_DATA_ENTRY(zvkt, PRIV_VERSION_1_12_0, ext_zvkt),
      ISA_EXT_DATA_ENTRY(zhinx, PRIV_VERSION_1_12_0, ext_zhinx),
      ISA_EXT_DATA_ENTRY(zhinxmin, PRIV_VERSION_1_12_0, ext_zhinxmin),
+    ISA_EXT_DATA_ENTRY(sdext, PRIV_VERSION_1_12_0, ext_sdext),
      ISA_EXT_DATA_ENTRY(sdtrig, PRIV_VERSION_1_12_0, ext_sdtrig),
      ISA_INTERNAL_EXT_DATA_ENTRY(shcounterenw, PRIV_VERSION_1_12_0,
                                  has_priv_1_12),
@@ -1091,6 +1092,12 @@ static void riscv_cpu_reset_hold(Object *obj,
 ResetType type)


 #ifndef CONFIG_USER_ONLY
 #ifdef CONFIG_TCG
+    env->debug_mode = false;
+    env->dcsr = DCSR_DEBUGVER(4);
+    env->dpc = 0;
+    env->dscratch[0] = 0;
+    env->dscratch[1] = 0;
+
     if (cpu->cfg.debug || cpu->cfg.ext_sdtrig) {
         riscv_trigger_reset_hold(env);
     }

target/riscv/cpu.h +0 -0
index c2138dbd4b..7b343d7a3e 100644
@@ -507,6 +507,10 @@ struct CPUArchState {


     /* True if in debugger mode.  */
     bool debugger;
+    bool debug_mode;
+    target_ulong dcsr;
+    target_ulong dpc;
+    target_ulong dscratch[2];

     uint64_t mstateen[SMSTATEEN_MAX_COUNT];
     uint64_t hstateen[SMSTATEEN_MAX_COUNT];

target/riscv/cpu_bits.h +33 -0
index c01050ce2b..769c945517 100644
@@ -467,6 +467,39 @@
  #define CSR_DCSR            0x7b0
  #define CSR_DPC             0x7b1
  #define CSR_DSCRATCH        0x7b2
+#define CSR_DSCRATCH1       0x7b3
+
+/* DCSR fields */
+#define DCSR_XDEBUGVER_SHIFT    28
+#define DCSR_XDEBUGVER_MASK     (0xfu << DCSR_XDEBUGVER_SHIFT)
+#define DCSR_DEBUGVER(val)      ((target_ulong)(val) <<
 DCSR_XDEBUGVER_SHIFT)
+#define DCSR_EXTCAUSE_SHIFT     24
+#define DCSR_EXTCAUSE_MASK      (0x7u << DCSR_EXTCAUSE_SHIFT)
+#define DCSR_CETRIG             BIT(19)
+#define DCSR_PELP               BIT(18)
+#define DCSR_EBREAKVS           BIT(17)
+#define DCSR_EBREAKVU           BIT(16)
+#define DCSR_EBREAKM            BIT(15)
+#define DCSR_EBREAKS            BIT(13)
+#define DCSR_EBREAKU            BIT(12)
+#define DCSR_STEPIE             BIT(11)
+#define DCSR_STOPCOUNT          BIT(10)
+#define DCSR_STOPTIME           BIT(9)
+#define DCSR_CAUSE_SHIFT        6
+#define DCSR_CAUSE_MASK         (0x7u << DCSR_CAUSE_SHIFT)
+#define DCSR_V                  BIT(5)
+#define DCSR_MPRVEN             BIT(4)
+#define DCSR_NMIP               BIT(3)
+#define DCSR_STEP               BIT(2)
+#define DCSR_PRV_MASK           0x3u
+
+#define DCSR_CAUSE_EBREAK       1
+#define DCSR_CAUSE_TRIGGER      2
+#define DCSR_CAUSE_HALTREQ      3
+#define DCSR_CAUSE_STEP         4
+#define DCSR_CAUSE_RESET        5
+#define DCSR_CAUSE_GROUP        6
+#define DCSR_CAUSE_OTHER        7


 /* Performance Counters */
 #define CSR_MHPMCOUNTER3    0xb03

target/riscv/cpu_cfg_fields.h.inc +1 -0
index f91c780299..24f3606f94 100644
@@ -105,6 +105,7 @@ BOOL_FIELD(ext_zvfbfmin)
  BOOL_FIELD(ext_zvfbfwma)
  BOOL_FIELD(ext_zvfh)
  BOOL_FIELD(ext_zvfhmin)
+BOOL_FIELD(ext_sdext)
  BOOL_FIELD(ext_sdtrig)
  BOOL_FIELD(ext_smaia)
  BOOL_FIELD(ext_ssaia)
target/riscv/machine.c +19 -0
index bf203bffce..8658d43643 100644
@@ -301,6 +301,25 @@ static const VMStateDescription vmstate_debug =
 {
          VMSTATE_VARRAY_UINT32(env.tdata3, RISCVCPU,
                                env.num_triggers, 0,
                                vmstate_info_uint64, uint64_t),
+        VMSTATE_BOOL_V(env.debug_mode, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dcsr, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dpc, RISCVCPU, 3),
+        VMSTATE_UINTTL_ARRAY_V(env.dscratch, RISCVCPU, 2, 3),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_sdext = {
+    .name = "cpu/sdext",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = debug_needed,
+    .post_load = debug_post_load,
+    .fields = (const VMStateField[]) {
+        VMSTATE_BOOL_V(env.debug_mode, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dcsr, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dpc, RISCVCPU, 3),
+        VMSTATE_UINTTL_ARRAY_V(env.dscratch, RISCVCPU, 2, 3),
          VMSTATE_END_OF_LIST()
      }

 };

target/riscv/machine.c +1 -0
@@ -576,6 +595,7 @@ const VMStateDescription vmstate_riscv_cpu = {
          &vmstate_ctr,
          &vmstate_sstc,
          &vmstate_mseccfg,
+        &vmstate_sdext,

         NULL
     }
 };

target/riscv/tcg/csr.c +0 -0
index bd4b6dc114..988261cccf 100644
@@ -3207,6 +3207,126 @@ static RISCVException
 write_mtval(CPURISCVState *env, int csrno,
      return RISCV_EXCP_NONE;
  }

target/riscv/tcg/csr.c +49 -0
+#if !defined(CONFIG_USER_ONLY)
+static RISCVException sdext(CPURISCVState *env, int csrno)
+{
+    if (!riscv_cpu_cfg(env)->ext_sdext) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
+    if (!env->debug_mode && !env->debugger) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
+    return RISCV_EXCP_NONE;
+}
+
+static target_ulong dcsr_visible_mask(CPURISCVState *env)
+{
+    target_ulong mask = (target_ulong)-1;
+    RISCVCPU *cpu = env_archcpu(env);
+
+    if (!riscv_has_ext(env, RVH)) {
+        mask &= ~(DCSR_EBREAKVS | DCSR_EBREAKVU | DCSR_V);
+    }
+    if (!riscv_has_ext(env, RVS)) {
+        mask &= ~DCSR_EBREAKS;
+    }
+    if (!riscv_has_ext(env, RVU)) {
+        mask &= ~DCSR_EBREAKU;
+    }
+    if (!cpu->cfg.ext_zicfilp) {
+        mask &= ~DCSR_PELP;
+    }
+    if (!cpu->cfg.ext_smdbltrp) {
+        mask &= ~DCSR_CETRIG;
+    }
+
+    return mask;
+}
+
+static RISCVException read_dcsr(CPURISCVState *env, int csrno,
+                                target_ulong *val)
+{
+    *val = env->dcsr & dcsr_visible_mask(env);
+    return RISCV_EXCP_NONE;
+}
+
+static target_ulong dcsr_writable_mask(CPURISCVState *env)
+{
+    target_ulong mask = DCSR_EBREAKM | DCSR_EBREAKS | DCSR_EBREAKU |
+                        DCSR_STEPIE | DCSR_STOPCOUNT | DCSR_STOPTIME

|
+                        DCSR_STEP | DCSR_PRV_MASK; +    RISCVCPU *cpu = env_archcpu(env);

target/riscv/tcg/csr.c +69 -0
+
+    mask |= DCSR_MPRVEN;
+
+    if (riscv_has_ext(env, RVH)) {
+        mask |= DCSR_EBREAKVS | DCSR_EBREAKVU | DCSR_V;
+    }
+    if (riscv_has_ext(env, RVS)) {
+        mask |= DCSR_EBREAKS;
+    }
+    if (riscv_has_ext(env, RVU)) {
+        mask |= DCSR_EBREAKU;
+    }
+    if (cpu->cfg.ext_zicfilp) {
+        mask |= DCSR_PELP;
+    }
+    if (cpu->cfg.ext_smdbltrp) {
+        mask |= DCSR_CETRIG;
+    }
+
+    return mask;
+}
+
+static RISCVException write_dcsr(CPURISCVState *env, int csrno,
+                                 target_ulong val, uintptr_t ra)
+{
+    target_ulong mask = dcsr_writable_mask(env);
+    target_ulong new_val = env->dcsr;
+
+    new_val &= ~mask;
+    new_val |= val & mask;
+    new_val &= ~DCSR_XDEBUGVER_MASK;
+    new_val |= DCSR_DEBUGVER(4);
+    env->dcsr = new_val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_dpc(CPURISCVState *env, int csrno,
+                               target_ulong *val)
+{
+    *val = env->dpc & get_xepc_mask(env);
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_dpc(CPURISCVState *env, int csrno,
+                                target_ulong val, uintptr_t ra)
+{
+    env->dpc = val & get_xepc_mask(env);
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_dscratch(CPURISCVState *env, int csrno,
+                                    target_ulong *val)
+{
+    int index = (csrno == CSR_DSCRATCH1) ? 1 : 0;
+
+    *val = env->dscratch[index];
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_dscratch(CPURISCVState *env, int csrno,
+                                     target_ulong val, uintptr_t ra)
+{
+    int index = (csrno == CSR_DSCRATCH1) ? 1 : 0;
+
+    env->dscratch[index] = val;
+    return RISCV_EXCP_NONE;
+}
+#endif /* !CONFIG_USER_ONLY */
+

 /* Execution environment configuration setup */
 static RISCVException read_menvcfg(CPURISCVState *env, int csrno, target_ulong *val)

target/riscv/tcg/csr.c +0 -0
@@ -6435,6 +6555,12 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =
 {
      [CSR_TDATA3]    =  { "tdata3",   debug, read_tdata,   
 write_tdata    },
      [CSR_TINFO]     =  { "tinfo",    debug, read_tinfo,   
 write_ignore   },
      [CSR_MCONTEXT]  =  { "mcontext", debug, read_mcontext,

write_mcontext },
+#if !defined(CONFIG_USER_ONLY)
+    [CSR_DCSR]      =  { "dcsr",      sdext, read_dcsr, write_dcsr },
+    [CSR_DPC]       =  { "dpc",       sdext, read_dpc, write_dpc },
+    [CSR_DSCRATCH]  =  { "dscratch0", sdext, read_dscratch, write_dscratch },
+    [CSR_DSCRATCH1] =  { "dscratch1", sdext, read_dscratch, write_dscratch },
+#endif

     [CSR_MCTRCTL]    = { "mctrctl",    ctr_mmode,  NULL, NULL, rmw_xctrctl    },
     [CSR_SCTRCTL]    = { "sctrctl",    ctr_smode,  NULL, NULL, rmw_xctrctl    },

#3 lore raw

Re: [PATCH v9 1/7] target/riscv: add sdext debug CSRs state

In reply to Daniel Henrique Barboza
Quoted 58 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Add architectural state for Sdext Debug Mode: debug_mode, dcsr, dpc and dscratch0/1. Wire up CSR access for dcsr/dpc/dscratch and gate them to Debug Mode (or host debugger access).

The Sdext is not fully implemented, so it is disabled by default.

Reviewed-by Daniel Henrique Barboza

<[email protected]>

Signed-off-by Daniel Henrique Barboza

<[email protected]>
---
 target/riscv/cpu.c                |   7 ++
 target/riscv/cpu.h                |   4 +
 target/riscv/cpu_bits.h           |  33 ++++++++
 target/riscv/cpu_cfg_fields.h.inc |   1 +
 target/riscv/machine.c            |  20 +++++
 target/riscv/tcg/csr.c            | 126
++++++++++++++++++++++++++++++

 6 files changed, 191 insertions(+)
target/riscv/cpu.c +1 -0
index 5fff9d745e..0fe9762022 100644
@@ -248,6 +248,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
      ISA_EXT_DATA_ENTRY(zvkt, PRIV_VERSION_1_12_0, ext_zvkt),
      ISA_EXT_DATA_ENTRY(zhinx, PRIV_VERSION_1_12_0, ext_zhinx),
      ISA_EXT_DATA_ENTRY(zhinxmin, PRIV_VERSION_1_12_0, ext_zhinxmin),
+    ISA_EXT_DATA_ENTRY(sdext, PRIV_VERSION_1_12_0, ext_sdext),
      ISA_EXT_DATA_ENTRY(sdtrig, PRIV_VERSION_1_12_0, ext_sdtrig),
      ISA_INTERNAL_EXT_DATA_ENTRY(shcounterenw, PRIV_VERSION_1_12_0,
                                  has_priv_1_12),
@@ -1091,6 +1092,12 @@ static void riscv_cpu_reset_hold(Object *obj,
 ResetType type)


 #ifndef CONFIG_USER_ONLY
 #ifdef CONFIG_TCG
+    env->debug_mode = false;
+    env->dcsr = DCSR_DEBUGVER(4);
+    env->dpc = 0;
+    env->dscratch[0] = 0;
+    env->dscratch[1] = 0;
+
     if (cpu->cfg.debug || cpu->cfg.ext_sdtrig) {
         riscv_trigger_reset_hold(env);
     }

target/riscv/cpu.h +0 -0
index c2138dbd4b..7b343d7a3e 100644
@@ -507,6 +507,10 @@ struct CPUArchState {
Quoted 5 lines


     /* True if in debugger mode.  */
     bool debugger;
+    bool debug_mode;
+    target_ulong dcsr;

Oh, this is only a 32-bit register!

Alistair

Quoted 258 lines

+    target_ulong dpc;
+    target_ulong dscratch[2];

     uint64_t mstateen[SMSTATEEN_MAX_COUNT];
     uint64_t hstateen[SMSTATEEN_MAX_COUNT];

target/riscv/cpu_bits.h +33 -0
index c01050ce2b..769c945517 100644
@@ -467,6 +467,39 @@
  #define CSR_DCSR            0x7b0
  #define CSR_DPC             0x7b1
  #define CSR_DSCRATCH        0x7b2
+#define CSR_DSCRATCH1       0x7b3
+
+/* DCSR fields */
+#define DCSR_XDEBUGVER_SHIFT    28
+#define DCSR_XDEBUGVER_MASK     (0xfu << DCSR_XDEBUGVER_SHIFT)
+#define DCSR_DEBUGVER(val)      ((target_ulong)(val) <<
 DCSR_XDEBUGVER_SHIFT)
+#define DCSR_EXTCAUSE_SHIFT     24
+#define DCSR_EXTCAUSE_MASK      (0x7u << DCSR_EXTCAUSE_SHIFT)
+#define DCSR_CETRIG             BIT(19)
+#define DCSR_PELP               BIT(18)
+#define DCSR_EBREAKVS           BIT(17)
+#define DCSR_EBREAKVU           BIT(16)
+#define DCSR_EBREAKM            BIT(15)
+#define DCSR_EBREAKS            BIT(13)
+#define DCSR_EBREAKU            BIT(12)
+#define DCSR_STEPIE             BIT(11)
+#define DCSR_STOPCOUNT          BIT(10)
+#define DCSR_STOPTIME           BIT(9)
+#define DCSR_CAUSE_SHIFT        6
+#define DCSR_CAUSE_MASK         (0x7u << DCSR_CAUSE_SHIFT)
+#define DCSR_V                  BIT(5)
+#define DCSR_MPRVEN             BIT(4)
+#define DCSR_NMIP               BIT(3)
+#define DCSR_STEP               BIT(2)
+#define DCSR_PRV_MASK           0x3u
+
+#define DCSR_CAUSE_EBREAK       1
+#define DCSR_CAUSE_TRIGGER      2
+#define DCSR_CAUSE_HALTREQ      3
+#define DCSR_CAUSE_STEP         4
+#define DCSR_CAUSE_RESET        5
+#define DCSR_CAUSE_GROUP        6
+#define DCSR_CAUSE_OTHER        7


 /* Performance Counters */
 #define CSR_MHPMCOUNTER3    0xb03

target/riscv/cpu_cfg_fields.h.inc +1 -0
index f91c780299..24f3606f94 100644
@@ -105,6 +105,7 @@ BOOL_FIELD(ext_zvfbfmin)
  BOOL_FIELD(ext_zvfbfwma)
  BOOL_FIELD(ext_zvfh)
  BOOL_FIELD(ext_zvfhmin)
+BOOL_FIELD(ext_sdext)
  BOOL_FIELD(ext_sdtrig)
  BOOL_FIELD(ext_smaia)
  BOOL_FIELD(ext_ssaia)
target/riscv/machine.c +19 -0
index bf203bffce..8658d43643 100644
@@ -301,6 +301,25 @@ static const VMStateDescription vmstate_debug =
 {
          VMSTATE_VARRAY_UINT32(env.tdata3, RISCVCPU,
                                env.num_triggers, 0,
                                vmstate_info_uint64, uint64_t),
+        VMSTATE_BOOL_V(env.debug_mode, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dcsr, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dpc, RISCVCPU, 3),
+        VMSTATE_UINTTL_ARRAY_V(env.dscratch, RISCVCPU, 2, 3),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_sdext = {
+    .name = "cpu/sdext",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = debug_needed,
+    .post_load = debug_post_load,
+    .fields = (const VMStateField[]) {
+        VMSTATE_BOOL_V(env.debug_mode, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dcsr, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dpc, RISCVCPU, 3),
+        VMSTATE_UINTTL_ARRAY_V(env.dscratch, RISCVCPU, 2, 3),
          VMSTATE_END_OF_LIST()
      }

 };

target/riscv/machine.c +1 -0
@@ -576,6 +595,7 @@ const VMStateDescription vmstate_riscv_cpu = {
          &vmstate_ctr,
          &vmstate_sstc,
          &vmstate_mseccfg,
+        &vmstate_sdext,

         NULL
     }
 };

target/riscv/tcg/csr.c +0 -0
index bd4b6dc114..988261cccf 100644
@@ -3207,6 +3207,126 @@ static RISCVException
 write_mtval(CPURISCVState *env, int csrno,
      return RISCV_EXCP_NONE;
  }

target/riscv/tcg/csr.c +49 -0
+#if !defined(CONFIG_USER_ONLY)
+static RISCVException sdext(CPURISCVState *env, int csrno)
+{
+    if (!riscv_cpu_cfg(env)->ext_sdext) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
+    if (!env->debug_mode && !env->debugger) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
+    return RISCV_EXCP_NONE;
+}
+
+static target_ulong dcsr_visible_mask(CPURISCVState *env)
+{
+    target_ulong mask = (target_ulong)-1;
+    RISCVCPU *cpu = env_archcpu(env);
+
+    if (!riscv_has_ext(env, RVH)) {
+        mask &= ~(DCSR_EBREAKVS | DCSR_EBREAKVU | DCSR_V);
+    }
+    if (!riscv_has_ext(env, RVS)) {
+        mask &= ~DCSR_EBREAKS;
+    }
+    if (!riscv_has_ext(env, RVU)) {
+        mask &= ~DCSR_EBREAKU;
+    }
+    if (!cpu->cfg.ext_zicfilp) {
+        mask &= ~DCSR_PELP;
+    }
+    if (!cpu->cfg.ext_smdbltrp) {
+        mask &= ~DCSR_CETRIG;
+    }
+
+    return mask;
+}
+
+static RISCVException read_dcsr(CPURISCVState *env, int csrno,
+                                target_ulong *val)
+{
+    *val = env->dcsr & dcsr_visible_mask(env);
+    return RISCV_EXCP_NONE;
+}
+
+static target_ulong dcsr_writable_mask(CPURISCVState *env)
+{
+    target_ulong mask = DCSR_EBREAKM | DCSR_EBREAKS | DCSR_EBREAKU |
+                        DCSR_STEPIE | DCSR_STOPCOUNT | DCSR_STOPTIME

|
+                        DCSR_STEP | DCSR_PRV_MASK; +    RISCVCPU *cpu = env_archcpu(env);

target/riscv/tcg/csr.c +69 -0
+
+    mask |= DCSR_MPRVEN;
+
+    if (riscv_has_ext(env, RVH)) {
+        mask |= DCSR_EBREAKVS | DCSR_EBREAKVU | DCSR_V;
+    }
+    if (riscv_has_ext(env, RVS)) {
+        mask |= DCSR_EBREAKS;
+    }
+    if (riscv_has_ext(env, RVU)) {
+        mask |= DCSR_EBREAKU;
+    }
+    if (cpu->cfg.ext_zicfilp) {
+        mask |= DCSR_PELP;
+    }
+    if (cpu->cfg.ext_smdbltrp) {
+        mask |= DCSR_CETRIG;
+    }
+
+    return mask;
+}
+
+static RISCVException write_dcsr(CPURISCVState *env, int csrno,
+                                 target_ulong val, uintptr_t ra)
+{
+    target_ulong mask = dcsr_writable_mask(env);
+    target_ulong new_val = env->dcsr;
+
+    new_val &= ~mask;
+    new_val |= val & mask;
+    new_val &= ~DCSR_XDEBUGVER_MASK;
+    new_val |= DCSR_DEBUGVER(4);
+    env->dcsr = new_val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_dpc(CPURISCVState *env, int csrno,
+                               target_ulong *val)
+{
+    *val = env->dpc & get_xepc_mask(env);
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_dpc(CPURISCVState *env, int csrno,
+                                target_ulong val, uintptr_t ra)
+{
+    env->dpc = val & get_xepc_mask(env);
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_dscratch(CPURISCVState *env, int csrno,
+                                    target_ulong *val)
+{
+    int index = (csrno == CSR_DSCRATCH1) ? 1 : 0;
+
+    *val = env->dscratch[index];
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_dscratch(CPURISCVState *env, int csrno,
+                                     target_ulong val, uintptr_t ra)
+{
+    int index = (csrno == CSR_DSCRATCH1) ? 1 : 0;
+
+    env->dscratch[index] = val;
+    return RISCV_EXCP_NONE;
+}
+#endif /* !CONFIG_USER_ONLY */
+

 /* Execution environment configuration setup */
 static RISCVException read_menvcfg(CPURISCVState *env, int csrno, target_ulong *val)

target/riscv/tcg/csr.c +0 -0
@@ -6435,6 +6555,12 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =
 {
      [CSR_TDATA3]    =  { "tdata3",   debug, read_tdata,   
 write_tdata    },
      [CSR_TINFO]     =  { "tinfo",    debug, read_tinfo,   
 write_ignore   },
      [CSR_MCONTEXT]  =  { "mcontext", debug, read_mcontext,

write_mcontext },
+#if !defined(CONFIG_USER_ONLY)
+    [CSR_DCSR]      =  { "dcsr",      sdext, read_dcsr, write_dcsr },
+    [CSR_DPC]       =  { "dpc",       sdext, read_dpc, write_dpc },
+    [CSR_DSCRATCH]  =  { "dscratch0", sdext, read_dscratch, write_dscratch },
+    [CSR_DSCRATCH1] =  { "dscratch1", sdext, read_dscratch, write_dscratch },
+#endif

     [CSR_MCTRCTL]    = { "mctrctl",    ctr_mmode,  NULL, NULL, rmw_xctrctl    },
     [CSR_SCTRCTL]    = { "sctrctl",    ctr_smode,  NULL, NULL, rmw_xctrctl    },

Reviewed-by on v9 1/7
#4 lore raw

Re: [PATCH v9 1/7] target/riscv: add sdext debug CSRs state

In reply to Daniel Henrique Barboza
Quoted 9 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Add architectural state for Sdext Debug Mode: debug_mode, dcsr, dpc and dscratch0/1. Wire up CSR access for dcsr/dpc/dscratch and gate them to Debug Mode (or host debugger access).

Quoted 6 lines

The Sdext is not fully implemented, so it is disabled by default.

Thanks,
Nutty

Quoted 287 lines
---
  target/riscv/cpu.c                |   7 ++
  target/riscv/cpu.h                |   4 +
  target/riscv/cpu_bits.h           |  33 ++++++++
  target/riscv/cpu_cfg_fields.h.inc |   1 +
  target/riscv/machine.c            |  20 +++++
  target/riscv/tcg/csr.c            | 126 ++++++++++++++++++++++++++++++
  6 files changed, 191 insertions(+)
target/riscv/cpu.c +7 -0
index 5fff9d745e..0fe9762022 100644
@@ -248,6 +248,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
      ISA_EXT_DATA_ENTRY(zvkt, PRIV_VERSION_1_12_0, ext_zvkt),
      ISA_EXT_DATA_ENTRY(zhinx, PRIV_VERSION_1_12_0, ext_zhinx),
      ISA_EXT_DATA_ENTRY(zhinxmin, PRIV_VERSION_1_12_0, ext_zhinxmin),
+    ISA_EXT_DATA_ENTRY(sdext, PRIV_VERSION_1_12_0, ext_sdext),
      ISA_EXT_DATA_ENTRY(sdtrig, PRIV_VERSION_1_12_0, ext_sdtrig),
      ISA_INTERNAL_EXT_DATA_ENTRY(shcounterenw, PRIV_VERSION_1_12_0,
                                  has_priv_1_12),
@@ -1091,6 +1092,12 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)
 
  #ifndef CONFIG_USER_ONLY
  #ifdef CONFIG_TCG
+    env->debug_mode = false;
+    env->dcsr = DCSR_DEBUGVER(4);
+    env->dpc = 0;
+    env->dscratch[0] = 0;
+    env->dscratch[1] = 0;
+
      if (cpu->cfg.debug || cpu->cfg.ext_sdtrig) {
          riscv_trigger_reset_hold(env);
      }
target/riscv/cpu.h +4 -0
index c2138dbd4b..7b343d7a3e 100644
@@ -507,6 +507,10 @@ struct CPUArchState {
 
      /* True if in debugger mode.  */
      bool debugger;
+    bool debug_mode;
+    target_ulong dcsr;
+    target_ulong dpc;
+    target_ulong dscratch[2];
 
      uint64_t mstateen[SMSTATEEN_MAX_COUNT];
      uint64_t hstateen[SMSTATEEN_MAX_COUNT];
target/riscv/cpu_bits.h +33 -0
index c01050ce2b..769c945517 100644
@@ -467,6 +467,39 @@
  #define CSR_DCSR            0x7b0
  #define CSR_DPC             0x7b1
  #define CSR_DSCRATCH        0x7b2
+#define CSR_DSCRATCH1       0x7b3
+
+/* DCSR fields */
+#define DCSR_XDEBUGVER_SHIFT    28
+#define DCSR_XDEBUGVER_MASK     (0xfu << DCSR_XDEBUGVER_SHIFT)
+#define DCSR_DEBUGVER(val)      ((target_ulong)(val) << DCSR_XDEBUGVER_SHIFT)
+#define DCSR_EXTCAUSE_SHIFT     24
+#define DCSR_EXTCAUSE_MASK      (0x7u << DCSR_EXTCAUSE_SHIFT)
+#define DCSR_CETRIG             BIT(19)
+#define DCSR_PELP               BIT(18)
+#define DCSR_EBREAKVS           BIT(17)
+#define DCSR_EBREAKVU           BIT(16)
+#define DCSR_EBREAKM            BIT(15)
+#define DCSR_EBREAKS            BIT(13)
+#define DCSR_EBREAKU            BIT(12)
+#define DCSR_STEPIE             BIT(11)
+#define DCSR_STOPCOUNT          BIT(10)
+#define DCSR_STOPTIME           BIT(9)
+#define DCSR_CAUSE_SHIFT        6
+#define DCSR_CAUSE_MASK         (0x7u << DCSR_CAUSE_SHIFT)
+#define DCSR_V                  BIT(5)
+#define DCSR_MPRVEN             BIT(4)
+#define DCSR_NMIP               BIT(3)
+#define DCSR_STEP               BIT(2)
+#define DCSR_PRV_MASK           0x3u
+
+#define DCSR_CAUSE_EBREAK       1
+#define DCSR_CAUSE_TRIGGER      2
+#define DCSR_CAUSE_HALTREQ      3
+#define DCSR_CAUSE_STEP         4
+#define DCSR_CAUSE_RESET        5
+#define DCSR_CAUSE_GROUP        6
+#define DCSR_CAUSE_OTHER        7
 
  /* Performance Counters */
  #define CSR_MHPMCOUNTER3    0xb03
target/riscv/cpu_cfg_fields.h.inc +1 -0
index f91c780299..24f3606f94 100644
@@ -105,6 +105,7 @@ BOOL_FIELD(ext_zvfbfmin)
  BOOL_FIELD(ext_zvfbfwma)
  BOOL_FIELD(ext_zvfh)
  BOOL_FIELD(ext_zvfhmin)
+BOOL_FIELD(ext_sdext)
  BOOL_FIELD(ext_sdtrig)
  BOOL_FIELD(ext_smaia)
  BOOL_FIELD(ext_ssaia)
target/riscv/machine.c +20 -0
index bf203bffce..8658d43643 100644
@@ -301,6 +301,25 @@ static const VMStateDescription vmstate_debug = {
          VMSTATE_VARRAY_UINT32(env.tdata3, RISCVCPU,
                                env.num_triggers, 0,
                                vmstate_info_uint64, uint64_t),
+        VMSTATE_BOOL_V(env.debug_mode, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dcsr, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dpc, RISCVCPU, 3),
+        VMSTATE_UINTTL_ARRAY_V(env.dscratch, RISCVCPU, 2, 3),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_sdext = {
+    .name = "cpu/sdext",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = debug_needed,
+    .post_load = debug_post_load,
+    .fields = (const VMStateField[]) {
+        VMSTATE_BOOL_V(env.debug_mode, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dcsr, RISCVCPU, 3),
+        VMSTATE_UINTTL_V(env.dpc, RISCVCPU, 3),
+        VMSTATE_UINTTL_ARRAY_V(env.dscratch, RISCVCPU, 2, 3),
          VMSTATE_END_OF_LIST()
      }
  };
@@ -576,6 +595,7 @@ const VMStateDescription vmstate_riscv_cpu = {
          &vmstate_ctr,
          &vmstate_sstc,
          &vmstate_mseccfg,
+        &vmstate_sdext,
          NULL
      }
  };
target/riscv/tcg/csr.c +126 -0
index bd4b6dc114..988261cccf 100644
@@ -3207,6 +3207,126 @@ static RISCVException write_mtval(CPURISCVState *env, int csrno,
      return RISCV_EXCP_NONE;
  }
 
+#if !defined(CONFIG_USER_ONLY)
+static RISCVException sdext(CPURISCVState *env, int csrno)
+{
+    if (!riscv_cpu_cfg(env)->ext_sdext) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
+    if (!env->debug_mode && !env->debugger) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
+    return RISCV_EXCP_NONE;
+}
+
+static target_ulong dcsr_visible_mask(CPURISCVState *env)
+{
+    target_ulong mask = (target_ulong)-1;
+    RISCVCPU *cpu = env_archcpu(env);
+
+    if (!riscv_has_ext(env, RVH)) {
+        mask &= ~(DCSR_EBREAKVS | DCSR_EBREAKVU | DCSR_V);
+    }
+    if (!riscv_has_ext(env, RVS)) {
+        mask &= ~DCSR_EBREAKS;
+    }
+    if (!riscv_has_ext(env, RVU)) {
+        mask &= ~DCSR_EBREAKU;
+    }
+    if (!cpu->cfg.ext_zicfilp) {
+        mask &= ~DCSR_PELP;
+    }
+    if (!cpu->cfg.ext_smdbltrp) {
+        mask &= ~DCSR_CETRIG;
+    }
+
+    return mask;
+}
+
+static RISCVException read_dcsr(CPURISCVState *env, int csrno,
+                                target_ulong *val)
+{
+    *val = env->dcsr & dcsr_visible_mask(env);
+    return RISCV_EXCP_NONE;
+}
+
+static target_ulong dcsr_writable_mask(CPURISCVState *env)
+{
+    target_ulong mask = DCSR_EBREAKM | DCSR_EBREAKS | DCSR_EBREAKU |
+                        DCSR_STEPIE | DCSR_STOPCOUNT | DCSR_STOPTIME |
+                        DCSR_STEP | DCSR_PRV_MASK;
+    RISCVCPU *cpu = env_archcpu(env);
+
+    mask |= DCSR_MPRVEN;
+
+    if (riscv_has_ext(env, RVH)) {
+        mask |= DCSR_EBREAKVS | DCSR_EBREAKVU | DCSR_V;
+    }
+    if (riscv_has_ext(env, RVS)) {
+        mask |= DCSR_EBREAKS;
+    }
+    if (riscv_has_ext(env, RVU)) {
+        mask |= DCSR_EBREAKU;
+    }
+    if (cpu->cfg.ext_zicfilp) {
+        mask |= DCSR_PELP;
+    }
+    if (cpu->cfg.ext_smdbltrp) {
+        mask |= DCSR_CETRIG;
+    }
+
+    return mask;
+}
+
+static RISCVException write_dcsr(CPURISCVState *env, int csrno,
+                                 target_ulong val, uintptr_t ra)
+{
+    target_ulong mask = dcsr_writable_mask(env);
+    target_ulong new_val = env->dcsr;
+
+    new_val &= ~mask;
+    new_val |= val & mask;
+    new_val &= ~DCSR_XDEBUGVER_MASK;
+    new_val |= DCSR_DEBUGVER(4);
+    env->dcsr = new_val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_dpc(CPURISCVState *env, int csrno,
+                               target_ulong *val)
+{
+    *val = env->dpc & get_xepc_mask(env);
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_dpc(CPURISCVState *env, int csrno,
+                                target_ulong val, uintptr_t ra)
+{
+    env->dpc = val & get_xepc_mask(env);
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_dscratch(CPURISCVState *env, int csrno,
+                                    target_ulong *val)
+{
+    int index = (csrno == CSR_DSCRATCH1) ? 1 : 0;
+
+    *val = env->dscratch[index];
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_dscratch(CPURISCVState *env, int csrno,
+                                     target_ulong val, uintptr_t ra)
+{
+    int index = (csrno == CSR_DSCRATCH1) ? 1 : 0;
+
+    env->dscratch[index] = val;
+    return RISCV_EXCP_NONE;
+}
+#endif /* !CONFIG_USER_ONLY */
+
  /* Execution environment configuration setup */
  static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
                                     target_ulong *val)
@@ -6435,6 +6555,12 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
      [CSR_TDATA3]    =  { "tdata3",   debug, read_tdata,    write_tdata    },
      [CSR_TINFO]     =  { "tinfo",    debug, read_tinfo,    write_ignore   },
      [CSR_MCONTEXT]  =  { "mcontext", debug, read_mcontext, write_mcontext },
+#if !defined(CONFIG_USER_ONLY)
+    [CSR_DCSR]      =  { "dcsr",      sdext, read_dcsr,     write_dcsr },
+    [CSR_DPC]       =  { "dpc",       sdext, read_dpc,      write_dpc },
+    [CSR_DSCRATCH]  =  { "dscratch0", sdext, read_dscratch, write_dscratch },
+    [CSR_DSCRATCH1] =  { "dscratch1", sdext, read_dscratch, write_dscratch },
+#endif
 
      [CSR_MCTRCTL]    = { "mctrctl",    ctr_mmode,  NULL, NULL, rmw_xctrctl    },
      [CSR_SCTRCTL]    = { "sctrctl",    ctr_smode,  NULL, NULL, rmw_xctrctl    },
#5 lore raw

PATCH v9 2/7 target/riscv: add sdext Debug Mode helpers

In reply to Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Add helpers to enter/leave Debug Mode and to update dpc/dcsr. Model resume without a Debug Module by leaving Debug Mode at cpu_exec_enter and continuing from dpc.

---
 target/riscv/cpu.h            |  3 ++
 target/riscv/tcg/cpu_helper.c | 85 +++++++++++++++++++++++++++++++++++
 target/riscv/tcg/debug.c      |  5 +++
 target/riscv/tcg/tcg-cpu.c    | 14 ++++++
 4 files changed, 107 insertions(+)
target/riscv/cpu.h +3 -0
index 7b343d7a3e..82eb61a18c 100644
@@ -671,6 +671,9 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
 char *riscv_isa_string(RISCVCPU *cpu);
 int riscv_cpu_max_xlen(RISCVCPUClass *mcc);
 bool riscv_cpu_option_set(RISCVCPU *cpu, const char *optname);
+void riscv_cpu_enter_debug_mode(CPURISCVState *env, target_ulong pc,
+                                uint32_t cause);
+void riscv_cpu_leave_debug_mode(CPURISCVState *env);
 
 #ifndef CONFIG_USER_ONLY
 void riscv_cpu_do_interrupt(CPUState *cpu);
target/riscv/tcg/cpu_helper.c +85 -0

91 lines not on this page · raw message

target/riscv/tcg/debug.c +5 -0
index e4d719a1da..2537515f54 100644
@@ -973,6 +973,11 @@ void riscv_cpu_debug_excp_handler(CPUState *cs)
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
 
+    /* Triggers must not match or fire while in Debug Mode. */
+    if (env->debug_mode) {
+        return;
+    }
+
     if (cs->watchpoint_hit) {
         if (cs->watchpoint_hit->flags & BP_CPU) {
             do_trigger_action(env, DBG_ACTION_BP);
target/riscv/tcg/tcg-cpu.c +14 -0
index b68160af83..959a1e89c8 100644
@@ -269,6 +269,19 @@ static vaddr riscv_pointer_wrap(CPUState *cs, int mmu_idx,
     }
     return extract64(result, 0, 64 - pm_len);
 }
+
+static void riscv_cpu_exec_enter(CPUState *cs)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    CPURISCVState *env = &cpu->env;
+
+    if (!cpu->cfg.ext_sdext || !env->debug_mode) {
+        return;
+    }
+    target_ulong pc = env->dpc;
+    riscv_cpu_leave_debug_mode(env);
+    env->pc = pc;
+}
 #endif
 
 const TCGCPUOps riscv_tcg_ops = {
@@ -285,6 +298,7 @@ const TCGCPUOps riscv_tcg_ops = {
 #ifndef CONFIG_USER_ONLY
     .tlb_fill = riscv_cpu_tlb_fill,
     .pointer_wrap = riscv_pointer_wrap,
+    .cpu_exec_enter = riscv_cpu_exec_enter,
     .cpu_exec_interrupt = riscv_cpu_exec_interrupt,
     .cpu_exec_halt = riscv_cpu_has_work,
     .cpu_exec_reset = cpu_reset,
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v9 2/7] target/riscv: add sdext Debug Mode helpers

Quoted 57 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Add helpers to enter/leave Debug Mode and to update dpc/dcsr. Model resume without a Debug Module by leaving Debug Mode at cpu_exec_enter and continuing from dpc.

Reviewed-by Daniel Henrique Barboza

<[email protected]>

Signed-off-by Daniel Henrique Barboza

<[email protected]>
---
 target/riscv/cpu.h            |  3 ++
 target/riscv/tcg/cpu_helper.c | 85
+++++++++++++++++++++++++++++++++++
 target/riscv/tcg/debug.c      |  5 +++
 target/riscv/tcg/tcg-cpu.c    | 14 ++++++

 4 files changed, 107 insertions(+)
target/riscv/cpu.h +3 -0
index 7b343d7a3e..82eb61a18c 100644
@@ -671,6 +671,9 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr
 address, int size,
  char *riscv_isa_string(RISCVCPU *cpu);
  int riscv_cpu_max_xlen(RISCVCPUClass *mcc);
  bool riscv_cpu_option_set(RISCVCPU *cpu, const char *optname);
+void riscv_cpu_enter_debug_mode(CPURISCVState *env, target_ulong pc,
+                                uint32_t cause);
+void riscv_cpu_leave_debug_mode(CPURISCVState *env);


 #ifndef CONFIG_USER_ONLY
 void riscv_cpu_do_interrupt(CPUState *cpu);

target/riscv/tcg/cpu_helper.c +0 -0
index 07d9222652..e6d948991c 100644
@@ -132,6 +132,91 @@ bool
 riscv_env_smode_dbltrp_enabled(CPURISCVState *env, bool virt)
  #endif
  }

target/riscv/tcg/cpu_helper.c +9 -0
+#ifndef CONFIG_USER_ONLY
+static bool riscv_sdext_enabled(CPURISCVState *env)
+{
+    return riscv_cpu_cfg(env)->ext_sdext;
+}
+#endif
+
+void riscv_cpu_enter_debug_mode(CPURISCVState *env, target_ulong pc,
+                                uint32_t cause)

target/riscv/tcg/cpu_helper.c bool riscv_env_smode_dbltrp_enabled(CPURISCVState *env, bool virt) · line 153

Quoted 10 lines
target/riscv/tcg/cpu_helper.c +10 -0
+{
+#ifndef CONFIG_USER_ONLY
+    if (!riscv_sdext_enabled(env)) {
+        return;
+    }
+
+    env->debug_mode = true;
+    env->dpc = pc & get_xepc_mask(env);
+    env->dcsr &= ~(DCSR_CAUSE_MASK | DCSR_PRV_MASK | DCSR_V);
+    env->dcsr |= ((target_ulong)(cause & 0x7)) << DCSR_CAUSE_SHIFT;

You shouldn't need the cast, as DCSR is only 32-bit

Quoted 15 lines

+    env->dcsr |= env->priv & DCSR_PRV_MASK;
+    if (env->virt_enabled && riscv_has_ext(env, RVH)) { +        env->dcsr |= DCSR_V;
+    }

target/riscv/tcg/cpu_helper.c +11 -0
+
+    if (env_archcpu(env)->cfg.ext_zicfilp) {
+        if (env->elp) {
+            env->dcsr |= DCSR_PELP;
+        } else {
+            env->dcsr &= ~DCSR_PELP;
+        }
+        env->elp = false;
+    }
+#endif
+}

target/riscv/tcg/cpu_helper.c bool riscv_env_smode_dbltrp_enabled(CPURISCVState *env, bool virt) · line 178

Quoted 10 lines
target/riscv/tcg/cpu_helper.c +10 -0
+
+void riscv_cpu_leave_debug_mode(CPURISCVState *env)
+{
+#ifndef CONFIG_USER_ONLY
+    if (!riscv_sdext_enabled(env)) {
+        return;
+    }
+
+    target_ulong new_priv = env->dcsr & DCSR_PRV_MASK;
+    bool new_virt = riscv_has_ext(env, RVH) && (env->dcsr & DCSR_V);

These should be at the top of the function

Alistair

Quoted 95 lines
target/riscv/tcg/cpu_helper.c +32 -0
+
+    if (new_priv > PRV_M) {
+        new_priv = PRV_M;
+    }
+    if (new_priv == PRV_M) {
+        new_virt = false;
+    }
+
+    if (new_priv == PRV_S && !riscv_has_ext(env, RVS)) {
+        new_priv = PRV_M;
+        new_virt = false;
+    } else if (new_priv == PRV_U && !riscv_has_ext(env, RVU)) {
+        new_priv = riscv_has_ext(env, RVS) ? PRV_S : PRV_M;
+        new_virt = false;
+    }
+
+    env->debug_mode = false;
+    riscv_cpu_set_mode(env, new_priv, new_virt);
+
+    if (env_archcpu(env)->cfg.ext_zicfilp) {
+        env->elp = cpu_get_fcfien(env) && (env->dcsr & DCSR_PELP);
+        env->dcsr &= ~DCSR_PELP;
+    }
+
+    if (new_priv != PRV_M) {
+        env->mstatus = set_field(env->mstatus, MSTATUS_MPRV, 0);
+    }
+
+    if (env_archcpu(env)->cfg.ext_smdbltrp && new_priv != PRV_M) {
+        env->mstatus = set_field(env->mstatus, MSTATUS_MDT, 0);
+    }
+    if (env_archcpu(env)->cfg.ext_ssdbltrp && (new_priv == PRV_U ||

new_virt)) {
+        env->mstatus = set_field(env->mstatus, MSTATUS_SDT, 0); +        if (new_virt && new_priv == PRV_U) {
+            env->vsstatus = set_field(env->vsstatus, MSTATUS_SDT, 0);
+        }
+    }

target/riscv/tcg/cpu_helper.c +4 -0
+#endif
+}
+
+

 /*
  * Returns the effective PMM field.
  *

target/riscv/tcg/debug.c +0 -0
index e4d719a1da..2537515f54 100644
@@ -973,6 +973,11 @@ void riscv_cpu_debug_excp_handler(CPUState *cs)
      RISCVCPU *cpu = RISCV_CPU(cs);
      CPURISCVState *env = &cpu->env;


+    /* Triggers must not match or fire while in Debug Mode. */ +    if (env->debug_mode) {
+        return;
+    }
+
     if (cs->watchpoint_hit) {
         if (cs->watchpoint_hit->flags & BP_CPU) { do_trigger_action(env, DBG_ACTION_BP);

target/riscv/tcg/tcg-cpu.c +13 -0
index b68160af83..959a1e89c8 100644
@@ -269,6 +269,19 @@ static vaddr riscv_pointer_wrap(CPUState *cs,
 int mmu_idx,
      }
      return extract64(result, 0, 64 - pm_len);
  }
+
+static void riscv_cpu_exec_enter(CPUState *cs)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    CPURISCVState *env = &cpu->env;
+
+    if (!cpu->cfg.ext_sdext || !env->debug_mode) {
+        return;
+    }
+    target_ulong pc = env->dpc;
+    riscv_cpu_leave_debug_mode(env);
+    env->pc = pc;
+}
  #endif


 const TCGCPUOps riscv_tcg_ops = {

target/riscv/tcg/tcg-cpu.c +1 -0
@@ -285,6 +298,7 @@ const TCGCPUOps riscv_tcg_ops = {
  #ifndef CONFIG_USER_ONLY
      .tlb_fill = riscv_cpu_tlb_fill,
      .pointer_wrap = riscv_pointer_wrap,
+    .cpu_exec_enter = riscv_cpu_exec_enter,
      .cpu_exec_interrupt = riscv_cpu_exec_interrupt,
      .cpu_exec_halt = riscv_cpu_has_work,
      .cpu_exec_reset = cpu_reset,
#7 lore raw

PATCH v9 3/7 target/riscv: add dret instruction

In reply to Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Add DRET decode/translate and a helper to leave Debug Mode and return to dpc. Executing DRET outside Debug Mode raises illegal instruction.

---
 target/riscv/helper.h                          |  1 +
 target/riscv/insn32.decode                     |  1 +
 .../tcg/insn_trans/trans_privileged.c.inc      | 18 ++++++++++++++++++
 target/riscv/tcg/op_helper.c                   | 16 ++++++++++++++++
 4 files changed, 36 insertions(+)
target/riscv/helper.h +1 -0
index 4fc2d3a155..b4c47d0738 100644
@@ -132,6 +132,7 @@ DEF_HELPER_6(csrrw_i128, tl, env, int, tl, tl, tl, tl)
 #ifndef CONFIG_USER_ONLY
 DEF_HELPER_1(sret, tl, env)
 DEF_HELPER_1(mret, tl, env)
+DEF_HELPER_1(dret, tl, env)
 DEF_HELPER_1(mnret, tl, env)
 DEF_HELPER_1(ctr_clear, void, env)
 DEF_HELPER_1(wfi, void, env)
target/riscv/insn32.decode +1 -0
index aa02dae3c9..46b00411ff 100644
@@ -118,6 +118,7 @@ sctrclr     000100000100     00000 000 00000 1110011
 uret        0000000    00010 00000 000 00000 1110011
 sret        0001000    00010 00000 000 00000 1110011
 mret        0011000    00010 00000 000 00000 1110011
+dret        0111101    10010 00000 000 00000 1110011
 wfi         0001000    00101 00000 000 00000 1110011
 sfence_vma  0001001    ..... ..... 000 00000 1110011 @sfence_vma
 
target/riscv/tcg/insn_trans/trans_privileged.c.inc +18 -0
index a8eaccef67..4e57901880 100644
@@ -125,6 +125,24 @@ static bool trans_mret(DisasContext *ctx, arg_mret *a)
 #endif
 }
 
+static bool trans_dret(DisasContext *ctx, arg_dret *a)
+{
+#ifndef CONFIG_USER_ONLY
+    if (!ctx->cfg_ptr->ext_sdext) {
+        return false;
+    }
+    decode_save_opc(ctx, 0);
+    translator_io_start(&ctx->base);
+    gen_update_pc(ctx, 0);
+    gen_helper_dret(cpu_pc, tcg_env);
+    exit_tb(ctx); /* no chaining */
+    ctx->base.is_jmp = DISAS_NORETURN;
+    return true;
+#else
+    return false;
+#endif
+}
+
 static bool trans_mnret(DisasContext *ctx, arg_mnret *a)
 {
 #ifndef CONFIG_USER_ONLY
target/riscv/tcg/op_helper.c +16 -0
index 3e94005d2b..54269efee1 100644
@@ -473,6 +473,22 @@ target_ulong helper_mret(CPURISCVState *env)
     return retpc;
 }
 
+target_ulong helper_dret(CPURISCVState *env)
+{
+    uintptr_t ra = GETPC();
+#ifdef CONFIG_USER_ONLY
+    riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
+    return 0;
+#else
+    if (!riscv_cpu_cfg(env)->ext_sdext || !env->debug_mode) {
+        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
+    }
+    target_ulong retpc = env->dpc & get_xepc_mask(env);
+    riscv_cpu_leave_debug_mode(env);
+    return retpc;
+#endif
+}
+
 target_ulong helper_mnret(CPURISCVState *env)
 {
     target_ulong retpc = env->mnepc;
Signature · 1 line
2.43.0
Reviewed-by on v9 3/7
#8 lore raw

Re: [PATCH v9 3/7] target/riscv: add dret instruction

Quoted 8 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Add DRET decode/translate and a helper to leave Debug Mode and return to dpc. Executing DRET outside Debug Mode raises illegal instruction.

Quoted 6 lines
Reviewed-by Daniel Henrique Barboza

<[email protected]>

Signed-off-by Daniel Henrique Barboza

<[email protected]>

Alistair

Quoted 93 lines

---
 target/riscv/helper.h                          |  1 + target/riscv/insn32.decode                     |  1 + .../tcg/insn_trans/trans_privileged.c.inc      | 18
++++++++++++++++++
 target/riscv/tcg/op_helper.c                   | 16 ++++++++++++++++

 4 files changed, 36 insertions(+)
target/riscv/helper.h +1 -0
index 4fc2d3a155..b4c47d0738 100644
@@ -132,6 +132,7 @@ DEF_HELPER_6(csrrw_i128, tl, env, int, tl, tl,
 tl, tl)
  #ifndef CONFIG_USER_ONLY
  DEF_HELPER_1(sret, tl, env)
  DEF_HELPER_1(mret, tl, env)
+DEF_HELPER_1(dret, tl, env)
  DEF_HELPER_1(mnret, tl, env)
  DEF_HELPER_1(ctr_clear, void, env)

 DEF_HELPER_1(wfi, void, env)

target/riscv/insn32.decode +0 -0
index aa02dae3c9..46b00411ff 100644
@@ -118,6 +118,7 @@ sctrclr     000100000100     00000 000 00000

1110011
 uret        0000000    00010 00000 000 00000 1110011 sret        0001000    00010 00000 000 00000 1110011 mret        0011000    00010 00000 000 00000 1110011 +dret        0111101    10010 00000 000 00000 1110011 wfi         0001000    00101 00000 000 00000 1110011 sfence_vma  0001001    ..... ..... 000 00000 1110011 @sfence_vma

target/riscv/tcg/insn_trans/trans_privileged.c.inc +0 -0
index a8eaccef67..4e57901880 100644
@@ -125,6 +125,24 @@ static bool trans_mret(DisasContext *ctx,
 arg_mret *a)
  #endif
  }

target/riscv/tcg/insn_trans/trans_privileged.c.inc +18 -0
+static bool trans_dret(DisasContext *ctx, arg_dret *a)
+{
+#ifndef CONFIG_USER_ONLY
+    if (!ctx->cfg_ptr->ext_sdext) {
+        return false;
+    }
+    decode_save_opc(ctx, 0);
+    translator_io_start(&ctx->base);
+    gen_update_pc(ctx, 0);
+    gen_helper_dret(cpu_pc, tcg_env);
+    exit_tb(ctx); /* no chaining */
+    ctx->base.is_jmp = DISAS_NORETURN;
+    return true;
+#else
+    return false;
+#endif
+}
+

 static bool trans_mnret(DisasContext *ctx, arg_mnret *a) {
 #ifndef CONFIG_USER_ONLY

target/riscv/tcg/op_helper.c +0 -0
index 3e94005d2b..54269efee1 100644
@@ -473,6 +473,22 @@ target_ulong helper_mret(CPURISCVState *env)
      return retpc;
  }

target/riscv/tcg/op_helper.c +16 -0
+target_ulong helper_dret(CPURISCVState *env)
+{
+    uintptr_t ra = GETPC();
+#ifdef CONFIG_USER_ONLY
+    riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
+    return 0;
+#else
+    if (!riscv_cpu_cfg(env)->ext_sdext || !env->debug_mode) {
+        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
+    }
+    target_ulong retpc = env->dpc & get_xepc_mask(env);
+    riscv_cpu_leave_debug_mode(env);
+    return retpc;
+#endif
+}
+

 target_ulong helper_mnret(CPURISCVState *env)
 {
     target_ulong retpc = env->mnepc;

Reviewed-by on v9 3/7
#9 lore raw

Re: [PATCH v9 3/7] target/riscv: add dret instruction

In reply to Daniel Henrique Barboza
Quoted 81 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Add DRET decode/translate and a helper to leave Debug Mode and return to dpc. Executing DRET outside Debug Mode raises illegal instruction.

---
  target/riscv/helper.h                          |  1 +
  target/riscv/insn32.decode                     |  1 +
  .../tcg/insn_trans/trans_privileged.c.inc      | 18 ++++++++++++++++++
  target/riscv/tcg/op_helper.c                   | 16 ++++++++++++++++
  4 files changed, 36 insertions(+)
target/riscv/helper.h +1 -0
index 4fc2d3a155..b4c47d0738 100644
@@ -132,6 +132,7 @@ DEF_HELPER_6(csrrw_i128, tl, env, int, tl, tl, tl, tl)
  #ifndef CONFIG_USER_ONLY
  DEF_HELPER_1(sret, tl, env)
  DEF_HELPER_1(mret, tl, env)
+DEF_HELPER_1(dret, tl, env)
  DEF_HELPER_1(mnret, tl, env)
  DEF_HELPER_1(ctr_clear, void, env)
  DEF_HELPER_1(wfi, void, env)
target/riscv/insn32.decode +1 -0
index aa02dae3c9..46b00411ff 100644
@@ -118,6 +118,7 @@ sctrclr     000100000100     00000 000 00000 1110011
  uret        0000000    00010 00000 000 00000 1110011
  sret        0001000    00010 00000 000 00000 1110011
  mret        0011000    00010 00000 000 00000 1110011
+dret        0111101    10010 00000 000 00000 1110011
  wfi         0001000    00101 00000 000 00000 1110011
  sfence_vma  0001001    ..... ..... 000 00000 1110011 @sfence_vma
 
target/riscv/tcg/insn_trans/trans_privileged.c.inc +18 -0
index a8eaccef67..4e57901880 100644
@@ -125,6 +125,24 @@ static bool trans_mret(DisasContext *ctx, arg_mret *a)
  #endif
  }
 
+static bool trans_dret(DisasContext *ctx, arg_dret *a)
+{
+#ifndef CONFIG_USER_ONLY
+    if (!ctx->cfg_ptr->ext_sdext) {
+        return false;
+    }
+    decode_save_opc(ctx, 0);
+    translator_io_start(&ctx->base);
+    gen_update_pc(ctx, 0);
+    gen_helper_dret(cpu_pc, tcg_env);
+    exit_tb(ctx); /* no chaining */
+    ctx->base.is_jmp = DISAS_NORETURN;
+    return true;
+#else
+    return false;
+#endif
+}
+
  static bool trans_mnret(DisasContext *ctx, arg_mnret *a)
  {
  #ifndef CONFIG_USER_ONLY
target/riscv/tcg/op_helper.c +1 -0
index 3e94005d2b..54269efee1 100644
@@ -473,6 +473,22 @@ target_ulong helper_mret(CPURISCVState *env)
      return retpc;
  }
 
+target_ulong helper_dret(CPURISCVState *env)

target/riscv/tcg/op_helper.c target_ulong helper_mret(CPURISCVState *env) · line 486

Quoted 10 lines
target/riscv/tcg/op_helper.c +10 -0
+{
+    uintptr_t ra = GETPC();
+#ifdef CONFIG_USER_ONLY
+    riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
+    return 0;
+#else
+    if (!riscv_cpu_cfg(env)->ext_sdext || !env->debug_mode) {
+        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
+    }
+    target_ulong retpc = env->dpc & get_xepc_mask(env);

Seems it should be at the top of the function as below:
+#else
+    target_ulong retpc = env->dpc & get_xepc_mask(env);

Otherwise,

Thanks,
Nutty

Quoted 8 lines
target/riscv/tcg/op_helper.c +5 -0
+    riscv_cpu_leave_debug_mode(env);
+    return retpc;
+#endif
+}
+
  target_ulong helper_mnret(CPURISCVState *env)
  {
      target_ulong retpc = env->mnepc;
#10 lore raw

PATCH v9 4/7 target/riscv: add sdext enter Debug Mode on ebreak

In reply to Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Route EBREAK via helper_sdext_ebreak. If Sdext is enabled and the matching dcsr.ebreak* bit is set, enter Debug Mode with cause=ebreak and stop with EXCP_DEBUG. Otherwise keep the normal breakpoint trap.

---
 target/riscv/helper.h                         |  1 +
 .../tcg/insn_trans/trans_privileged.c.inc     |  6 ++++
 target/riscv/tcg/op_helper.c                  | 34 +++++++++++++++++++
 3 files changed, 41 insertions(+)
target/riscv/helper.h +1 -0
index b4c47d0738..0d9ae15e3e 100644
@@ -142,6 +142,7 @@ DEF_HELPER_1(tlb_flush_all, void, env)
 DEF_HELPER_4(ctr_add_entry, void, env, tl, tl, tl)
 /* Native Debug */
 DEF_HELPER_1(itrigger_match, void, env)
+DEF_HELPER_2(sdext_ebreak, void, env, tl)
 #endif
 
 /* Hypervisor functions */
target/riscv/tcg/insn_trans/trans_privileged.c.inc +6 -0
index 4e57901880..42bf2ffb53 100644
@@ -68,9 +68,15 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
     if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
         generate_exception(ctx, RISCV_EXCP_SEMIHOST);
     } else {
+#ifndef CONFIG_USER_ONLY
+        gen_update_pc(ctx, 0);
+        gen_helper_sdext_ebreak(tcg_env, tcg_constant_tl(ebreak_addr));
+        ctx->base.is_jmp = DISAS_NORETURN;
+#else
         tcg_gen_st_i64(tcg_constant_i64(ebreak_addr), tcg_env,
                       offsetof(CPURISCVState, badaddr));
         generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
+#endif
     }
     return true;
 }
target/riscv/tcg/op_helper.c +34 -0
index 54269efee1..5db4f6aacd 100644
@@ -489,6 +489,40 @@ target_ulong helper_dret(CPURISCVState *env)
 #endif
 }
 
+void helper_sdext_ebreak(CPURISCVState *env, target_ulong pc)
+{
+    CPUState *cs = env_cpu(env);
+    bool enter_debug = false;
+
+    if (riscv_cpu_cfg(env)->ext_sdext && !env->debug_mode) {
+        if (env->virt_enabled) {
+            if (env->priv == PRV_S) {
+                enter_debug = env->dcsr & DCSR_EBREAKVS;
+            } else if (env->priv == PRV_U) {
+                enter_debug = env->dcsr & DCSR_EBREAKVU;
+            }
+        } else {
+            if (env->priv == PRV_M) {
+                enter_debug = env->dcsr & DCSR_EBREAKM;
+            } else if (env->priv == PRV_S) {
+                enter_debug = env->dcsr & DCSR_EBREAKS;
+            } else if (env->priv == PRV_U) {
+                enter_debug = env->dcsr & DCSR_EBREAKU;
+            }
+        }
+    }
+
+    env->badaddr = pc;
+
+    if (enter_debug) {
+        riscv_cpu_enter_debug_mode(env, pc, DCSR_CAUSE_EBREAK);
+        cs->exception_index = EXCP_DEBUG;
+        cpu_loop_exit_restore(cs, GETPC());
+    }
+
+    riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, GETPC());
+}
+
 target_ulong helper_mnret(CPURISCVState *env)
 {
     target_ulong retpc = env->mnepc;
Signature · 1 line
2.43.0
Reviewed-by on v9 4/7
#11 lore raw

Re: [PATCH v9 4/7] target/riscv: add sdext enter Debug Mode on ebreak

Quoted 9 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Route EBREAK via helper_sdext_ebreak. If Sdext is enabled and the matching dcsr.ebreak* bit is set, enter Debug Mode with cause=ebreak and stop with EXCP_DEBUG. Otherwise keep the normal breakpoint trap.

Quoted 6 lines
Reviewed-by Daniel Henrique Barboza

<[email protected]>

Signed-off-by Daniel Henrique Barboza

<[email protected]>

Alistair

Quoted 89 lines

---
 target/riscv/helper.h                         |  1 + .../tcg/insn_trans/trans_privileged.c.inc     |  6 ++++ target/riscv/tcg/op_helper.c                  | 34
+++++++++++++++++++

 3 files changed, 41 insertions(+)
target/riscv/helper.h +1 -0
index b4c47d0738..0d9ae15e3e 100644
@@ -142,6 +142,7 @@ DEF_HELPER_1(tlb_flush_all, void, env)
  DEF_HELPER_4(ctr_add_entry, void, env, tl, tl, tl)
  /* Native Debug */
  DEF_HELPER_1(itrigger_match, void, env)
+DEF_HELPER_2(sdext_ebreak, void, env, tl)
  #endif


 /* Hypervisor functions */

target/riscv/tcg/insn_trans/trans_privileged.c.inc +0 -0
index 4e57901880..42bf2ffb53 100644
@@ -68,9 +68,15 @@ static bool trans_ebreak(DisasContext *ctx,
 arg_ebreak *a)

     if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
         generate_exception(ctx, RISCV_EXCP_SEMIHOST); } else {
+#ifndef CONFIG_USER_ONLY
+        gen_update_pc(ctx, 0);
+        gen_helper_sdext_ebreak(tcg_env, tcg_constant_tl(ebreak_addr));
+        ctx->base.is_jmp = DISAS_NORETURN;
+#else
         tcg_gen_st_i64(tcg_constant_i64(ebreak_addr), tcg_env, offsetof(CPURISCVState, badaddr)); generate_exception(ctx, RISCV_EXCP_BREAKPOINT); +#endif
     }
     return true;
 }

target/riscv/tcg/op_helper.c +0 -0
index 54269efee1..5db4f6aacd 100644
@@ -489,6 +489,40 @@ target_ulong helper_dret(CPURISCVState *env)
  #endif
  }

target/riscv/tcg/op_helper.c +34 -0
+void helper_sdext_ebreak(CPURISCVState *env, target_ulong pc)
+{
+    CPUState *cs = env_cpu(env);
+    bool enter_debug = false;
+
+    if (riscv_cpu_cfg(env)->ext_sdext && !env->debug_mode) {
+        if (env->virt_enabled) {
+            if (env->priv == PRV_S) {
+                enter_debug = env->dcsr & DCSR_EBREAKVS;
+            } else if (env->priv == PRV_U) {
+                enter_debug = env->dcsr & DCSR_EBREAKVU;
+            }
+        } else {
+            if (env->priv == PRV_M) {
+                enter_debug = env->dcsr & DCSR_EBREAKM;
+            } else if (env->priv == PRV_S) {
+                enter_debug = env->dcsr & DCSR_EBREAKS;
+            } else if (env->priv == PRV_U) {
+                enter_debug = env->dcsr & DCSR_EBREAKU;
+            }
+        }
+    }
+
+    env->badaddr = pc;
+
+    if (enter_debug) {
+        riscv_cpu_enter_debug_mode(env, pc, DCSR_CAUSE_EBREAK);
+        cs->exception_index = EXCP_DEBUG;
+        cpu_loop_exit_restore(cs, GETPC());
+    }
+
+    riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, GETPC());
+}
+

 target_ulong helper_mnret(CPURISCVState *env)
 {
     target_ulong retpc = env->mnepc;

Reviewed-by on v9 4/7
#12 lore raw

Re: [PATCH v9 4/7] target/riscv: add sdext enter Debug Mode on ebreak

In reply to Daniel Henrique Barboza
Quoted 5 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Quoted 8 lines

Route EBREAK via helper_sdext_ebreak. If Sdext is enabled and the matching dcsr.ebreak* bit is set, enter Debug Mode with cause=ebreak and stop with EXCP_DEBUG. Otherwise keep the normal breakpoint trap.

Thanks,
Nutty

Quoted 83 lines
---
  target/riscv/helper.h                         |  1 +
  .../tcg/insn_trans/trans_privileged.c.inc     |  6 ++++
  target/riscv/tcg/op_helper.c                  | 34 +++++++++++++++++++
  3 files changed, 41 insertions(+)
target/riscv/helper.h +1 -0
index b4c47d0738..0d9ae15e3e 100644
@@ -142,6 +142,7 @@ DEF_HELPER_1(tlb_flush_all, void, env)
  DEF_HELPER_4(ctr_add_entry, void, env, tl, tl, tl)
  /* Native Debug */
  DEF_HELPER_1(itrigger_match, void, env)
+DEF_HELPER_2(sdext_ebreak, void, env, tl)
  #endif
 
  /* Hypervisor functions */
target/riscv/tcg/insn_trans/trans_privileged.c.inc +6 -0
index 4e57901880..42bf2ffb53 100644
@@ -68,9 +68,15 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
      if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
          generate_exception(ctx, RISCV_EXCP_SEMIHOST);
      } else {
+#ifndef CONFIG_USER_ONLY
+        gen_update_pc(ctx, 0);
+        gen_helper_sdext_ebreak(tcg_env, tcg_constant_tl(ebreak_addr));
+        ctx->base.is_jmp = DISAS_NORETURN;
+#else
          tcg_gen_st_i64(tcg_constant_i64(ebreak_addr), tcg_env,
                        offsetof(CPURISCVState, badaddr));
          generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
+#endif
      }
      return true;
  }
target/riscv/tcg/op_helper.c +34 -0
index 54269efee1..5db4f6aacd 100644
@@ -489,6 +489,40 @@ target_ulong helper_dret(CPURISCVState *env)
  #endif
  }
 
+void helper_sdext_ebreak(CPURISCVState *env, target_ulong pc)
+{
+    CPUState *cs = env_cpu(env);
+    bool enter_debug = false;
+
+    if (riscv_cpu_cfg(env)->ext_sdext && !env->debug_mode) {
+        if (env->virt_enabled) {
+            if (env->priv == PRV_S) {
+                enter_debug = env->dcsr & DCSR_EBREAKVS;
+            } else if (env->priv == PRV_U) {
+                enter_debug = env->dcsr & DCSR_EBREAKVU;
+            }
+        } else {
+            if (env->priv == PRV_M) {
+                enter_debug = env->dcsr & DCSR_EBREAKM;
+            } else if (env->priv == PRV_S) {
+                enter_debug = env->dcsr & DCSR_EBREAKS;
+            } else if (env->priv == PRV_U) {
+                enter_debug = env->dcsr & DCSR_EBREAKU;
+            }
+        }
+    }
+
+    env->badaddr = pc;
+
+    if (enter_debug) {
+        riscv_cpu_enter_debug_mode(env, pc, DCSR_CAUSE_EBREAK);
+        cs->exception_index = EXCP_DEBUG;
+        cpu_loop_exit_restore(cs, GETPC());
+    }
+
+    riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, GETPC());
+}
+
  target_ulong helper_mnret(CPURISCVState *env)
  {
      target_ulong retpc = env->mnepc;
#13 lore raw

PATCH v9 5/7 target/riscv: add sdext single-step support

In reply to Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Use a TB flag when dcsr.step is set (and we are not in Debug Mode). When the flag is on, build 1-insn TBs and do not chain to the next TB. Add a TB-exit helper that enters Debug Mode with cause=step and sets dpc to the next pc, then stops with EXCP_DEBUG.

If dcsr.stepie is 0, do not take interrupts while stepping. Treat WFI as a nop so the hart does not sleep during a step.

PS: This patch references Max Chou's handling of ext_tb_flags.
https://lore.kernel.org/qemu-devel/[email protected]/

---
 include/exec/translation-block.h |  4 ++--
 target/riscv/cpu.h               |  2 ++
 target/riscv/helper.h            |  1 +
 target/riscv/tcg/cpu_helper.c    |  6 ++++++
 target/riscv/tcg/op_helper.c     | 20 ++++++++++++++++++++
 target/riscv/tcg/tcg-cpu.c       |  5 +++++
 target/riscv/tcg/translate.c     | 15 +++++++++++++--
 7 files changed, 49 insertions(+), 4 deletions(-)
include/exec/translation-block.h +2 -2
index 40cc699031..ee15608c89 100644
@@ -64,8 +64,8 @@ struct TranslationBlock {
      * x86: the original user, the Code Segment virtual base,
      * arm: an extension of tb->flags,
      * s390x: instruction data for EXECUTE,
-     * sparc: the next pc of the instruction queue (for delay slots).
-     * riscv: an extension of tb->flags,
+     * sparc: the next pc of the instruction queue (for delay slots),
+     * riscv: an extension of tb->flags.
      */
     uint64_t cs_base;
 
target/riscv/cpu.h +2 -0
index 82eb61a18c..db49ad7682 100644
@@ -755,6 +755,8 @@ FIELD(TB_FLAGS, PM_SIGNEXTEND, 31, 1)
 FIELD(EXT_TB_FLAGS, MISA_EXT, 0, 32)
 FIELD(EXT_TB_FLAGS, ALTFMT, 32, 1)
 FIELD(EXT_TB_FLAGS, BIG_ENDIAN, 33, 1)
+/* sdext single-step needs a TB flag to build 1-insn TBs */
+FIELD(EXT_TB_FLAGS, SDEXT_STEP, 34, 1)
 
 #ifdef TARGET_RISCV32
 #define riscv_cpu_mxl(env)  ((void)(env), MXL_RV32)
target/riscv/helper.h +1 -0
index 0d9ae15e3e..5193440e1c 100644
@@ -142,6 +142,7 @@ DEF_HELPER_1(tlb_flush_all, void, env)
 DEF_HELPER_4(ctr_add_entry, void, env, tl, tl, tl)
 /* Native Debug */
 DEF_HELPER_1(itrigger_match, void, env)
+DEF_HELPER_1(sdext_step, void, env)
 DEF_HELPER_2(sdext_ebreak, void, env, tl)
 #endif
 
target/riscv/tcg/cpu_helper.c +6 -0
index e6d948991c..a7344b1be2 100644
@@ -500,6 +500,12 @@ bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
     if (interrupt_request & mask) {
         RISCVCPU *cpu = RISCV_CPU(cs);
         CPURISCVState *env = &cpu->env;
+
+        if (cpu->cfg.ext_sdext && !env->debug_mode &&
+            (env->dcsr & DCSR_STEP) && !(env->dcsr & DCSR_STEPIE)) {
+            return false;
+        }
+
         int interruptno = riscv_cpu_local_irq_pending(env);
         if (interruptno >= 0) {
             cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno;
target/riscv/tcg/op_helper.c +20 -0
index 5db4f6aacd..489aac28e7 100644
@@ -489,6 +489,22 @@ target_ulong helper_dret(CPURISCVState *env)
 #endif
 }
 
+void helper_sdext_step(CPURISCVState *env)
+{
+#ifndef CONFIG_USER_ONLY
+    CPUState *cs = env_cpu(env);
+
+    if (!riscv_cpu_cfg(env)->ext_sdext || env->debug_mode ||
+        !(env->dcsr & DCSR_STEP)) {
+        return;
+    }
+
+    riscv_cpu_enter_debug_mode(env, env->pc, DCSR_CAUSE_STEP);
+    cs->exception_index = EXCP_DEBUG;
+    cpu_loop_exit_restore(cs, GETPC());
+#endif
+}
+
 void helper_sdext_ebreak(CPURISCVState *env, target_ulong pc)
 {
     CPUState *cs = env_cpu(env);
@@ -621,6 +637,10 @@ void helper_wfi(CPURISCVState *env)
                (prv_u || (prv_s && get_field(env->hstatus, HSTATUS_VTW)))) {
         riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
     } else {
+        if (riscv_cpu_cfg(env)->ext_sdext && !env->debug_mode &&
+            (env->dcsr & DCSR_STEP)) {
+            return;
+        }
         cs->halted = 1;
         cs->exception_index = EXCP_HLT;
         cpu_loop_exit(cs);
target/riscv/tcg/tcg-cpu.c +5 -0
index 959a1e89c8..16d26cee5c 100644
@@ -196,6 +196,11 @@ static TCGTBCPUState riscv_get_tb_cpu_state(CPUState *cs)
     ext_flags = FIELD_DP64(ext_flags, EXT_TB_FLAGS, MISA_EXT, env->misa_ext);
     ext_flags = FIELD_DP64(ext_flags, EXT_TB_FLAGS, BIG_ENDIAN,
                            mo_endian_env(env) == MO_BE);
+#ifndef CONFIG_USER_ONLY
+    if (cpu->cfg.ext_sdext && !env->debug_mode && (env->dcsr & DCSR_STEP)) {
+        ext_flags = FIELD_DP64(ext_flags, EXT_TB_FLAGS, SDEXT_STEP, 1);
+    }
+#endif
 
     return (TCGTBCPUState){
         .pc = env->xl == MXL_RV32 ? env->pc & UINT32_MAX : env->pc,
target/riscv/tcg/translate.c +13 -2
index 9684dbe752..1a38d6aeb5 100644
@@ -112,6 +112,8 @@ typedef struct DisasContext {
     bool ztso;
     /* Use icount trigger for native debug */
     bool itrigger;
+    /* Enter Debug Mode after next instruction (sdext single-step). */
+    bool sdext_step;
     /* FRM is known to contain a valid value. */
     bool frm_valid;
     bool insn_start_updated;
@@ -286,6 +288,9 @@ static void lookup_and_goto_ptr(DisasContext *ctx)
     if (ctx->itrigger) {
         gen_helper_itrigger_match(tcg_env);
     }
+    if (ctx->sdext_step) {
+        gen_helper_sdext_step(tcg_env);
+    }
 #endif
     tcg_gen_lookup_and_goto_ptr();
 }
@@ -296,6 +301,9 @@ static void exit_tb(DisasContext *ctx)
     if (ctx->itrigger) {
         gen_helper_itrigger_match(tcg_env);
     }
+    if (ctx->sdext_step) {
+        gen_helper_sdext_step(tcg_env);
+    }
 #endif
     tcg_gen_exit_tb(NULL, 0);
 }
@@ -309,7 +317,8 @@ static void gen_goto_tb(DisasContext *ctx, unsigned tb_slot_idx,
       * Under itrigger, instruction executes one by one like singlestep,
       * direct block chain benefits will be small.
       */
-    if (translator_use_goto_tb(&ctx->base, dest) && !ctx->itrigger) {
+    if (translator_use_goto_tb(&ctx->base, dest) &&
+        !ctx->itrigger && !ctx->sdext_step) {
         /*
          * For pcrel, the pc must always be up-to-date on entry to
          * the linked TB, so that it can use simple additions for all
@@ -1350,6 +1359,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
     ctx->bcfi_enabled = FIELD_EX32(tb_flags, TB_FLAGS, BCFI_ENABLED);
     ctx->fcfi_lp_expected = FIELD_EX32(tb_flags, TB_FLAGS, FCFI_LP_EXPECTED);
     ctx->fcfi_enabled = FIELD_EX32(tb_flags, TB_FLAGS, FCFI_ENABLED);
+    ctx->sdext_step = FIELD_EX64(ext_tb_flags, EXT_TB_FLAGS, SDEXT_STEP);
     ctx->zero = tcg_constant_tl(0);
     ctx->virt_inst_excp = false;
     ctx->decoders = cpu->decoders;
@@ -1402,7 +1412,8 @@ static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
 
     /* Only the first insn within a TB is allowed to cross a page boundary. */
     if (ctx->base.is_jmp == DISAS_NEXT) {
-        if (ctx->itrigger || !translator_is_same_page(&ctx->base, ctx->base.pc_next)) {
+        if (ctx->itrigger || ctx->sdext_step ||
+            !translator_is_same_page(&ctx->base, ctx->base.pc_next)) {
             ctx->base.is_jmp = DISAS_TOO_MANY;
         } else {
             unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK;
Signature · 1 line
2.43.0
Reviewed-by on v9 5/7
#14 lore raw

Re: [PATCH v9 5/7] target/riscv: add sdext single-step support

Quoted 17 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Use a TB flag when dcsr.step is set (and we are not in Debug Mode). When the flag is on, build 1-insn TBs and do not chain to the next TB.
Add a TB-exit helper that enters Debug Mode with cause=step and sets dpc to the next pc, then stops with EXCP_DEBUG.

If dcsr.stepie is 0, do not take interrupts while stepping. Treat WFI as a nop so the hart does not sleep during a step.

PS: This patch references Max Chou's handling of ext_tb_flags.
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 6 lines
Reviewed-by Daniel Henrique Barboza

<[email protected]>

Signed-off-by Daniel Henrique Barboza

<[email protected]>

Alistair

Quoted 211 lines

---
 include/exec/translation-block.h |  4 ++-- target/riscv/cpu.h               |  2 ++ target/riscv/helper.h            |  1 + target/riscv/tcg/cpu_helper.c    |  6 ++++++ target/riscv/tcg/op_helper.c     | 20 ++++++++++++++++++++ target/riscv/tcg/tcg-cpu.c       |  5 +++++ target/riscv/tcg/translate.c     | 15 +++++++++++++--

 7 files changed, 49 insertions(+), 4 deletions(-)
include/exec/translation-block.h +0 -1
index 40cc699031..ee15608c89 100644
@@ -64,8 +64,8 @@ struct TranslationBlock {
       * x86: the original user, the Code Segment virtual base,
       * arm: an extension of tb->flags,
       * s390x: instruction data for EXECUTE,
-     * sparc: the next pc of the instruction queue (for delay

slots).
-     * riscv: an extension of tb->flags,
+     * sparc: the next pc of the instruction queue (for delay slots),
+     * riscv: an extension of tb->flags.
      */
     uint64_t cs_base;

target/riscv/cpu.h +2 -0
index 82eb61a18c..db49ad7682 100644
@@ -755,6 +755,8 @@ FIELD(TB_FLAGS, PM_SIGNEXTEND, 31, 1)
  FIELD(EXT_TB_FLAGS, MISA_EXT, 0, 32)
  FIELD(EXT_TB_FLAGS, ALTFMT, 32, 1)
  FIELD(EXT_TB_FLAGS, BIG_ENDIAN, 33, 1)
+/* sdext single-step needs a TB flag to build 1-insn TBs */
+FIELD(EXT_TB_FLAGS, SDEXT_STEP, 34, 1)


 #ifdef TARGET_RISCV32
 #define riscv_cpu_mxl(env)  ((void)(env), MXL_RV32)

target/riscv/helper.h +1 -0
index 0d9ae15e3e..5193440e1c 100644
@@ -142,6 +142,7 @@ DEF_HELPER_1(tlb_flush_all, void, env)
  DEF_HELPER_4(ctr_add_entry, void, env, tl, tl, tl)
  /* Native Debug */
  DEF_HELPER_1(itrigger_match, void, env)
+DEF_HELPER_1(sdext_step, void, env)
  DEF_HELPER_2(sdext_ebreak, void, env, tl)
  #endif

target/riscv/tcg/cpu_helper.c +6 -0
index e6d948991c..a7344b1be2 100644
@@ -500,6 +500,12 @@ bool riscv_cpu_exec_interrupt(CPUState *cs, int
 interrupt_request)
      if (interrupt_request & mask) {
          RISCVCPU *cpu = RISCV_CPU(cs);
          CPURISCVState *env = &cpu->env;
+
+        if (cpu->cfg.ext_sdext && !env->debug_mode &&
+            (env->dcsr & DCSR_STEP) && !(env->dcsr & DCSR_STEPIE)) {
+            return false;
+        }
+
          int interruptno = riscv_cpu_local_irq_pending(env);
          if (interruptno >= 0) {

             cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno;

target/riscv/tcg/op_helper.c +0 -0
index 5db4f6aacd..489aac28e7 100644
@@ -489,6 +489,22 @@ target_ulong helper_dret(CPURISCVState *env)
  #endif
  }

target/riscv/tcg/op_helper.c +16 -0
+void helper_sdext_step(CPURISCVState *env)
+{
+#ifndef CONFIG_USER_ONLY
+    CPUState *cs = env_cpu(env);
+
+    if (!riscv_cpu_cfg(env)->ext_sdext || env->debug_mode ||
+        !(env->dcsr & DCSR_STEP)) {
+        return;
+    }
+
+    riscv_cpu_enter_debug_mode(env, env->pc, DCSR_CAUSE_STEP);
+    cs->exception_index = EXCP_DEBUG;
+    cpu_loop_exit_restore(cs, GETPC());
+#endif
+}
+

 void helper_sdext_ebreak(CPURISCVState *env, target_ulong pc) {
     CPUState *cs = env_cpu(env);

target/riscv/tcg/op_helper.c +4 -0
@@ -621,6 +637,10 @@ void helper_wfi(CPURISCVState *env)
                 (prv_u || (prv_s && get_field(env->hstatus,
 HSTATUS_VTW)))) {
          riscv_raise_exception(env,
 RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
      } else {
+        if (riscv_cpu_cfg(env)->ext_sdext && !env->debug_mode &&
+            (env->dcsr & DCSR_STEP)) {
+            return;
+        }
          cs->halted = 1;

         cs->exception_index = EXCP_HLT;
         cpu_loop_exit(cs);

target/riscv/tcg/tcg-cpu.c +0 -0
index 959a1e89c8..16d26cee5c 100644
@@ -196,6 +196,11 @@ static TCGTBCPUState
 riscv_get_tb_cpu_state(CPUState *cs)

     ext_flags = FIELD_DP64(ext_flags, EXT_TB_FLAGS, MISA_EXT, env-

Quoted 1 line

misa_ext);

     ext_flags = FIELD_DP64(ext_flags, EXT_TB_FLAGS, BIG_ENDIAN, mo_endian_env(env) == MO_BE); +#ifndef CONFIG_USER_ONLY
+    if (cpu->cfg.ext_sdext && !env->debug_mode && (env->dcsr & DCSR_STEP)) {
+        ext_flags = FIELD_DP64(ext_flags, EXT_TB_FLAGS, SDEXT_STEP, 1);
+    }
+#endif

     return (TCGTBCPUState){
         .pc = env->xl == MXL_RV32 ? env->pc & UINT32_MAX : env->pc,

target/riscv/tcg/translate.c +2 -0
index 9684dbe752..1a38d6aeb5 100644
@@ -112,6 +112,8 @@ typedef struct DisasContext {
      bool ztso;
      /* Use icount trigger for native debug */
      bool itrigger;
+    /* Enter Debug Mode after next instruction (sdext single-step).
 */
+    bool sdext_step;
      /* FRM is known to contain a valid value. */
      bool frm_valid;

     bool insn_start_updated;

target/riscv/tcg/translate.c +3 -0
@@ -286,6 +288,9 @@ static void lookup_and_goto_ptr(DisasContext
 *ctx)
      if (ctx->itrigger) {
          gen_helper_itrigger_match(tcg_env);
      }
+    if (ctx->sdext_step) {
+        gen_helper_sdext_step(tcg_env);
+    }
  #endif
      tcg_gen_lookup_and_goto_ptr();

 }

target/riscv/tcg/translate.c +5 -1
@@ -296,6 +301,9 @@ static void exit_tb(DisasContext *ctx)
      if (ctx->itrigger) {
          gen_helper_itrigger_match(tcg_env);
      }
+    if (ctx->sdext_step) {
+        gen_helper_sdext_step(tcg_env);
+    }
  #endif
      tcg_gen_exit_tb(NULL, 0);
  }
@@ -309,7 +317,8 @@ static void gen_goto_tb(DisasContext *ctx,
 unsigned tb_slot_idx,
        * Under itrigger, instruction executes one by one like
 singlestep,
        * direct block chain benefits will be small.
        */
-    if (translator_use_goto_tb(&ctx->base, dest) && !ctx->itrigger)
 {
+    if (translator_use_goto_tb(&ctx->base, dest) &&
+        !ctx->itrigger && !ctx->sdext_step) {

         /*
          * For pcrel, the pc must always be up-to-date on entry to
          * the linked TB, so that it can use simple additions for all

target/riscv/tcg/translate.c +0 -0
@@ -1350,6 +1359,7 @@ static void
 riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
      ctx->bcfi_enabled = FIELD_EX32(tb_flags, TB_FLAGS,
 BCFI_ENABLED);
      ctx->fcfi_lp_expected = FIELD_EX32(tb_flags, TB_FLAGS,
 FCFI_LP_EXPECTED);
      ctx->fcfi_enabled = FIELD_EX32(tb_flags, TB_FLAGS,

FCFI_ENABLED);
+    ctx->sdext_step = FIELD_EX64(ext_tb_flags, EXT_TB_FLAGS, SDEXT_STEP);
     ctx->zero = tcg_constant_tl(0); ctx->virt_inst_excp = false;
     ctx->decoders = cpu->decoders;

target/riscv/tcg/translate.c +0 -0
@@ -1402,7 +1412,8 @@ static void
 riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)


     /* Only the first insn within a TB is allowed to cross a page boundary. */
     if (ctx->base.is_jmp == DISAS_NEXT) {
-        if (ctx->itrigger || !translator_is_same_page(&ctx->base, ctx->base.pc_next)) {
+        if (ctx->itrigger || ctx->sdext_step ||
+            !translator_is_same_page(&ctx->base, ctx->base.pc_next)) {
             ctx->base.is_jmp = DISAS_TOO_MANY;
         } else {
             unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK;

Reviewed-by on v9 5/7
#15 lore raw

Re: [PATCH v9 5/7] target/riscv: add sdext single-step support

In reply to Daniel Henrique Barboza
Quoted 13 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Use a TB flag when dcsr.step is set (and we are not in Debug Mode). When the flag is on, build 1-insn TBs and do not chain to the next TB. Add a TB-exit helper that enters Debug Mode with cause=step and sets dpc to the next pc, then stops with EXCP_DEBUG.

If dcsr.stepie is 0, do not take interrupts while stepping. Treat WFI as a nop so the hart does not sleep during a step.

Quoted 7 lines

PS: This patch references Max Chou's handling of ext_tb_flags.
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks,
Nutty

Quoted 182 lines
---
  include/exec/translation-block.h |  4 ++--
  target/riscv/cpu.h               |  2 ++
  target/riscv/helper.h            |  1 +
  target/riscv/tcg/cpu_helper.c    |  6 ++++++
  target/riscv/tcg/op_helper.c     | 20 ++++++++++++++++++++
  target/riscv/tcg/tcg-cpu.c       |  5 +++++
  target/riscv/tcg/translate.c     | 15 +++++++++++++--
  7 files changed, 49 insertions(+), 4 deletions(-)
include/exec/translation-block.h +2 -2
index 40cc699031..ee15608c89 100644
@@ -64,8 +64,8 @@ struct TranslationBlock {
       * x86: the original user, the Code Segment virtual base,
       * arm: an extension of tb->flags,
       * s390x: instruction data for EXECUTE,
-     * sparc: the next pc of the instruction queue (for delay slots).
-     * riscv: an extension of tb->flags,
+     * sparc: the next pc of the instruction queue (for delay slots),
+     * riscv: an extension of tb->flags.
       */
      uint64_t cs_base;
 
target/riscv/cpu.h +2 -0
index 82eb61a18c..db49ad7682 100644
@@ -755,6 +755,8 @@ FIELD(TB_FLAGS, PM_SIGNEXTEND, 31, 1)
  FIELD(EXT_TB_FLAGS, MISA_EXT, 0, 32)
  FIELD(EXT_TB_FLAGS, ALTFMT, 32, 1)
  FIELD(EXT_TB_FLAGS, BIG_ENDIAN, 33, 1)
+/* sdext single-step needs a TB flag to build 1-insn TBs */
+FIELD(EXT_TB_FLAGS, SDEXT_STEP, 34, 1)
 
  #ifdef TARGET_RISCV32
  #define riscv_cpu_mxl(env)  ((void)(env), MXL_RV32)
target/riscv/helper.h +1 -0
index 0d9ae15e3e..5193440e1c 100644
@@ -142,6 +142,7 @@ DEF_HELPER_1(tlb_flush_all, void, env)
  DEF_HELPER_4(ctr_add_entry, void, env, tl, tl, tl)
  /* Native Debug */
  DEF_HELPER_1(itrigger_match, void, env)
+DEF_HELPER_1(sdext_step, void, env)
  DEF_HELPER_2(sdext_ebreak, void, env, tl)
  #endif
 
target/riscv/tcg/cpu_helper.c +6 -0
index e6d948991c..a7344b1be2 100644
@@ -500,6 +500,12 @@ bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
      if (interrupt_request & mask) {
          RISCVCPU *cpu = RISCV_CPU(cs);
          CPURISCVState *env = &cpu->env;
+
+        if (cpu->cfg.ext_sdext && !env->debug_mode &&
+            (env->dcsr & DCSR_STEP) && !(env->dcsr & DCSR_STEPIE)) {
+            return false;
+        }
+
          int interruptno = riscv_cpu_local_irq_pending(env);
          if (interruptno >= 0) {
              cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno;
target/riscv/tcg/op_helper.c +20 -0
index 5db4f6aacd..489aac28e7 100644
@@ -489,6 +489,22 @@ target_ulong helper_dret(CPURISCVState *env)
  #endif
  }
 
+void helper_sdext_step(CPURISCVState *env)
+{
+#ifndef CONFIG_USER_ONLY
+    CPUState *cs = env_cpu(env);
+
+    if (!riscv_cpu_cfg(env)->ext_sdext || env->debug_mode ||
+        !(env->dcsr & DCSR_STEP)) {
+        return;
+    }
+
+    riscv_cpu_enter_debug_mode(env, env->pc, DCSR_CAUSE_STEP);
+    cs->exception_index = EXCP_DEBUG;
+    cpu_loop_exit_restore(cs, GETPC());
+#endif
+}
+
  void helper_sdext_ebreak(CPURISCVState *env, target_ulong pc)
  {
      CPUState *cs = env_cpu(env);
@@ -621,6 +637,10 @@ void helper_wfi(CPURISCVState *env)
                 (prv_u || (prv_s && get_field(env->hstatus, HSTATUS_VTW)))) {
          riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
      } else {
+        if (riscv_cpu_cfg(env)->ext_sdext && !env->debug_mode &&
+            (env->dcsr & DCSR_STEP)) {
+            return;
+        }
          cs->halted = 1;
          cs->exception_index = EXCP_HLT;
          cpu_loop_exit(cs);
target/riscv/tcg/tcg-cpu.c +5 -0
index 959a1e89c8..16d26cee5c 100644
@@ -196,6 +196,11 @@ static TCGTBCPUState riscv_get_tb_cpu_state(CPUState *cs)
      ext_flags = FIELD_DP64(ext_flags, EXT_TB_FLAGS, MISA_EXT, env->misa_ext);
      ext_flags = FIELD_DP64(ext_flags, EXT_TB_FLAGS, BIG_ENDIAN,
                             mo_endian_env(env) == MO_BE);
+#ifndef CONFIG_USER_ONLY
+    if (cpu->cfg.ext_sdext && !env->debug_mode && (env->dcsr & DCSR_STEP)) {
+        ext_flags = FIELD_DP64(ext_flags, EXT_TB_FLAGS, SDEXT_STEP, 1);
+    }
+#endif
 
      return (TCGTBCPUState){
          .pc = env->xl == MXL_RV32 ? env->pc & UINT32_MAX : env->pc,
target/riscv/tcg/translate.c +13 -2
index 9684dbe752..1a38d6aeb5 100644
@@ -112,6 +112,8 @@ typedef struct DisasContext {
      bool ztso;
      /* Use icount trigger for native debug */
      bool itrigger;
+    /* Enter Debug Mode after next instruction (sdext single-step). */
+    bool sdext_step;
      /* FRM is known to contain a valid value. */
      bool frm_valid;
      bool insn_start_updated;
@@ -286,6 +288,9 @@ static void lookup_and_goto_ptr(DisasContext *ctx)
      if (ctx->itrigger) {
          gen_helper_itrigger_match(tcg_env);
      }
+    if (ctx->sdext_step) {
+        gen_helper_sdext_step(tcg_env);
+    }
  #endif
      tcg_gen_lookup_and_goto_ptr();
  }
@@ -296,6 +301,9 @@ static void exit_tb(DisasContext *ctx)
      if (ctx->itrigger) {
          gen_helper_itrigger_match(tcg_env);
      }
+    if (ctx->sdext_step) {
+        gen_helper_sdext_step(tcg_env);
+    }
  #endif
      tcg_gen_exit_tb(NULL, 0);
  }
@@ -309,7 +317,8 @@ static void gen_goto_tb(DisasContext *ctx, unsigned tb_slot_idx,
        * Under itrigger, instruction executes one by one like singlestep,
        * direct block chain benefits will be small.
        */
-    if (translator_use_goto_tb(&ctx->base, dest) && !ctx->itrigger) {
+    if (translator_use_goto_tb(&ctx->base, dest) &&
+        !ctx->itrigger && !ctx->sdext_step) {
          /*
           * For pcrel, the pc must always be up-to-date on entry to
           * the linked TB, so that it can use simple additions for all
@@ -1350,6 +1359,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
      ctx->bcfi_enabled = FIELD_EX32(tb_flags, TB_FLAGS, BCFI_ENABLED);
      ctx->fcfi_lp_expected = FIELD_EX32(tb_flags, TB_FLAGS, FCFI_LP_EXPECTED);
      ctx->fcfi_enabled = FIELD_EX32(tb_flags, TB_FLAGS, FCFI_ENABLED);
+    ctx->sdext_step = FIELD_EX64(ext_tb_flags, EXT_TB_FLAGS, SDEXT_STEP);
      ctx->zero = tcg_constant_tl(0);
      ctx->virt_inst_excp = false;
      ctx->decoders = cpu->decoders;
@@ -1402,7 +1412,8 @@ static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
 
      /* Only the first insn within a TB is allowed to cross a page boundary. */
      if (ctx->base.is_jmp == DISAS_NEXT) {
-        if (ctx->itrigger || !translator_is_same_page(&ctx->base, ctx->base.pc_next)) {
+        if (ctx->itrigger || ctx->sdext_step ||
+            !translator_is_same_page(&ctx->base, ctx->base.pc_next)) {
              ctx->base.is_jmp = DISAS_TOO_MANY;
          } else {
              unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK;
#16 lore raw

PATCH v9 6/7 target/riscv: add sdtrig trigger action=debug mode

In reply to Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Allow mcontrol/mcontrol6 action=1 when Sdext is enabled. When such a trigger hits, enter Debug Mode with cause=trigger and stop with EXCP_DEBUG.

Also report inst-count triggers in tinfo and read their action field.

---
 target/riscv/tcg/debug.c | 54 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
target/riscv/tcg/debug.c +52 -2
index 2537515f54..aaf2dca17e 100644
@@ -34,6 +34,7 @@
 #include "exec/watchpoint.h"
 #include "system/cpu-timers.h"
 #include "exec/icount.h"
+#include "accel/tcg/cpu-loop.h"
 
 /*
  * The following M-mode trigger CSRs are implemented:
@@ -117,6 +118,8 @@ static trigger_action_t get_trigger_action(CPURISCVState *env,
         action = (tdata1 & TYPE6_ACTION) >> 12;
         break;
     case TRIGGER_TYPE_INST_CNT:
+        action = tdata1 & ITRIGGER_ACTION;
+        break;
     case TRIGGER_TYPE_INT:
     case TRIGGER_TYPE_EXCP:
     case TRIGGER_TYPE_EXT_SRC:
@@ -287,6 +290,7 @@ static target_ulong textra_validate(CPURISCVState *env, target_ulong tdata3)
 
 static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index)
 {
+    CPUState *cs = env_cpu(env);
     trigger_action_t action = get_trigger_action(env, trigger_index);
 
     switch (action) {
@@ -296,6 +300,21 @@ static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index)
         riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
         break;
     case DBG_ACTION_DBG_MODE:
+        if (!env_archcpu(env)->cfg.ext_sdext) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "trigger action=debug mode requires Sdext\n");
+            riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
+        }
+        riscv_cpu_enter_debug_mode(env, env->pc, DCSR_CAUSE_TRIGGER);
+        /*
+         * If this came from the Trigger Module's CPU breakpoint/watchpoint,
+         * we're already returning via EXCP_DEBUG. Otherwise, stop now.
+         */
+        if (cs->exception_index != EXCP_DEBUG) {
+            cs->exception_index = EXCP_DEBUG;
+            cpu_loop_exit_restore(cs, GETPC());
+        }
+        break;
     case DBG_ACTION_TRACE0:
     case DBG_ACTION_TRACE1:
     case DBG_ACTION_TRACE2:
@@ -448,6 +467,7 @@ static target_ulong type2_mcontrol_validate(CPURISCVState *env,
 {
     target_ulong val;
     uint32_t size;
+    uint32_t action;
 
     /* validate the generic part first */
     val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH);
@@ -455,11 +475,25 @@ static target_ulong type2_mcontrol_validate(CPURISCVState *env,
     /* validate unimplemented (always zero) bits */
     warn_always_zero_bit(ctrl, TYPE2_MATCH, "match");
     warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain");
-    warn_always_zero_bit(ctrl, TYPE2_ACTION, "action");
     warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing");
     warn_always_zero_bit(ctrl, TYPE2_SELECT, "select");
     warn_always_zero_bit(ctrl, TYPE2_HIT, "hit");
 
+    action = (ctrl & TYPE2_ACTION) >> 12;
+    if (action == DBG_ACTION_BP) {
+        val |= ctrl & TYPE2_ACTION;
+    } else if (action == DBG_ACTION_DBG_MODE) {
+        if (env_archcpu(env)->cfg.ext_sdext) {
+            val |= ctrl & TYPE2_ACTION;
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "trigger action=debug mode requires Sdext\n");
+        }
+    } else {
+        qemu_log_mask(LOG_UNIMP, "trigger action: %u is not supported\n",
+                      action);
+    }
+
     /* validate size encoding */
     size = type2_breakpoint_size(env, ctrl);
     if (access_size[size] == -1) {
@@ -576,6 +610,7 @@ static target_ulong type6_mcontrol6_validate(CPURISCVState *env,
 {
     target_ulong val;
     uint32_t size;
+    uint32_t action;
 
     /* validate the generic part first */
     val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH6);
@@ -583,7 +618,6 @@ static target_ulong type6_mcontrol6_validate(CPURISCVState *env,
     /* validate unimplemented (always zero) bits */
     warn_always_zero_bit(ctrl, TYPE6_MATCH, "match");
     warn_always_zero_bit(ctrl, TYPE6_CHAIN, "chain");
-    warn_always_zero_bit(ctrl, TYPE6_ACTION, "action");
     warn_always_zero_bit(ctrl, TYPE6_SELECT, "select");
     warn_always_zero_bit(ctrl, TYPE6_HIT, "hit");
 
@@ -593,6 +627,21 @@ static target_ulong type6_mcontrol6_validate(CPURISCVState *env,
         warn_always_zero_bit(ctrl, TYPE6_TIMING_0_13, "timing");
     }
 
+    action = (ctrl & TYPE6_ACTION) >> 12;
+    if (action == DBG_ACTION_BP) {
+        val |= ctrl & TYPE6_ACTION;
+    } else if (action == DBG_ACTION_DBG_MODE) {
+        if (env_archcpu(env)->cfg.ext_sdext) {
+            val |= ctrl & TYPE6_ACTION;
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "trigger action=debug mode requires Sdext\n");
+        }
+    } else {
+        qemu_log_mask(LOG_UNIMP, "trigger action: %u is not supported\n",
+                      action);
+    }
+
     /* validate size encoding */
     size = extract32(ctrl, 16, 4);
     if (access_size[size] == -1) {
@@ -959,6 +1008,7 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
 target_ulong tinfo_csr_read(CPURISCVState *env)
 {
     target_ulong val = BIT(TRIGGER_TYPE_AD_MATCH) |
+                       BIT(TRIGGER_TYPE_INST_CNT) |
                        BIT(TRIGGER_TYPE_AD_MATCH6);
 
     if (debug_trigger_version_1_0(env)) {
Signature · 1 line
2.43.0
Acked-by on v9 6/7
#17 lore raw

Re: [PATCH v9 6/7] target/riscv: add sdtrig trigger action=debug mode

Quoted 9 lines · Daniel Henrique Barboza

From: Chao Liu <[email protected]>

RISC-V Debug Specification:
https://github.com/riscv/riscv-debug-spec/releases/tag/1.0

Allow mcontrol/mcontrol6 action=1 when Sdext is enabled. When such a trigger hits, enter Debug Mode with cause=trigger and stop with EXCP_DEBUG.

Quoted 8 lines

Also report inst-count triggers in tinfo and read their action field.

Reviewed-by Daniel Henrique Barboza

<[email protected]>

Signed-off-by Daniel Henrique Barboza

<[email protected]>

Alistair

Quoted 156 lines

---
 target/riscv/tcg/debug.c | 54
++++++++++++++++++++++++++++++++++++++--

 1 file changed, 52 insertions(+), 2 deletions(-)
target/riscv/tcg/debug.c +1 -0
index 2537515f54..aaf2dca17e 100644
@@ -34,6 +34,7 @@
  #include "exec/watchpoint.h"
  #include "system/cpu-timers.h"
  #include "exec/icount.h"
+#include "accel/tcg/cpu-loop.h"


 /*
  * The following M-mode trigger CSRs are implemented:

target/riscv/tcg/debug.c +0 -0
@@ -117,6 +118,8 @@ static trigger_action_t
 get_trigger_action(CPURISCVState *env,
          action = (tdata1 & TYPE6_ACTION) >> 12;
          break;

     case TRIGGER_TYPE_INST_CNT:
+        action = tdata1 & ITRIGGER_ACTION;
+        break;
     case TRIGGER_TYPE_INT:
     case TRIGGER_TYPE_EXCP:
     case TRIGGER_TYPE_EXT_SRC:

target/riscv/tcg/debug.c +0 -0
@@ -287,6 +290,7 @@ static target_ulong textra_validate(CPURISCVState
 *env, target_ulong tdata3)


 static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index)
 {
+    CPUState *cs = env_cpu(env);
     trigger_action_t action = get_trigger_action(env, trigger_index);

     switch (action) {

target/riscv/tcg/debug.c +0 -0
@@ -296,6 +300,21 @@ static void do_trigger_action(CPURISCVState
 *env, target_ulong trigger_index)
          riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
          break;

     case DBG_ACTION_DBG_MODE:
+        if (!env_archcpu(env)->cfg.ext_sdext) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "trigger action=debug mode requires Sdext\n");
+            riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0); +        }
+        riscv_cpu_enter_debug_mode(env, env->pc, DCSR_CAUSE_TRIGGER);
+        /*
+         * If this came from the Trigger Module's CPU breakpoint/watchpoint,
+         * we're already returning via EXCP_DEBUG. Otherwise, stop now.
+         */
+        if (cs->exception_index != EXCP_DEBUG) {
+            cs->exception_index = EXCP_DEBUG;
+            cpu_loop_exit_restore(cs, GETPC());
+        }
+        break;
     case DBG_ACTION_TRACE0:
     case DBG_ACTION_TRACE1:
     case DBG_ACTION_TRACE2:

target/riscv/tcg/debug.c +1 -0
@@ -448,6 +467,7 @@ static target_ulong
 type2_mcontrol_validate(CPURISCVState *env,
  {
      target_ulong val;
      uint32_t size;
+    uint32_t action;


     /* validate the generic part first */
     val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH);

target/riscv/tcg/debug.c +0 -1
@@ -455,11 +475,25 @@ static target_ulong
 type2_mcontrol_validate(CPURISCVState *env,
      /* validate unimplemented (always zero) bits */
      warn_always_zero_bit(ctrl, TYPE2_MATCH, "match");
      warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain");
-    warn_always_zero_bit(ctrl, TYPE2_ACTION, "action");
      warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing");
      warn_always_zero_bit(ctrl, TYPE2_SELECT, "select");
      warn_always_zero_bit(ctrl, TYPE2_HIT, "hit");


+    action = (ctrl & TYPE2_ACTION) >> 12;
+    if (action == DBG_ACTION_BP) {
+        val |= ctrl & TYPE2_ACTION;
+    } else if (action == DBG_ACTION_DBG_MODE) {
+        if (env_archcpu(env)->cfg.ext_sdext) {
+            val |= ctrl & TYPE2_ACTION;
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "trigger action=debug mode requires Sdext\n");
+        }
+    } else {
+        qemu_log_mask(LOG_UNIMP, "trigger action: %u is not supported\n",
+                      action);
+    }
+
     /* validate size encoding */
     size = type2_breakpoint_size(env, ctrl);
     if (access_size[size] == -1) {

target/riscv/tcg/debug.c +1 -0
@@ -576,6 +610,7 @@ static target_ulong
 type6_mcontrol6_validate(CPURISCVState *env,
  {
      target_ulong val;
      uint32_t size;
+    uint32_t action;


     /* validate the generic part first */
     val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH6);

target/riscv/tcg/debug.c +0 -1
@@ -583,7 +618,6 @@ static target_ulong
 type6_mcontrol6_validate(CPURISCVState *env,
      /* validate unimplemented (always zero) bits */
      warn_always_zero_bit(ctrl, TYPE6_MATCH, "match");
      warn_always_zero_bit(ctrl, TYPE6_CHAIN, "chain");
-    warn_always_zero_bit(ctrl, TYPE6_ACTION, "action");
      warn_always_zero_bit(ctrl, TYPE6_SELECT, "select");
      warn_always_zero_bit(ctrl, TYPE6_HIT, "hit");

target/riscv/tcg/debug.c +0 -0
@@ -593,6 +627,21 @@ static target_ulong
 type6_mcontrol6_validate(CPURISCVState *env,
          warn_always_zero_bit(ctrl, TYPE6_TIMING_0_13, "timing");
      }


+    action = (ctrl & TYPE6_ACTION) >> 12;
+    if (action == DBG_ACTION_BP) {
+        val |= ctrl & TYPE6_ACTION;
+    } else if (action == DBG_ACTION_DBG_MODE) {
+        if (env_archcpu(env)->cfg.ext_sdext) {
+            val |= ctrl & TYPE6_ACTION;
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "trigger action=debug mode requires Sdext\n");
+        }
+    } else {
+        qemu_log_mask(LOG_UNIMP, "trigger action: %u is not supported\n",
+                      action);
+    }
+
     /* validate size encoding */
     size = extract32(ctrl, 16, 4);
     if (access_size[size] == -1) {

target/riscv/tcg/debug.c +0 -0
@@ -959,6 +1008,7 @@ void tdata_csr_write(CPURISCVState *env, int
 tdata_index, target_ulong val)
  target_ulong tinfo_csr_read(CPURISCVState *env)
  {

     target_ulong val = BIT(TRIGGER_TYPE_AD_MATCH) | +                       BIT(TRIGGER_TYPE_INST_CNT) | BIT(TRIGGER_TYPE_AD_MATCH6);
     if (debug_trigger_version_1_0(env)) {

#18 lore raw

PATCH v9 7/7 tests/tcg/riscv64: add sdext "not on Debug mode" sigill tests

In reply to Daniel Henrique Barboza

Adding proper TCG automated tests to sdext CSRs requires enabling gdbstub support for riscv64, which we don't have, since the nature of the CSRs added is that they run in a higher privilege than M mode.

DCSR has bits to allow for context switching from other modes to Debug mode, but those bits must be set via an external entity (e.g. gdb) beforehand.

What we can do for now is a "negative" TCG test to ensure that we'll throw illegal insn traps when trying to write sdext CSRs in machine mode.

---
 tests/tcg/riscv64/system/meson.build  |  8 +++
 tests/tcg/riscv64/test-sdext-sigill.S | 83 +++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-sdext-sigill.S
tests/tcg/riscv64/system/meson.build +8 -0
index 8604c2a45a..6a836994e6 100644
@@ -61,6 +61,14 @@ tests += {
   }
 }
 
+tests += {
+  'test-sdext-sigill.S': {
+    'exe_name': 'test-sdext-sigill',
+    'cflags': cflags + ['-march=rv64gcv'],
+    'qemu_args': ['-cpu', 'rv64,sdtrig=true,sdext=true', qemu_args],
+  }
+}
+
 if 'qemu-system-riscv64' in emulators
   tcg_tests += {
     'riscv64-softmmu': {
tests/tcg/riscv64/test-sdext-sigill.S new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
2.43.0
Acked-by on v9 7/7
#19 lore raw

Re: [PATCH v9 7/7] tests/tcg/riscv64: add sdext "not on Debug mode" sigill tests

Quoted 9 lines · Daniel Henrique Barboza

Adding proper TCG automated tests to sdext CSRs requires enabling gdbstub support for riscv64, which we don't have, since the nature of the CSRs added is that they run in a higher privilege than M mode.

DCSR has bits to allow for context switching from other modes to Debug
mode, but those bits must be set via an external entity (e.g. gdb) beforehand.

Quoted 6 lines

What we can do for now is a "negative" TCG test to ensure that we'll throw illegal insn traps when trying to write sdext CSRs in machine mode.

Signed-off-by: Daniel Henrique Barboza
<[email protected]>

Alistair

Quoted 119 lines

---
 tests/tcg/riscv64/system/meson.build  |  8 +++
 tests/tcg/riscv64/test-sdext-sigill.S | 83
+++++++++++++++++++++++++++

 2 files changed, 91 insertions(+)

 create mode 100644 tests/tcg/riscv64/test-sdext-sigill.S

tests/tcg/riscv64/system/meson.build +0 -0
index 8604c2a45a..6a836994e6 100644
@@ -61,6 +61,14 @@ tests += {
    }
  }

tests/tcg/riscv64/system/meson.build +8 -0
+tests += {
+  'test-sdext-sigill.S': {
+    'exe_name': 'test-sdext-sigill',
+    'cflags': cflags + ['-march=rv64gcv'],
+    'qemu_args': ['-cpu', 'rv64,sdtrig=true,sdext=true', qemu_args],
+  }
+}
+

 if 'qemu-system-riscv64' in emulators
   tcg_tests += {
     'riscv64-softmmu': {

tests/tcg/riscv64/test-sdext-sigill.S new file +5 -0
new file mode 100644
index 0000000000..80a6c06465
@@ -0,0 +1,83 @@
+/*
+ * RISC-V sdext extension "SIGILL" test: check if sdext CSRs
+ * and 'dret' will throw SIGILLs outside of Debug Mode.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its

subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */

tests/tcg/riscv64/test-sdext-sigill.S new file +36 -0
+
+.equ CSR_DCSR,      0x7b0
+.equ CSR_DPC,       0x7b1
+.equ CSR_DSCRATCH0, 0x7b2
+
+	.option norvc
+	.text
+
+	.global _start
+
+_start:
+	la t0, trap_handler
+	csrw mtvec, t0
+
+	dret
+	j _fail
+
+	li t0, 1
+	csrw CSR_DPC, t0
+	j _fail
+
+	li t0, 1
+	csrw CSR_DSCRATCH0, t0
+	j _fail
+
+	li t0, 1
+	csrw CSR_DCSR, t0
+	j _fail
+
+	li a0, 0
+	j _exit
+
+_fail:
+	li a0, 1
+
+/* Exit through the semihosting SYS_EXIT_EXTENDED call with a0 as

the code. */

tests/tcg/riscv64/test-sdext-sigill.S new file +40 -0
+_exit:
+	la	a1, semiargs
+	li	t0, 0x20026	/* ADP_Stopped_ApplicationExit */
+	sd	t0, 0(a1)
+	sd	a0, 8(a1)
+	li	a0, 0x20	/* TARGET_SYS_EXIT_EXTENDED */
+	.balign	16
+	slli	zero, zero, 0x1f
+	ebreak
+	srai	zero, zero, 0x7
+	j	.
+
+
+	.balign	4
+trap_handler:
+	csrr	t4, mcause
+	la	t5, trap_mcause
+	sd	t4, 0(t5)
+	csrr	t4, mtval
+	la	t5, trap_mtval
+	sd	t4, 0(t5)
+	csrr	t4, mepc
+
+	/*
+	 * Skip 2 insns (8 increment in mepc) to skip both the
+	 * insn that caused the trap and the "j  _fail" insn
+	 * that follows it.
+	 */
+	addi	t4, t4, 8
+	csrw	mepc, t4
+	mret
+
+	.data
+	.balign	8
+semiargs:
+	.space	16
+trap_mcause:
+	.space	8
+trap_mtval:
+	.space	8