target/cortex_m: workaround Cortex-M7 erratum 3092511
[openocd.git] / src / rtos / hwthread.c
blob937d01b874e5eeed87a706c12317a2e4bd192984
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <helper/time_support.h>
8 #include <jtag/jtag.h>
9 #include "target/target.h"
10 #include "target/register.h"
11 #include <target/smp.h>
12 #include "rtos.h"
13 #include "helper/log.h"
14 #include "helper/types.h"
15 #include "server/gdb_server.h"
17 static bool hwthread_detect_rtos(struct target *target);
18 static int hwthread_create(struct target *target);
19 static int hwthread_update_threads(struct rtos *rtos);
20 static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
21 uint32_t reg_num, struct rtos_reg *rtos_reg);
22 static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
23 struct rtos_reg **reg_list, int *num_regs);
24 static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
25 static int hwthread_smp_init(struct target *target);
26 static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value);
27 static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
28 uint32_t size, uint8_t *buffer);
29 static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
30 uint32_t size, const uint8_t *buffer);
32 #define HW_THREAD_NAME_STR_SIZE (32)
34 static inline threadid_t threadid_from_target(const struct target *target)
36 return target->coreid + 1;
39 const struct rtos_type hwthread_rtos = {
40 .name = "hwthread",
41 .detect_rtos = hwthread_detect_rtos,
42 .create = hwthread_create,
43 .update_threads = hwthread_update_threads,
44 .get_thread_reg_list = hwthread_get_thread_reg_list,
45 .get_thread_reg = hwthread_get_thread_reg,
46 .get_symbol_list_to_lookup = hwthread_get_symbol_list_to_lookup,
47 .smp_init = hwthread_smp_init,
48 .set_reg = hwthread_set_reg,
49 .read_buffer = hwthread_read_buffer,
50 .write_buffer = hwthread_write_buffer,
53 struct hwthread_params {
54 int dummy_param;
57 static int hwthread_fill_thread(struct rtos *rtos, struct target *curr, int thread_num)
59 char tmp_str[HW_THREAD_NAME_STR_SIZE];
60 threadid_t tid = threadid_from_target(curr);
62 memset(tmp_str, 0, HW_THREAD_NAME_STR_SIZE);
64 /* thread-id is the core-id of this core inside the SMP group plus 1 */
65 rtos->thread_details[thread_num].threadid = tid;
66 /* create the thread name */
67 rtos->thread_details[thread_num].exists = true;
68 rtos->thread_details[thread_num].thread_name_str = strdup(target_name(curr));
69 snprintf(tmp_str, HW_THREAD_NAME_STR_SIZE-1, "state: %s", debug_reason_name(curr));
70 rtos->thread_details[thread_num].extra_info_str = strdup(tmp_str);
72 return ERROR_OK;
75 static int hwthread_update_threads(struct rtos *rtos)
77 int threads_found = 0;
78 int thread_list_size = 0;
79 struct target_list *head;
80 struct target *target;
81 int64_t current_thread = 0;
82 int64_t current_threadid = rtos->current_threadid; /* thread selected by GDB */
83 enum target_debug_reason current_reason = DBG_REASON_UNDEFINED;
85 if (!rtos)
86 return -1;
88 target = rtos->target;
90 /* wipe out previous thread details if any */
91 rtos_free_threadlist(rtos);
93 /* determine the number of "threads" */
94 if (target->smp) {
95 foreach_smp_target(head, target->smp_targets) {
96 struct target *curr = head->target;
98 if (!target_was_examined(curr))
99 continue;
101 ++thread_list_size;
103 } else
104 thread_list_size = 1;
106 /* restore the threadid which is currently selected by GDB
107 * because rtos_free_threadlist() wipes out it
108 * (GDB thread id is 1-based indexing) */
109 if (current_threadid <= thread_list_size)
110 rtos->current_threadid = current_threadid;
111 else
112 LOG_WARNING("SMP node change, disconnect GDB from core/thread %" PRId64,
113 current_threadid);
115 /* create space for new thread details */
116 rtos->thread_details = malloc(sizeof(struct thread_detail) * thread_list_size);
118 if (target->smp) {
119 /* loop over all threads */
120 foreach_smp_target(head, target->smp_targets) {
121 struct target *curr = head->target;
123 if (!target_was_examined(curr))
124 continue;
126 threadid_t tid = threadid_from_target(curr);
128 hwthread_fill_thread(rtos, curr, threads_found);
130 /* find an interesting thread to set as current */
131 switch (current_reason) {
132 case DBG_REASON_UNDEFINED:
133 current_reason = curr->debug_reason;
134 current_thread = tid;
135 break;
136 case DBG_REASON_SINGLESTEP:
137 /* single-step can only be overridden by itself */
138 if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
139 if (tid == rtos->current_threadid)
140 current_thread = tid;
142 break;
143 case DBG_REASON_BREAKPOINT:
144 /* single-step overrides breakpoint */
145 if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
146 current_reason = curr->debug_reason;
147 current_thread = tid;
148 } else
149 /* multiple breakpoints, prefer gdbs' threadid */
150 if (curr->debug_reason == DBG_REASON_BREAKPOINT) {
151 if (tid == rtos->current_threadid)
152 current_thread = tid;
154 break;
155 case DBG_REASON_WATCHPOINT:
156 /* breakpoint and single-step override watchpoint */
157 if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
158 curr->debug_reason == DBG_REASON_BREAKPOINT) {
159 current_reason = curr->debug_reason;
160 current_thread = tid;
162 break;
163 case DBG_REASON_DBGRQ:
164 /* all other reasons override debug-request */
165 if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
166 curr->debug_reason == DBG_REASON_WATCHPOINT ||
167 curr->debug_reason == DBG_REASON_BREAKPOINT) {
168 current_reason = curr->debug_reason;
169 current_thread = tid;
170 } else
171 if (curr->debug_reason == DBG_REASON_DBGRQ) {
172 if (tid == rtos->current_threadid)
173 current_thread = tid;
176 break;
178 default:
179 break;
182 threads_found++;
184 } else {
185 hwthread_fill_thread(rtos, target, threads_found);
186 current_thread = threadid_from_target(target);
187 threads_found++;
190 rtos->thread_count = threads_found;
192 /* we found an interesting thread, set it as current */
193 if (current_thread != 0)
194 rtos->current_thread = current_thread;
195 else if (rtos->current_threadid != 0)
196 rtos->current_thread = rtos->current_threadid;
197 else
198 rtos->current_thread = threadid_from_target(target);
200 LOG_DEBUG("%s current_thread=%i", __func__, (int)rtos->current_thread);
201 return 0;
204 static int hwthread_smp_init(struct target *target)
206 return hwthread_update_threads(target->rtos);
209 static struct target *hwthread_find_thread(struct target *target, int64_t thread_id)
211 /* Find the thread with that thread_id */
212 if (!target)
213 return NULL;
214 if (target->smp) {
215 struct target_list *head;
216 foreach_smp_target(head, target->smp_targets) {
217 if (thread_id == threadid_from_target(head->target))
218 return head->target;
220 } else if (thread_id == threadid_from_target(target)) {
221 return target;
223 return NULL;
226 static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
227 struct rtos_reg **rtos_reg_list, int *rtos_reg_list_size)
229 if (!rtos)
230 return ERROR_FAIL;
232 struct target *target = rtos->target;
234 struct target *curr = hwthread_find_thread(target, thread_id);
235 if (!curr)
236 return ERROR_FAIL;
238 if (!target_was_examined(curr))
239 return ERROR_FAIL;
241 int reg_list_size;
242 struct reg **reg_list;
243 int retval = target_get_gdb_reg_list(curr, &reg_list, &reg_list_size,
244 REG_CLASS_GENERAL);
245 if (retval != ERROR_OK)
246 return retval;
248 int j = 0;
249 for (int i = 0; i < reg_list_size; i++) {
250 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
251 continue;
252 j++;
254 *rtos_reg_list_size = j;
255 *rtos_reg_list = calloc(*rtos_reg_list_size, sizeof(struct rtos_reg));
256 if (!*rtos_reg_list) {
257 free(reg_list);
258 return ERROR_FAIL;
261 j = 0;
262 for (int i = 0; i < reg_list_size; i++) {
263 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
264 continue;
265 if (!reg_list[i]->valid) {
266 retval = reg_list[i]->type->get(reg_list[i]);
267 if (retval != ERROR_OK) {
268 LOG_ERROR("Couldn't get register %s.", reg_list[i]->name);
269 free(reg_list);
270 free(*rtos_reg_list);
271 return retval;
274 (*rtos_reg_list)[j].number = reg_list[i]->number;
275 (*rtos_reg_list)[j].size = reg_list[i]->size;
276 memcpy((*rtos_reg_list)[j].value, reg_list[i]->value,
277 DIV_ROUND_UP(reg_list[i]->size, 8));
278 j++;
280 free(reg_list);
282 return ERROR_OK;
285 static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
286 uint32_t reg_num, struct rtos_reg *rtos_reg)
288 if (!rtos)
289 return ERROR_FAIL;
291 struct target *target = rtos->target;
293 struct target *curr = hwthread_find_thread(target, thread_id);
294 if (!curr) {
295 LOG_ERROR("Couldn't find RTOS thread for id %" PRId64 ".", thread_id);
296 return ERROR_FAIL;
299 if (!target_was_examined(curr)) {
300 LOG_ERROR("Target %d hasn't been examined yet.", curr->coreid);
301 return ERROR_FAIL;
304 struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
305 if (!reg) {
306 LOG_ERROR("Couldn't find register %" PRIu32 " in thread %" PRId64 ".", reg_num,
307 thread_id);
308 return ERROR_FAIL;
311 if (reg->type->get(reg) != ERROR_OK)
312 return ERROR_FAIL;
314 rtos_reg->number = reg->number;
315 rtos_reg->size = reg->size;
316 unsigned bytes = (reg->size + 7) / 8;
317 assert(bytes <= sizeof(rtos_reg->value));
318 memcpy(rtos_reg->value, reg->value, bytes);
320 return ERROR_OK;
323 static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value)
325 if (!rtos)
326 return ERROR_FAIL;
328 struct target *target = rtos->target;
330 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
331 if (!curr)
332 return ERROR_FAIL;
334 struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
335 if (!reg)
336 return ERROR_FAIL;
338 return reg->type->set(reg, reg_value);
341 static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
343 /* return an empty list, we don't have any symbols to look up */
344 *symbol_list = calloc(1, sizeof(struct symbol_table_elem));
345 (*symbol_list)[0].symbol_name = NULL;
346 return 0;
349 static int hwthread_target_for_threadid(struct connection *connection, int64_t thread_id, struct target **p_target)
351 struct target *target = get_target_from_connection(connection);
353 struct target *curr = hwthread_find_thread(target, thread_id);
354 if (!curr)
355 return ERROR_FAIL;
357 *p_target = curr;
359 return ERROR_OK;
362 static bool hwthread_detect_rtos(struct target *target)
364 /* always return 0, avoid auto-detection */
365 return false;
368 static int hwthread_thread_packet(struct connection *connection, const char *packet, int packet_size)
370 struct target *target = get_target_from_connection(connection);
372 struct target *curr = NULL;
373 int64_t current_threadid;
375 if (packet[0] == 'H' && packet[1] == 'g') {
376 sscanf(packet, "Hg%16" SCNx64, &current_threadid);
378 if (current_threadid > 0) {
379 if (hwthread_target_for_threadid(connection, current_threadid, &curr) != ERROR_OK) {
380 LOG_ERROR("hwthread: cannot find thread id %"PRId64, current_threadid);
381 gdb_put_packet(connection, "E01", 3);
382 return ERROR_FAIL;
384 target->rtos->current_thread = current_threadid;
385 } else
386 if (current_threadid == 0 || current_threadid == -1)
387 target->rtos->current_thread = threadid_from_target(target);
389 target->rtos->current_threadid = current_threadid;
391 gdb_put_packet(connection, "OK", 2);
392 return ERROR_OK;
395 return rtos_thread_packet(connection, packet, packet_size);
398 static int hwthread_create(struct target *target)
400 LOG_INFO("Hardware thread awareness created");
402 target->rtos->rtos_specific_params = NULL;
403 target->rtos->current_thread = 0;
404 target->rtos->thread_details = NULL;
405 target->rtos->gdb_target_for_threadid = hwthread_target_for_threadid;
406 target->rtos->gdb_thread_packet = hwthread_thread_packet;
407 return 0;
410 static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
411 uint32_t size, uint8_t *buffer)
413 if (!rtos)
414 return ERROR_FAIL;
416 struct target *target = rtos->target;
418 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
419 if (!curr)
420 return ERROR_FAIL;
422 return target_read_buffer(curr, address, size, buffer);
425 static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
426 uint32_t size, const uint8_t *buffer)
428 if (!rtos)
429 return ERROR_FAIL;
431 struct target *target = rtos->target;
433 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
434 if (!curr)
435 return ERROR_FAIL;
437 return target_write_buffer(curr, address, size, buffer);