Skip to contentSkip to conversation

target/riscv: set (m|s)tval on

PATCH Superseded Daniel Henrique Barboza 2 participants 4 messages Apr 16, 2024 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover target/riscv: set (m|s)tval on

Hi,

This is a re-send of the patch sent as a 9.0 bugfix in [1], now reframed as a non-bug fix chabge,

A new patch (2) was added to handle a similar scenario with ebreak and tval.

Changes from v1:
- patch 1:
- rewrite commit msg to make it clear that this is a non-bug
fix change
- new patch (2):
- set mtval/stval during ebreak
- v1 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 | 4 ++++
 3 files changed, 8 insertions(+)
Signature · 1 line
2.44.0
#1 lore raw

PATCH v2 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
#2 lore raw

PATCH v2 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 | 4 ++++
 1 file changed, 4 insertions(+)
target/riscv/insn_trans/trans_privileged.c.inc +4 -0
index 620ab54eb0..b89e3bd5b5 100644
@@ -62,6 +62,10 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
     if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
         generate_exception(ctx, RISCV_EXCP_SEMIHOST);
     } else {
+        TCGv temp = tcg_temp_new();
+        tcg_gen_movi_tl(temp, ebreak_addr);
+        tcg_gen_st_tl(temp, tcg_env, offsetof(CPURISCVState, badaddr));
+
         generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
     }
     return true;
Signature · 1 line
2.44.0
#3 lore raw

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

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

Quoted 7 lines · Daniel Henrique Barboza
target/riscv/insn_trans/trans_privileged.c.inc +3 -0
@@ -62,6 +62,10 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
      if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
          generate_exception(ctx, RISCV_EXCP_SEMIHOST);
      } else {
+        TCGv temp = tcg_temp_new();
+        tcg_gen_movi_tl(temp, ebreak_addr);
+        tcg_gen_st_tl(temp, tcg_env, offsetof(CPURISCVState, badaddr));

tcg_gen_st_tl(tcg_constant_tl(ebreak_addr), ...)

r~