Skip to contentSkip to conversation

target/riscv: set tval in breakpoints

PATCH Applied Daniel Henrique Barboza 4 participants 9 messages Apr 16, 2024 – Apr 29, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

v3 cover target/riscv: set tval in breakpoints

Hi,

This new version has a change suggested by Richard in v2. No other changes made.

Changes from v2:
- patch 2:
- use tcg_constant_tl() instead of loading a temp and doing a

    movi_tl()

- v2 link: https://lore.kernel.org/qemu-riscv/[email protected]/

Daniel Henrique Barboza (2):
target/riscv/debug: set tval=pc in breakpoint exceptions trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint

 target/riscv/cpu_helper.c                      | 1 +
 target/riscv/debug.c                           | 3 +++
 target/riscv/insn_trans/trans_privileged.c.inc | 2 ++
 3 files changed, 6 insertions(+)
Signature · 1 line
2.44.0
#1 lore raw

PATCH v3 1/2 target/riscv/debug: set tval=pc in breakpoint exceptions

We're not setting (s/m)tval when triggering breakpoints of type 2 (mcontrol) and 6 (mcontrol6). According to the debug spec section 5.7.12, "Match Control Type 6":

"The Privileged Spec says that breakpoint exceptions that occur on instruction fetches, loads, or stores update the tval CSR with either zero or the faulting virtual address. The faulting virtual address for an mcontrol6 trigger with action = 0 is the address being accessed and which caused that trigger to fire."

A similar text is also found in the Debug spec section 5.7.11 w.r.t. mcontrol.

Note that what we're doing ATM is not violating the spec, but it's simple enough to set mtval/stval and it makes life easier for any software that relies on this info.

Given that we always use action = 0, save the faulting address for the mcontrol and mcontrol6 trigger breakpoints into env->badaddr, which is used as as scratch area for traps with address information. 'tval' is then set during riscv_cpu_do_interrupt().

---
 target/riscv/cpu_helper.c | 1 +
 target/riscv/debug.c      | 3 +++
 2 files changed, 4 insertions(+)
target/riscv/cpu_helper.c +1 -0
index fc090d729a..f9c6d7053b 100644
@@ -1717,6 +1717,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)
             tval = env->bins;
             break;
         case RISCV_EXCP_BREAKPOINT:
+            tval = env->badaddr;
             if (cs->watchpoint_hit) {
                 tval = cs->watchpoint_hit->hitaddr;
                 cs->watchpoint_hit = NULL;
target/riscv/debug.c +3 -0
index e30d99cc2f..b110370ea6 100644
@@ -798,6 +798,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
                 if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) {
                     /* check U/S/M bit against current privilege level */
                     if ((ctrl >> 3) & BIT(env->priv)) {
+                        env->badaddr = pc;
                         return true;
                     }
                 }
@@ -810,11 +811,13 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
                     if (env->virt_enabled) {
                         /* check VU/VS bit against current privilege level */
                         if ((ctrl >> 23) & BIT(env->priv)) {
+                            env->badaddr = pc;
                             return true;
                         }
                     } else {
                         /* check U/S/M bit against current privilege level */
                         if ((ctrl >> 3) & BIT(env->priv)) {
+                            env->badaddr = pc;
                             return true;
                         }
                     }
Signature · 1 line
2.44.0
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH for-9.1 v3 1/2] target/riscv/debug: set tval=pc in breakpoint exceptions

Quoted 56 lines · Daniel Henrique Barboza

We're not setting (s/m)tval when triggering breakpoints of type 2 (mcontrol) and 6 (mcontrol6). According to the debug spec section 5.7.12, "Match Control Type 6":

"The Privileged Spec says that breakpoint exceptions that occur on instruction fetches, loads, or stores update the tval CSR with either zero or the faulting virtual address. The faulting virtual address for an mcontrol6 trigger with action = 0 is the address being accessed and which caused that trigger to fire."

A similar text is also found in the Debug spec section 5.7.11 w.r.t. mcontrol.

Note that what we're doing ATM is not violating the spec, but it's simple enough to set mtval/stval and it makes life easier for any software that relies on this info.

Given that we always use action = 0, save the faulting address for the mcontrol and mcontrol6 trigger breakpoints into env->badaddr, which is used as as scratch area for traps with address information. 'tval' is then set during riscv_cpu_do_interrupt().

---
  target/riscv/cpu_helper.c | 1 +
  target/riscv/debug.c      | 3 +++
  2 files changed, 4 insertions(+)
target/riscv/cpu_helper.c +1 -0
index fc090d729a..f9c6d7053b 100644
@@ -1717,6 +1717,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)
              tval = env->bins;
              break;
          case RISCV_EXCP_BREAKPOINT:
