1 // SPDX-License-Identifier: GPL-2.0-or-later
7 #include <helper/time_support.h>
9 #include "target/target.h"
10 #include "target/register.h"
11 #include <target/smp.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
= {
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
{
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
);
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
;
88 target
= rtos
->target
;
90 /* wipe out previous thread details if any */
91 rtos_free_threadlist(rtos
);
93 /* determine the number of "threads" */
95 foreach_smp_target(head
, target
->smp_targets
) {
96 struct target
*curr
= head
->target
;
98 if (!target_was_examined(curr
))
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
;
112 LOG_WARNING("SMP node change, disconnect GDB from core/thread %" PRId64
,
115 /* create space for new thread details */
116 rtos
->thread_details
= malloc(sizeof(struct thread_detail
) * thread_list_size
);
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
))
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
;
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
;
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
;
149 /* multiple breakpoints, prefer gdbs' threadid */
150 if (curr
->debug_reason
== DBG_REASON_BREAKPOINT
) {
151 if (tid
== rtos
->current_threadid
)
152 current_thread
= tid
;
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
;
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
;
171 if (curr
->debug_reason
== DBG_REASON_DBGRQ
) {
172 if (tid
== rtos
->current_threadid
)
173 current_thread
= tid
;
185 hwthread_fill_thread(rtos
, target
, threads_found
);
186 current_thread
= threadid_from_target(target
);
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
;
198 rtos
->current_thread
= threadid_from_target(target
);
200 LOG_DEBUG("%s current_thread=%i", __func__
, (int)rtos
->current_thread
);
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 */
215 struct target_list
*head
;
216 foreach_smp_target(head
, target
->smp_targets
) {
217 if (thread_id
== threadid_from_target(head
->target
))
220 } else if (thread_id
== threadid_from_target(target
)) {
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
)
232 struct target
*target
= rtos
->target
;
234 struct target
*curr
= hwthread_find_thread(target
, thread_id
);
238 if (!target_was_examined(curr
))
242 struct reg
**reg_list
;
243 int retval
= target_get_gdb_reg_list(curr
, ®_list
, ®_list_size
,
245 if (retval
!= ERROR_OK
)
249 for (int i
= 0; i
< reg_list_size
; i
++) {
250 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
254 *rtos_reg_list_size
= j
;
255 *rtos_reg_list
= calloc(*rtos_reg_list_size
, sizeof(struct rtos_reg
));
256 if (!*rtos_reg_list
) {
262 for (int i
= 0; i
< reg_list_size
; i
++) {
263 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
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
);
270 free(*rtos_reg_list
);
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));
285 static int hwthread_get_thread_reg(struct rtos
*rtos
, int64_t thread_id
,
286 uint32_t reg_num
, struct rtos_reg
*rtos_reg
)
291 struct target
*target
= rtos
->target
;
293 struct target
*curr
= hwthread_find_thread(target
, thread_id
);
295 LOG_ERROR("Couldn't find RTOS thread for id %" PRId64
".", thread_id
);
299 if (!target_was_examined(curr
)) {
300 LOG_ERROR("Target %d hasn't been examined yet.", curr
->coreid
);
304 struct reg
*reg
= register_get_by_number(curr
->reg_cache
, reg_num
, true);
306 LOG_ERROR("Couldn't find register %" PRIu32
" in thread %" PRId64
".", reg_num
,
311 if (reg
->type
->get(reg
) != ERROR_OK
)
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
);
323 static int hwthread_set_reg(struct rtos
*rtos
, uint32_t reg_num
, uint8_t *reg_value
)
328 struct target
*target
= rtos
->target
;
330 struct target
*curr
= hwthread_find_thread(target
, rtos
->current_thread
);
334 struct reg
*reg
= register_get_by_number(curr
->reg_cache
, reg_num
, true);
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
;
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
);
362 static bool hwthread_detect_rtos(struct target
*target
)
364 /* always return 0, avoid auto-detection */
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
, ¤t_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);
384 target
->rtos
->current_thread
= current_threadid
;
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);
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
;
410 static int hwthread_read_buffer(struct rtos
*rtos
, target_addr_t address
,
411 uint32_t size
, uint8_t *buffer
)
416 struct target
*target
= rtos
->target
;
418 struct target
*curr
= hwthread_find_thread(target
, rtos
->current_thread
);
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
)
431 struct target
*target
= rtos
->target
;
433 struct target
*curr
= hwthread_find_thread(target
, rtos
->current_thread
);
437 return target_write_buffer(curr
, address
, size
, buffer
);