1 /***************************************************************************
3 * This program is free software; you can redistribute it and/or modify *
4 * it under the terms of the GNU General Public License as published by *
5 * the Free Software Foundation; either version 2 of the License, or *
6 * (at your option) any later version. *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU General Public License for more details. *
13 * You should have received a copy of the GNU General Public License *
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
15 ***************************************************************************/
21 #include <helper/time_support.h>
22 #include <jtag/jtag.h>
23 #include "target/target.h"
24 #include "target/target_type.h"
25 #include "target/register.h"
26 #include <target/smp.h>
28 #include "helper/log.h"
29 #include "helper/types.h"
30 #include "server/gdb_server.h"
32 static bool hwthread_detect_rtos(struct target *target);
33 static int hwthread_create(struct target *target);
34 static int hwthread_update_threads(struct rtos *rtos);
35 static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
36 uint32_t reg_num, struct rtos_reg *rtos_reg);
37 static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
38 struct rtos_reg **reg_list, int *num_regs);
39 static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
40 static int hwthread_smp_init(struct target *target);
41 static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value);
42 static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
43 uint32_t size, uint8_t *buffer);
44 static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
45 uint32_t size, const uint8_t *buffer);
47 #define HW_THREAD_NAME_STR_SIZE (32)
49 extern int rtos_thread_packet(struct connection *connection, const char *packet, int packet_size);
51 static inline threadid_t threadid_from_target(const struct target *target)
53 return target->coreid + 1;
56 const struct rtos_type hwthread_rtos = {
58 .detect_rtos = hwthread_detect_rtos,
59 .create = hwthread_create,
60 .update_threads = hwthread_update_threads,
61 .get_thread_reg_list = hwthread_get_thread_reg_list,
62 .get_thread_reg = hwthread_get_thread_reg,
63 .get_symbol_list_to_lookup = hwthread_get_symbol_list_to_lookup,
64 .smp_init = hwthread_smp_init,
65 .set_reg = hwthread_set_reg,
66 .read_buffer = hwthread_read_buffer,
67 .write_buffer = hwthread_write_buffer,
70 struct hwthread_params {
74 static int hwthread_fill_thread(struct rtos *rtos, struct target *curr, int thread_num)
76 char tmp_str[HW_THREAD_NAME_STR_SIZE];
77 threadid_t tid = threadid_from_target(curr);
79 memset(tmp_str, 0, HW_THREAD_NAME_STR_SIZE);
81 /* thread-id is the core-id of this core inside the SMP group plus 1 */
82 rtos->thread_details[thread_num].threadid = tid;
83 /* create the thread name */
84 rtos->thread_details[thread_num].exists = true;
85 rtos->thread_details[thread_num].thread_name_str = strdup(target_name(curr));
86 snprintf(tmp_str, HW_THREAD_NAME_STR_SIZE-1, "state: %s", debug_reason_name(curr));
87 rtos->thread_details[thread_num].extra_info_str = strdup(tmp_str);
92 static int hwthread_update_threads(struct rtos *rtos)
94 int threads_found = 0;
95 int thread_list_size = 0;
96 struct target_list *head;
97 struct target *target;
98 int64_t current_thread = 0;
99 enum target_debug_reason current_reason = DBG_REASON_UNDEFINED;
104 target = rtos->target;
106 /* wipe out previous thread details if any */
107 rtos_free_threadlist(rtos);
109 /* determine the number of "threads" */
111 foreach_smp_target(head, target->smp_targets) {
112 struct target *curr = head->target;
114 if (!target_was_examined(curr))
120 thread_list_size = 1;
122 /* create space for new thread details */
123 rtos->thread_details = malloc(sizeof(struct thread_detail) * thread_list_size);
126 /* loop over all threads */
127 foreach_smp_target(head, target->smp_targets) {
128 struct target *curr = head->target;
130 if (!target_was_examined(curr))
133 threadid_t tid = threadid_from_target(curr);
135 hwthread_fill_thread(rtos, curr, threads_found);
137 /* find an interesting thread to set as current */
138 switch (current_reason) {
139 case DBG_REASON_UNDEFINED:
140 current_reason = curr->debug_reason;
141 current_thread = tid;
143 case DBG_REASON_SINGLESTEP:
144 /* single-step can only be overridden by itself */
145 if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
146 if (tid == rtos->current_threadid)
147 current_thread = tid;
150 case DBG_REASON_BREAKPOINT:
151 /* single-step overrides breakpoint */
152 if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
153 current_reason = curr->debug_reason;
154 current_thread = tid;
156 /* multiple breakpoints, prefer gdbs' threadid */
157 if (curr->debug_reason == DBG_REASON_BREAKPOINT) {
158 if (tid == rtos->current_threadid)
159 current_thread = tid;
162 case DBG_REASON_WATCHPOINT:
163 /* breakpoint and single-step override watchpoint */
164 if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
165 curr->debug_reason == DBG_REASON_BREAKPOINT) {
166 current_reason = curr->debug_reason;
167 current_thread = tid;
170 case DBG_REASON_DBGRQ:
171 /* all other reasons override debug-request */
172 if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
173 curr->debug_reason == DBG_REASON_WATCHPOINT ||
174 curr->debug_reason == DBG_REASON_BREAKPOINT) {
175 current_reason = curr->debug_reason;
176 current_thread = tid;
178 if (curr->debug_reason == DBG_REASON_DBGRQ) {
179 if (tid == rtos->current_threadid)
180 current_thread = tid;
192 hwthread_fill_thread(rtos, target, threads_found);
193 current_thread = threadid_from_target(target);
197 rtos->thread_count = threads_found;
199 /* we found an interesting thread, set it as current */
200 if (current_thread != 0)
201 rtos->current_thread = current_thread;
202 else if (rtos->current_threadid != 0)
203 rtos->current_thread = rtos->current_threadid;
205 rtos->current_thread = threadid_from_target(target);
207 LOG_DEBUG("%s current_thread=%i", __func__, (int)rtos->current_thread);
211 static int hwthread_smp_init(struct target *target)
213 return hwthread_update_threads(target->rtos);
216 static struct target *hwthread_find_thread(struct target *target, int64_t thread_id)
218 /* Find the thread with that thread_id */
222 struct target_list *head;
223 foreach_smp_target(head, target->smp_targets) {
224 if (thread_id == threadid_from_target(head->target))
227 } else if (thread_id == threadid_from_target(target)) {
233 static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
234 struct rtos_reg **rtos_reg_list, int *rtos_reg_list_size)
239 struct target *target = rtos->target;
241 struct target *curr = hwthread_find_thread(target, thread_id);
245 if (!target_was_examined(curr))
249 struct reg **reg_list;
250 int retval = target_get_gdb_reg_list(curr, ®_list, ®_list_size,
252 if (retval != ERROR_OK)
256 for (int i = 0; i < reg_list_size; i++) {
257 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
261 *rtos_reg_list_size = j;
262 *rtos_reg_list = calloc(*rtos_reg_list_size, sizeof(struct rtos_reg));
263 if (!*rtos_reg_list) {
269 for (int i = 0; i < reg_list_size; i++) {
270 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
272 (*rtos_reg_list)[j].number = (*reg_list)[i].number;
273 (*rtos_reg_list)[j].size = (*reg_list)[i].size;
274 memcpy((*rtos_reg_list)[j].value, (*reg_list)[i].value,
275 ((*reg_list)[i].size + 7) / 8);
283 static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
284 uint32_t reg_num, struct rtos_reg *rtos_reg)
289 struct target *target = rtos->target;
291 struct target *curr = hwthread_find_thread(target, thread_id);
293 LOG_ERROR("Couldn't find RTOS thread for id %" PRId64 ".", thread_id);
297 if (!target_was_examined(curr)) {
298 LOG_ERROR("Target %d hasn't been examined yet.", curr->coreid);
302 struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
304 LOG_ERROR("Couldn't find register %" PRIu32 " in thread %" PRId64 ".", reg_num,
309 if (reg->type->get(reg) != ERROR_OK)
312 rtos_reg->number = reg->number;
313 rtos_reg->size = reg->size;
314 unsigned bytes = (reg->size + 7) / 8;
315 assert(bytes <= sizeof(rtos_reg->value));
316 memcpy(rtos_reg->value, reg->value, bytes);
321 static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value)
326 struct target *target = rtos->target;
328 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
332 struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
336 return reg->type->set(reg, reg_value);
339 static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
341 /* return an empty list, we don't have any symbols to look up */
342 *symbol_list = calloc(1, sizeof(struct symbol_table_elem));
343 (*symbol_list)[0].symbol_name = NULL;
347 static int hwthread_target_for_threadid(struct connection *connection, int64_t thread_id, struct target **p_target)
349 struct target *target = get_target_from_connection(connection);
351 struct target *curr = hwthread_find_thread(target, thread_id);
360 static bool hwthread_detect_rtos(struct target *target)
362 /* always return 0, avoid auto-detection */
366 static int hwthread_thread_packet(struct connection *connection, const char *packet, int packet_size)
368 struct target *target = get_target_from_connection(connection);
370 struct target *curr = NULL;
371 int64_t current_threadid;
373 if (packet[0] == 'H' && packet[1] == 'g') {
374 sscanf(packet, "Hg%16" SCNx64, ¤t_threadid);
376 if (current_threadid > 0) {
377 if (hwthread_target_for_threadid(connection, current_threadid, &curr) != ERROR_OK) {
378 LOG_ERROR("hwthread: cannot find thread id %"PRId64, current_threadid);
379 gdb_put_packet(connection, "E01", 3);
382 target->rtos->current_thread = current_threadid;
384 if (current_threadid == 0 || current_threadid == -1)
385 target->rtos->current_thread = threadid_from_target(target);
387 target->rtos->current_threadid = current_threadid;
389 gdb_put_packet(connection, "OK", 2);
393 return rtos_thread_packet(connection, packet, packet_size);
396 static int hwthread_create(struct target *target)
398 LOG_INFO("Hardware thread awareness created");
400 target->rtos->rtos_specific_params = NULL;
401 target->rtos->current_thread = 0;
402 target->rtos->thread_details = NULL;
403 target->rtos->gdb_target_for_threadid = hwthread_target_for_threadid;
404 target->rtos->gdb_thread_packet = hwthread_thread_packet;
408 static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
409 uint32_t size, uint8_t *buffer)
414 struct target *target = rtos->target;
416 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
420 return target_read_buffer(curr, address, size, buffer);
423 static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
424 uint32_t size, const uint8_t *buffer)
429 struct target *target = rtos->target;
431 struct target *curr = hwthread_find_thread(target, rtos->current_thread);
435 return target_write_buffer(curr, address, size, buffer);