Skip to contentSkip to conversation

target/riscv: set tval for triggered watchpoints

PATCH Applied Sergey Matyukevich 4 participants 6 messages Jan 30, 2023 – Jan 31, 2023 RISC-V

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: set tval for triggered watchpoints

From: Sergey Matyukevich <[email protected]>

According to priviledged spec, if [sm]tval is written with a nonzero value when a breakpoint exception occurs, then [sm]tval will contain the faulting virtual address. Set tval to hit address when breakpoint exception is triggered by hardware watchpoint.

---
 target/riscv/cpu_helper.c | 3 +++
 target/riscv/debug.c      | 1 +
 2 files changed, 4 insertions(+)
target/riscv/cpu_helper.c +3 -0
index 9a28816521..d3be8c0511 100644
@@ -1641,6 +1641,9 @@ void riscv_cpu_do_interrupt(CPUState *cs)
         case RISCV_EXCP_VIRT_INSTRUCTION_FAULT:
             tval = env->bins;
             break;
+        case RISCV_EXCP_BREAKPOINT:
+            tval = env->badaddr;
+            break;
         default:
             break;
         }
target/riscv/debug.c +1 -0
index bf4840a6a3..48ef3c59ea 100644
@@ -761,6 +761,7 @@ void riscv_cpu_debug_excp_handler(CPUState *cs)
 
     if (cs->watchpoint_hit) {
         if (cs->watchpoint_hit->flags & BP_CPU) {
+            env->badaddr = cs->watchpoint_hit->hitaddr;
             cs->watchpoint_hit = NULL;
             do_trigger_action(env, DBG_ACTION_BP);
         }
Signature · 1 line
2.39.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Sergey Matyukevich

From: Sergey Matyukevich<[email protected]>

According to priviledged spec, if [sm]tval is written with a nonzero value when a breakpoint exception occurs, then [sm]tval will contain the faulting virtual address. Set tval to hit address when breakpoint exception is triggered by hardware watchpoint.

Quoted 5 lines
---
  target/riscv/cpu_helper.c | 3 +++
  target/riscv/debug.c      | 1 +
  2 files changed, 4 insertions(+)

r~

Reviewed-by on v1 1/1
#2 lore raw
In reply to Sergey Matyukevich
Quoted 9 lines · Sergey Matyukevich

From: Sergey Matyukevich <[email protected]>

According to priviledged spec, if [sm]tval is written with a nonzero value when a breakpoint exception occurs, then [sm]tval will contain the faulting virtual address. Set tval to hit address when breakpoint exception is triggered by hardware watchpoint.

Alistair

Quoted 35 lines
---
 target/riscv/cpu_helper.c | 3 +++
 target/riscv/debug.c      | 1 +
 2 files changed, 4 insertions(+)
target/riscv/cpu_helper.c +3 -0
index 9a28816521..d3be8c0511 100644
@@ -1641,6 +1641,9 @@ void riscv_cpu_do_interrupt(CPUState *cs)
         case RISCV_EXCP_VIRT_INSTRUCTION_FAULT:
             tval = env->bins;
             break;
+        case RISCV_EXCP_BREAKPOINT:
+            tval = env->badaddr;
+            break;
         default:
             break;
         }
target/riscv/debug.c +1 -1
index bf4840a6a3..48ef3c59ea 100644
@@ -761,6 +761,7 @@ void riscv_cpu_debug_excp_handler(CPUState *cs)
 
     if (cs->watchpoint_hit) {
         if (cs->watchpoint_hit->flags & BP_CPU) {
+            env->badaddr = cs->watchpoint_hit->hitaddr;
             cs->watchpoint_hit = NULL;
             do_trigger_action(env, DBG_ACTION_BP);
         }
--

2.39.0

#3 lore raw
In reply to Sergey Matyukevich
Quoted 9 lines · Sergey Matyukevich

From: Sergey Matyukevich <[email protected]>

According to priviledged spec, if [sm]tval is written with a nonzero value when a breakpoint exception occurs, then [sm]tval will contain the faulting virtual address. Set tval to hit address when breakpoint exception is triggered by hardware watchpoint.

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 35 lines
---
 target/riscv/cpu_helper.c | 3 +++
 target/riscv/debug.c      | 1 +
 2 files changed, 4 insertions(+)
target/riscv/cpu_helper.c +3 -0
index 9a28816521..d3be8c0511 100644
@@ -1641,6 +1641,9 @@ void riscv_cpu_do_interrupt(CPUState *cs)
         case RISCV_EXCP_VIRT_INSTRUCTION_FAULT:
             tval = env->bins;
             break;
+        case RISCV_EXCP_BREAKPOINT:
+            tval = env->badaddr;
+            break;
         default:
             break;
         }
