Skip to content

Commit e44b56d

Browse files
author
zhusonghe
committed
target/riscv:Perform single step before resume if necessary
Two cases where single step is needed before resume: 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 e44b56d

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

src/target/riscv/riscv.c

+79-8
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)
@@ -1561,6 +1561,52 @@ int riscv_remove_watchpoint(struct target *target,
15611561
return ERROR_OK;
15621562
}
15631563

1564+
typedef enum {
1565+
M6_HIT_ERROR,
1566+
M6_HIT_NOT_SUPPORTED,
1567+
M6_NOT_HIT,
1568+
M6_HIT_BEFORE,
1569+
M6_HIT_AFTER,
1570+
M6_HIT_IMM_AFTER
1571+
} mctrl6hitstatus;
1572+
1573+
static mctrl6hitstatus check_mcontrol6_hit_status(struct target *target,
1574+
riscv_reg_t tdata1, uint64_t hit_mask)
1575+
{
1576+
uint32_t hit0 = get_field(tdata1, CSR_MCONTROL6_HIT0);
1577+
uint32_t hit1 = get_field(tdata1, CSR_MCONTROL6_HIT1);
1578+
uint32_t hit_info = (hit1 << 1) | hit0;
1579+
if (hit_info == CSR_MCONTROL6_HIT0_BEFORE)
1580+
return M6_HIT_BEFORE;
1581+
1582+
if (hit_info == CSR_MCONTROL6_HIT0_AFTER)
1583+
return M6_HIT_AFTER;
1584+
1585+
if (hit_info == CSR_MCONTROL6_HIT0_IMMEDIATELY_AFTER)
1586+
return M6_HIT_IMM_AFTER;
1587+
1588+
if (hit_info == CSR_MCONTROL6_HIT0_FALSE) {
1589+
riscv_reg_t tdata1_tests[] = {
1590+
set_field(tdata1, CSR_MCONTROL6_HIT0, 1),
1591+
set_field(tdata1, CSR_MCONTROL6_HIT1, 1),
1592+
set_field(tdata1, CSR_MCONTROL6_HIT0, 1) | field_value(CSR_MCONTROL6_HIT1, 1)
1593+
};
1594+
riscv_reg_t tdata1_test_rb;
1595+
for (uint64_t i = 0; i < ARRAY_SIZE(tdata1_tests); ++i) {
1596+
if (riscv_reg_set(target, GDB_REGNO_TDATA1, tdata1_tests[i]) != ERROR_OK)
1597+
return M6_HIT_ERROR;
1598+
if (riscv_reg_get(target, &tdata1_test_rb, GDB_REGNO_TDATA1) != ERROR_OK)
1599+
return M6_HIT_ERROR;
1600+
if (riscv_reg_set(target, GDB_REGNO_TDATA1, tdata1_test_rb & ~hit_mask) != ERROR_OK)
1601+
return M6_HIT_ERROR;
1602+
if (tdata1_test_rb == tdata1_tests[i])
1603+
return M6_NOT_HIT;
1604+
}
1605+
}
1606+
return M6_HIT_NOT_SUPPORTED;
1607+
}
1608+
1609+
15641610
/**
15651611
* Look at the trigger hit bits to find out which trigger is the reason we're
15661612
* halted. Sets *unique_id to the unique ID of that trigger. If *unique_id is
@@ -1576,6 +1622,7 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
15761622

15771623
// FIXME: Add hit bits support detection and caching
15781624
RISCV_INFO(r);
1625+
r->need_single_step = false;
15791626

15801627
riscv_reg_t tselect;
15811628
if (riscv_reg_get(target, &tselect, GDB_REGNO_TSELECT) != ERROR_OK)
@@ -1601,9 +1648,24 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
16011648
break;
16021649
case CSR_TDATA1_TYPE_MCONTROL:
16031650
hit_mask = CSR_MCONTROL_HIT;
1651+
r->need_single_step = true;
16041652
break;
16051653
case CSR_TDATA1_TYPE_MCONTROL6:
1606-
hit_mask = CSR_MCONTROL6_HIT0 | CSR_MCONTROL6_HIT1;
1654+
riscv_reg_t tinfo, tinfo_version;
1655+
int result = riscv_reg_get(target, &tinfo, GDB_REGNO_TINFO);
1656+
if (result == ERROR_OK) {
1657+
tinfo_version = get_field(tinfo, CSR_TINFO_VERSION);
1658+
if (tinfo_version == CSR_TINFO_VERSION_0)
1659+
r->need_single_step = true;
1660+
}
1661+
if (result != ERROR_OK || tinfo_version == CSR_TINFO_VERSION_1) {
1662+
mctrl6hitstatus hits_status = check_mcontrol6_hit_status(target,
1663+
tdata1, hit_mask);
1664+
if (hits_status == M6_HIT_ERROR)
1665+
return ERROR_FAIL;
1666+
if (hits_status == M6_HIT_BEFORE || hits_status == M6_HIT_NOT_SUPPORTED)
1667+
r->need_single_step = true;
1668+
}
16071669
break;
16081670
case CSR_TDATA1_TYPE_ICOUNT:
16091671
hit_mask = CSR_ICOUNT_HIT;
@@ -2553,10 +2615,19 @@ static int resume_prep(struct target *target, int current,
25532615
if (handle_breakpoints) {
25542616
/* To be able to run off a trigger, we perform a step operation and then
25552617
* 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;
2618+
* pending breakpoints so we can safely perform the step.
2619+
*
2620+
* Two cases where single step is needed before resuming:
2621+
* 1. ebreak used in software breakpoint;
2622+
* 2. a trigger that is taken just before the instruction that triggered it is retired.
2623+
*/
2624+
if (target->debug_reason == DBG_REASON_BREAKPOINT
2625+
|| (target->debug_reason == DBG_REASON_WATCHPOINT
2626+
&& r->need_single_step)) {
2627+
if (old_or_new_riscv_step_impl(target, current, address, handle_breakpoints,
2628+
false /* callbacks are not called */) != ERROR_OK)
2629+
return ERROR_FAIL;
2630+
}
25602631
}
25612632

25622633
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 need_single_step;
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)