From ef3e61bebc53f619f42bb06cfd194cd547cceb69 Mon Sep 17 00:00:00 2001 From: Tim Newsome Date: Tue, 5 Oct 2021 10:03:53 -0700 Subject: [PATCH] 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 --- src/rtos/FreeRTOS.c | 23 +++++++++++++++++------ src/rtos/hwthread.c | 23 +++++++++++++---------- src/rtos/rtos.c | 29 +++++++++++++++++++++++------ src/rtos/rtos.h | 8 ++++++-- 4 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/rtos/FreeRTOS.c b/src/rtos/FreeRTOS.c index c3b5ed8d7..ee6bdb322 100644 --- a/src/rtos/FreeRTOS.c +++ b/src/rtos/FreeRTOS.c @@ -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[]); @@ -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, }; @@ -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. */ @@ -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, ®); + *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) diff --git a/src/rtos/hwthread.c b/src/rtos/hwthread.c index d147aadeb..a8f1b594e 100644 --- a/src/rtos/hwthread.c +++ b/src/rtos/hwthread.c @@ -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[]); @@ -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, @@ -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; @@ -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; } diff --git a/src/rtos/rtos.c b/src/rtos/rtos.c index ffae89269..e0b285b93 100644 --- a/src/rtos/rtos.c +++ b/src/rtos/rtos.c @@ -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, ®_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, ®_size, ®_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(®_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, diff --git a/src/rtos/rtos.h b/src/rtos/rtos.h index bf0cdc5a4..641bdf2bf 100644 --- a/src/rtos/rtos.h +++ b/src/rtos/rtos.h @@ -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 { @@ -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);