1 /***************************************************************************
2 * Copyright (C) 2011 by Broadcom Corporation *
3 * Evan Hunter - ehunter@broadcom.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
23 #include <helper/time_support.h>
24 #include <jtag/jtag.h>
25 #include "target/target.h"
26 #include "target/target_type.h"
28 #include "helper/log.h"
29 #include "helper/types.h"
30 #include "rtos_standard_stackings.h"
31 #include "target/armv7m.h"
32 #include "target/cortex_m.h"
36 #define FREERTOS_MAX_PRIORITIES 63
38 #define FreeRTOS_STRUCT(int_type, ptr_type, list_prev_offset)
40 /* FIXME: none of the _width parameters are actually observed properly!
41 * you WILL need to edit more if you actually attempt to target a 8/16/64
45 struct FreeRTOS_params {
46 const char *target_name;
47 const unsigned char thread_count_width;
48 const unsigned char pointer_width;
49 const unsigned char list_next_offset;
50 const unsigned char list_width;
51 const unsigned char list_elem_next_offset;
52 const unsigned char list_elem_content_offset;
53 const unsigned char thread_stack_offset;
54 const unsigned char thread_name_offset;
55 const struct rtos_register_stacking *stacking_info_cm3;
56 const struct rtos_register_stacking *stacking_info_cm4f;
57 const struct rtos_register_stacking *stacking_info_cm4f_fpu;
60 static const struct FreeRTOS_params FreeRTOS_params_list[] = {
62 "cortex_m", /* target_name */
63 4, /* thread_count_width; */
64 4, /* pointer_width; */
65 16, /* list_next_offset; */
67 8, /* list_elem_next_offset; */
68 12, /* list_elem_content_offset */
69 0, /* thread_stack_offset; */
70 52, /* thread_name_offset; */
71 &rtos_standard_Cortex_M3_stacking, /* stacking_info */
72 &rtos_standard_Cortex_M4F_stacking,
73 &rtos_standard_Cortex_M4F_FPU_stacking,
76 "hla_target", /* target_name */
77 4, /* thread_count_width; */
78 4, /* pointer_width; */
79 16, /* list_next_offset; */
81 8, /* list_elem_next_offset; */
82 12, /* list_elem_content_offset */
83 0, /* thread_stack_offset; */
84 52, /* thread_name_offset; */
85 &rtos_standard_Cortex_M3_stacking, /* stacking_info */
86 &rtos_standard_Cortex_M4F_stacking,
87 &rtos_standard_Cortex_M4F_FPU_stacking,
90 "nds32_v3", /* target_name */
91 4, /* thread_count_width; */
92 4, /* pointer_width; */
93 16, /* list_next_offset; */
95 8, /* list_elem_next_offset; */
96 12, /* list_elem_content_offset */
97 0, /* thread_stack_offset; */
98 52, /* thread_name_offset; */
99 &rtos_standard_NDS32_N1068_stacking, /* stacking_info */
100 &rtos_standard_Cortex_M4F_stacking,
101 &rtos_standard_Cortex_M4F_FPU_stacking,
105 static bool FreeRTOS_detect_rtos(struct target *target);
106 static int FreeRTOS_create(struct target *target);
107 static int FreeRTOS_update_threads(struct rtos *rtos);
108 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
109 struct rtos_reg **reg_list, int *num_regs);
110 static int FreeRTOS_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
112 struct rtos_type FreeRTOS_rtos = {
115 .detect_rtos = FreeRTOS_detect_rtos,
116 .create = FreeRTOS_create,
117 .update_threads = FreeRTOS_update_threads,
118 .get_thread_reg_list = FreeRTOS_get_thread_reg_list,
119 .get_symbol_list_to_lookup = FreeRTOS_get_symbol_list_to_lookup,
122 enum FreeRTOS_symbol_values {
123 FreeRTOS_VAL_pxCurrentTCB = 0,
124 FreeRTOS_VAL_pxReadyTasksLists = 1,
125 FreeRTOS_VAL_xDelayedTaskList1 = 2,
126 FreeRTOS_VAL_xDelayedTaskList2 = 3,
127 FreeRTOS_VAL_pxDelayedTaskList = 4,
128 FreeRTOS_VAL_pxOverflowDelayedTaskList = 5,
129 FreeRTOS_VAL_xPendingReadyList = 6,
130 FreeRTOS_VAL_xTasksWaitingTermination = 7,
131 FreeRTOS_VAL_xSuspendedTaskList = 8,
132 FreeRTOS_VAL_uxCurrentNumberOfTasks = 9,
133 FreeRTOS_VAL_uxTopUsedPriority = 10,
141 static const struct symbols FreeRTOS_symbol_list[] = {
142 { "pxCurrentTCB", false },
143 { "pxReadyTasksLists", false },
144 { "xDelayedTaskList1", false },
145 { "xDelayedTaskList2", false },
146 { "pxDelayedTaskList", false },
147 { "pxOverflowDelayedTaskList", false },
148 { "xPendingReadyList", false },
149 { "xTasksWaitingTermination", true }, /* Only if INCLUDE_vTaskDelete */
150 { "xSuspendedTaskList", true }, /* Only if INCLUDE_vTaskSuspend */
151 { "uxCurrentNumberOfTasks", false },
152 { "uxTopUsedPriority", true }, /* Unavailable since v7.5.3 */
157 /* this is not safe for little endian yet */
158 /* may be problems reading if sizes are not 32 bit long integers. */
159 /* test mallocs for failure */
161 static int FreeRTOS_update_threads(struct rtos *rtos)
164 unsigned int tasks_found = 0;
165 const struct FreeRTOS_params *param;
167 if (rtos->rtos_specific_params == NULL)
170 param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
172 if (rtos->symbols == NULL) {
173 LOG_ERROR("No symbols for FreeRTOS");
177 if (rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address == 0) {
178 LOG_ERROR("Don't have the number of threads in FreeRTOS");
182 uint32_t thread_list_size = 0;
183 retval = target_read_u32(rtos->target,
184 rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address,
186 LOG_DEBUG("FreeRTOS: Read uxCurrentNumberOfTasks at 0x%" PRIx64 ", value %" PRIu32,
187 rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address,
190 if (retval != ERROR_OK) {
191 LOG_ERROR("Could not read FreeRTOS thread count from target");
195 /* wipe out previous thread details if any */
196 rtos_free_threadlist(rtos);
198 /* read the current thread */
199 uint32_t pointer_casts_are_bad;
200 retval = target_read_u32(rtos->target,
201 rtos->symbols[FreeRTOS_VAL_pxCurrentTCB].address,
202 &pointer_casts_are_bad);
203 if (retval != ERROR_OK) {
204 LOG_ERROR("Error reading current thread in FreeRTOS thread list");
207 rtos->current_thread = pointer_casts_are_bad;
208 LOG_DEBUG("FreeRTOS: Read pxCurrentTCB at 0x%" PRIx64 ", value 0x%" PRIx64,
209 rtos->symbols[FreeRTOS_VAL_pxCurrentTCB].address,
210 rtos->current_thread);
212 if ((thread_list_size == 0) || (rtos->current_thread == 0)) {
213 /* Either : No RTOS threads - there is always at least the current execution though */
214 /* OR : No current thread - all threads suspended - show the current execution
216 char tmp_str[] = "Current Execution";
219 rtos->thread_details = malloc(
220 sizeof(struct thread_detail) * thread_list_size);
221 if (!rtos->thread_details) {
222 LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
225 rtos->thread_details->threadid = 1;
226 rtos->thread_details->exists = true;
227 rtos->thread_details->extra_info_str = NULL;
228 rtos->thread_details->thread_name_str = malloc(sizeof(tmp_str));
229 strcpy(rtos->thread_details->thread_name_str, tmp_str);
231 if (thread_list_size == 1) {
232 rtos->thread_count = 1;
236 /* create space for new thread details */
237 rtos->thread_details = malloc(
238 sizeof(struct thread_detail) * thread_list_size);
239 if (!rtos->thread_details) {
240 LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
245 /* Find out how many lists are needed to be read from pxReadyTasksLists, */
246 if (rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address == 0) {
247 LOG_ERROR("FreeRTOS: uxTopUsedPriority is not defined, consult the OpenOCD manual for a work-around");
250 uint32_t top_used_priority = 0;
251 retval = target_read_u32(rtos->target,
252 rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address,
254 if (retval != ERROR_OK)
256 LOG_DEBUG("FreeRTOS: Read uxTopUsedPriority at 0x%" PRIx64 ", value %" PRIu32,
257 rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address,
259 if (top_used_priority > FREERTOS_MAX_PRIORITIES) {
260 LOG_ERROR("FreeRTOS top used priority is unreasonably big, not proceeding: %" PRIu32,
265 /* uxTopUsedPriority was defined as configMAX_PRIORITIES - 1
266 * in old FreeRTOS versions (before V7.5.3)
267 * Use contrib/rtos-helpers/FreeRTOS-openocd.c to get compatible symbol
268 * in newer FreeRTOS versions.
269 * Here we restore the original configMAX_PRIORITIES value */
270 unsigned int config_max_priorities = top_used_priority + 1;
272 symbol_address_t *list_of_lists =
273 malloc(sizeof(symbol_address_t) * (config_max_priorities + 5));
274 if (!list_of_lists) {
275 LOG_ERROR("Error allocating memory for %u priorities", config_max_priorities);
279 unsigned int num_lists;
280 for (num_lists = 0; num_lists < config_max_priorities; num_lists++)
281 list_of_lists[num_lists] = rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address +
282 num_lists * param->list_width;
284 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList1].address;
285 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList2].address;
286 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xPendingReadyList].address;
287 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xSuspendedTaskList].address;
288 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xTasksWaitingTermination].address;
290 for (unsigned int i = 0; i < num_lists; i++) {
291 if (list_of_lists[i] == 0)
294 /* Read the number of threads in this list */
295 uint32_t list_thread_count = 0;
296 retval = target_read_u32(rtos->target,
299 if (retval != ERROR_OK) {
300 LOG_ERROR("Error reading number of threads in FreeRTOS thread list");
304 LOG_DEBUG("FreeRTOS: Read thread count for list %u at 0x%" PRIx64 ", value %" PRIu32,
305 i, list_of_lists[i], list_thread_count);
307 if (list_thread_count == 0)
310 /* Read the location of first list item */
311 uint32_t prev_list_elem_ptr = -1;
312 uint32_t list_elem_ptr = 0;
313 retval = target_read_u32(rtos->target,
314 list_of_lists[i] + param->list_next_offset,
316 if (retval != ERROR_OK) {
317 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
321 LOG_DEBUG("FreeRTOS: Read first item for list %u at 0x%" PRIx64 ", value 0x%" PRIx32,
322 i, list_of_lists[i] + param->list_next_offset, list_elem_ptr);
324 while ((list_thread_count > 0) && (list_elem_ptr != 0) &&
325 (list_elem_ptr != prev_list_elem_ptr) &&
326 (tasks_found < thread_list_size)) {
327 /* Get the location of the thread structure. */
328 rtos->thread_details[tasks_found].threadid = 0;
329 retval = target_read_u32(rtos->target,
330 list_elem_ptr + param->list_elem_content_offset,
331 &pointer_casts_are_bad);
332 if (retval != ERROR_OK) {
333 LOG_ERROR("Error reading thread list item object in FreeRTOS thread list");
337 rtos->thread_details[tasks_found].threadid = pointer_casts_are_bad;
338 LOG_DEBUG("FreeRTOS: Read Thread ID at 0x%" PRIx32 ", value 0x%" PRIx64,
339 list_elem_ptr + param->list_elem_content_offset,
340 rtos->thread_details[tasks_found].threadid);
342 /* get thread name */
344 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
345 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
347 /* Read the thread name */
348 retval = target_read_buffer(rtos->target,
349 rtos->thread_details[tasks_found].threadid + param->thread_name_offset,
350 FREERTOS_THREAD_NAME_STR_SIZE,
351 (uint8_t *)&tmp_str);
352 if (retval != ERROR_OK) {
353 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
357 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
358 LOG_DEBUG("FreeRTOS: Read Thread Name at 0x%" PRIx64 ", value '%s'",
359 rtos->thread_details[tasks_found].threadid + param->thread_name_offset,
362 if (tmp_str[0] == '\x00')
363 strcpy(tmp_str, "No Name");
365 rtos->thread_details[tasks_found].thread_name_str =
366 malloc(strlen(tmp_str)+1);
367 strcpy(rtos->thread_details[tasks_found].thread_name_str, tmp_str);
368 rtos->thread_details[tasks_found].exists = true;
370 if (rtos->thread_details[tasks_found].threadid == rtos->current_thread) {
371 char running_str[] = "State: Running";
372 rtos->thread_details[tasks_found].extra_info_str = malloc(
373 sizeof(running_str));
374 strcpy(rtos->thread_details[tasks_found].extra_info_str,
377 rtos->thread_details[tasks_found].extra_info_str = NULL;
382 prev_list_elem_ptr = list_elem_ptr;
384 retval = target_read_u32(rtos->target,
385 prev_list_elem_ptr + param->list_elem_next_offset,
387 if (retval != ERROR_OK) {
388 LOG_ERROR("Error reading next thread item location in FreeRTOS thread list");
392 LOG_DEBUG("FreeRTOS: Read next thread location at 0x%" PRIx32 ", value 0x%" PRIx32,
393 prev_list_elem_ptr + param->list_elem_next_offset,
399 rtos->thread_count = tasks_found;
403 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
404 struct rtos_reg **reg_list, int *num_regs)
407 const struct FreeRTOS_params *param;
408 int64_t stack_ptr = 0;
416 if (rtos->rtos_specific_params == NULL)
419 param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
421 /* Read the stack pointer */
422 uint32_t pointer_casts_are_bad;
423 retval = target_read_u32(rtos->target,
424 thread_id + param->thread_stack_offset,
425 &pointer_casts_are_bad);
426 if (retval != ERROR_OK) {
427 LOG_ERROR("Error reading stack frame from FreeRTOS thread");
430 stack_ptr = pointer_casts_are_bad;
431 LOG_DEBUG("FreeRTOS: Read stack pointer at 0x%" PRIx64 ", value 0x%" PRIx64,
432 thread_id + param->thread_stack_offset,
435 /* Check for armv7m with *enabled* FPU, i.e. a Cortex-M4F */
436 int cm4_fpu_enabled = 0;
437 struct armv7m_common *armv7m_target = target_to_armv7m(rtos->target);
438 if (is_armv7m(armv7m_target)) {
439 if (armv7m_target->fp_feature == FPV4_SP) {
440 /* Found ARM v7m target which includes a FPU */
443 retval = target_read_u32(rtos->target, FPU_CPACR, &cpacr);
444 if (retval != ERROR_OK) {
445 LOG_ERROR("Could not read CPACR register to check FPU state");
449 /* Check if CP10 and CP11 are set to full access. */
450 if (cpacr & 0x00F00000) {
451 /* Found target with enabled FPU */
457 if (cm4_fpu_enabled == 1) {
458 /* Read the LR to decide between stacking with or without FPU */
460 retval = target_read_u32(rtos->target,
463 if (retval != ERROR_OK) {
464 LOG_OUTPUT("Error reading stack frame from FreeRTOS thread");
467 if ((LR_svc & 0x10) == 0)
468 return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f_fpu, stack_ptr, reg_list, num_regs);
470 return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f, stack_ptr, reg_list, num_regs);
472 return rtos_generic_stack_read(rtos->target, param->stacking_info_cm3, stack_ptr, reg_list, num_regs);
475 static int FreeRTOS_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
478 *symbol_list = calloc(
479 ARRAY_SIZE(FreeRTOS_symbol_list), sizeof(struct symbol_table_elem));
481 for (i = 0; i < ARRAY_SIZE(FreeRTOS_symbol_list); i++) {
482 (*symbol_list)[i].symbol_name = FreeRTOS_symbol_list[i].name;
483 (*symbol_list)[i].optional = FreeRTOS_symbol_list[i].optional;
491 static int FreeRTOS_set_current_thread(struct rtos *rtos, threadid_t thread_id)
496 static int FreeRTOS_get_thread_ascii_info(struct rtos *rtos, threadid_t thread_id, char **info)
499 const struct FreeRTOS_params *param;
507 if (rtos->rtos_specific_params == NULL)
510 param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
512 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
513 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
515 /* Read the thread name */
516 retval = target_read_buffer(rtos->target,
517 thread_id + param->thread_name_offset,
518 FREERTOS_THREAD_NAME_STR_SIZE,
519 (uint8_t *)&tmp_str);
520 if (retval != ERROR_OK) {
521 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
524 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
526 if (tmp_str[0] == '\x00')
527 strcpy(tmp_str, "No Name");
529 *info = malloc(strlen(tmp_str)+1);
530 strcpy(*info, tmp_str);
536 static bool FreeRTOS_detect_rtos(struct target *target)
538 if ((target->rtos->symbols != NULL) &&
539 (target->rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address != 0)) {
540 /* looks like FreeRTOS */
546 static int FreeRTOS_create(struct target *target)
548 for (unsigned int i = 0; i < ARRAY_SIZE(FreeRTOS_params_list); i++)
549 if (strcmp(FreeRTOS_params_list[i].target_name, target->type->name) == 0) {
550 target->rtos->rtos_specific_params = (void *)&FreeRTOS_params_list[i];
554 LOG_ERROR("Could not find target in FreeRTOS compatibility list");