Skip to content

Commit

Permalink
Merge pull request #964 from aap-sc/aap-sc/dbgbuf_prg_exec_status
Browse files Browse the repository at this point in the history
Introduce execution status for program buffer and abstract commands
  • Loading branch information
JanMatCodasip committed Jan 4, 2024
2 parents 62758f2 + aded275 commit 02901ff
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 128 deletions.
34 changes: 21 additions & 13 deletions src/target/riscv/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "helper/log.h"

#include "asm.h"
#include "debug_defines.h"
#include "encoding.h"

/* Program interface. */
Expand All @@ -23,19 +24,19 @@ int riscv_program_init(struct riscv_program *p, struct target *target)
for (size_t i = 0; i < RISCV_REGISTER_COUNT; ++i)
p->writes_xreg[i] = 0;

for (size_t i = 0; i < RISCV_MAX_DEBUG_BUFFER_SIZE; ++i)
p->debug_buffer[i] = -1;
for (size_t i = 0; i < RISCV_MAX_PROGBUF_SIZE; ++i)
p->progbuf[i] = -1;

p->execution_result = RISCV_PROGBUF_EXEC_RESULT_NOT_EXECUTED;
return ERROR_OK;
}

int riscv_program_write(struct riscv_program *program)
{
for (unsigned i = 0; i < program->instruction_count; ++i) {
LOG_TARGET_DEBUG(program->target, "debug_buffer[%02x] = DASM(0x%08x)",
i, program->debug_buffer[i]);
if (riscv_write_debug_buffer(program->target, i,
program->debug_buffer[i]) != ERROR_OK)
LOG_TARGET_DEBUG(program->target, "progbuf[%02x] = DASM(0x%08x)",
i, program->progbuf[i]);
if (riscv_write_progbuf(program->target, i, program->progbuf[i]) != ERROR_OK)
return ERROR_FAIL;
}
return ERROR_OK;
Expand All @@ -46,6 +47,7 @@ int riscv_program_exec(struct riscv_program *p, struct target *t)
{
keep_alive();

p->execution_result = RISCV_PROGBUF_EXEC_RESULT_UNKNOWN;
riscv_reg_t saved_registers[GDB_REGNO_XPR31 + 1];
for (size_t i = GDB_REGNO_ZERO + 1; i <= GDB_REGNO_XPR31; ++i) {
if (p->writes_xreg[i]) {
Expand All @@ -58,19 +60,25 @@ int riscv_program_exec(struct riscv_program *p, struct target *t)

if (riscv_program_ebreak(p) != ERROR_OK) {
LOG_TARGET_ERROR(t, "Unable to insert ebreak into program buffer");
for (size_t i = 0; i < riscv_debug_buffer_size(p->target); ++i)
for (size_t i = 0; i < riscv_progbuf_size(p->target); ++i)
LOG_TARGET_ERROR(t, "ram[%02x]: DASM(0x%08" PRIx32 ") [0x%08" PRIx32 "]",
(int)i, p->debug_buffer[i], p->debug_buffer[i]);
(int)i, p->progbuf[i], p->progbuf[i]);
return ERROR_FAIL;
}

if (riscv_program_write(p) != ERROR_OK)
return ERROR_FAIL;

if (riscv_execute_debug_buffer(t) != ERROR_OK) {
uint32_t cmderr;
if (riscv_execute_progbuf(t, &cmderr) != ERROR_OK) {
p->execution_result = (cmderr == DM_ABSTRACTCS_CMDERR_EXCEPTION)
? RISCV_PROGBUF_EXEC_RESULT_EXCEPTION
: RISCV_PROGBUF_EXEC_RESULT_UNKNOWN_ERROR;
/* TODO: what happens if we fail here, but need to restore registers? */
LOG_TARGET_DEBUG(t, "Unable to execute program %p", p);
return ERROR_FAIL;
}
p->execution_result = RISCV_PROGBUF_EXEC_RESULT_SUCCESS;

for (size_t i = GDB_REGNO_ZERO; i <= GDB_REGNO_XPR31; ++i)
if (p->writes_xreg[i])
Expand Down Expand Up @@ -191,7 +199,7 @@ int riscv_program_ebreak(struct riscv_program *p)
{
struct target *target = p->target;
RISCV_INFO(r);
if (p->instruction_count == riscv_debug_buffer_size(p->target) &&
if (p->instruction_count == riscv_progbuf_size(p->target) &&
r->impebreak) {
return ERROR_OK;
}
Expand All @@ -205,14 +213,14 @@ int riscv_program_addi(struct riscv_program *p, enum gdb_regno d, enum gdb_regno

int riscv_program_insert(struct riscv_program *p, riscv_insn_t i)
{
if (p->instruction_count >= riscv_debug_buffer_size(p->target)) {
if (p->instruction_count >= riscv_progbuf_size(p->target)) {
LOG_TARGET_ERROR(p->target, "Unable to insert program into progbuf, "
"capacity would be exceeded (progbufsize=%d).",
(int)riscv_debug_buffer_size(p->target));
(int)riscv_progbuf_size(p->target));
return ERROR_FAIL;
}

p->debug_buffer[p->instruction_count] = i;
p->progbuf[p->instruction_count] = i;
p->instruction_count++;
return ERROR_OK;
}
16 changes: 14 additions & 2 deletions src/target/riscv/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@

#include "riscv.h"

#define RISCV_MAX_DEBUG_BUFFER_SIZE 32
#define RISCV_MAX_PROGBUF_SIZE 32
#define RISCV_REGISTER_COUNT 32
#define RISCV_DSCRATCH_COUNT 2

typedef enum {
RISCV_PROGBUF_EXEC_RESULT_NOT_EXECUTED,
RISCV_PROGBUF_EXEC_RESULT_UNKNOWN,
RISCV_PROGBUF_EXEC_RESULT_EXCEPTION,
RISCV_PROGBUF_EXEC_RESULT_UNKNOWN_ERROR,
RISCV_PROGBUF_EXEC_RESULT_SUCCESS
} riscv_progbuf_exec_result_t;

/* The various RISC-V debug specifications all revolve around setting up
* program buffers and executing them on the target. This structure contains a
* single program, which can then be executed on targets. */
struct riscv_program {
struct target *target;

uint32_t debug_buffer[RISCV_MAX_DEBUG_BUFFER_SIZE];
uint32_t progbuf[RISCV_MAX_PROGBUF_SIZE];

/* Number of 32-bit instructions in the program. */
size_t instruction_count;
Expand All @@ -26,6 +34,10 @@ struct riscv_program {

/* XLEN on the target. */
int target_xlen;

/* execution result of the program */
/* TODO: remove this field. We should make it a parameter to riscv_program_exec */
riscv_progbuf_exec_result_t execution_result;
};

/* Initializes a program with the header. */
Expand Down
Loading

0 comments on commit 02901ff

Please sign in to comment.