Skip to contentSkip to conversation

RISC-V: Implement CSR tcontrol in debug spec

PATCH In review Alvin Chang via 3 participants 12 messages Feb 16, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

cover RISC-V: Implement CSR tcontrol in debug spec

The RISC-V Debug specification defines CSR "tcontrol" in the trigger module:
https://github.com/riscv/riscv-debug-spec

This series implements it and the related operations.

Alvin Chang (4):
target/riscv: Add CSR tcontrol of debug trigger module target/riscv: Reset CSR tcontrol when the trigger module resets target/riscv: Set the value of CSR tcontrol when trapping to M-mode target/riscv: Set the value of CSR tcontrol when mret is executed

 target/riscv/cpu.h        |  1 +
 target/riscv/cpu_bits.h   |  3 +++
 target/riscv/cpu_helper.c |  6 ++++++
 target/riscv/csr.c        | 15 +++++++++++++++
 target/riscv/debug.c      |  1 +
 target/riscv/op_helper.c  |  6 ++++++
 6 files changed, 32 insertions(+)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 target/riscv: Add CSR tcontrol of debug trigger module

The RISC-V debug specification defines an optional CSR "tcontrol" within the trigger module. This commit adds its read/write operations and related bit-field definitions.

---
 target/riscv/cpu.h      |  1 +
 target/riscv/cpu_bits.h |  3 +++
 target/riscv/csr.c      | 15 +++++++++++++++
 3 files changed, 19 insertions(+)
