Dynamically allocate memory for RTOS registers. (#647)

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 <tim@sifive.com>
This commit is contained in:
Tim Newsome 2021-10-05 10:03:53 -07:00 committed by GitHub
parent f139080376
commit ef3e61bebc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 24 deletions

View File

@ -210,8 +210,8 @@ static int freertos_create(struct target *target);
static int freertos_update_threads(struct rtos *rtos); static int freertos_update_threads(struct rtos *rtos);
static int freertos_get_thread_reg_list(struct rtos *rtos, threadid_t thread_id, static int freertos_get_thread_reg_list(struct rtos *rtos, threadid_t thread_id,
struct rtos_reg **reg_list, int *num_regs); struct rtos_reg **reg_list, int *num_regs);
static int freertos_get_thread_reg(struct rtos *rtos, threadid_t thread_id, static int freertos_get_thread_reg_value(struct rtos *rtos, threadid_t thread_id,
uint32_t reg_num, struct rtos_reg *reg); 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_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[]); static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
@ -222,7 +222,7 @@ struct rtos_type freertos_rtos = {
.create = freertos_create, .create = freertos_create,
.update_threads = freertos_update_threads, .update_threads = freertos_update_threads,
.get_thread_reg_list = freertos_get_thread_reg_list, .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, .set_reg = freertos_set_reg,
.get_symbol_list_to_lookup = freertos_get_symbol_list_to_lookup, .get_symbol_list_to_lookup = freertos_get_symbol_list_to_lookup,
}; };
@ -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); 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, static int freertos_get_thread_reg_value(struct rtos *rtos, threadid_t thread_id,
uint32_t reg_num, struct rtos_reg *reg) uint32_t reg_num, uint32_t *size, uint8_t **value)
{ {
LOG_DEBUG("reg_num=%d", reg_num); LOG_DEBUG("reg_num=%d", reg_num);
/* Let the caller read registers directly for the current thread. */ /* Let the caller read registers directly for the current thread. */
@ -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) if (freertos_get_stacking_info(rtos, thread_id, &stacking_info, &stack_ptr) != ERROR_OK)
return ERROR_FAIL; 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) static int freertos_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value)

View File

@ -31,8 +31,8 @@
static bool hwthread_detect_rtos(struct target *target); static bool hwthread_detect_rtos(struct target *target);
static int hwthread_create(struct target *target); static int hwthread_create(struct target *target);
static int hwthread_update_threads(struct rtos *rtos); static int hwthread_update_threads(struct rtos *rtos);
static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id, static int hwthread_get_thread_reg_value(struct rtos *rtos, int64_t thread_id,
uint32_t reg_num, struct rtos_reg *rtos_reg); uint32_t reg_num, uint32_t *size, uint8_t **value);
static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
struct rtos_reg **reg_list, int *num_regs); struct rtos_reg **reg_list, int *num_regs);
static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]); static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
@ -59,7 +59,7 @@ const struct rtos_type hwthread_rtos = {
.create = hwthread_create, .create = hwthread_create,
.update_threads = hwthread_update_threads, .update_threads = hwthread_update_threads,
.get_thread_reg_list = hwthread_get_thread_reg_list, .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, .get_symbol_list_to_lookup = hwthread_get_symbol_list_to_lookup,
.smp_init = hwthread_smp_init, .smp_init = hwthread_smp_init,
.set_reg = hwthread_set_reg, .set_reg = hwthread_set_reg,
@ -282,8 +282,8 @@ static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
return ERROR_OK; return ERROR_OK;
} }
static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id, static int hwthread_get_thread_reg_value(struct rtos *rtos, int64_t thread_id,
uint32_t reg_num, struct rtos_reg *rtos_reg) uint32_t reg_num, uint32_t *size, uint8_t **value)
{ {
if (!rtos) if (!rtos)
return ERROR_FAIL; return ERROR_FAIL;
@ -311,11 +311,14 @@ static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
if (reg->type->get(reg) != ERROR_OK) if (reg->type->get(reg) != ERROR_OK)
return ERROR_FAIL; return ERROR_FAIL;
rtos_reg->number = reg->number; *size = reg->size;
rtos_reg->size = reg->size; unsigned bytes = DIV_ROUND_UP(reg->size, 8);
unsigned bytes = (reg->size + 7) / 8; *value = malloc(bytes);
assert(bytes <= sizeof(rtos_reg->value)); if (!*value) {
memcpy(rtos_reg->value, reg->value, bytes); LOG_ERROR("Failed to allocate memory for %d-bit register.", reg->size);
return ERROR_FAIL;
}
memcpy(*value, reg->value, bytes);
return ERROR_OK; return ERROR_OK;
} }

View File

@ -510,16 +510,33 @@ int rtos_get_gdb_reg(struct connection *connection, int reg_num)
target->rtos->current_thread); target->rtos->current_thread);
int retval; int retval;
if (target->rtos->type->get_thread_reg) { if (target->rtos->type->get_thread_reg_value) {
reg_list = calloc(1, sizeof(*reg_list)); uint32_t reg_size;
reg_list[0].number = reg_num; uint8_t *reg_value;
num_regs = 1; retval = target->rtos->type->get_thread_reg_value(target->rtos,
retval = target->rtos->type->get_thread_reg(target->rtos, current_threadid, reg_num, &reg_size, &reg_value);
current_threadid, reg_num, &reg_list[0]);
if (retval != ERROR_OK) { if (retval != ERROR_OK) {
LOG_ERROR("RTOS: failed to get register %d", reg_num); LOG_ERROR("RTOS: failed to get register %d", reg_num);
return retval; 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 { } else {
retval = target->rtos->type->get_thread_reg_list(target->rtos, retval = target->rtos->type->get_thread_reg_list(target->rtos,
current_threadid, current_threadid,

View File

@ -68,6 +68,8 @@ struct rtos_reg {
uint32_t number; uint32_t number;
uint32_t size; uint32_t size;
uint8_t value[16]; 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 { struct rtos_type {
@ -79,8 +81,10 @@ struct rtos_type {
/** Return a list of general registers, with their values filled out. */ /** Return a list of general registers, with their values filled out. */
int (*get_thread_reg_list)(struct rtos *rtos, threadid_t thread_id, int (*get_thread_reg_list)(struct rtos *rtos, threadid_t thread_id,
struct rtos_reg **reg_list, int *num_regs); struct rtos_reg **reg_list, int *num_regs);
int (*get_thread_reg)(struct rtos *rtos, threadid_t thread_id, /** Return the size and value of the specified reg_num. The value is
uint32_t reg_num, struct rtos_reg *reg); * 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 (*get_symbol_list_to_lookup)(struct symbol_table_elem *symbol_list[]);
int (*clean)(struct target *target); int (*clean)(struct target *target);
char * (*ps_command)(struct target *target); char * (*ps_command)(struct target *target);