Skip to content

Commit 427b9ee

Browse files
target/riscv/riscv.c: support auto-selection of software breakpoint size
Add auto-selection of breakpoint size for RISC-V targets. The new length depends on C extension. If the C extension is used, the auto-selected length is 2 bytes. Otherwise, the default length is 4 bytes. Change-Id: Ie3473514beace15b76714aa6d5441122cd3262aa Signed-off-by: Kulyatskaya Alexandra <a.kulyatskaya@syntacore.com>
1 parent 6f84e90 commit 427b9ee

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/target/riscv/riscv.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,16 @@ static int riscv_add_breakpoint(struct target *target, struct breakpoint *breakp
16091609
LOG_TARGET_DEBUG(target, "@0x%" TARGET_PRIxADDR, breakpoint->address);
16101610
assert(breakpoint);
16111611
if (breakpoint->type == BKPT_SOFT) {
1612+
if (breakpoint->length == 0) {
1613+
breakpoint->length = riscv_supports_extension(target, 'c') ? 2 : 4;
1614+
uint8_t *instr_buffer = (uint8_t *)realloc (breakpoint->orig_instr, breakpoint->length);
1615+
if (!instr_buffer) {
1616+
LOG_ERROR("Fail to realloc memory with original instruction");
1617+
return ERROR_FAIL;
1618+
}
1619+
breakpoint->orig_instr = instr_buffer;
1620+
}
1621+
16121622
/** @todo check RVC for size/alignment */
16131623
if (!(breakpoint->length == 4 || breakpoint->length == 2)) {
16141624
LOG_TARGET_ERROR(target, "Invalid breakpoint length %d", breakpoint->length);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
proc is_c_extension {} {
2+
set misa [ocdtlb_rsp_get [ocdtlb_run_expect "reg misa"]]
3+
set c_ext_bit 2
4+
return [expr {($misa >> $c_ext_bit) & 1}]
5+
}
6+
7+
proc check_bp {ENTRY_ADDR length} {
8+
ocdtlb_run_expect "bp $ENTRY_ADDR 0"
9+
set real_bp_memory [ocdtlb_run_expect "read_memory $ENTRY_ADDR 8 4"]
10+
ocdtlb_run_expect "rbp $ENTRY_ADDR"
11+
12+
ocdtlb_run_expect "bp $ENTRY_ADDR $length"
13+
set expected_bp_memory [ocdtlb_run_expect "read_memory $ENTRY_ADDR 8 4"]
14+
ocdtlb_run_expect "rbp $ENTRY_ADDR"
15+
16+
ocdtlb_expect_equal $real_bp_memory $expected_bp_memory
17+
}
18+
19+
proc run_test {} {
20+
set default_bin_prefix [tool_state_get default_bin_prefix]
21+
ocd_compile_executable "basic.S" "$default_bin_prefix.exe"
22+
23+
# created by compile executable
24+
set ENTRY_ADDR [tool_state_get_program_symbol _entry]
25+
26+
ocd_connect
27+
28+
ocdtlb_run_expect "targets 0"
29+
ocdtlb_run_expect "halt"
30+
31+
if {[is_c_extension]} {
32+
set length 2
33+
} else {
34+
set length 4
35+
}
36+
37+
check_bp $ENTRY_ADDR $length
38+
39+
return [ocd_test_pass]
40+
}
41+
42+
return [run_test]

0 commit comments

Comments
 (0)