hwthread: Add register validity check in get_thread_reg_list
[openocd.git] / src / rtos / hwthread.c
blob1540168c30abd830406e8a6481d6132aee1e3cd4
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/target_type.h"
11 #include "target/register.h"
12 #include <target/smp.h>
13 #include "rtos.h"
14 #include "helper/log.h"
15 #include "helper/types.h"
16 #include "server/gdb_server.h"
18 static bool hwthread_detect_rtos(struct target *target);
19 static int hwthread_create(struct target *target);
20 static int hwthread_update_threads(struct rtos *rtos);
21 static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
22 uint32_t reg_num, struct rtos_reg *rtos_reg);
23 static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
24 struct rtos_reg **reg_list, int *num_regs);
25 static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
26 static int hwthread_smp_init(struct target *target);
27 static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value);
28 static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
29 uint32_t size, uint8_t *buffer);
30 static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
31 uint32_t size, const uint8_t *buffer);
33 #define HW_THREAD_NAME_STR_SIZE (32)
35 extern int rtos_thread_packet(struct connection *connection, const char *packet, int packet_size);
37 static inline threadid_t threadid_from_target(const struct target *target)
39 return target->coreid + 1;
42 const struct rtos_type hwthread_rtos = {
43 .name = "hwthread",
44 .detect_rtos = hwthread_detect_rtos,
45 .create = hwthread_create,
46 .update_threads = hwthread_update_threads,
47 .get_thread_reg_list = hwthread_get_thread_reg_list,
48 .get_thread_reg = hwthread_get_thread_reg,
49 .get_symbol_list_to_lookup = hwthread_get_symbol_list_to_lookup,
50 .smp_init = hwthread_smp_init,
51 .set_reg = hwthread_set_reg,
52 .read_buffer = hwthread_read_buffer,
53 .write_buffer = hwthread_write_buffer,
56 struct hwthread_params {
57 int dummy_param;
60 static int hwthread_fill_thread(struct rtos *rtos, struct target *curr, int thread_num)
62 char tmp_str[HW_THREAD_NAME_STR_SIZE];
63 threadid_t tid = threadid_from_target(curr);
65 memset(tmp_str, 0, HW_THREAD_NAME_STR_SIZE);
67 /* thread-id is the core-id of this core inside the SMP group plus 1 */
68 rtos->thread_details[thread_num].threadid = tid;
69 /* create the thread name */
70 rtos->thread_details[thread_num].exists = true;
71 rtos->thread_details[thread_num].thread_name_str = strdup(target_name(curr));
72 snprintf(tmp_str, HW_THREAD_NAME_STR_SIZE-1, "state: %s", debug_reason_name(curr));
73 rtos->thread_details[thread_num].extra_info_str = strdup(tmp_str);
75 return ERROR_OK;
78 static int hwthread_update_threads(struct rtos *rtos)
80 int threads_found = 0;
81 int thread_list_size = 0;
82 struct target_list *head;
83 struct target *target;
84 int64_t current_thread = 0;
85 enum target_debug_reason current_reason = DBG_REASON_UNDEFINED;
87 if (!rtos)
88 return -1;
90 target = rtos->target;
92 /* wipe out previous thread details if any */
93 rtos_free_threadlist(rtos);
95 /* determine the number of "threads" */
96 if (target->smp) {
97 foreach_smp_target(head, target->smp_targets) {
98 struct target *curr = head->target;
100 if (!target_was_examined(curr))
101 continue;
103 ++thread_list_size;
105 } else
106 thread_list_size = 1;
108 /* create space for new thread details */
109 rtos->thread_details = malloc(sizeof(struct thread_detail) * thread_list_size);
111 if (target->smp) {
112 /* loop over all threads */
113 foreach_smp_target(head, target->smp_targets) {
114 struct target *curr = head->target;
116 if (!target_was_examined(curr))
117 continue;
119 threadid_t tid = threadid_from_target(curr);
121 hwthread_fill_thread(rtos, curr, threads_found);
123 /* find an interesting thread to set as current */
124 switch (current_reason) {
125 case DBG_REASON_UNDEFINED:
126 current_reason = curr->debug_reason;
127 current_thread = tid;
128 break;
129 case DBG_REASON_SINGLESTEP:
130 /* single-step can only be overridden by itself */
131 if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
132 if (tid == rtos->current_threadid)
133 current_thread = tid;
135 break;
136 case DBG_REASON_BREAKPOINT:
137 /* single-step overrides breakpoint */
138 if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
139 current_reason = curr->debug_reason;
140 current_thread = tid;
141 } else
142 /* multiple breakpoints, prefer gdbs' threadid */
143 if (curr->debug_reason == DBG_REASON_BREAKPOINT) {
144 if (tid == rtos->current_threadid)
145 current_thread = tid;
147 break;
148 case DBG_REASON_WATCHPOINT:
149 /* breakpoint and single-step override watchpoint */
150 if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
151 curr->debug_reason == DBG_REASON_BREAKPOINT) {
152 current_reason = curr->debug_reason;
153 current_thread = tid;
155 break;
156 case DBG_REASON_DBGRQ:
157 /* all other reasons override debug-request */
158 if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
159 curr->debug_reason == DBG_REASON_WATCHPOINT ||
160 curr->debug_reason == DBG_REASON_BREAKPOINT) {
161 current_reason = curr->debug_reason;
162 current_thread = tid;
163 } else
164 if (curr->debug_reason == DBG_REASON_DBGRQ) {
165 if (tid == rtos->current_threadid)
166 current_thread = tid;
169 break;
171 default:
172 break;
175 threads_found++;
177 } else {
178 hwthread_fill_thread(rtos, target, threads_found);
179 current_thread = threadid_from_target(target);
180 threads_found++;
183 rtos->thread_count = threads_found;
185 /* we found an interesting thread, set it as current */
186 if (current_thread != 0)
187 rtos->current_thread = current_thread;
188 else if (rtos->current_threadid != 0)
189 rtos->current_thread = rtos->current_threadid;
190 else
191 rtos->current_thread = threadid_from_target(target);
193 LOG_DEBUG("%s current_thread=%i", __func__, (int)rtos->current_thread);
194 return 0;
197 static int hwthread_smp_init(struct target *target)
199 return hwthread_update_threads(target->rtos);
202 static struct target *hwthread_find_thread(struct target *target, int64_t thread_id)
204 /* Find the thread with that thread_id */
205 if (!target)
206 return NULL;
207 if (target->smp) {
208 struct target_list *head;
209 foreach_smp_target(head, target->smp_targets) {
210 if (thread_id == threadid_from_target(head->target))
211 return head->target;
213 } else if (thread_id == threadid_from_target(target)) {
214 return target;
216 return NULL;
219 static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
220 struct rtos_reg **rtos_reg_list, int *rtos_reg_list_size)
222 if (!rtos)
223 return ERROR_FAIL;
225 struct target *target = rtos->target;
227 struct target *curr = hwthread_find_thread(target, thread_id);
228 if (!curr)
229 return ERROR_FAIL;
231 if (!target_was_examined(curr))
232 return ERROR_FAIL;
234 int reg_list_size;
235 struct reg **reg_list;
236 int retval = target_get_gdb_reg_list(curr, &reg_list, &reg_list_size,
237 REG_CLASS_GENERAL);
238 if (retval != ERROR_OK)
239 return retval;
241 int j = 0;
242 for (int i = 0; i < reg_list_size; i++) {
243 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
244 continue;
245 j++;
247 *rtos_reg_list_size = j;
248 *rtos_reg_list = calloc(*rtos_reg_list_size, sizeof(struct rtos_reg));
249 if (!*rtos_reg_list) {
250 free(reg_list);
251 return ERROR_FAIL;
254 j = 0;
255 for (int i = 0; i < reg_list_size; i++) {
256 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
257 continue;
258 if (!reg_list[i]->valid) {
259 retval = reg_list[i]->type->get(reg_list[i]);
260 if (retval != ERROR_OK) {
261 LOG_ERROR("Couldn't get register %s.", reg_list[i]->name);
262 free(reg_list);
263 free(*rtos_reg_list);
264 return retval;
267 (*rtos_reg_list)[j].number = reg_list[i]->number;
268 (*rtos_reg_list)[j].size = reg_list[i]->size;
269 memcpy((*rtos_reg_list)[j].value, reg_list[i]->value,
270 DIV_ROUND_UP(reg_list[i]->size, 8));
271 j++;
273 free(reg_list);
275 return ERROR_OK;
278 static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
279 uint32_t reg_num, struct rtos_reg *rtos_reg)
281 if (!rtos)
282 return ERROR_FAIL;
284 struct target *target = rtos->target;
286 struct target *curr = hwthread_find_thread(target, thread_id);
287 if (!curr) {
288 LOG_ERROR("Couldn't find RTOS thread for id %" PRId64 ".", thread_id);
289 return ERROR_FAIL;
292 if (!target_was_examined(curr)) {
293 LOG_ERROR("Target %d hasn't been examined yet.", curr->coreid);
294 return ERROR_FAIL;
297 struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
298 if (!reg) {
299 LOG_ERROR("Couldn't find register %" PRIu32 " in thread %" PRId64 ".", reg_num,
300 thread_id);
301 return ERROR_FAIL;
304 if (reg->type->get(reg) != ERROR_OK)
305 return ERROR_FAIL;
307 rtos_reg->number = reg->number;
308 rtos_reg->size = reg->size;
309 unsigned bytes = (reg->size + 7) / 8;
310 assert(bytes <= sizeof(rtos_reg->value));
311 memcpy(rtos_reg->value, reg->value, bytes);
313 return ERROR_OK;
316 static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value)
318 if (!rtos)
319 return ERROR_FAIL;
321 struct target *target = rtos->target;
323 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
324 if (!curr)
325 return ERROR_FAIL;
327 struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
328 if (!reg)
329 return ERROR_FAIL;
331 return reg->type->set(reg, reg_value);
334 static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
336 /* return an empty list, we don't have any symbols to look up */
337 *symbol_list = calloc(1, sizeof(struct symbol_table_elem));
338 (*symbol_list)[0].symbol_name = NULL;
339 return 0;
342 static int hwthread_target_for_threadid(struct connection *connection, int64_t thread_id, struct target **p_target)
344 struct target *target = get_target_from_connection(connection);
346 struct target *curr = hwthread_find_thread(target, thread_id);
347 if (!curr)
348 return ERROR_FAIL;
350 *p_target = curr;
352 return ERROR_OK;
355 static bool hwthread_detect_rtos(struct target *target)
357 /* always return 0, avoid auto-detection */
358 return false;
361 static int hwthread_thread_packet(struct connection *connection, const char *packet, int packet_size)
363 struct target *target = get_target_from_connection(connection);
365 struct target *curr = NULL;
366 int64_t current_threadid;
368 if (packet[0] == 'H' && packet[1] == 'g') {
369 sscanf(packet, "Hg%16" SCNx64, &current_threadid);
371 if (current_threadid > 0) {
372 if (hwthread_target_for_threadid(connection, current_threadid, &curr) != ERROR_OK) {
373 LOG_ERROR("hwthread: cannot find thread id %"PRId64, current_threadid);
374 gdb_put_packet(connection, "E01", 3);
375 return ERROR_FAIL;
377 target->rtos->current_thread = current_threadid;
378 } else
379 if (current_threadid == 0 || current_threadid == -1)
380 target->rtos->current_thread = threadid_from_target(target);
382 target->rtos->current_threadid = current_threadid;
384 gdb_put_packet(connection, "OK", 2);
385 return ERROR_OK;
388 return rtos_thread_packet(connection, packet, packet_size);
391 static int hwthread_create(struct target *target)
393 LOG_INFO("Hardware thread awareness created");
395 target->rtos->rtos_specific_params = NULL;
396 target->rtos->current_thread = 0;
397 target->rtos->thread_details = NULL;
398 target->rtos->gdb_target_for_threadid = hwthread_target_for_threadid;
399 target->rtos->gdb_thread_packet = hwthread_thread_packet;
400 return 0;
403 static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
404 uint32_t size, uint8_t *buffer)
406 if (!rtos)
407 return ERROR_FAIL;
409 struct target *target = rtos->target;
411 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
412 if (!curr)
413 return ERROR_FAIL;
415 return target_read_buffer(curr, address, size, buffer);
418 static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
419 uint32_t size, const uint8_t *buffer)
421 if (!rtos)
422 return ERROR_FAIL;
424 struct target *target = rtos->target;
426 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
427 if (!curr)
428 return ERROR_FAIL;
430 return target_write_buffer(curr, address, size, buffer);