Skip to content

Commit

Permalink
Dynamically allocate memory for RTOS registers. (#647)
Browse files Browse the repository at this point in the history
This makes things work on RISC-V cores with large vector registers
(which can be up to kilobytes in size).

Change-Id: Ie53cb43a88e2a475f695cd5c1e28605569926817
Signed-off-by: Tim Newsome <[email protected]>
  • Loading branch information
timsifive authored Oct 5, 2021
1 parent f139080 commit ef3e61b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
23 changes: 17 additions & 6 deletions src/rtos/FreeRTOS.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ static int freertos_create(struct target *target);
static int freertos_update_threads(struct rtos *rtos);
static int freertos_get_thread_reg_list(struct rtos *rtos, threadid_t thread_id,
struct rtos_reg **reg_list, int *num_regs);
static int freertos_get_thread_reg(struct rtos *rtos, threadid_t thread_id,
uint32_t reg_num, struct rtos_reg *reg);
static int freertos_get_thread_reg_value(struct rtos *rtos, threadid_t thread_id,
uint32_t reg_num, uint32_t *size, uint8_t **value);
static int freertos_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value);
static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);

Expand All @@ -222,7 +222,7 @@ struct rtos_type freertos_rtos = {
.create = freertos_create,
.update_threads = freertos_update_threads,
.get_thread_reg_list = freertos_get_thread_reg_list,
.get_thread_reg = freertos_get_thread_reg,
.get_thread_reg_value = freertos_get_thread_reg_value,
.set_reg = freertos_set_reg,
.get_symbol_list_to_lookup = freertos_get_symbol_list_to_lookup,
};
Expand Down Expand Up @@ -769,8 +769,8 @@ static int freertos_get_thread_reg_list(struct rtos *rtos, threadid_t thread_id,
return rtos_generic_stack_read(rtos->target, stacking_info, stack_ptr, reg_list, num_regs);
}

static int freertos_get_thread_reg(struct rtos *rtos, threadid_t thread_id,
uint32_t reg_num, struct rtos_reg *reg)
static int freertos_get_thread_reg_value(struct rtos *rtos, threadid_t thread_id,
uint32_t reg_num, uint32_t *size, uint8_t **value)
{
LOG_DEBUG("reg_num=%d", reg_num);
/* Let the caller read registers directly for the current thread. */
Expand All @@ -782,7 +782,18 @@ static int freertos_get_thread_reg(struct rtos *rtos, threadid_t thread_id,
if (freertos_get_stacking_info(rtos, thread_id, &stacking_info, &stack_ptr) != ERROR_OK)
return ERROR_FAIL;

return rtos_generic_stack_read_reg(rtos->target, stacking_info, stack_ptr, reg_num, reg);
struct rtos_reg reg;
reg.number = reg_num;
int result = rtos_generic_stack_read_reg(rtos->target, stacking_info,
stack_ptr, reg_num, &reg);
*size = reg.size;
*value = malloc(DIV_ROUND_UP(reg.size, 8));
if (!*value) {
LOG_ERROR("Failed to allocate memory for %d-bit register.", reg.size);
return ERROR_FAIL;
}
memcpy(*value, reg.value, DIV_ROUND_UP(reg.size, 8));
return result;
}

static int freertos_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value)
Expand Down
23 changes: 13 additions & 10 deletions src/rtos/hwthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
static bool hwthread_detect_rtos(struct target *target);
static int hwthread_create(struct target *target);
static int hwthread_update_threads(struct rtos *rtos);
static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
uint32_t reg_num, struct rtos_reg *rtos_reg);
static int hwthread_get_thread_reg_value(struct rtos *rtos, int64_t thread_id,
uint32_t reg_num, uint32_t *size, uint8_t **value);
static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
struct rtos_reg **reg_list, int *num_regs);
static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
Expand All @@ -59,7 +59,7 @@ const struct rtos_type hwthread_rtos = {
.create = hwthread_create,
.update_threads = hwthread_update_threads,
.get_thread_reg_list = hwthread_get_thread_reg_list,
.get_thread_reg = hwthread_get_thread_reg,
.get_thread_reg_value = hwthread_get_thread_reg_value,
.get_symbol_list_to_lookup = hwthread_get_symbol_list_to_lookup,
.smp_init = hwthread_smp_init,
.set_reg = hwthread_set_reg,
Expand Down Expand Up @@ -282,8 +282,8 @@ static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
return ERROR_OK;
}

