|
| 1 | +#include <assert.h> |
| 2 | +#include "mini-gdbstub/include/gdbstub.h" |
| 3 | +#include "riscv_private.h" |
| 4 | + |
| 5 | +static size_t rv_read_reg(void *args, int regno) |
| 6 | +{ |
| 7 | + struct riscv_t *rv = (struct riscv_t *) args; |
| 8 | + |
| 9 | + if (regno < 32) { |
| 10 | + return rv_get_reg(rv, regno); |
| 11 | + } |
| 12 | + |
| 13 | + if (regno == 32) { |
| 14 | + return rv_get_pc(rv); |
| 15 | + } |
| 16 | + |
| 17 | + return -1; |
| 18 | +} |
| 19 | + |
| 20 | +static void rv_read_mem(void *args, size_t addr, size_t len, void *val) |
| 21 | +{ |
| 22 | + struct riscv_t *rv = (struct riscv_t *) args; |
| 23 | + |
| 24 | + for (size_t i = 0; i < len; i++) |
| 25 | + *((uint8_t *) val + i) = rv->io.mem_read_b(rv, addr); |
| 26 | +} |
| 27 | + |
| 28 | +static gdb_action_t rv_cont(void *args) |
| 29 | +{ |
| 30 | + struct riscv_t *rv = (struct riscv_t *) args; |
| 31 | + const uint32_t cycles_per_step = 1; |
| 32 | + |
| 33 | + for (; !rv_has_halted(rv);) { |
| 34 | + if (rv->breakpoint_specified && (rv_get_pc(rv) == rv->breakpoint_addr)) { |
| 35 | + break; |
| 36 | + } |
| 37 | + rv_step(rv, cycles_per_step); |
| 38 | + } |
| 39 | + |
| 40 | + return ACT_RESUME; |
| 41 | +} |
| 42 | + |
| 43 | +static gdb_action_t rv_stepi(void *args) |
| 44 | +{ |
| 45 | + struct riscv_t *rv = (struct riscv_t *) args; |
| 46 | + rv_step(rv, 1); |
| 47 | + return ACT_RESUME; |
| 48 | +} |
| 49 | + |
| 50 | +static bool rv_set_bp(void *args, size_t addr, bp_type_t type) |
| 51 | +{ |
| 52 | + struct riscv_t *rv = (struct riscv_t *) args; |
| 53 | + if (type != BP_SOFTWARE || rv->breakpoint_specified) |
| 54 | + return false; |
| 55 | + |
| 56 | + rv->breakpoint_specified = true; |
| 57 | + rv->breakpoint_addr = addr; |
| 58 | + return true;; |
| 59 | +} |
| 60 | + |
| 61 | +static bool rv_rm_bp(void *args, size_t addr, bp_type_t type) |
| 62 | +{ |
| 63 | + struct riscv_t *rv = (struct riscv_t *) args; |
| 64 | + if (type != BP_SOFTWARE) |
| 65 | + return false; |
| 66 | + // It's fine when there's no matched breakpoint, just doing nothing |
| 67 | + if(!rv->breakpoint_specified || addr != rv->breakpoint_addr) |
| 68 | + return true; |
| 69 | + |
| 70 | + rv->breakpoint_specified = false; |
| 71 | + rv->breakpoint_addr = 0; |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +struct target_ops rv_ops = { |
| 76 | + .read_reg = rv_read_reg, |
| 77 | + .read_mem = rv_read_mem, |
| 78 | + .cont = rv_cont, |
| 79 | + .stepi = rv_stepi, |
| 80 | + .set_bp = rv_set_bp, |
| 81 | + .rm_bp = rv_rm_bp, |
| 82 | +}; |
0 commit comments