target/riscv/debug.c +1 -1
index bf4840a6a3..48ef3c59ea 100644
@@ -761,6 +761,7 @@ void riscv_cpu_debug_excp_handler(CPUState *cs)
 
     if (cs->watchpoint_hit) {
         if (cs->watchpoint_hit->flags & BP_CPU) {
+            env->badaddr = cs->watchpoint_hit->hitaddr;
             cs->watchpoint_hit = NULL;
             do_trigger_action(env, DBG_ACTION_BP);
         }
--

2.39.0

#4 lore raw
Quoted 10 lines · Alistair Francis
Quoted 8 lines · Sergey Matyukevich

From: Sergey Matyukevich <[email protected]>

According to priviledged spec, if [sm]tval is written with a nonzero value when a breakpoint exception occurs, then [sm]tval will contain the faulting virtual address. Set tval to hit address when breakpoint exception is triggered by hardware watchpoint.

Quoted 5 lines
Quoted 1 line

Thanks!

Applied to riscv-to-apply.next

Oops, too quick, but I have one comment :)

Quoted 8 lines

Alistair

Quoted 5 lines
---
 target/riscv/cpu_helper.c | 3 +++
 target/riscv/debug.c      | 1 +
 2 files changed, 4 insertions(+)
Quoted 10 lines

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

Quoted 10 lines
target/riscv/cpu_helper.c +2 -0
index 9a28816521..d3be8c0511 100644
@@ -1641,6 +1641,9 @@ void riscv_cpu_do_interrupt(CPUState *cs)
         case RISCV_EXCP_VIRT_INSTRUCTION_FAULT:
             tval = env->bins;
             break;
+        case RISCV_EXCP_BREAKPOINT:
+            tval = env->badaddr;

RISCV_EXCP_BREAKPOINT may come from 'ebreak' so we should test if this exception comes from the debug module.

The spec also says about icount trigger that:

"If the trigger fires with action =0 then zero is written to the tval CSR on the breakpoint trap."

So we can't blindly set tval for every breakpoint exception.

Quoted 10 lines
Quoted 10 lines
target/riscv/cpu_helper.c +1 -0
+            break;
         default:
             break;
         }
target/riscv/debug.c +0 -0
index bf4840a6a3..48ef3c59ea 100644
@@ -761,6 +761,7 @@ void riscv_cpu_debug_excp_handler(CPUState *cs)
 
Quoted 7 lines

target/riscv/debug.c void riscv_cpu_debug_excp_handler(CPUState *cs)

Quoted 7 lines
target/riscv/debug.c +1 -1
     if (cs->watchpoint_hit) {
         if (cs->watchpoint_hit->flags & BP_CPU) {
+            env->badaddr = cs->watchpoint_hit->hitaddr;
             cs->watchpoint_hit = NULL;
             do_trigger_action(env, DBG_ACTION_BP);
         }
--

Regards,
Bin

#5 lore raw

Hi Bin,

Quoted 35 lines
Quoted 10 lines
Quoted 6 lines

According to priviledged spec, if [sm]tval is written with a nonzero value when a breakpoint exception occurs, then [sm]tval will contain the faulting virtual address. Set tval to hit address when breakpoint exception is triggered by hardware watchpoint.

Thanks!

Applied to riscv-to-apply.next

Oops, too quick, but I have one comment :)

Quoted 8 lines

Alistair

Quoted 5 lines
---
 target/riscv/cpu_helper.c | 3 +++
 target/riscv/debug.c      | 1 +
 2 files changed, 4 insertions(+)
Quoted 10 lines

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

Quoted 10 lines
target/riscv/cpu_helper.c +2 -0
index 9a28816521..d3be8c0511 100644
@@ -1641,6 +1641,9 @@ void riscv_cpu_do_interrupt(CPUState *cs)
         case RISCV_EXCP_VIRT_INSTRUCTION_FAULT:
             tval = env->bins;
             break;
+        case RISCV_EXCP_BREAKPOINT:
+            tval = env->badaddr;

RISCV_EXCP_BREAKPOINT may come from 'ebreak' so we should test if this exception comes from the debug module.

Quoted 7 lines

The spec also says about icount trigger that:

"If the trigger fires with action =0 then zero is written to the tval CSR on the breakpoint trap."

So we can't blindly set tval for every breakpoint exception.

Thanks for catching ! Initial idea was to set badaddr value only when it is needed in target/riscv/debug.c. For instance, icount code does not set badaddr, so tval will remain zero. On the other hand, breakpoint exception may come from ebreak and badaddr may keep non-zero value from some previous unrelated exception.

Explicit check should be more safe, e.g. something like that:

target/riscv/cpu_helper.c +4 -1
index d3be8c0511..f1a0c65ad3 100644
@@ -1642,7 +1642,10 @@ 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;
+            }
             break;
         default:
             break;
target/riscv/debug.c +0 -2
index 48ef3c59ea..b091293069 100644
@@ -761,8 +761,6 @@ void riscv_cpu_debug_excp_handler(CPUState *cs)
 
     if (cs->watchpoint_hit) {
         if (cs->watchpoint_hit->flags & BP_CPU) {
-            env->badaddr = cs->watchpoint_hit->hitaddr;
-            cs->watchpoint_hit = NULL;
             do_trigger_action(env, DBG_ACTION_BP);
         }
     } else {

I will a fixup after testing.

Regards,
Sergey