target/riscv/cpu.h +1 -0
index f52dce78ba..f9ae3e3025 100644
@@ -364,6 +364,7 @@ struct CPUArchState {
     target_ulong tdata1[RV_MAX_TRIGGERS];
     target_ulong tdata2[RV_MAX_TRIGGERS];
     target_ulong tdata3[RV_MAX_TRIGGERS];
+    target_ulong tcontrol;
     target_ulong mcontext;
     struct CPUBreakpoint *cpu_breakpoint[RV_MAX_TRIGGERS];
     struct CPUWatchpoint *cpu_watchpoint[RV_MAX_TRIGGERS];
target/riscv/cpu_bits.h +3 -0
index fc2068ee4d..3b3a7a0fa4 100644
@@ -353,6 +353,7 @@
 #define CSR_TDATA2          0x7a2
 #define CSR_TDATA3          0x7a3
 #define CSR_TINFO           0x7a4
+#define CSR_TCONTROL        0x7a5
 #define CSR_MCONTEXT        0x7a8
 
 /* Debug Mode Registers */
@@ -900,6 +901,8 @@ typedef enum RISCVException {
 #define JVT_BASE                           (~0x3F)
 
 /* Debug Sdtrig CSR masks */
+#define TCONTROL_MTE                       BIT(3)
+#define TCONTROL_MPTE                      BIT(7)
 #define MCONTEXT32                         0x0000003F
 #define MCONTEXT64                         0x0000000000001FFFULL
 #define MCONTEXT32_HCONTEXT                0x0000007F
target/riscv/csr.c +15 -0
index d4e8ac13b9..816c530481 100644
@@ -3937,6 +3937,20 @@ static RISCVException read_tinfo(CPURISCVState *env, int csrno,
     return RISCV_EXCP_NONE;
 }
 
+static RISCVException read_tcontrol(CPURISCVState *env, int csrno,
+                                    target_ulong *val)
+{
+    *val = env->tcontrol;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_tcontrol(CPURISCVState *env, int csrno,
+                                     target_ulong val)
+{
+    env->tcontrol = val & (TCONTROL_MPTE | TCONTROL_MTE);
+    return RISCV_EXCP_NONE;
+}
+
 static RISCVException read_mcontext(CPURISCVState *env, int csrno,
                                     target_ulong *val)
 {
@@ -4861,6 +4875,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
     [CSR_TDATA2]    =  { "tdata2",   debug, read_tdata,    write_tdata    },
     [CSR_TDATA3]    =  { "tdata3",   debug, read_tdata,    write_tdata    },
     [CSR_TINFO]     =  { "tinfo",    debug, read_tinfo,    write_ignore   },
+    [CSR_TCONTROL]  =  { "tcontrol", debug, read_tcontrol, write_tcontrol },
     [CSR_MCONTEXT]  =  { "mcontext", debug, read_mcontext, write_mcontext },
 
     /* User Pointer Masking */
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/4] target/riscv: Add CSR tcontrol of debug trigger module

Quoted 70 lines · Alvin Chang

The RISC-V debug specification defines an optional CSR "tcontrol" within the trigger module. This commit adds its read/write operations and related bit-field definitions.

---
  target/riscv/cpu.h      |  1 +
  target/riscv/cpu_bits.h |  3 +++
  target/riscv/csr.c      | 15 +++++++++++++++
  3 files changed, 19 insertions(+)
target/riscv/cpu.h +1 -0
index f52dce78ba..f9ae3e3025 100644
@@ -364,6 +364,7 @@ struct CPUArchState {
      target_ulong tdata1[RV_MAX_TRIGGERS];
      target_ulong tdata2[RV_MAX_TRIGGERS];
      target_ulong tdata3[RV_MAX_TRIGGERS];
+    target_ulong tcontrol;
      target_ulong mcontext;
      struct CPUBreakpoint *cpu_breakpoint[RV_MAX_TRIGGERS];
      struct CPUWatchpoint *cpu_watchpoint[RV_MAX_TRIGGERS];
target/riscv/cpu_bits.h +3 -0
index fc2068ee4d..3b3a7a0fa4 100644
@@ -353,6 +353,7 @@
  #define CSR_TDATA2          0x7a2
  #define CSR_TDATA3          0x7a3
  #define CSR_TINFO           0x7a4
+#define CSR_TCONTROL        0x7a5
  #define CSR_MCONTEXT        0x7a8
 
  /* Debug Mode Registers */
@@ -900,6 +901,8 @@ typedef enum RISCVException {
  #define JVT_BASE                           (~0x3F)
 
  /* Debug Sdtrig CSR masks */
+#define TCONTROL_MTE                       BIT(3)
+#define TCONTROL_MPTE                      BIT(7)
  #define MCONTEXT32                         0x0000003F
  #define MCONTEXT64                         0x0000000000001FFFULL
  #define MCONTEXT32_HCONTEXT                0x0000007F
target/riscv/csr.c +14 -0
index d4e8ac13b9..816c530481 100644
@@ -3937,6 +3937,20 @@ static RISCVException read_tinfo(CPURISCVState *env, int csrno,
      return RISCV_EXCP_NONE;
  }
 
+static RISCVException read_tcontrol(CPURISCVState *env, int csrno,
+                                    target_ulong *val)
+{
+    *val = env->tcontrol;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_tcontrol(CPURISCVState *env, int csrno,
+                                     target_ulong val)
+{
+    env->tcontrol = val & (TCONTROL_MPTE | TCONTROL_MTE);
+    return RISCV_EXCP_NONE;
+}
+
  static RISCVException read_mcontext(CPURISCVState *env, int csrno,
                                      target_ulong *val)
  {

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 4878

Quoted 4 lines
target/riscv/csr.c +1 -0
@@ -4861,6 +4875,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
      [CSR_TDATA2]    =  { "tdata2",   debug, read_tdata,    write_tdata    },
      [CSR_TDATA3]    =  { "tdata3",   debug, read_tdata,    write_tdata    },
      [CSR_TINFO]     =  { "tinfo",    debug, read_tinfo,    write_ignore   },
+    [CSR_TCONTROL]  =  { "tcontrol", debug, read_tcontrol, write_tcontrol },

The spec reads:

"This optional register is only accessible in M-mode and Debug Mode and provides
various control bits related to triggers."

"debug()" is checking only if we have the 'debug' cpu option enabled:

static RISCVException debug(CPURISCVState *env, int csrno)

{
    if (riscv_cpu_cfg(env)->debug) {
        return RISCV_EXCP_NONE;
    }

    return RISCV_EXCP_ILLEGAL_INST;
}

It looks like we don't have a "Debug Mode" model.

Section 4.1 of the spec mentions the following about "Debug Mode":

"1. All implemented instructions operate just as they do in M-mode, unless an
exception is mentioned in this list.
2. All operations are executed with machine mode privilege, except that additional Debug Mode CSRs are accessible and MPRV in mstatus may be ignored according to mprven. Full permission checks, or a relaxed set of permission checks, will apply according to relaxedpriv (...)"

So, if the operations are "executed with machine mode privilege" then can we expect env->priv == PRV_M ? As it is now tcontrol will execute in any mode, so checking for PRV_M seems reasonable.

Thanks,

Daniel

Quoted 3 lines
target/riscv/csr.c +0 -0
      [CSR_MCONTEXT]  =  { "mcontext", debug, read_mcontext, write_mcontext },
 
      /* User Pointer Masking */
#3 lore raw

RE: [PATCH 1/4] target/riscv: Add CSR tcontrol of debug trigger module

Hi Daniel,

Quoted 109 lines

-----Original Message-----
From: Daniel Henrique Barboza <[email protected]>
Sent: Friday, February 16, 2024 8:51 PM
To: Alvin Che-Chia Chang(張哲嘉) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/4] target/riscv: Add CSR tcontrol of debug trigger module

Quoted 36 lines · Alvin Chang

The RISC-V debug specification defines an optional CSR "tcontrol" within the trigger module. This commit adds its read/write operations and related bit-field definitions.

---
  target/riscv/cpu.h      |  1 +
  target/riscv/cpu_bits.h |  3 +++
  target/riscv/csr.c      | 15 +++++++++++++++
  3 files changed, 19 insertions(+)
target/riscv/cpu.h index renamed from target/riscv/cpu.h +0 -0

f52dce78ba..f9ae3e3025 100644

target/riscv/cpu.h +1 -1
@@ -364,6 +364,7 @@ struct CPUArchState {
      target_ulong tdata1[RV_MAX_TRIGGERS];
      target_ulong tdata2[RV_MAX_TRIGGERS];
      target_ulong tdata3[RV_MAX_TRIGGERS];
+    target_ulong tcontrol;
      target_ulong mcontext;
      struct CPUBreakpoint *cpu_breakpoint[RV_MAX_TRIGGERS];
      struct CPUWatchpoint *cpu_watchpoint[RV_MAX_TRIGGERS]; diff
--git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index

fc2068ee4d..3b3a7a0fa4 100644

target/riscv/cpu_bits.h +1 -0
@@ -353,6 +353,7 @@
  #define CSR_TDATA2          0x7a2
  #define CSR_TDATA3          0x7a3
  #define CSR_TINFO           0x7a4
+#define CSR_TCONTROL        0x7a5
  #define CSR_MCONTEXT        0x7a8
 
  /* Debug Mode Registers */

target/riscv/cpu_bits.h typedef enum RISCVException { · line 907

Quoted 7 lines
target/riscv/cpu_bits.h +2 -0
@@ -900,6 +901,8 @@ typedef enum RISCVException {
  #define JVT_BASE                           (~0x3F)
 
  /* Debug Sdtrig CSR masks */
+#define TCONTROL_MTE                       BIT(3)
+#define TCONTROL_MPTE                      BIT(7)
  #define MCONTEXT32                         0x0000003F
  #define MCONTEXT64

0x0000000000001FFFULL

target/riscv/csr.c static RISCVException read_tinfo(CPURISCVState

Quoted 6 lines
target/riscv/cpu_bits.h +0 -0
  #define MCONTEXT32_HCONTEXT                0x0000007F
target/riscv/csr.c index renamed from target/riscv/csr.c +0 -0

d4e8ac13b9..816c530481 100644

target/riscv/csr.c +0 -0
@@ -3937,6 +3937,20 @@ static RISCVException read_tinfo(CPURISCVState

*env, int csrno,

Quoted 19 lines
target/riscv/csr.c +12 -0
      return RISCV_EXCP_NONE;
  }
 
+static RISCVException read_tcontrol(CPURISCVState *env, int csrno,
+                                    target_ulong *val) {
+    *val = env->tcontrol;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_tcontrol(CPURISCVState *env, int csrno,
+                                     target_ulong val) {
+    env->tcontrol = val & (TCONTROL_MPTE | TCONTROL_MTE);
+    return RISCV_EXCP_NONE;
+}
+
  static RISCVException read_mcontext(CPURISCVState *env, int csrno,
                                      target_ulong *val)
  {

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 4875

Quoted 1 line
target/riscv/csr.c +0 -0
@@ -4861,6 +4875,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
      [CSR_TDATA2]    =  { "tdata2",   debug, read_tdata,

write_tdata },

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 4876

Quoted 1 line
target/riscv/csr.c +0 -0
      [CSR_TDATA3]    =  { "tdata3",   debug, read_tdata,

write_tdata },

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 4877

Quoted 1 line
target/riscv/csr.c +0 -0
      [CSR_TINFO]     =  { "tinfo",    debug, read_tinfo,

write_ignore },

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 4879

Quoted 2 lines
target/riscv/csr.c +2 -0
+    [CSR_TCONTROL]  =  { "tcontrol", debug, read_tcontrol,
+ write_tcontrol },

The spec reads:

"This optional register is only accessible in M-mode and Debug Mode and provides
various control bits related to triggers."

"debug()" is checking only if we have the 'debug' cpu option enabled:

static RISCVException debug(CPURISCVState *env, int csrno) {
     if (riscv_cpu_cfg(env)->debug) {
         return RISCV_EXCP_NONE;
     }

     return RISCV_EXCP_ILLEGAL_INST;
}
Quoted 1 line

It looks like we don't have a "Debug Mode" model.

Yes, currently RISC-V QEMU doesn't have "Debug Mode", so I just ignore it and only consider M-mode here.

Quoted 14 lines

Section 4.1 of the spec mentions the following about "Debug Mode":

"1. All implemented instructions operate just as they do in M-mode, unless an
exception is mentioned in this list.
2. All operations are executed with machine mode privilege, except that
additional
Debug Mode CSRs are accessible and MPRV in mstatus may be ignored
according to
mprven. Full permission checks, or a relaxed set of permission checks, will
apply
according to relaxedpriv (...)"

Quoted 5 lines

So, if the operations are "executed with machine mode privilege" then can we expect
env->priv == PRV_M ? As it is now tcontrol will execute in any mode, so env->checking
for PRV_M seems reasonable.

The riscv_csrrw_check() function has checked the privilege level by the accessed CSR address. Please see https://github.com/qemu/qemu/blob/master/target/riscv/csr.c#L4360.
If the current privilege level is lower than the accessed CSR, the illegal instruction exception is raised.

According to privilege spec, each CSR address has two bits to encode the lowest privilege level to access that CSR. Sdtrig CSRs (tselect, tdata{1~3}, tinfo, mcontext, and tcontrol) are encoded within 0x7A0~0x7AF, Therefore, their lowest required privilege level is M-mode.
If env->priv < PRV_M, the checking on those CSR fails in riscv_csrrw_check().

Is this enough, or do I miss something?

Sincerely,
Alvin Chang

Quoted 15 lines

Thanks,

Daniel

Quoted 4 lines
target/riscv/csr.c +0 -0
      [CSR_MCONTEXT]  =  { "mcontext", debug, read_mcontext,
 write_mcontext },
 
      /* User Pointer Masking */
#4 lore raw

Re: [PATCH 1/4] target/riscv: Add CSR tcontrol of debug trigger module

Quoted 139 lines · Alvin Che-Chia Chang(張哲嘉)

Hi Daniel,

Quoted 109 lines

-----Original Message-----
From: Daniel Henrique Barboza <[email protected]>
Sent: Friday, February 16, 2024 8:51 PM
To: Alvin Che-Chia Chang(張哲嘉) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/4] target/riscv: Add CSR tcontrol of debug trigger module

Quoted 36 lines · Alvin Chang

The RISC-V debug specification defines an optional CSR "tcontrol" within the trigger module. This commit adds its read/write operations and related bit-field definitions.

---
   target/riscv/cpu.h      |  1 +
   target/riscv/cpu_bits.h |  3 +++
   target/riscv/csr.c      | 15 +++++++++++++++
   3 files changed, 19 insertions(+)
target/riscv/cpu.h index renamed from target/riscv/cpu.h +0 -0

f52dce78ba..f9ae3e3025 100644

target/riscv/cpu.h +1 -1
@@ -364,6 +364,7 @@ struct CPUArchState {
       target_ulong tdata1[RV_MAX_TRIGGERS];
       target_ulong tdata2[RV_MAX_TRIGGERS];
       target_ulong tdata3[RV_MAX_TRIGGERS];
+    target_ulong tcontrol;
       target_ulong mcontext;
       struct CPUBreakpoint *cpu_breakpoint[RV_MAX_TRIGGERS];
       struct CPUWatchpoint *cpu_watchpoint[RV_MAX_TRIGGERS]; diff
--git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index

fc2068ee4d..3b3a7a0fa4 100644

target/riscv/cpu_bits.h +1 -0
@@ -353,6 +353,7 @@
   #define CSR_TDATA2          0x7a2
   #define CSR_TDATA3          0x7a3
   #define CSR_TINFO           0x7a4
+#define CSR_TCONTROL        0x7a5
   #define CSR_MCONTEXT        0x7a8
 
   /* Debug Mode Registers */

target/riscv/cpu_bits.h typedef enum RISCVException { · line 907

Quoted 7 lines
target/riscv/cpu_bits.h +2 -0
@@ -900,6 +901,8 @@ typedef enum RISCVException {
   #define JVT_BASE                           (~0x3F)
 
   /* Debug Sdtrig CSR masks */
+#define TCONTROL_MTE                       BIT(3)
+#define TCONTROL_MPTE                      BIT(7)
   #define MCONTEXT32                         0x0000003F
   #define MCONTEXT64

0x0000000000001FFFULL

target/riscv/csr.c static RISCVException read_tinfo(CPURISCVState

Quoted 6 lines
target/riscv/cpu_bits.h +0 -0
   #define MCONTEXT32_HCONTEXT                0x0000007F
target/riscv/csr.c index renamed from target/riscv/csr.c +0 -0

d4e8ac13b9..816c530481 100644

target/riscv/csr.c +0 -0
@@ -3937,6 +3937,20 @@ static RISCVException read_tinfo(CPURISCVState

*env, int csrno,

Quoted 19 lines
target/riscv/csr.c +12 -0
       return RISCV_EXCP_NONE;
   }
 
+static RISCVException read_tcontrol(CPURISCVState *env, int csrno,
+                                    target_ulong *val) {
+    *val = env->tcontrol;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_tcontrol(CPURISCVState *env, int csrno,
+                                     target_ulong val) {
+    env->tcontrol = val & (TCONTROL_MPTE | TCONTROL_MTE);
+    return RISCV_EXCP_NONE;
+}
+
   static RISCVException read_mcontext(CPURISCVState *env, int csrno,
                                       target_ulong *val)
   {

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 4875

Quoted 1 line
target/riscv/csr.c +0 -0
@@ -4861,6 +4875,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
       [CSR_TDATA2]    =  { "tdata2",   debug, read_tdata,

write_tdata },

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 4876

Quoted 1 line
target/riscv/csr.c +0 -0
       [CSR_TDATA3]    =  { "tdata3",   debug, read_tdata,

write_tdata },

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 4877

Quoted 1 line
target/riscv/csr.c +0 -0
       [CSR_TINFO]     =  { "tinfo",    debug, read_tinfo,

write_ignore },

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 4879

Quoted 2 lines
target/riscv/csr.c +2 -0
+    [CSR_TCONTROL]  =  { "tcontrol", debug, read_tcontrol,
+ write_tcontrol },

The spec reads:

"This optional register is only accessible in M-mode and Debug Mode and provides
various control bits related to triggers."

"debug()" is checking only if we have the 'debug' cpu option enabled:

static RISCVException debug(CPURISCVState *env, int csrno) {
      if (riscv_cpu_cfg(env)->debug) {
          return RISCV_EXCP_NONE;
      }

      return RISCV_EXCP_ILLEGAL_INST;
}
Quoted 1 line

It looks like we don't have a "Debug Mode" model.

Yes, currently RISC-V QEMU doesn't have "Debug Mode", so I just ignore it and only consider M-mode here.

Quoted 14 lines

Section 4.1 of the spec mentions the following about "Debug Mode":

"1. All implemented instructions operate just as they do in M-mode, unless an
exception is mentioned in this list.
2. All operations are executed with machine mode privilege, except that
additional
Debug Mode CSRs are accessible and MPRV in mstatus may be ignored
according to
mprven. Full permission checks, or a relaxed set of permission checks, will
apply
according to relaxedpriv (...)"

Quoted 5 lines

So, if the operations are "executed with machine mode privilege" then can we expect
env->priv == PRV_M ? As it is now tcontrol will execute in any mode, so env->checking
for PRV_M seems reasonable.

The riscv_csrrw_check() function has checked the privilege level by the accessed CSR address. Please see https://github.com/qemu/qemu/blob/master/target/riscv/csr.c#L4360.
If the current privilege level is lower than the accessed CSR, the illegal instruction exception is raised.

Quoted 7 lines

According to privilege spec, each CSR address has two bits to encode the lowest privilege level to access that CSR. Sdtrig CSRs (tselect, tdata{1~3}, tinfo, mcontext, and tcontrol) are encoded within 0x7A0~0x7AF, Therefore, their lowest required privilege level is M-mode.
If env->priv < PRV_M, the checking on those CSR fails in riscv_csrrw_check().

Is this enough, or do I miss something?

This is enough, thanks for clarifying.

And since we're not modelling a "Debug Mode" I think this is already the best we can do ATM.

Thanks,

Daniel

Quoted 19 lines

Sincerely,
Alvin Chang

Quoted 15 lines

Thanks,

Daniel

Quoted 4 lines
target/riscv/csr.c +0 -0
       [CSR_MCONTEXT]  =  { "mcontext", debug, read_mcontext,
 write_mcontext },
 
       /* User Pointer Masking */
Reviewed-by on v1 1/4
#5 lore raw

Re: [PATCH 1/4] target/riscv: Add CSR tcontrol of debug trigger module

In reply to Alvin Chang via
Quoted 6 lines · Alvin Chang

The RISC-V debug specification defines an optional CSR "tcontrol" within the trigger module. This commit adds its read/write operations and related bit-field definitions.

---

Quoted 71 lines
  target/riscv/cpu.h      |  1 +
  target/riscv/cpu_bits.h |  3 +++
  target/riscv/csr.c      | 15 +++++++++++++++
  3 files changed, 19 insertions(+)
target/riscv/cpu.h +1 -0
index f52dce78ba..f9ae3e3025 100644
@@ -364,6 +364,7 @@ struct CPUArchState {
      target_ulong tdata1[RV_MAX_TRIGGERS];
      target_ulong tdata2[RV_MAX_TRIGGERS];
      target_ulong tdata3[RV_MAX_TRIGGERS];
+    target_ulong tcontrol;
      target_ulong mcontext;
      struct CPUBreakpoint *cpu_breakpoint[RV_MAX_TRIGGERS];
      struct CPUWatchpoint *cpu_watchpoint[RV_MAX_TRIGGERS];
target/riscv/cpu_bits.h +3 -0
index fc2068ee4d..3b3a7a0fa4 100644
@@ -353,6 +353,7 @@
  #define CSR_TDATA2          0x7a2
  #define CSR_TDATA3          0x7a3
  #define CSR_TINFO           0x7a4
+#define CSR_TCONTROL        0x7a5
  #define CSR_MCONTEXT        0x7a8
 
  /* Debug Mode Registers */
@@ -900,6 +901,8 @@ typedef enum RISCVException {
  #define JVT_BASE                           (~0x3F)
 
  /* Debug Sdtrig CSR masks */
+#define TCONTROL_MTE                       BIT(3)
+#define TCONTROL_MPTE                      BIT(7)
  #define MCONTEXT32                         0x0000003F
  #define MCONTEXT64                         0x0000000000001FFFULL
  #define MCONTEXT32_HCONTEXT                0x0000007F
target/riscv/csr.c +15 -0
index d4e8ac13b9..816c530481 100644
@@ -3937,6 +3937,20 @@ static RISCVException read_tinfo(CPURISCVState *env, int csrno,
      return RISCV_EXCP_NONE;
  }
 
+static RISCVException read_tcontrol(CPURISCVState *env, int csrno,
+                                    target_ulong *val)
+{
+    *val = env->tcontrol;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_tcontrol(CPURISCVState *env, int csrno,
+                                     target_ulong val)
+{
+    env->tcontrol = val & (TCONTROL_MPTE | TCONTROL_MTE);
+    return RISCV_EXCP_NONE;
+}
+
  static RISCVException read_mcontext(CPURISCVState *env, int csrno,
                                      target_ulong *val)
  {
@@ -4861,6 +4875,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
      [CSR_TDATA2]    =  { "tdata2",   debug, read_tdata,    write_tdata    },
      [CSR_TDATA3]    =  { "tdata3",   debug, read_tdata,    write_tdata    },
      [CSR_TINFO]     =  { "tinfo",    debug, read_tinfo,    write_ignore   },
+    [CSR_TCONTROL]  =  { "tcontrol", debug, read_tcontrol, write_tcontrol },
      [CSR_MCONTEXT]  =  { "mcontext", debug, read_mcontext, write_mcontext },
 
      /* User Pointer Masking */
#6 lore raw

PATCH 2/4 target/riscv: Reset CSR tcontrol when the trigger module resets

In reply to Alvin Chang via

When the trigger module resets, reset the value of CSR tcontrol as zero.

---
 target/riscv/debug.c | 1 +
 1 file changed, 1 insertion(+)
target/riscv/debug.c +1 -0
index e30d99cc2f..e3832a643e 100644
@@ -941,5 +941,6 @@ void riscv_trigger_reset_hold(CPURISCVState *env)
         timer_del(env->itrigger_timer[i]);
     }
 
+    env->tcontrol = 0;
     env->mcontext = 0;
 }
Signature · 1 line
2.34.1
Reviewed-by on v1 2/4
#7 lore raw

Re: [PATCH 2/4] target/riscv: Reset CSR tcontrol when the trigger module resets

Quoted 4 lines · Alvin Chang

When the trigger module resets, reset the value of CSR tcontrol as zero.

---

Quoted 14 lines
  target/riscv/debug.c | 1 +
  1 file changed, 1 insertion(+)
target/riscv/debug.c +1 -0
index e30d99cc2f..e3832a643e 100644
@@ -941,5 +941,6 @@ void riscv_trigger_reset_hold(CPURISCVState *env)
          timer_del(env->itrigger_timer[i]);
      }
 
+    env->tcontrol = 0;
      env->mcontext = 0;
  }
#8 lore raw

PATCH 3/4 target/riscv: Set the value of CSR tcontrol when trapping to M-mode

In reply to Alvin Chang via

From the RISC-V debug specification, it defines the following operations for CSR tcontrol when any trap into M-mode is taken:
1. tcontrol.MPTE is set to the value of tcontrol.MTE
2. tcontrol.MTE is set to 0

This commit implements the above operations into
riscv_cpu_do_interrupt().

---
 target/riscv/cpu_helper.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/riscv/cpu_helper.c +6 -0
index d462d95ee1..037ae21062 100644
@@ -1806,6 +1806,12 @@ void riscv_cpu_do_interrupt(CPUState *cs)
             riscv_cpu_set_virt_enabled(env, 0);
         }
 
+        /* Trapping to M-mode. Set tcontrol CSR in debug Sdtrig extension. */
+        s = env->tcontrol;
+        s = set_field(s, TCONTROL_MPTE, get_field(s, TCONTROL_MTE));
+        s = set_field(s, TCONTROL_MTE, 0);
+        env->tcontrol = s;
+
         s = env->mstatus;
         s = set_field(s, MSTATUS_MPIE, get_field(s, MSTATUS_MIE));
         s = set_field(s, MSTATUS_MPP, env->priv);
Signature · 1 line
2.34.1
Reviewed-by on v1 3/4
#9 lore raw

Re: [PATCH 3/4] target/riscv: Set the value of CSR tcontrol when trapping to M-mode

Quoted 10 lines · Alvin Chang

From the RISC-V debug specification, it defines the following operations
for CSR tcontrol when any trap into M-mode is taken:
1. tcontrol.MPTE is set to the value of tcontrol.MTE
2. tcontrol.MTE is set to 0

This commit implements the above operations into
riscv_cpu_do_interrupt().

---

Quoted 20 lines
  target/riscv/cpu_helper.c | 6 ++++++
  1 file changed, 6 insertions(+)
target/riscv/cpu_helper.c +6 -0
index d462d95ee1..037ae21062 100644
@@ -1806,6 +1806,12 @@ void riscv_cpu_do_interrupt(CPUState *cs)
              riscv_cpu_set_virt_enabled(env, 0);
          }
 
+        /* Trapping to M-mode. Set tcontrol CSR in debug Sdtrig extension. */
+        s = env->tcontrol;
+        s = set_field(s, TCONTROL_MPTE, get_field(s, TCONTROL_MTE));
+        s = set_field(s, TCONTROL_MTE, 0);
+        env->tcontrol = s;
+
          s = env->mstatus;
          s = set_field(s, MSTATUS_MPIE, get_field(s, MSTATUS_MIE));
          s = set_field(s, MSTATUS_MPP, env->priv);
#10 lore raw

PATCH 4/4 target/riscv: Set the value of CSR tcontrol when mret is executed

In reply to Alvin Chang via

The RISC-V debug specification defines the following operation for CSR tcontrol when "mret" is executed:
- tcontrol.MTE is set to the value of tcontrol.MPTE

This commit implements the above operation into helper_mret().

Note that from tech-debug mailing list:
https://lists.riscv.org/g/tech-debug/topic/102702615#1461
The debug specification does not mention the operation to tcontrol.MPTE when "mret" is executed. Therefore, we just keep its current value.

---
 target/riscv/op_helper.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/riscv/op_helper.c +6 -0
index f414aaebdb..12822b3afa 100644
@@ -347,6 +347,12 @@ target_ulong helper_mret(CPURISCVState *env)
         mstatus = set_field(mstatus, MSTATUS_MPRV, 0);
     }
     env->mstatus = mstatus;
+
+    uint64_t tcontrol = env->tcontrol;
+    tcontrol = set_field(tcontrol, TCONTROL_MTE,
+                         get_field(tcontrol, TCONTROL_MPTE));
+    env->tcontrol = tcontrol;
+
     riscv_cpu_set_mode(env, prev_priv);
 
     if (riscv_has_ext(env, RVH)) {
Signature · 1 line
2.34.1
Reviewed-by on v1 4/4
#11 lore raw

Re: [PATCH 4/4] target/riscv: Set the value of CSR tcontrol when mret is executed

Quoted 6 lines · Alvin Chang

The RISC-V debug specification defines the following operation for CSR tcontrol when "mret" is executed:
- tcontrol.MTE is set to the value of tcontrol.MPTE

This commit implements the above operation into helper_mret().

Quoted 7 lines

Note that from tech-debug mailing list:
https://lists.riscv.org/g/tech-debug/topic/102702615#1461
The debug specification does not mention the operation to tcontrol.MPTE when "mret" is executed. Therefore, we just keep its current value.

---

Quoted 20 lines
  target/riscv/op_helper.c | 6 ++++++
  1 file changed, 6 insertions(+)
target/riscv/op_helper.c +6 -0
index f414aaebdb..12822b3afa 100644
@@ -347,6 +347,12 @@ target_ulong helper_mret(CPURISCVState *env)
          mstatus = set_field(mstatus, MSTATUS_MPRV, 0);
      }
      env->mstatus = mstatus;
+
+    uint64_t tcontrol = env->tcontrol;
+    tcontrol = set_field(tcontrol, TCONTROL_MTE,
+                         get_field(tcontrol, TCONTROL_MPTE));
+    env->tcontrol = tcontrol;
+
      riscv_cpu_set_mode(env, prev_priv);
 
      if (riscv_has_ext(env, RVH)) {