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"
15 #include "target/target_type.h"
17 #include "helper/log.h"
18 #include "helper/types.h"
19 #include "rtos_standard_stackings.h"
20 #include "target/armv7m.h"
21 #include "target/cortex_m.h"
23 #define FREERTOS_MAX_PRIORITIES 63
25 /* FIXME: none of the _width parameters are actually observed properly!
26 * you WILL need to edit more if you actually attempt to target a 8/16/64
30 struct freertos_params
{
31 const char *target_name
;
32 const unsigned char thread_count_width
;
33 const unsigned char pointer_width
;
34 const unsigned char list_next_offset
;
35 const unsigned char list_width
;
36 const unsigned char list_elem_next_offset
;
37 const unsigned char list_elem_content_offset
;
38 const unsigned char thread_stack_offset
;
39 const unsigned char thread_name_offset
;
40 const struct rtos_register_stacking
*stacking_info_cm3
;
41 const struct rtos_register_stacking
*stacking_info_cm4f
;
42 const struct rtos_register_stacking
*stacking_info_cm4f_fpu
;
45 static const struct freertos_params freertos_params_list
[] = {
47 "cortex_m", /* target_name */
48 4, /* thread_count_width; */
49 4, /* pointer_width; */
50 16, /* list_next_offset; */
52 8, /* list_elem_next_offset; */
53 12, /* list_elem_content_offset */
54 0, /* thread_stack_offset; */
55 52, /* thread_name_offset; */
56 &rtos_standard_cortex_m3_stacking
, /* stacking_info */
57 &rtos_standard_cortex_m4f_stacking
,
58 &rtos_standard_cortex_m4f_fpu_stacking
,
61 "hla_target", /* target_name */
62 4, /* thread_count_width; */
63 4, /* pointer_width; */
64 16, /* list_next_offset; */
66 8, /* list_elem_next_offset; */
67 12, /* list_elem_content_offset */
68 0, /* thread_stack_offset; */
69 52, /* thread_name_offset; */
70 &rtos_standard_cortex_m3_stacking
, /* stacking_info */
71 &rtos_standard_cortex_m4f_stacking
,
72 &rtos_standard_cortex_m4f_fpu_stacking
,
76 static bool freertos_detect_rtos(struct target
*target
);
77 static int freertos_create(struct target
*target
);
78 static int freertos_update_threads(struct rtos
*rtos
);
79 static int freertos_get_thread_reg_list(struct rtos
*rtos
, int64_t thread_id
,
80 struct rtos_reg
**reg_list
, int *num_regs
);
81 static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem
*symbol_list
[]);
83 struct rtos_type freertos_rtos
= {
86 .detect_rtos
= freertos_detect_rtos
,
87 .create
= freertos_create
,
88 .update_threads
= freertos_update_threads
,
89 .get_thread_reg_list
= freertos_get_thread_reg_list
,
90 .get_symbol_list_to_lookup
= freertos_get_symbol_list_to_lookup
,
93 enum freertos_symbol_values
{
94 FREERTOS_VAL_PX_CURRENT_TCB
= 0,
95 FREERTOS_VAL_PX_READY_TASKS_LISTS
= 1,
96 FREERTOS_VAL_X_DELAYED_TASK_LIST1
= 2,
97 FREERTOS_VAL_X_DELAYED_TASK_LIST2
= 3,
98 FREERTOS_VAL_PX_DELAYED_TASK_LIST
= 4,
99 FREERTOS_VAL_PX_OVERFLOW_DELAYED_TASK_LIST
= 5,
100 FREERTOS_VAL_X_PENDING_READY_LIST
= 6,
101 FREERTOS_VAL_X_TASKS_WAITING_TERMINATION
= 7,
102 FREERTOS_VAL_X_SUSPENDED_TASK_LIST
= 8,
103 FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS
= 9,
104 FREERTOS_VAL_UX_TOP_USED_PRIORITY
= 10,
105 FREERTOS_VAL_X_SCHEDULER_RUNNING
= 11,
113 static const struct symbols freertos_symbol_list
[] = {
114 { "pxCurrentTCB", false },
115 { "pxReadyTasksLists", false },
116 { "xDelayedTaskList1", false },
117 { "xDelayedTaskList2", false },
118 { "pxDelayedTaskList", false },
119 { "pxOverflowDelayedTaskList", false },
120 { "xPendingReadyList", false },
121 { "xTasksWaitingTermination", true }, /* Only if INCLUDE_vTaskDelete */
122 { "xSuspendedTaskList", true }, /* Only if INCLUDE_vTaskSuspend */
123 { "uxCurrentNumberOfTasks", false },
124 { "uxTopUsedPriority", true }, /* Unavailable since v7.5.3 */
125 { "xSchedulerRunning", false },
130 /* this is not safe for little endian yet */
131 /* may be problems reading if sizes are not 32 bit long integers. */
132 /* test mallocs for failure */
134 static int freertos_update_threads(struct rtos
*rtos
)
137 unsigned int tasks_found
= 0;
138 const struct freertos_params
*param
;
140 if (!rtos
->rtos_specific_params
)
143 param
= (const struct freertos_params
*) rtos
->rtos_specific_params
;
145 if (!rtos
->symbols
) {
146 LOG_ERROR("No symbols for FreeRTOS");
150 if (rtos
->symbols
[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS
].address
== 0) {
151 LOG_ERROR("Don't have the number of threads in FreeRTOS");
155 uint32_t thread_list_size
= 0;
156 retval
= target_read_u32(rtos
->target
,
157 rtos
->symbols
[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS
].address
,
159 LOG_DEBUG("FreeRTOS: Read uxCurrentNumberOfTasks at 0x%" PRIx64
", value %" PRIu32
,
160 rtos
->symbols
[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS
].address
,
163 if (retval
!= ERROR_OK
) {
164 LOG_ERROR("Could not read FreeRTOS thread count from target");
168 /* wipe out previous thread details if any */
169 rtos_free_threadlist(rtos
);
171 /* read the current thread */
172 uint32_t pointer_casts_are_bad
;
173 retval
= target_read_u32(rtos
->target
,
174 rtos
->symbols
[FREERTOS_VAL_PX_CURRENT_TCB
].address
,
175 &pointer_casts_are_bad
);
176 if (retval
!= ERROR_OK
) {
177 LOG_ERROR("Error reading current thread in FreeRTOS thread list");
180 rtos
->current_thread
= pointer_casts_are_bad
;
181 LOG_DEBUG("FreeRTOS: Read pxCurrentTCB at 0x%" PRIx64
", value 0x%" PRIx64
,
182 rtos
->symbols
[FREERTOS_VAL_PX_CURRENT_TCB
].address
,
183 rtos
->current_thread
);
185 /* read scheduler running */
186 uint32_t scheduler_running
;
187 retval
= target_read_u32(rtos
->target
,
188 rtos
->symbols
[FREERTOS_VAL_X_SCHEDULER_RUNNING
].address
,
190 if (retval
!= ERROR_OK
) {
191 LOG_ERROR("Error reading FreeRTOS scheduler state");
194 LOG_DEBUG("FreeRTOS: Read xSchedulerRunning at 0x%" PRIx64
", value 0x%" PRIx32
,
195 rtos
->symbols
[FREERTOS_VAL_X_SCHEDULER_RUNNING
].address
,
198 if ((thread_list_size
== 0) || (rtos
->current_thread
== 0) || (scheduler_running
!= 1)) {
199 /* Either : No RTOS threads - there is always at least the current execution though */
200 /* OR : No current thread - all threads suspended - show the current execution
202 char tmp_str
[] = "Current Execution";
205 rtos
->thread_details
= malloc(
206 sizeof(struct thread_detail
) * thread_list_size
);
207 if (!rtos
->thread_details
) {
208 LOG_ERROR("Error allocating memory for %d threads", thread_list_size
);
211 rtos
->current_thread
= 1;
212 rtos
->thread_details
->threadid
= rtos
->current_thread
;
213 rtos
->thread_details
->exists
= true;
214 rtos
->thread_details
->extra_info_str
= NULL
;
215 rtos
->thread_details
->thread_name_str
= malloc(sizeof(tmp_str
));
216 strcpy(rtos
->thread_details
->thread_name_str
, tmp_str
);
218 if (thread_list_size
== 1) {
219 rtos
->thread_count
= 1;
223 /* create space for new thread details */
224 rtos
->thread_details
= malloc(
225 sizeof(struct thread_detail
) * thread_list_size
);
226 if (!rtos
->thread_details
) {
227 LOG_ERROR("Error allocating memory for %d threads", thread_list_size
);
232 /* Find out how many lists are needed to be read from pxReadyTasksLists, */
233 if (rtos
->symbols
[FREERTOS_VAL_UX_TOP_USED_PRIORITY
].address
== 0) {
234 LOG_ERROR("FreeRTOS: uxTopUsedPriority is not defined, consult the OpenOCD manual for a work-around");
237 uint32_t top_used_priority
= 0;
238 retval
= target_read_u32(rtos
->target
,
239 rtos
->symbols
[FREERTOS_VAL_UX_TOP_USED_PRIORITY
].address
,
241 if (retval
!= ERROR_OK
)
243 LOG_DEBUG("FreeRTOS: Read uxTopUsedPriority at 0x%" PRIx64
", value %" PRIu32
,
244 rtos
->symbols
[FREERTOS_VAL_UX_TOP_USED_PRIORITY
].address
,
246 if (top_used_priority
> FREERTOS_MAX_PRIORITIES
) {
247 LOG_ERROR("FreeRTOS top used priority is unreasonably big, not proceeding: %" PRIu32
,
252 /* uxTopUsedPriority was defined as configMAX_PRIORITIES - 1
253 * in old FreeRTOS versions (before V7.5.3)
254 * Use contrib/rtos-helpers/FreeRTOS-openocd.c to get compatible symbol
255 * in newer FreeRTOS versions.
256 * Here we restore the original configMAX_PRIORITIES value */
257 unsigned int config_max_priorities
= top_used_priority
+ 1;
259 symbol_address_t
*list_of_lists
=
260 malloc(sizeof(symbol_address_t
) * (config_max_priorities
+ 5));
261 if (!list_of_lists
) {
262 LOG_ERROR("Error allocating memory for %u priorities", config_max_priorities
);
266 unsigned int num_lists
;
267 for (num_lists
= 0; num_lists
< config_max_priorities
; num_lists
++)
268 list_of_lists
[num_lists
] = rtos
->symbols
[FREERTOS_VAL_PX_READY_TASKS_LISTS
].address
+
269 num_lists
* param
->list_width
;
271 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_DELAYED_TASK_LIST1
].address
;
272 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_DELAYED_TASK_LIST2
].address
;
273 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_PENDING_READY_LIST
].address
;
274 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_SUSPENDED_TASK_LIST
].address
;
275 list_of_lists
[num_lists
++] = rtos
->symbols
[FREERTOS_VAL_X_TASKS_WAITING_TERMINATION
].address
;
277 for (unsigned int i
= 0; i
< num_lists
; i
++) {
278 if (list_of_lists
[i
] == 0)
281 /* Read the number of threads in this list */
282 uint32_t list_thread_count
= 0;
283 retval
= target_read_u32(rtos
->target
,
286 if (retval
!= ERROR_OK
) {
287 LOG_ERROR("Error reading number of threads in FreeRTOS thread list");
291 LOG_DEBUG("FreeRTOS: Read thread count for list %u at 0x%" PRIx64
", value %" PRIu32
,
292 i
, list_of_lists
[i
], list_thread_count
);
294 if (list_thread_count
== 0)
297 /* Read the location of first list item */
298 uint32_t prev_list_elem_ptr
= -1;
299 uint32_t list_elem_ptr
= 0;
300 retval
= target_read_u32(rtos
->target
,
301 list_of_lists
[i
] + param
->list_next_offset
,
303 if (retval
!= ERROR_OK
) {
304 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
308 LOG_DEBUG("FreeRTOS: Read first item for list %u at 0x%" PRIx64
", value 0x%" PRIx32
,
309 i
, list_of_lists
[i
] + param
->list_next_offset
, list_elem_ptr
);
311 while ((list_thread_count
> 0) && (list_elem_ptr
!= 0) &&
312 (list_elem_ptr
!= prev_list_elem_ptr
) &&
313 (tasks_found
< thread_list_size
)) {
314 /* Get the location of the thread structure. */
315 rtos
->thread_details
[tasks_found
].threadid
= 0;
316 retval
= target_read_u32(rtos
->target
,
317 list_elem_ptr
+ param
->list_elem_content_offset
,
318 &pointer_casts_are_bad
);
319 if (retval
!= ERROR_OK
) {
320 LOG_ERROR("Error reading thread list item object in FreeRTOS thread list");
324 rtos
->thread_details
[tasks_found
].threadid
= pointer_casts_are_bad
;
325 LOG_DEBUG("FreeRTOS: Read Thread ID at 0x%" PRIx32
", value 0x%" PRIx64
,
326 list_elem_ptr
+ param
->list_elem_content_offset
,
327 rtos
->thread_details
[tasks_found
].threadid
);
329 /* get thread name */
331 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
332 char tmp_str
[FREERTOS_THREAD_NAME_STR_SIZE
];
334 /* Read the thread name */
335 retval
= target_read_buffer(rtos
->target
,
336 rtos
->thread_details
[tasks_found
].threadid
+ param
->thread_name_offset
,
337 FREERTOS_THREAD_NAME_STR_SIZE
,
338 (uint8_t *)&tmp_str
);
339 if (retval
!= ERROR_OK
) {
340 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
344 tmp_str
[FREERTOS_THREAD_NAME_STR_SIZE
-1] = '\x00';
345 LOG_DEBUG("FreeRTOS: Read Thread Name at 0x%" PRIx64
", value '%s'",
346 rtos
->thread_details
[tasks_found
].threadid
+ param
->thread_name_offset
,
349 if (tmp_str
[0] == '\x00')
350 strcpy(tmp_str
, "No Name");
352 rtos
->thread_details
[tasks_found
].thread_name_str
=
353 malloc(strlen(tmp_str
)+1);
354 strcpy(rtos
->thread_details
[tasks_found
].thread_name_str
, tmp_str
);
355 rtos
->thread_details
[tasks_found
].exists
= true;
357 if (rtos
->thread_details
[tasks_found
].threadid
== rtos
->current_thread
) {
358 char running_str
[] = "State: Running";
359 rtos
->thread_details
[tasks_found
].extra_info_str
= malloc(
360 sizeof(running_str
));
361 strcpy(rtos
->thread_details
[tasks_found
].extra_info_str
,
364 rtos
->thread_details
[tasks_found
].extra_info_str
= NULL
;
369 prev_list_elem_ptr
= list_elem_ptr
;
371 retval
= target_read_u32(rtos
->target
,
372 prev_list_elem_ptr
+ param
->list_elem_next_offset
,
374 if (retval
!= ERROR_OK
) {
375 LOG_ERROR("Error reading next thread item location in FreeRTOS thread list");
379 LOG_DEBUG("FreeRTOS: Read next thread location at 0x%" PRIx32
", value 0x%" PRIx32
,
380 prev_list_elem_ptr
+ param
->list_elem_next_offset
,
386 rtos
->thread_count
= tasks_found
;
390 static int freertos_get_thread_reg_list(struct rtos
*rtos
, int64_t thread_id
,
391 struct rtos_reg
**reg_list
, int *num_regs
)
394 const struct freertos_params
*param
;
395 int64_t stack_ptr
= 0;
403 if (!rtos
->rtos_specific_params
)
406 param
= (const struct freertos_params
*) rtos
->rtos_specific_params
;
408 /* Read the stack pointer */
409 uint32_t pointer_casts_are_bad
;
410 retval
= target_read_u32(rtos
->target
,
411 thread_id
+ param
->thread_stack_offset
,
412 &pointer_casts_are_bad
);
413 if (retval
!= ERROR_OK
) {
414 LOG_ERROR("Error reading stack frame from FreeRTOS thread");
417 stack_ptr
= pointer_casts_are_bad
;
418 LOG_DEBUG("FreeRTOS: Read stack pointer at 0x%" PRIx64
", value 0x%" PRIx64
,
419 thread_id
+ param
->thread_stack_offset
,
422 /* Check for armv7m with *enabled* FPU, i.e. a Cortex-M4F */
423 int cm4_fpu_enabled
= 0;
424 struct armv7m_common
*armv7m_target
= target_to_armv7m(rtos
->target
);
425 if (is_armv7m(armv7m_target
)) {
426 if ((armv7m_target
->fp_feature
== FPV4_SP
) || (armv7m_target
->fp_feature
== FPV5_SP
) ||
427 (armv7m_target
->fp_feature
== FPV5_DP
)) {
428 /* Found ARM v7m target which includes a FPU */
431 retval
= target_read_u32(rtos
->target
, FPU_CPACR
, &cpacr
);
432 if (retval
!= ERROR_OK
) {
433 LOG_ERROR("Could not read CPACR register to check FPU state");
437 /* Check if CP10 and CP11 are set to full access. */
438 if (cpacr
& 0x00F00000) {
439 /* Found target with enabled FPU */
445 if (cm4_fpu_enabled
== 1) {
446 /* Read the LR to decide between stacking with or without FPU */
448 retval
= target_read_u32(rtos
->target
,
451 if (retval
!= ERROR_OK
) {
452 LOG_OUTPUT("Error reading stack frame from FreeRTOS thread");
455 if ((lr_svc
& 0x10) == 0)
456 return rtos_generic_stack_read(rtos
->target
, param
->stacking_info_cm4f_fpu
, stack_ptr
, reg_list
, num_regs
);
458 return rtos_generic_stack_read(rtos
->target
, param
->stacking_info_cm4f
, stack_ptr
, reg_list
, num_regs
);
460 return rtos_generic_stack_read(rtos
->target
, param
->stacking_info_cm3
, stack_ptr
, reg_list
, num_regs
);
463 static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem
*symbol_list
[])
466 *symbol_list
= calloc(
467 ARRAY_SIZE(freertos_symbol_list
), sizeof(struct symbol_table_elem
));
469 for (i
= 0; i
< ARRAY_SIZE(freertos_symbol_list
); i
++) {
470 (*symbol_list
)[i
].symbol_name
= freertos_symbol_list
[i
].name
;
471 (*symbol_list
)[i
].optional
= freertos_symbol_list
[i
].optional
;
479 static int freertos_set_current_thread(struct rtos
*rtos
, threadid_t thread_id
)
484 static int freertos_get_thread_ascii_info(struct rtos
*rtos
, threadid_t thread_id
, char **info
)
487 const struct freertos_params
*param
;
495 if (!rtos
->rtos_specific_params
)
498 param
= (const struct freertos_params
*) rtos
->rtos_specific_params
;
500 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
501 char tmp_str
[FREERTOS_THREAD_NAME_STR_SIZE
];
503 /* Read the thread name */
504 retval
= target_read_buffer(rtos
->target
,
505 thread_id
+ param
->thread_name_offset
,
506 FREERTOS_THREAD_NAME_STR_SIZE
,
507 (uint8_t *)&tmp_str
);
508 if (retval
!= ERROR_OK
) {
509 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
512 tmp_str
[FREERTOS_THREAD_NAME_STR_SIZE
-1] = '\x00';
514 if (tmp_str
[0] == '\x00')
515 strcpy(tmp_str
, "No Name");
517 *info
= malloc(strlen(tmp_str
)+1);
518 strcpy(*info
, tmp_str
);
524 static bool freertos_detect_rtos(struct target
*target
)
526 if ((target
->rtos
->symbols
) &&
527 (target
->rtos
->symbols
[FREERTOS_VAL_PX_READY_TASKS_LISTS
].address
!= 0)) {
528 /* looks like FreeRTOS */
534 static int freertos_create(struct target
*target
)
536 for (unsigned int i
= 0; i
< ARRAY_SIZE(freertos_params_list
); i
++)
537 if (strcmp(freertos_params_list
[i
].target_name
, target
->type
->name
) == 0) {
538 target
->rtos
->rtos_specific_params
= (void *)&freertos_params_list
[i
];
542 LOG_ERROR("Could not find target in FreeRTOS compatibility list");
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)