Skip to content

Commit dc4fe85

Browse files
cgsfvtimsifive
authored andcommitted
Old fixes from June (#311)
* Changed logging level * Added logging statement * Removed halt event when attaching to target * Extended some packet handling * Extended handling of rtos_hart_id and clearing of register cache * Extended execute_fence to handle all harts * Removing logging statement again * Updated according to review comments * Forgot to re-add the return statement * Was removing too much for the if statement to work * This needs to >= 3 now to handle both a fence and a fence.i
1 parent e54511f commit dc4fe85

File tree

4 files changed

+63
-36
lines changed

4 files changed

+63
-36
lines changed

src/rtos/riscv_debug.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ static int riscv_gdb_thread_packet(struct connection *connection, const char *pa
124124
return ERROR_OK;
125125
}
126126

127+
if (strcmp(packet, "qTStatus") == 0) {
128+
gdb_put_packet(connection, "T0", 2);
129+
return ERROR_OK;
130+
}
131+
127132
if (strcmp(packet, "qC") == 0) {
128133
char rep_str[32];
129134
snprintf(rep_str, 32, "QC%" PRIx64, rtos->current_threadid);
@@ -250,6 +255,7 @@ static int riscv_gdb_v_packet(struct connection *connection, const char *packet,
250255
if (strcmp(packet_stttrr, "vCont;c") == 0) {
251256
target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
252257
target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
258+
riscv_set_all_rtos_harts(target);
253259
riscv_openocd_resume(target, 1, 0, 0, 0);
254260
target->state = TARGET_RUNNING;
255261
gdb_set_frontend_state_running(connection);

src/target/riscv/batch.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ int riscv_batch_run(struct riscv_batch *batch)
5151

5252
keep_alive();
5353

54-
LOG_DEBUG("running a batch of %ld scans", (long)batch->used_scans);
5554
riscv_batch_add_nop(batch);
5655

5756
for (size_t i = 0; i < batch->used_scans; ++i) {
@@ -60,7 +59,6 @@ int riscv_batch_run(struct riscv_batch *batch)
6059
jtag_add_runtest(batch->idle_count, TAP_IDLE);
6160
}
6261

63-
LOG_DEBUG("executing queue");
6462
if (jtag_execute_queue() != ERROR_OK) {
6563
LOG_ERROR("Unable to execute JTAG queue");
6664
return ERROR_FAIL;
@@ -102,9 +100,6 @@ size_t riscv_batch_add_dmi_read(struct riscv_batch *batch, unsigned address)
102100
riscv_batch_add_nop(batch);
103101

104102
batch->read_keys[batch->read_keys_used] = batch->used_scans - 1;
105-
LOG_DEBUG("read key %u for batch 0x%p is %u (0x%p)",
106-
(unsigned) batch->read_keys_used, batch, (unsigned) (batch->used_scans - 1),
107-
batch->data_in + sizeof(uint64_t) * (batch->used_scans + 1));
108103
return batch->read_keys_used++;
109104
}
110105

@@ -135,7 +130,6 @@ void riscv_batch_add_nop(struct riscv_batch *batch)
135130
riscv_fill_dmi_nop_u64(batch->target, (char *)field->in_value);
136131
batch->last_scan = RISCV_SCAN_TYPE_NOP;
137132
batch->used_scans++;
138-
LOG_DEBUG(" added NOP with in_value=0x%p", field->in_value);
139133
}
140134

141135
void dump_field(const struct scan_field *field)

src/target/riscv/riscv-013.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,13 +1771,38 @@ static void write_to_buf(uint8_t *buffer, uint64_t value, unsigned size)
17711771

17721772
static int execute_fence(struct target *target)
17731773
{
1774-
struct riscv_program program;
1775-
riscv_program_init(&program, target);
1776-
riscv_program_fence(&program);
1777-
int result = riscv_program_exec(&program, target);
1778-
if (result != ERROR_OK)
1779-
LOG_ERROR("Unable to execute fence");
1780-
return result;
1774+
int old_hartid = riscv_current_hartid(target);
1775+
1776+
/* FIXME: For non-coherent systems we need to flush the caches right
1777+
* here, but there's no ISA-defined way of doing that. */
1778+
{
1779+
struct riscv_program program;
1780+
riscv_program_init(&program, target);
1781+
riscv_program_fence_i(&program);
1782+
riscv_program_fence(&program);
1783+
int result = riscv_program_exec(&program, target);
1784+
if (result != ERROR_OK)
1785+
LOG_DEBUG("Unable to execute pre-fence");
1786+
}
1787+
1788+
for (int i = 0; i < riscv_count_harts(target); ++i) {
1789+
if (!riscv_hart_enabled(target, i))
1790+
continue;
1791+
1792+
riscv_set_current_hartid(target, i);
1793+
1794+
struct riscv_program program;
1795+
riscv_program_init(&program, target);
1796+
riscv_program_fence_i(&program);
1797+
riscv_program_fence(&program);
1798+
int result = riscv_program_exec(&program, target);
1799+
if (result != ERROR_OK)
1800+
LOG_DEBUG("Unable to execute fence on hart %d", i);
1801+
}
1802+
1803+
riscv_set_current_hartid(target, old_hartid);
1804+
1805+
return ERROR_OK;
17811806
}
17821807

17831808
static void log_memory_access(target_addr_t address, uint64_t value,
@@ -3344,15 +3369,8 @@ static int maybe_execute_fence_i(struct target *target)
33443369
{
33453370
RISCV013_INFO(info);
33463371
RISCV_INFO(r);
3347-
if (info->progbufsize + r->impebreak >= 2) {
3348-
struct riscv_program program;
3349-
riscv_program_init(&program, target);
3350-
if (riscv_program_fence_i(&program) != ERROR_OK)
3351-
return ERROR_FAIL;
3352-
if (riscv_program_exec(&program, target) != ERROR_OK) {
3353-
LOG_ERROR("Failed to execute fence.i");
3354-
return ERROR_FAIL;
3355-
}
3372+
if (info->progbufsize + r->impebreak >= 3) {
3373+
return execute_fence(target);
33563374
}
33573375
return ERROR_OK;
33583376
}

src/target/riscv/riscv.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ static int riscv_init_target(struct command_context *cmd_ctx,
266266

267267
riscv_semihosting_init(target);
268268

269+
target->debug_reason = DBG_REASON_DBGRQ;
270+
269271
return ERROR_OK;
270272
}
271273

@@ -856,9 +858,11 @@ static int old_or_new_riscv_resume(
856858
static int riscv_select_current_hart(struct target *target)
857859
{
858860
RISCV_INFO(r);
859-
if (r->rtos_hartid != -1 && riscv_rtos_enabled(target))
861+
if (riscv_rtos_enabled(target)) {
862+
if (r->rtos_hartid == -1)
863+
r->rtos_hartid = target->rtos->current_threadid - 1;
860864
return riscv_set_current_hartid(target, r->rtos_hartid);
861-
else
865+
} else
862866
return riscv_set_current_hartid(target, target->coreid);
863867
}
864868

@@ -1196,8 +1200,12 @@ int riscv_openocd_halt(struct target *target)
11961200

11971201
register_cache_invalidate(target->reg_cache);
11981202
if (riscv_rtos_enabled(target)) {
1199-
target->rtos->current_threadid = r->rtos_hartid + 1;
1200-
target->rtos->current_thread = r->rtos_hartid + 1;
1203+
if (r->rtos_hartid != -1) {
1204+
LOG_DEBUG("halt requested on RTOS hartid %d", r->rtos_hartid);
1205+
target->rtos->current_threadid = r->rtos_hartid + 1;
1206+
target->rtos->current_thread = r->rtos_hartid + 1;
1207+
} else
1208+
LOG_DEBUG("halt requested, but no known RTOS hartid");
12011209
}
12021210

12031211
target->state = TARGET_HALTED;
@@ -1817,6 +1825,8 @@ int riscv_halt_all_harts(struct target *target)
18171825
riscv_halt_one_hart(target, i);
18181826
}
18191827

1828+
riscv_invalidate_register_cache(target);
1829+
18201830
return ERROR_OK;
18211831
}
18221832

@@ -1869,7 +1879,7 @@ int riscv_step_rtos_hart(struct target *target)
18691879
if (riscv_rtos_enabled(target)) {
18701880
hartid = r->rtos_hartid;
18711881
if (hartid == -1) {
1872-
LOG_USER("GDB has asked me to step \"any\" thread, so I'm stepping hart 0.");
1882+
LOG_DEBUG("GDB has asked me to step \"any\" thread, so I'm stepping hart 0.");
18731883
hartid = 0;
18741884
}
18751885
}
@@ -1942,15 +1952,6 @@ int riscv_set_current_hartid(struct target *target, int hartid)
19421952
if (!target_was_examined(target))
19431953
return ERROR_OK;
19441954

1945-
/* Avoid invalidating the register cache all the time. */
1946-
if (r->registers_initialized
1947-
&& (!riscv_rtos_enabled(target) || (previous_hartid == hartid))
1948-
&& target->reg_cache->reg_list[GDB_REGNO_ZERO].size == (unsigned)riscv_xlen(target)
1949-
&& (!riscv_rtos_enabled(target) || (r->rtos_hartid != -1))) {
1950-
return ERROR_OK;
1951-
} else
1952-
LOG_DEBUG("Initializing registers: xlen=%d", riscv_xlen(target));
1953-
19541955
riscv_invalidate_register_cache(target);
19551956
return ERROR_OK;
19561957
}
@@ -2031,7 +2032,15 @@ int riscv_get_register_on_hart(struct target *target, riscv_reg_t *value,
20312032
int hartid, enum gdb_regno regid)
20322033
{
20332034
RISCV_INFO(r);
2035+
2036+
if (hartid != riscv_current_hartid(target))
2037+
riscv_invalidate_register_cache(target);
2038+
20342039
int result = r->get_register(target, value, hartid, regid);
2040+
2041+
if (hartid != riscv_current_hartid(target))
2042+
riscv_invalidate_register_cache(target);
2043+
20352044
LOG_DEBUG("[%d] %s: %" PRIx64, hartid, gdb_regno_name(regid), *value);
20362045
return result;
20372046
}

0 commit comments

Comments
 (0)