+            tval = env->badaddr;
              if (cs->watchpoint_hit) {
                  tval = cs->watchpoint_hit->hitaddr;
                  cs->watchpoint_hit = NULL;
target/riscv/debug.c +1 -0
index e30d99cc2f..b110370ea6 100644
@@ -798,6 +798,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
                  if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) {
                      /* check U/S/M bit against current privilege level */
                      if ((ctrl >> 3) & BIT(env->priv)) {
+                        env->badaddr = pc;
                          return true;
                      }
                  }
@@ -810,11 +811,13 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
                      if (env->virt_enabled) {
                          /* check VU/VS bit against current privilege level */
                          if ((ctrl >> 23) & BIT(env->priv)) {

target/riscv/debug.c bool riscv_cpu_debug_check_breakpoint(CPUState *cs) · line 823

Quoted 10 lines
target/riscv/debug.c +2 -0
+                            env->badaddr = pc;
                              return true;
                          }
                      } else {
                          /* check U/S/M bit against current privilege level */
                          if ((ctrl >> 3) & BIT(env->priv)) {
+                            env->badaddr = pc;
                              return true;
                          }
                      }

Zhiwei

Reviewed-by on v3 1/2
#3 lore raw

Re: [PATCH for-9.1 v3 1/2] target/riscv/debug: set tval=pc in breakpoint exceptions

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

We're not setting (s/m)tval when triggering breakpoints of type 2 (mcontrol) and 6 (mcontrol6). According to the debug spec section 5.7.12, "Match Control Type 6":

"The Privileged Spec says that breakpoint exceptions that occur on instruction fetches, loads, or stores update the tval CSR with either zero or the faulting virtual address. The faulting virtual address for an mcontrol6 trigger with action = 0 is the address being accessed and which caused that trigger to fire."

A similar text is also found in the Debug spec section 5.7.11 w.r.t. mcontrol.

Note that what we're doing ATM is not violating the spec, but it's simple enough to set mtval/stval and it makes life easier for any software that relies on this info.

Quoted 6 lines

Given that we always use action = 0, save the faulting address for the mcontrol and mcontrol6 trigger breakpoints into env->badaddr, which is used as as scratch area for traps with address information. 'tval' is then set during riscv_cpu_do_interrupt().

Alistair

Quoted 47 lines
---
 target/riscv/cpu_helper.c | 1 +
 target/riscv/debug.c      | 3 +++
 2 files changed, 4 insertions(+)
target/riscv/cpu_helper.c +1 -0
index fc090d729a..f9c6d7053b 100644
@@ -1717,6 +1717,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)
             tval = env->bins;
             break;
         case RISCV_EXCP_BREAKPOINT:
+            tval = env->badaddr;
             if (cs->watchpoint_hit) {
                 tval = cs->watchpoint_hit->hitaddr;
                 cs->watchpoint_hit = NULL;
target/riscv/debug.c +3 -1
index e30d99cc2f..b110370ea6 100644
@@ -798,6 +798,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
                 if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) {
                     /* check U/S/M bit against current privilege level */
                     if ((ctrl >> 3) & BIT(env->priv)) {
+                        env->badaddr = pc;
                         return true;
                     }
                 }
@@ -810,11 +811,13 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
                     if (env->virt_enabled) {
                         /* check VU/VS bit against current privilege level */
                         if ((ctrl >> 23) & BIT(env->priv)) {
+                            env->badaddr = pc;
                             return true;
                         }
                     } else {
                         /* check U/S/M bit against current privilege level */
                         if ((ctrl >> 3) & BIT(env->priv)) {
+                            env->badaddr = pc;
                             return true;
                         }
                     }
--

2.44.0

#4 lore raw

PATCH v3 2/2 trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint

In reply to Daniel Henrique Barboza

Privileged spec section 4.1.9 mentions:

"When a trap is taken into S-mode, stval is written with exception-specific information to assist software in handling the trap. (...)

If stval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or page-fault exception occurs on an instruction fetch, load, or store, then stval will contain the faulting virtual address."

A similar text is found for mtval in section 3.1.16.

Setting mtval/stval in this scenario is optional, but some softwares read these regs when handling ebreaks.

Write 'badaddr' in all ebreak breakpoints to write the appropriate 'tval' during riscv_do_cpu_interrrupt().

---
 target/riscv/insn_trans/trans_privileged.c.inc | 2 ++
 1 file changed, 2 insertions(+)
target/riscv/insn_trans/trans_privileged.c.inc +2 -0
index 620ab54eb0..bc5263a4e0 100644
@@ -62,6 +62,8 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
     if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
         generate_exception(ctx, RISCV_EXCP_SEMIHOST);
     } else {
+        tcg_gen_st_tl(tcg_constant_tl(ebreak_addr), tcg_env,
+                      offsetof(CPURISCVState, badaddr));
         generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
     }
     return true;
