Skip to content

Commit

Permalink
Merge pull request #115 from RinHizakura/master
Browse files Browse the repository at this point in the history
Update mini-gdbstub for on_interrupt feature
  • Loading branch information
jserv authored Feb 24, 2023
2 parents 5b772f3 + 9065858 commit 587313e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ void rv_debug(riscv_t *rv)
}

rv->breakpoint_map = breakpoint_map_new();
rv->is_interrupted = false;

if (!gdbstub_run(&rv->gdbstub, (void *) rv))
return;
Expand Down
19 changes: 18 additions & 1 deletion src/gdbstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,26 @@ static void rv_write_mem(void *args, size_t addr, size_t len, void *val)
rv->io.mem_write_b(rv, addr + i, *((uint8_t *) val + i));
}

static inline bool rv_is_interrupt(riscv_t *rv)
{
return __atomic_load_n(&rv->is_interrupted, __ATOMIC_RELAXED);
}

static gdb_action_t rv_cont(void *args)
{
riscv_t *rv = (riscv_t *) args;
const uint32_t cycles_per_step = 1;

for (; !rv_has_halted(rv);) {
for (; !rv_has_halted(rv) && !rv_is_interrupt(rv);) {
if (breakpoint_map_find(rv->breakpoint_map, rv_get_pc(rv)))
break;

rv_step(rv, cycles_per_step);
}

/* Clear the interrupt if it's pending */
__atomic_store_n(&rv->is_interrupted, false, __ATOMIC_RELAXED);

return ACT_RESUME;
}

Expand Down Expand Up @@ -97,6 +105,14 @@ static bool rv_del_bp(void *args, size_t addr, bp_type_t type)
return true;
}

static void rv_on_interrupt(void *args)
{
riscv_t *rv = (riscv_t *) args;

/* Notify the emulator to break out the for loop in rv_cont */
__atomic_store_n(&rv->is_interrupted, true, __ATOMIC_RELAXED);
}

const struct target_ops gdbstub_ops = {
.read_reg = rv_read_reg,
.write_reg = rv_write_reg,
Expand All @@ -106,4 +122,5 @@ const struct target_ops gdbstub_ops = {
.stepi = rv_stepi,
.set_bp = rv_set_bp,
.del_bp = rv_del_bp,
.on_interrupt = rv_on_interrupt,
};
2 changes: 1 addition & 1 deletion src/mini-gdbstub
4 changes: 4 additions & 0 deletions src/riscv_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ struct riscv_internal {

/* GDB instruction breakpoint */
breakpoint_map_t breakpoint_map;

/* The flag to notify interrupt from GDB client: it should
* be accessed by atomic operation when starting the GDBSTUB. */
bool is_interrupted;
#endif

#if RV32_HAS(EXT_F)
Expand Down

0 comments on commit 587313e

Please sign in to comment.