Skip to content

Commit 83a97ca

Browse files
author
zhusonghe
committed
target/riscv:Perform single step before resume if necessary
Two cases where single step is needed before resuming: 1. ebreak used in software breakpoint; 2. a trigger that is taken just before the instruction that triggered it is retired. Signed-off-by: Songhe Zhu <[email protected]> Co-developed-by: Fei Gao <[email protected]> Co-developed-by: xiatianyi <[email protected]>
1 parent a4020f1 commit 83a97ca

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/target/riscv/riscv.c

+30-7
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,12 @@ static int find_first_trigger_by_id(struct target *target, int unique_id)
623623

624624
static unsigned int count_trailing_ones(riscv_reg_t reg)
625625
{
626-
assert(sizeof(riscv_reg_t) * 8 == 64);
627-
for (unsigned int i = 0; i < 64; i++) {
626+
const unsigned int riscv_reg_bits = sizeof(riscv_reg_t) * CHAR_BIT;
627+
for (unsigned int i = 0; i < riscv_reg_bits; i++) {
628628
if ((1 & (reg >> i)) == 0)
629629
return i;
630630
}
631-
return 64;
631+
return riscv_reg_bits;
632632
}
633633

634634
static int set_trigger(struct target *target, unsigned int idx, riscv_reg_t tdata1, riscv_reg_t tdata2)
@@ -668,6 +668,16 @@ static int set_trigger(struct target *target, unsigned int idx, riscv_reg_t tdat
668668
const uint32_t type = get_field(tdata1, CSR_TDATA1_TYPE(riscv_xlen(target)));
669669
const bool is_mcontrol = type == CSR_TDATA1_TYPE_MCONTROL;
670670

671+
if (type == CSR_TDATA1_TYPE_MCONTROL) {
672+
if (get_field(tdata1_rb, CSR_MCONTROL_TIMING) == CSR_MCONTROL_TIMING_BEFORE)
673+
r->halt_before_dpc = true;
674+
} else if (type == CSR_TDATA1_TYPE_MCONTROL6) {
675+
int hit0 = get_field(tdata1_rb, CSR_MCONTROL6_HIT0);
676+
int hit1 = get_field(tdata1_rb, CSR_MCONTROL6_HIT1);
677+
if (((hit1 << 1) | hit0) == CSR_MCONTROL6_HIT0_BEFORE)
678+
r->halt_before_dpc = true;
679+
}
680+
671681
/* Determine if tdata1 supports what we need.
672682
* For mcontrol triggers, we don't care about
673683
* the value in the read-only "maskmax" field.
@@ -2547,16 +2557,29 @@ static int resume_prep(struct target *target, int current,
25472557
assert(target->state == TARGET_HALTED);
25482558
RISCV_INFO(r);
25492559

2560+
riscv_reg_t dcsr;
2561+
if (riscv_reg_get(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
2562+
return ERROR_FAIL;
2563+
25502564
if (!current && riscv_reg_set(target, GDB_REGNO_PC, address) != ERROR_OK)
25512565
return ERROR_FAIL;
25522566

25532567
if (handle_breakpoints) {
25542568
/* To be able to run off a trigger, we perform a step operation and then
25552569
* resume. If handle_breakpoints is true then step temporarily disables
2556-
* pending breakpoints so we can safely perform the step. */
2557-
if (old_or_new_riscv_step_impl(target, current, address, handle_breakpoints,
2558-
false /* callbacks are not called */) != ERROR_OK)
2559-
return ERROR_FAIL;
2570+
* pending breakpoints so we can safely perform the step.
2571+
*
2572+
* Two cases where single step is needed before resuming:
2573+
* 1. ebreak used in software breakpoint;
2574+
* 2. a trigger that is taken just before the instruction that triggered it is retired.
2575+
*/
2576+
if (get_field(dcsr, CSR_DCSR_CAUSE) == CSR_DCSR_CAUSE_EBREAK
2577+
|| (get_field(dcsr, CSR_DCSR_CAUSE) == CSR_DCSR_CAUSE_TRIGGER
2578+
&& r->halt_before_dpc)) {
2579+
if (old_or_new_riscv_step_impl(target, current, address, handle_breakpoints,
2580+
false /* callbacks are not called */) != ERROR_OK)
2581+
return ERROR_FAIL;
2582+
}
25602583
}
25612584

25622585
if (r->get_hart_state) {

src/target/riscv/riscv.h

+3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ struct riscv_info {
152152
/* record the tinfo of each trigger */
153153
unsigned int trigger_tinfo[RISCV_MAX_TRIGGERS];
154154

155+
/* record the dpc that triggered it is retired. */
156+
bool halt_before_dpc;
157+
155158
/* For each physical trigger contains:
156159
* -1: the hwbp is available
157160
* -4: The trigger is used by the itrigger command

0 commit comments

Comments
 (0)