target/riscv: dynamic allocate memory for hawindow

Change-Id: Id2f1a2568a39eec0a9dd4fe0f155619b11f9d6ba
Signed-off-by: Mark Zhuang <mark.zhuang@spacemit.com>
This commit is contained in:
Mark Zhuang 2023-07-13 21:21:32 +08:00
parent 04d8cfc48c
commit d5425c253c
1 changed files with 19 additions and 10 deletions

View File

@ -4448,9 +4448,9 @@ static int select_prepped_harts(struct target *target)
assert(dm->hart_count); assert(dm->hart_count);
unsigned hawindow_count = (dm->hart_count + 31) / 32; unsigned hawindow_count = (dm->hart_count + 31) / 32;
uint32_t hawindow[hawindow_count]; uint32_t *hawindow = calloc(hawindow_count, sizeof(uint32_t));
if (!hawindow)
memset(hawindow, 0, sizeof(uint32_t) * hawindow_count); return ERROR_FAIL;
target_list_t *entry; target_list_t *entry;
unsigned total_selected = 0; unsigned total_selected = 0;
@ -4472,22 +4472,31 @@ static int select_prepped_harts(struct target *target)
if (total_selected == 0) { if (total_selected == 0) {
LOG_TARGET_ERROR(target, "No harts were prepped!"); LOG_TARGET_ERROR(target, "No harts were prepped!");
free(hawindow);
return ERROR_FAIL; return ERROR_FAIL;
} else if (total_selected == 1) { } else if (total_selected == 1) {
/* Don't use hasel if we only need to talk to one hart. */ /* Don't use hasel if we only need to talk to one hart. */
free(hawindow);
return dm013_select_hart(target, selected_index); return dm013_select_hart(target, selected_index);
} }
if (dm013_select_hart(target, HART_INDEX_MULTIPLE) != ERROR_OK) if (dm013_select_hart(target, HART_INDEX_MULTIPLE) != ERROR_OK) {
free(hawindow);
return ERROR_FAIL; return ERROR_FAIL;
for (unsigned i = 0; i < hawindow_count; i++) {
if (dmi_write(target, DM_HAWINDOWSEL, i) != ERROR_OK)
return ERROR_FAIL;
if (dmi_write(target, DM_HAWINDOW, hawindow[i]) != ERROR_OK)
return ERROR_FAIL;
} }
for (unsigned i = 0; i < hawindow_count; i++) {
if (dmi_write(target, DM_HAWINDOWSEL, i) != ERROR_OK) {
free(hawindow);
return ERROR_FAIL;
}
if (dmi_write(target, DM_HAWINDOW, hawindow[i]) != ERROR_OK) {
free(hawindow);
return ERROR_FAIL;
}
}
free(hawindow);
return ERROR_OK; return ERROR_OK;
} }