static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
uint32_t reg_num, struct rtos_reg *rtos_reg)
static int hwthread_get_thread_reg_value(struct rtos *rtos, int64_t thread_id,
uint32_t reg_num, uint32_t *size, uint8_t **value)
{
if (!rtos)
return ERROR_FAIL;
Expand Down Expand Up @@ -311,11 +311,14 @@ static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
if (reg->type->get(reg) != ERROR_OK)
return ERROR_FAIL;

rtos_reg->number = reg->number;
rtos_reg->size = reg->size;
unsigned bytes = (reg->size + 7) / 8;
assert(bytes <= sizeof(rtos_reg->value));
memcpy(rtos_reg->value, reg->value, bytes);
*size = reg->size;
unsigned bytes = DIV_ROUND_UP(reg->size, 8);
*value = malloc(bytes);
if (!*value) {
LOG_ERROR("Failed to allocate memory for %d-bit register.", reg->size);
return ERROR_FAIL;
}
memcpy(*value, reg->value, bytes);

return ERROR_OK;
}
Expand Down
29 changes: 23 additions & 6 deletions src/rtos/rtos.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,33 @@ int rtos_get_gdb_reg(struct connection *connection, int reg_num)
target->rtos->current_thread);

int retval;
if (target->rtos->type->get_thread_reg) {
reg_list = calloc(1, sizeof(*reg_list));
reg_list[0].number = reg_num;
num_regs = 1;
retval = target->rtos->type->get_thread_reg(target->rtos,
current_threadid, reg_num, &reg_list[0]);
if (target->rtos->type->get_thread_reg_value) {
uint32_t reg_size;
uint8_t *reg_value;
retval = target->rtos->type->get_thread_reg_value(target->rtos,
current_threadid, reg_num, &reg_size, &reg_value);
if (retval != ERROR_OK) {
LOG_ERROR("RTOS: failed to get register %d", reg_num);
return retval;
}

/* Create a reg_list with one register that can
* accommodate the full size of the one we just got the
* value for. To do that we allocate extra space off the
* end of the struct, relying on the fact that
* rtos_reg.value is the last element in the struct. */
reg_list = calloc(1, sizeof(*reg_list) + DIV_ROUND_UP(reg_size, 8));
if (!reg_list) {
free(reg_value);
LOG_ERROR("Failed to allocated reg_list for %d-byte register.",
reg_size);
return ERROR_FAIL;
}
reg_list[0].number = reg_num;
reg_list[0].size = reg_size;
memcpy(&reg_list[0].value, reg_value, DIV_ROUND_UP(reg_size, 8));
free(reg_value);
num_regs = 1;
} else {
retval = target->rtos->type->get_thread_reg_list(target->rtos,
current_threadid,
Expand Down
8 changes: 6 additions & 2 deletions src/rtos/rtos.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ struct rtos_reg {
uint32_t number;
uint32_t size;
uint8_t value[16];
/* WARNING: rtos_get_gdb_reg() relies on the fact that value is the last
* element of this struct. Any new fields should be added *before* value. */
};

struct rtos_type {
Expand All @@ -79,8 +81,10 @@ struct rtos_type {
/** Return a list of general registers, with their values filled out. */
int (*get_thread_reg_list)(struct rtos *rtos, threadid_t thread_id,
struct rtos_reg **reg_list, int *num_regs);
int (*get_thread_reg)(struct rtos *rtos, threadid_t thread_id,
uint32_t reg_num, struct rtos_reg *reg);
/** Return the size and value of the specified reg_num. The value is
* allocated by the callee and freed by the caller. */
int (*get_thread_reg_value)(struct rtos *rtos, threadid_t thread_id,
uint32_t reg_num, uint32_t *size, uint8_t **value);
int (*get_symbol_list_to_lookup)(struct symbol_table_elem *symbol_list[]);
int (*clean)(struct target *target);
char * (*ps_command)(struct target *target);
Expand Down

0 comments on commit ef3e61b

Please sign in to comment.