1 // SPDX-License-Identifier: GPL-2.0-or-later
7 #include <helper/time_support.h>
9 #include "target/target.h"
10 #include "target/target_type.h"
11 #include "target/register.h"
12 #include <target/smp.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
= {
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
{
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
);
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
;
90 target
= rtos
->target
;
92 /* wipe out previous thread details if any */
93 rtos_free_threadlist(rtos
);
95 /* determine the number of "threads" */
97 foreach_smp_target(head
, target
->smp_targets
) {
98 struct target
*curr
= head
->target
;
100 if (!target_was_examined(curr
))
106 thread_list_size
= 1;
108 /* create space for new thread details */
109 rtos
->thread_details
= malloc(sizeof(struct thread_detail
) * thread_list_size
);
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
))
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
;
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
;
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
;
142 /* multiple breakpoints, prefer gdbs' threadid */
143 if (curr
->debug_reason
== DBG_REASON_BREAKPOINT
) {
144 if (tid
== rtos
->current_threadid
)
145 current_thread
= tid
;
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
;
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
;
164 if (curr
->debug_reason
== DBG_REASON_DBGRQ
) {
165 if (tid
== rtos
->current_threadid
)
166 current_thread
= tid
;
178 hwthread_fill_thread(rtos
, target
, threads_found
);
179 current_thread
= threadid_from_target(target
);
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
;
191 rtos
->current_thread
= threadid_from_target(target
);
193 LOG_DEBUG("%s current_thread=%i", __func__
, (int)rtos
->current_thread
);
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 */
208 struct target_list
*head
;
209 foreach_smp_target(head
, target
->smp_targets
) {
210 if (thread_id
== threadid_from_target(head
->target
))
213 } else if (thread_id
== threadid_from_target(target
)) {
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
)
225 struct target
*target
= rtos
->target
;
227 struct target
*curr
= hwthread_find_thread(target
, thread_id
);
231 if (!target_was_examined(curr
))
235 struct reg
**reg_list
;
236 int retval
= target_get_gdb_reg_list(curr
, ®_list
, ®_list_size
,
238 if (retval
!= ERROR_OK
)
242 for (int i
= 0; i
< reg_list_size
; i
++) {
243 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
247 *rtos_reg_list_size
= j
;
248 *rtos_reg_list
= calloc(*rtos_reg_list_size
, sizeof(struct rtos_reg
));
249 if (!*rtos_reg_list
) {
255 for (int i
= 0; i
< reg_list_size
; i
++) {
256 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
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
);
263 free(*rtos_reg_list
);
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));
278 static int hwthread_get_thread_reg(struct rtos
*rtos
, int64_t thread_id
,
279 uint32_t reg_num
, struct rtos_reg
*rtos_reg
)
284 struct target
*target
= rtos
->target
;
286 struct target
*curr
= hwthread_find_thread(target
, thread_id
);
288 LOG_ERROR("Couldn't find RTOS thread for id %" PRId64
".", thread_id
);
292 if (!target_was_examined(curr
)) {
293 LOG_ERROR("Target %d hasn't been examined yet.", curr
->coreid
);
297 struct reg
*reg
= register_get_by_number(curr
->reg_cache
, reg_num
, true);
299 LOG_ERROR("Couldn't find register %" PRIu32
" in thread %" PRId64
".", reg_num
,
304 if (reg
->type
->get(reg
) != ERROR_OK
)
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
);
316 static int hwthread_set_reg(struct rtos
*rtos
, uint32_t reg_num
, uint8_t *reg_value
)
321 struct target
*target
= rtos
->target
;
323 struct target
*curr
= hwthread_find_thread(target
, rtos
->current_thread
);
327 struct reg
*reg
= register_get_by_number(curr
->reg_cache
, reg_num
, true);
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
;
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
);
355 static bool hwthread_detect_rtos(struct target
*target
)
357 /* always return 0, avoid auto-detection */
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
, ¤t_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);
377 target
->rtos
->current_thread
= current_threadid
;
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);
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
;
403 static int hwthread_read_buffer(struct rtos
*rtos
, target_addr_t address
,
404 uint32_t size
, uint8_t *buffer
)
409 struct target
*target
= rtos
->target
;
411 struct target
*curr
= hwthread_find_thread(target
, rtos
->current_thread
);
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
)
424 struct target
*target
= rtos
->target
;
426 struct target
*curr
= hwthread_find_thread(target
, rtos
->current_thread
);
430 return target_write_buffer(curr
, address
, size
, buffer
);