1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2011 by Broadcom Corporation *
5 * Evan Hunter - ehunter@broadcom.com *
6 ***************************************************************************/
12 #include <helper/time_support.h>
13 #include <jtag/jtag.h>
14 #include "target/target.h"
16 #include "helper/log.h"
17 #include "helper/types.h"
18 #include "rtos_standard_stackings.h"
19 #include "target/armv7m.h"
20 #include "target/cortex_m.h"
22 #define FREERTOS_MAX_PRIORITIES 63
24 /* FIXME: none of the _width parameters are actually observed properly!
25 * you WILL need to edit more if you actually attempt to target a 8/16/64
29 struct freertos_params
{
30 const char *target_name
;
31 const unsigned char thread_count_width
;
32 const unsigned char pointer_width
;
33 const unsigned char list_next_offset
;
34 const unsigned char list_width
;
35 const unsigned char list_elem_next_offset
;
36 const unsigned char list_elem_content_offset
;
37 const unsigned char thread_stack_offset
;
38 const unsigned char thread_name_offset
;
39 const struct rtos_register_stacking
*stacking_info_cm3
;
40 const struct rtos_register_stacking
*stacking_info_cm4f
;
41 const struct rtos_register_stacking
*stacking_info_cm4f_fpu
;
44 static const struct freertos_params freertos_params_list
[] = {
46 "cortex_m", /* target_name */
47 4, /* thread_count_width; */
48 4, /* pointer_width; */
49 16, /* list_next_offset; */
51 8, /* list_elem_next_offset; */
52 12, /* list_elem_content_offset */
53 0, /* thread_stack_offset; */
54 52, /* thread_name_offset; */
55 &rtos_standard_cortex_m3_stacking
, /* stacking_info */
56 &rtos_standard_cortex_m4f_stacking
,
57 &rtos_standard_cortex_m4f_fpu_stacking
,
60 "hla_target", /* target_name */
61 4, /* thread_count_width; */
62 4, /* pointer_width; */
63 16, /* list_next_offset; */
65 8, /* list_elem_next_offset; */
66 12, /* list_elem_content_offset */
67 0, /* thread_stack_offset; */
68 52, /* thread_name_offset; */
69 &rtos_standard_cortex_m3_stacking
, /* stacking_info */
70 &rtos_standard_cortex_m4f_stacking
,
71 &rtos_standard_cortex_m4f_fpu_stacking
,
75 static bool freertos_detect_rtos(struct target
*target
);
76 static int freertos_create(struct target
*target
);
77 static int freertos_update_threads(struct rtos
*rtos
);
78 static int freertos_get_thread_reg_list(struct rtos
*rtos
, int64_t thread_id
,
79 struct rtos_reg
**reg_list
, int *num_regs
);
80 static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem
*symbol_list
[]);
82 const struct rtos_type freertos_rtos
= {
85 .detect_rtos
= freertos_detect_rtos
,
86 .create
= freertos_create
,
87 .update_threads
= freertos_update_threads
,
88 .get_thread_reg_list
= freertos_get_thread_reg_list
,
89 .get_symbol_list_to_lookup
= freertos_get_symbol_list_to_lookup
,
92 enum freertos_symbol_values
{
93 FREERTOS_VAL_PX_CURRENT_TCB
= 0,
94 FREERTOS_VAL_PX_READY_TASKS_LISTS
= 1,
95 FREERTOS_VAL_X_DELAYED_TASK_LIST1
= 2,
96 FREERTOS_VAL_X_DELAYED_TASK_LIST2
= 3,
97 FREERTOS_VAL_PX_DELAYED_TASK_LIST
= 4,
98 FREERTOS_VAL_PX_OVERFLOW_DELAYED_TASK_LIST
= 5,
99 FREERTOS_VAL_X_PENDING_READY_LIST
= 6,
100 FREERTOS_VAL_X_TASKS_WAITING_TERMINATION
= 7,
101 FREERTOS_VAL_X_SUSPENDED_TASK_LIST
= 8,
102 FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS
= 9,
103 FREERTOS_VAL_UX_TOP_USED_PRIORITY
= 10,
104 FREERTOS_VAL_X_SCHEDULER_RUNNING
= 11,
112 static const struct symbols freertos_symbol_list
[] = {
113 { "pxCurrentTCB", false },
114 { "pxReadyTasksLists", false },
115 { "xDelayedTaskList1", false },
116 { "xDelayedTaskList2", false },
117 { "pxDelayedTaskList", false },
118 { "pxOverflowDelayedTaskList", false },
119 { "xPendingReadyList", false },
120 { "xTasksWaitingTermination", true }, /* Only if INCLUDE_vTaskDelete */
121 { "xSuspendedTaskList", true }, /* Only if INCLUDE_vTaskSuspend */
122 { "uxCurrentNumberOfTasks", false },
123 { "uxTopUsedPriority", true }, /* Unavailable since v7.5.3 */
124 { "xSchedulerRunning", false },
129 /* this is not safe for little endian yet */
130 /* may be problems reading if sizes are not 32 bit long integers. */
131 /* test mallocs for failure */
133 static int freertos_update_threads(struct rtos
*rtos
)
136 unsigned int tasks_found
= 0;
137 const struct freertos_params
*param
;
139 if (!rtos
->rtos_specific_params
)
142 param
= (const struct freertos_params
*) rtos
->rtos_specific_params
;
144 if (!rtos
->symbols
) {
145 LOG_ERROR("No symbols for FreeRTOS");
149 if (rtos
->symbols
[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS
].address
== 0) {
150 LOG_ERROR("Don't have the number of threads in FreeRTOS");
154 uint32_t thread_list_size
= 0;
155 retval
= target_read_u32(rtos
->target
,
156 rtos
->symbols
[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS
].address
,
158 LOG_DEBUG("FreeRTOS: Read uxCurrentNumberOfTasks at 0x%" PRIx64
", value %" PRIu32
,
159 rtos
->symbols
[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS
].address
,
162 if (retval
!= ERROR_OK
) {
163 LOG_ERROR("Could not read FreeRTOS thread count from target");
167 /* wipe out previous thread details if any */
168 rtos_free_threadlist(rtos
);
170 /* read the current thread */
171 uint32_t pointer_casts_are_bad
;
172 retval
= target_read_u32(rtos
->target
,
173 rtos
->symbols
[FREERTOS_VAL_PX_CURRENT_TCB
].address
,
174 &pointer_casts_are_bad
);
175 if (retval
!= ERROR_OK
) {
176 LOG_ERROR("Error reading current thread in FreeRTOS thread list");
179 rtos
->current_thread
= pointer_casts_are_bad
;
180 LOG_DEBUG("FreeRTOS: Read pxCurrentTCB at 0x%" PRIx64
", value 0x%" PRIx64
,
181 rtos
->symbols
[FREERTOS_VAL_PX_CURRENT_TCB
].address
,
182 rtos
->current_thread
);
184 /* read scheduler running */
185 uint32_t scheduler_running
;
186 retval
= target_read_u32(rtos
->target
,
187 rtos
->symbols
[FREERTOS_VAL_X_SCHEDULER_RUNNING
].address
,
189 if (retval
!= ERROR_OK
) {
190 LOG_ERROR("Error reading FreeRTOS scheduler state");
193 LOG_DEBUG("FreeRTOS: Read xSchedulerRunning at 0x%" PRIx64
", value 0x%" PRIx32
,
194 rtos
->symbols
[FREERTOS_VAL_X_SCHEDULER_RUNNING
].address
,
197 if ((thread_list_size
== 0) || (rtos
->current_thread
== 0) || (scheduler_running
!= 1)) {
198 /* Either : No RTOS threads - there is always at least the current execution though */
199 /* OR : No current thread - all threads suspended - show the current execution
201 char tmp_str
[] = "Current Execution";
204 rtos
->thread_details
= malloc(
205 sizeof(struct thread_detail
) * thread_list_size
);
206 if (!rtos
->thread_details
) {
207 LOG_ERROR("Error allocating memory for %d threads", thread_list_size
);
210 rtos
->current_thread
= 1;
211 rtos
->thread_details
->threadid
= rtos
->current_thread
;
212 rtos
->thread_details
->exists
= true;
213 rtos
->thread_details
->extra_info_str
= NULL
;
214 rtos
->thread_details
->thread_name_str
= malloc(sizeof(tmp_str
));
215 strcpy(rtos
->thread_details
->thread_name_str
, tmp_str
);
217 if (thread_list_size
== 1) {
218 rtos
->thread_count
= 1;
222 /* create space for new thread details */
223 rtos
->thread_details
= malloc(
224 sizeof(struct thread_detail
) * thread_list_size
);
225 if (!rtos
->thread_details
) {
226 LOG_ERROR("Error allocating memory for %d threads", thread_list_size
);
231 /* Find out how many lists are needed to be read from pxReadyTasksLists, */
232 if (rtos
->symbols
[FREERTOS_VAL_UX_TOP_USED_PRIORITY
].address
== 0) {
233 LOG_ERROR("FreeRTOS: uxTopUsedPriority is not defined, consult the OpenOCD manual for a work-around");
236 uint32_t top_used_priority
= 0;
237 retval
= target_read_u32(rtos
->target
,
238 rtos
->symbols
[FREERTOS_VAL_UX_TOP_USED_PRIORITY
].address
,
240 if (retval
!= ERROR_OK
)
242 LOG_DEBUG("FreeRTOS: Read uxTopUsedPriority at 0x%" PRIx64
", value %" PRIu32
,
243 rtos
->symbols
[FREERTOS_VAL_UX_TOP_USED_PRIORITY
].address
,
245 if (top_used_priority
> FREERTOS_MAX_PRIORITIES
) {
246 LOG_ERROR("FreeRTOS top used priority is unreasonably big, not proceeding: %" PRIu32
,
251 /* uxTopUsedPriority was defined as configMAX_PRIORITIES - 1
252 * in old FreeRTOS versions (before V7.5.3)
253 * Use contrib/rtos-helpers/FreeRTOS-openocd.c to get compatible symbol
254 * in newer FreeRTOS versions.
255 * Here we restore the original configMAX_PRIORITIES value */
256 unsigned int config_max_priorities
= top_used_priority
+ 1;
258 symbol_address_t
*list_of_lists
=
259 malloc(sizeof(symbol_address_t
) * (config_max_priorities
+ 5));
260 if (!list_of_lists
) {
261 LOG_ERROR("Error allocating memory for %u priorities", config_max_priorities
);
265 unsigned int num_lists
;
266 for (num_lists
= 0; num_lists
< config_max_priorities
; num_lists
++)
267 list_of_lists
[num_lists
] = rtos
->symbols
[FREERTOS_VAL_PX_READY_TASKS_LISTS
].address
+
268 num_lists
* param
->list_width
;
270 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_DELAYED_TASK_LIST1
].address
;
271 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_DELAYED_TASK_LIST2
].address
;
272 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_PENDING_READY_LIST
].address
;
273 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_SUSPENDED_TASK_LIST
].address
;
274 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_TASKS_WAITING_TERMINATION
].address
;
276 for (unsigned int i
= 0; i
< num_lists
; i
++) {
277 if (list_of_lists
[i
] == 0)
280 /* Read the number of threads in this list */
281 uint32_t list_thread_count
= 0;
282 retval
= target_read_u32(rtos
->target
,
285 if (retval
!= ERROR_OK
) {
286 LOG_ERROR("Error reading number of threads in FreeRTOS thread list");
290 LOG_DEBUG("FreeRTOS: Read thread count for list %u at 0x%" PRIx64
", value %" PRIu32
,
291 i
, list_of_lists
[i
], list_thread_count
);
293 if (list_thread_count
== 0)
296 /* Read the location of first list item */
297 uint32_t prev_list_elem_ptr
= -1;
298 uint32_t list_elem_ptr
= 0;
299 retval
= target_read_u32(rtos
->target
,
300 list_of_lists
[i
] + param
->list_next_offset
,
302 if (retval
!= ERROR_OK
) {
303 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
307 LOG_DEBUG("FreeRTOS: Read first item for list %u at 0x%" PRIx64
", value 0x%" PRIx32
,
308 i
, list_of_lists
[i
] + param
->list_next_offset
, list_elem_ptr
);
310 while ((list_thread_count
> 0) && (list_elem_ptr
!= 0) &&
311 (list_elem_ptr
!= prev_list_elem_ptr
) &&
312 (tasks_found
< thread_list_size
)) {
313 /* Get the location of the thread structure. */
314 retval
= target_read_u32(rtos
->target
,
315 list_elem_ptr
+ param
->list_elem_content_offset
,
316 &pointer_casts_are_bad
);
317 if (retval
!= ERROR_OK
) {
318 LOG_ERROR("Error reading thread list item object in FreeRTOS thread list");
322 rtos
->thread_details
[tasks_found
].threadid
= pointer_casts_are_bad
;
323 LOG_DEBUG("FreeRTOS: Read Thread ID at 0x%" PRIx32
", value 0x%" PRIx64
,
324 list_elem_ptr
+ param
->list_elem_content_offset
,
325 rtos
->thread_details
[tasks_found
].threadid
);
327 /* get thread name */
329 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
330 char tmp_str
[FREERTOS_THREAD_NAME_STR_SIZE
];
332 /* Read the thread name */
333 retval
= target_read_buffer(rtos
->target
,
334 rtos
->thread_details
[tasks_found
].threadid
+ param
->thread_name_offset
,
335 FREERTOS_THREAD_NAME_STR_SIZE
,
336 (uint8_t *)&tmp_str
);
337 if (retval
!= ERROR_OK
) {
338 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
342 tmp_str
[FREERTOS_THREAD_NAME_STR_SIZE
-1] = '\x00';
343 LOG_DEBUG("FreeRTOS: Read Thread Name at 0x%" PRIx64
", value '%s'",
344 rtos
->thread_details
[tasks_found
].threadid
+ param
->thread_name_offset
,
347 if (tmp_str
[0] == '\x00')
348 strcpy(tmp_str
, "No Name");
350 rtos
->thread_details
[tasks_found
].thread_name_str
=
351 malloc(strlen(tmp_str
)+1);
352 strcpy(rtos
->thread_details
[tasks_found
].thread_name_str
, tmp_str
);
353 rtos
->thread_details
[tasks_found
].exists
= true;
355 if (rtos
->thread_details
[tasks_found
].threadid
== rtos
->current_thread
) {
356 char running_str
[] = "State: Running";
357 rtos
->thread_details
[tasks_found
].extra_info_str
= malloc(
358 sizeof(running_str
));
359 strcpy(rtos
->thread_details
[tasks_found
].extra_info_str
,
362 rtos
->thread_details
[tasks_found
].extra_info_str
= NULL
;
366 rtos
->thread_count
= tasks_found
;
368 prev_list_elem_ptr
= list_elem_ptr
;
370 retval
= target_read_u32(rtos
->target
,
371 prev_list_elem_ptr
+ param
->list_elem_next_offset
,
373 if (retval
!= ERROR_OK
) {
374 LOG_ERROR("Error reading next thread item location in FreeRTOS thread list");
378 LOG_DEBUG("FreeRTOS: Read next thread location at 0x%" PRIx32
", value 0x%" PRIx32
,
379 prev_list_elem_ptr
+ param
->list_elem_next_offset
,
388 static int freertos_get_thread_reg_list(struct rtos
*rtos
, int64_t thread_id
,
389 struct rtos_reg
**reg_list
, int *num_regs
)
392 const struct freertos_params
*param
;
393 int64_t stack_ptr
= 0;
401 if (!rtos
->rtos_specific_params
)
404 param
= (const struct freertos_params
*) rtos
->rtos_specific_params
;
406 /* Read the stack pointer */
407 uint32_t pointer_casts_are_bad
;
408 retval
= target_read_u32(rtos
->target
,
409 thread_id
+ param
->thread_stack_offset
,
410 &pointer_casts_are_bad
);
411 if (retval
!= ERROR_OK
) {
412 LOG_ERROR("Error reading stack frame from FreeRTOS thread");
415 stack_ptr
= pointer_casts_are_bad
;
416 LOG_DEBUG("FreeRTOS: Read stack pointer at 0x%" PRIx64
", value 0x%" PRIx64
,
417 thread_id
+ param
->thread_stack_offset
,
420 /* Check for armv7m with *enabled* FPU, i.e. a Cortex-M4F */
421 int cm4_fpu_enabled
= 0;
422 struct armv7m_common
*armv7m_target
= target_to_armv7m(rtos
->target
);
423 if (is_armv7m(armv7m_target
)) {
424 if ((armv7m_target
->fp_feature
== FPV4_SP
) || (armv7m_target
->fp_feature
== FPV5_SP
) ||
425 (armv7m_target
->fp_feature
== FPV5_DP
)) {
426 /* Found ARM v7m target which includes a FPU */
429 retval
= target_read_u32(rtos
->target
, FPU_CPACR
, &cpacr
);
430 if (retval
!= ERROR_OK
) {
431 LOG_ERROR("Could not read CPACR register to check FPU state");
435 /* Check if CP10 and CP11 are set to full access. */
436 if (cpacr
& 0x00F00000) {
437 /* Found target with enabled FPU */
443 if (cm4_fpu_enabled
== 1) {
444 /* Read the LR to decide between stacking with or without FPU */
446 retval
= target_read_u32(rtos
->target
,
449 if (retval
!= ERROR_OK
) {
450 LOG_OUTPUT("Error reading stack frame from FreeRTOS thread");
453 if ((lr_svc
& 0x10) == 0)
454 return rtos_generic_stack_read(rtos
->target
, param
->stacking_info_cm4f_fpu
, stack_ptr
, reg_list
, num_regs
);
456 return rtos_generic_stack_read(rtos
->target
, param
->stacking_info_cm4f
, stack_ptr
, reg_list
, num_regs
);
458 return rtos_generic_stack_read(rtos
->target
, param
->stacking_info_cm3
, stack_ptr
, reg_list
, num_regs
);
461 static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem
*symbol_list
[])
464 *symbol_list
= calloc(
465 ARRAY_SIZE(freertos_symbol_list
), sizeof(struct symbol_table_elem
));
467 for (i
= 0; i
< ARRAY_SIZE(freertos_symbol_list
); i
++) {
468 (*symbol_list
)[i
].symbol_name
= freertos_symbol_list
[i
].name
;
469 (*symbol_list
)[i
].optional
= freertos_symbol_list
[i
].optional
;
477 static int freertos_set_current_thread(struct rtos
*rtos
, threadid_t thread_id
)
482 static int freertos_get_thread_ascii_info(struct rtos
*rtos
, threadid_t thread_id
, char **info
)
485 const struct freertos_params
*param
;
493 if (!rtos
->rtos_specific_params
)
496 param
= (const struct freertos_params
*) rtos
->rtos_specific_params
;
498 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
499 char tmp_str
[FREERTOS_THREAD_NAME_STR_SIZE
];
501 /* Read the thread name */
502 retval
= target_read_buffer(rtos
->target
,
503 thread_id
+ param
->thread_name_offset
,
504 FREERTOS_THREAD_NAME_STR_SIZE
,
505 (uint8_t *)&tmp_str
);
506 if (retval
!= ERROR_OK
) {
507 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
510 tmp_str
[FREERTOS_THREAD_NAME_STR_SIZE
-1] = '\x00';
512 if (tmp_str
[0] == '\x00')
513 strcpy(tmp_str
, "No Name");
515 *info
= malloc(strlen(tmp_str
)+1);
516 strcpy(*info
, tmp_str
);
522 static bool freertos_detect_rtos(struct target
*target
)
524 if ((target
->rtos
->symbols
) &&
525 (target
->rtos
->symbols
[FREERTOS_VAL_PX_READY_TASKS_LISTS
].address
!= 0)) {
526 /* looks like FreeRTOS */
532 static int freertos_create(struct target
*target
)
534 for (unsigned int i
= 0; i
< ARRAY_SIZE(freertos_params_list
); i
++)
535 if (strcmp(freertos_params_list
[i
].target_name
, target_type_name(target
)) == 0) {
536 target
->rtos
->rtos_specific_params
= (void *)&freertos_params_list
[i
];
540 LOG_ERROR("Could not find target in FreeRTOS compatibility list");