Signature · 1 line
2.44.0
Reviewed-by on v3 2/2
#5 lore raw

Re: [PATCH for-9.1 v3 2/2] trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint

Quoted 16 lines · Daniel Henrique Barboza

Privileged spec section 4.1.9 mentions:

"When a trap is taken into S-mode, stval is written with exception-specific information to assist software in handling the trap. (...)

If stval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or page-fault exception occurs on an instruction fetch, load, or store, then stval will contain the faulting virtual address."

A similar text is found for mtval in section 3.1.16.

Setting mtval/stval in this scenario is optional, but some softwares read these regs when handling ebreaks.

Quoted 7 lines

Write 'badaddr' in all ebreak breakpoints to write the appropriate 'tval' during riscv_do_cpu_interrrupt().

---
  target/riscv/insn_trans/trans_privileged.c.inc | 2 ++
  1 file changed, 2 insertions(+)

r~

Reviewed-by on v3 2/2
#6 lore raw

Re: [PATCH for-9.1 v3 2/2] trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint

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

Privileged spec section 4.1.9 mentions:

"When a trap is taken into S-mode, stval is written with exception-specific information to assist software in handling the trap. (...)

If stval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or page-fault exception occurs on an instruction fetch, load, or store, then stval will contain the faulting virtual address."

A similar text is found for mtval in section 3.1.16.

Setting mtval/stval in this scenario is optional, but some softwares read these regs when handling ebreaks.

Write 'badaddr' in all ebreak breakpoints to write the appropriate 'tval' during riscv_do_cpu_interrrupt().

---
  target/riscv/insn_trans/trans_privileged.c.inc | 2 ++
  1 file changed, 2 insertions(+)
target/riscv/insn_trans/trans_privileged.c.inc +0 -0
index 620ab54eb0..bc5263a4e0 100644

target/riscv/insn_trans/trans_privileged.c.inc static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a) · line 66

Quoted 5 lines
target/riscv/insn_trans/trans_privileged.c.inc +2 -0
@@ -62,6 +62,8 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
      if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
          generate_exception(ctx, RISCV_EXCP_SEMIHOST);
      } else {
+        tcg_gen_st_tl(tcg_constant_tl(ebreak_addr), tcg_env,
+                      offsetof(CPURISCVState, badaddr));

Zhiwei

Quoted 3 lines
target/riscv/insn_trans/trans_privileged.c.inc +0 -0
          generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
      }
      return true;
Reviewed-by on v3 2/2
#7 lore raw

Re: [PATCH for-9.1 v3 2/2] trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint

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

Privileged spec section 4.1.9 mentions:

"When a trap is taken into S-mode, stval is written with exception-specific information to assist software in handling the trap. (...)

If stval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or page-fault exception occurs on an instruction fetch, load, or store, then stval will contain the faulting virtual address."

A similar text is found for mtval in section 3.1.16.

Quoted 7 lines

Setting mtval/stval in this scenario is optional, but some softwares read these regs when handling ebreaks.

Write 'badaddr' in all ebreak breakpoints to write the appropriate 'tval' during riscv_do_cpu_interrrupt().

Alistair

Quoted 21 lines
---
 target/riscv/insn_trans/trans_privileged.c.inc | 2 ++
 1 file changed, 2 insertions(+)
target/riscv/insn_trans/trans_privileged.c.inc +2 -1
index 620ab54eb0..bc5263a4e0 100644
@@ -62,6 +62,8 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
     if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
         generate_exception(ctx, RISCV_EXCP_SEMIHOST);
     } else {
+        tcg_gen_st_tl(tcg_constant_tl(ebreak_addr), tcg_env,
+                      offsetof(CPURISCVState, badaddr));
         generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
     }
     return true;
--

2.44.0

#8 lore raw
In reply to Daniel Henrique Barboza
Quoted 9 lines · Daniel Henrique Barboza

Hi,

This new version has a change suggested by Richard in v2. No other changes made.

Changes from v2:
- patch 2:
- use tcg_constant_tl() instead of loading a temp and doing a

Quoted 7 lines
    movi_tl()

- v2 link: https://lore.kernel.org/qemu-riscv/[email protected]/

Daniel Henrique Barboza (2):
target/riscv/debug: set tval=pc in breakpoint exceptions trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 10 lines
 target/riscv/cpu_helper.c                      | 1 +
 target/riscv/debug.c                           | 3 +++
 target/riscv/insn_trans/trans_privileged.c.inc | 2 ++
 3 files changed, 6 insertions(+)

--
2.44.0