@@ -706,7 +706,7 @@ static int init_target(struct command_context *cmd_ctx,
706706 LOG_DEBUG ("init" );
707707 riscv_info_t * generic_info = (riscv_info_t * ) target -> arch_info ;
708708
709- riscv_info_init (generic_info );
709+ riscv_info_init (target , generic_info );
710710 generic_info -> get_register = & riscv013_get_register ;
711711 generic_info -> set_register = & riscv013_set_register ;
712712 generic_info -> select_current_hart = & riscv013_select_current_hart ;
@@ -832,6 +832,9 @@ static int add_trigger(struct target *target, struct trigger *trigger)
832832
833833 uint64_t tdata1_rb ;
834834 for (int hartid = 0 ; hartid < riscv_count_harts (target ); ++ hartid ) {
835+ if (!riscv_hart_enabled (target , hartid ))
836+ continue ;
837+
835838 riscv_set_current_hartid (target , hartid );
836839
837840 if (hartid > 0 ) {
@@ -918,6 +921,9 @@ static int remove_trigger(struct target *target, struct trigger *trigger)
918921 }
919922 LOG_DEBUG ("Stop using resource %d for bp %d" , i , trigger -> unique_id );
920923 for (int hartid = 0 ; hartid < riscv_count_harts (target ); ++ hartid ) {
924+ if (!riscv_hart_enabled (target , hartid ))
925+ continue ;
926+
921927 riscv_set_current_hartid (target , hartid );
922928 register_write_direct (target , GDB_REGNO_TSELECT , i );
923929 register_write_direct (target , GDB_REGNO_TDATA1 , 0 );
@@ -1123,17 +1129,23 @@ static int examine(struct target *target)
11231129
11241130 /* Before doing anything else we must first enumerate the harts. */
11251131 RISCV_INFO (r );
1126- if (riscv_rtos_enabled (target )) {
1127- for (int i = 0 ; i < RISCV_MAX_HARTS ; ++ i ) {
1128- riscv_set_current_hartid (target , i );
1129- uint32_t s = dmi_read (target , DMI_DMSTATUS );
1130- if (get_field (s , DMI_DMSTATUS_ANYNONEXISTENT ))
1131- break ;
1132- r -> hart_count = i + 1 ;
1132+ int original_coreid = target -> coreid ;
1133+ for (int i = 0 ; i < RISCV_MAX_HARTS ; ++ i ) {
1134+ /* Fake being a non-RTOS targeted to this core so we can see if
1135+ * it exists. This avoids the assertion in
1136+ * riscv_set_current_hartid() that ensures non-RTOS targets
1137+ * don't touch the harts they're not assigned to. */
1138+ target -> coreid = i ;
1139+ r -> hart_count = i + 1 ;
1140+ riscv_set_current_hartid (target , i );
1141+
1142+ uint32_t s = dmi_read (target , DMI_DMSTATUS );
1143+ if (get_field (s , DMI_DMSTATUS_ANYNONEXISTENT )) {
1144+ r -> hart_count -- ;
1145+ break ;
11331146 }
1134- } else {
1135- r -> hart_count = 1 ;
11361147 }
1148+ target -> coreid = original_coreid ;
11371149
11381150 LOG_DEBUG ("Enumerated %d harts" , r -> hart_count );
11391151
@@ -1143,6 +1155,9 @@ static int examine(struct target *target)
11431155 /* Find the address of the program buffer, which must be done without
11441156 * knowing anything about the target. */
11451157 for (int i = 0 ; i < riscv_count_harts (target ); ++ i ) {
1158+ if (!riscv_hart_enabled (target , i ))
1159+ continue ;
1160+
11461161 riscv_set_current_hartid (target , i );
11471162
11481163 /* Without knowing anything else we can at least mess with the
@@ -1225,6 +1240,9 @@ static int examine(struct target *target)
12251240
12261241 /* Then we check the number of triggers availiable to each hart. */
12271242 for (int i = 0 ; i < riscv_count_harts (target ); ++ i ) {
1243+ if (!riscv_hart_enabled (target , i ))
1244+ continue ;
1245+
12281246 for (uint32_t t = 0 ; t < RISCV_MAX_TRIGGERS ; ++ t ) {
12291247 riscv_set_current_hartid (target , i );
12301248
@@ -1239,6 +1257,7 @@ static int examine(struct target *target)
12391257
12401258 /* Resumes all the harts, so the debugger can later pause them. */
12411259 riscv_resume_all_harts (target );
1260+ target -> state = TARGET_RUNNING ;
12421261 target_set_examined (target );
12431262
12441263 if (target -> rtos ) {
@@ -1333,9 +1352,6 @@ static int read_memory(struct target *target, target_addr_t address,
13331352 size , address );
13341353
13351354 select_dmi (target );
1336- /* There was a bug in the memory system and only accesses from hart 0 actually
1337- * worked correctly. This should be obselete now. -palmer */
1338- riscv_set_current_hartid (target , 0 );
13391355
13401356 /* This program uses two temporary registers. A word of data and the
13411357 * associated address are stored at some location in memory. The
@@ -1531,9 +1547,6 @@ static int write_memory(struct target *target, target_addr_t address,
15311547 LOG_DEBUG ("writing %d words of %d bytes to 0x%08lx" , count , size , (long )address );
15321548
15331549 select_dmi (target );
1534- /* There was a bug in the memory system and only accesses from hart 0 actually
1535- * worked correctly. This should be obselete now. -palmer */
1536- riscv_set_current_hartid (target , 0 );
15371550
15381551 /* This program uses two temporary registers. A word of data and the
15391552 * associated address are stored at some location in memory. The
0 commit comments