Skip to content

Commit 788908f

Browse files
Factor out checking if harts should be used
Rather than having a bunch of "if rtos" stuff, I now just check "if hart_enabled". This makes some code paths cleaner, all of which were buggy in the non-RTOS multi-hart mode.
1 parent 9f4cac5 commit 788908f

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

src/target/riscv/riscv-013.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,9 @@ static int examine(struct target *target)
11431143
/* Find the address of the program buffer, which must be done without
11441144
* knowing anything about the target. */
11451145
for (int i = 0; i < riscv_count_harts(target); ++i) {
1146+
if (!riscv_hart_enabled(target, i))
1147+
continue;
1148+
11461149
riscv_set_current_hartid(target, i);
11471150

11481151
/* Without knowing anything else we can at least mess with the
@@ -1213,6 +1216,9 @@ static int examine(struct target *target)
12131216

12141217
/* Then we check the number of triggers availiable to each hart. */
12151218
for (int i = 0; i < riscv_count_harts(target); ++i) {
1219+
if (!riscv_hart_enabled(target, i))
1220+
continue;
1221+
12161222
for (uint32_t t = 0; t < RISCV_MAX_TRIGGERS; ++t) {
12171223
riscv_set_current_hartid(target, i);
12181224

src/target/riscv/riscv.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -890,11 +890,11 @@ void riscv_info_init(struct target *target, riscv_info_t *r)
890890

891891
int riscv_halt_all_harts(struct target *target)
892892
{
893-
if (riscv_rtos_enabled(target)) {
894-
for (int i = 0; i < riscv_count_harts(target); ++i)
895-
riscv_halt_one_hart(target, i);
896-
} else {
897-
riscv_halt_one_hart(target, riscv_current_hartid(target));
893+
for (int i = 0; i < riscv_count_harts(target); ++i) {
894+
if (!riscv_hart_enabled(target, i))
895+
continue;
896+
897+
riscv_halt_one_hart(target, i);
898898
}
899899

900900
return ERROR_OK;
@@ -916,11 +916,11 @@ int riscv_halt_one_hart(struct target *target, int hartid)
916916

917917
int riscv_resume_all_harts(struct target *target)
918918
{
919-
if (riscv_rtos_enabled(target)) {
920-
for (int i = 0; i < riscv_count_harts(target); ++i)
921-
riscv_resume_one_hart(target, i);
922-
} else {
923-
riscv_resume_one_hart(target, riscv_current_hartid(target));
919+
for (int i = 0; i < riscv_count_harts(target); ++i) {
920+
if (!riscv_hart_enabled(target, i))
921+
continue;
922+
923+
riscv_resume_one_hart(target, i);
924924
}
925925

926926
riscv_invalidate_register_cache(target);
@@ -944,11 +944,11 @@ int riscv_resume_one_hart(struct target *target, int hartid)
944944

945945
int riscv_reset_all_harts(struct target *target)
946946
{
947-
if (riscv_rtos_enabled(target)) {
948-
for (int i = 0; i < riscv_count_harts(target); ++i)
949-
riscv_reset_one_hart(target, i);
950-
} else {
951-
riscv_reset_one_hart(target, riscv_current_hartid(target));
947+
for (int i = 0; i < riscv_count_harts(target); ++i) {
948+
if (!riscv_hart_enabled(target, i))
949+
continue;
950+
951+
riscv_reset_one_hart(target, i);
952952
}
953953

954954
riscv_invalidate_register_cache(target);
@@ -1018,10 +1018,9 @@ void riscv_set_current_hartid(struct target *target, int hartid)
10181018

10191019
int previous_hartid = riscv_current_hartid(target);
10201020
r->current_hartid = hartid;
1021-
assert(riscv_rtos_enabled(target) || target->coreid == hartid);
1021+
assert(riscv_hart_enabled(target, hartid));
10221022
LOG_DEBUG("setting hartid to %d, was %d", hartid, previous_hartid);
1023-
if (riscv_rtos_enabled(target))
1024-
r->select_current_hart(target);
1023+
r->select_current_hart(target);
10251024

10261025
/* This might get called during init, in which case we shouldn't be
10271026
* setting up the register cache. */
@@ -1234,3 +1233,12 @@ int riscv_dmi_write_u64_bits(struct target *target)
12341233
RISCV_INFO(r);
12351234
return r->dmi_write_u64_bits(target);
12361235
}
1236+
1237+
bool riscv_hart_enabled(struct target *target, int hartid)
1238+
{
1239+
/* FIXME: Add a hart mask to the RTOS. */
1240+
if (riscv_rtos_enabled(target))
1241+
return hartid < riscv_count_harts(target);
1242+
1243+
return hartid == target->coreid;
1244+
}

src/target/riscv/riscv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,7 @@ int riscv_dmi_write_u64_bits(struct target *target);
215215
/* Invalidates the register cache. */
216216
void riscv_invalidate_register_cache(struct target *target);
217217

218+
/* Returns TRUE when a hart is enabled in this target. */
219+
bool riscv_hart_enabled(struct target *target, int hartid);
220+
218221
#endif

0 commit comments

Comments
 (0)