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 #define FREERTOS_NUM_PARAMS ((int)(sizeof(FreeRTOS_params_list)/sizeof(struct FreeRTOS_params)))
107 static bool FreeRTOS_detect_rtos(struct target *target);
108 static int FreeRTOS_create(struct target *target);
109 static int FreeRTOS_update_threads(struct rtos *rtos);
110 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
111 struct rtos_reg **reg_list, int *num_regs);
112 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[]);
114 struct rtos_type FreeRTOS_rtos = {
117 .detect_rtos = FreeRTOS_detect_rtos,
118 .create = FreeRTOS_create,
119 .update_threads = FreeRTOS_update_threads,
120 .get_thread_reg_list = FreeRTOS_get_thread_reg_list,
121 .get_symbol_list_to_lookup = FreeRTOS_get_symbol_list_to_lookup,
124 enum FreeRTOS_symbol_values {
125 FreeRTOS_VAL_pxCurrentTCB = 0,
126 FreeRTOS_VAL_pxReadyTasksLists = 1,
127 FreeRTOS_VAL_xDelayedTaskList1 = 2,
128 FreeRTOS_VAL_xDelayedTaskList2 = 3,
129 FreeRTOS_VAL_pxDelayedTaskList = 4,
130 FreeRTOS_VAL_pxOverflowDelayedTaskList = 5,
131 FreeRTOS_VAL_xPendingReadyList = 6,
132 FreeRTOS_VAL_xTasksWaitingTermination = 7,
133 FreeRTOS_VAL_xSuspendedTaskList = 8,
134 FreeRTOS_VAL_uxCurrentNumberOfTasks = 9,
135 FreeRTOS_VAL_uxTopUsedPriority = 10,
143 static const struct symbols FreeRTOS_symbol_list[] = {
144 { "pxCurrentTCB", false },
145 { "pxReadyTasksLists", false },
146 { "xDelayedTaskList1", false },
147 { "xDelayedTaskList2", false },
148 { "pxDelayedTaskList", false },
149 { "pxOverflowDelayedTaskList", false },
150 { "xPendingReadyList", false },
151 { "xTasksWaitingTermination", true }, /* Only if INCLUDE_vTaskDelete */
152 { "xSuspendedTaskList", true }, /* Only if INCLUDE_vTaskSuspend */
153 { "uxCurrentNumberOfTasks", false },
154 { "uxTopUsedPriority", true }, /* Unavailable since v7.5.3 */
159 /* this is not safe for little endian yet */
160 /* may be problems reading if sizes are not 32 bit long integers. */
161 /* test mallocs for failure */
163 static int FreeRTOS_update_threads(struct rtos *rtos)
166 unsigned int tasks_found = 0;
167 const struct FreeRTOS_params *param;
169 if (rtos->rtos_specific_params == NULL)
172 param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
174 if (rtos->symbols == NULL) {
175 LOG_ERROR("No symbols for FreeRTOS");
179 if (rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address == 0) {
180 LOG_ERROR("Don't have the number of threads in FreeRTOS");
184 uint32_t thread_list_size = 0;
185 retval = target_read_u32(rtos->target,
186 rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address,
188 LOG_DEBUG("FreeRTOS: Read uxCurrentNumberOfTasks at 0x%" PRIx64 ", value %" PRIu32,
189 rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address,
192 if (retval != ERROR_OK) {
193 LOG_ERROR("Could not read FreeRTOS thread count from target");
197 /* wipe out previous thread details if any */
198 rtos_free_threadlist(rtos);
200 /* read the current thread */
201 uint32_t pointer_casts_are_bad;
202 retval = target_read_u32(rtos->target,
203 rtos->symbols[FreeRTOS_VAL_pxCurrentTCB].address,
204 &pointer_casts_are_bad);
205 if (retval != ERROR_OK) {
206 LOG_ERROR("Error reading current thread in FreeRTOS thread list");
209 rtos->current_thread = pointer_casts_are_bad;
210 LOG_DEBUG("FreeRTOS: Read pxCurrentTCB at 0x%" PRIx64 ", value 0x%" PRIx64,
211 rtos->symbols[FreeRTOS_VAL_pxCurrentTCB].address,
212 rtos->current_thread);
214 if ((thread_list_size == 0) || (rtos->current_thread == 0)) {
215 /* Either : No RTOS threads - there is always at least the current execution though */
216 /* OR : No current thread - all threads suspended - show the current execution
218 char tmp_str[] = "Current Execution";
221 rtos->thread_details = malloc(
222 sizeof(struct thread_detail) * thread_list_size);
223 if (!rtos->thread_details) {
224 LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
227 rtos->thread_details->threadid = 1;
228 rtos->thread_details->exists = true;
229 rtos->thread_details->extra_info_str = NULL;
230 rtos->thread_details->thread_name_str = malloc(sizeof(tmp_str));
231 strcpy(rtos->thread_details->thread_name_str, tmp_str);
233 if (thread_list_size == 1) {
234 rtos->thread_count = 1;
238 /* create space for new thread details */
239 rtos->thread_details = malloc(
240 sizeof(struct thread_detail) * thread_list_size);
241 if (!rtos->thread_details) {
242 LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
247 /* Find out how many lists are needed to be read from pxReadyTasksLists, */
248 if (rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address == 0) {
249 LOG_ERROR("FreeRTOS: uxTopUsedPriority is not defined, consult the OpenOCD manual for a work-around");
252 uint32_t top_used_priority = 0;
253 retval = target_read_u32(rtos->target,
254 rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address,
256 if (retval != ERROR_OK)
258 LOG_DEBUG("FreeRTOS: Read uxTopUsedPriority at 0x%" PRIx64 ", value %" PRIu32,
259 rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address,
261 if (top_used_priority > FREERTOS_MAX_PRIORITIES) {
262 LOG_ERROR("FreeRTOS top used priority is unreasonably big, not proceeding: %" PRIu32,
267 /* uxTopUsedPriority was defined as configMAX_PRIORITIES - 1
268 * in old FreeRTOS versions (before V7.5.3)
269 * Use contrib/rtos-helpers/FreeRTOS-openocd.c to get compatible symbol
270 * in newer FreeRTOS versions.
271 * Here we restore the original configMAX_PRIORITIES value */
272 unsigned int config_max_priorities = top_used_priority + 1;
274 symbol_address_t *list_of_lists =
275 malloc(sizeof(symbol_address_t) * (config_max_priorities + 5));
276 if (!list_of_lists) {
277 LOG_ERROR("Error allocating memory for %u priorities", config_max_priorities);
281 unsigned int num_lists;
282 for (num_lists = 0; num_lists < config_max_priorities; num_lists++)
283 list_of_lists[num_lists] = rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address +
284 num_lists * param->list_width;
286 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList1].address;
287 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList2].address;
288 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xPendingReadyList].address;
289 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xSuspendedTaskList].address;
290 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xTasksWaitingTermination].address;
292 for (unsigned int i = 0; i < num_lists; i++) {
293 if (list_of_lists[i] == 0)
296 /* Read the number of threads in this list */
297 uint32_t list_thread_count = 0;
298 retval = target_read_u32(rtos->target,
301 if (retval != ERROR_OK) {
302 LOG_ERROR("Error reading number of threads in FreeRTOS thread list");
306 LOG_DEBUG("FreeRTOS: Read thread count for list %u at 0x%" PRIx64 ", value %" PRIu32,
307 i, list_of_lists[i], list_thread_count);
309 if (list_thread_count == 0)
312 /* Read the location of first list item */
313 uint32_t prev_list_elem_ptr = -1;
314 uint32_t list_elem_ptr = 0;
315 retval = target_read_u32(rtos->target,
316 list_of_lists[i] + param->list_next_offset,
318 if (retval != ERROR_OK) {
319 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
323 LOG_DEBUG("FreeRTOS: Read first item for list %u at 0x%" PRIx64 ", value 0x%" PRIx32,
324 i, list_of_lists[i] + param->list_next_offset, list_elem_ptr);
326 while ((list_thread_count > 0) && (list_elem_ptr != 0) &&
327 (list_elem_ptr != prev_list_elem_ptr) &&
328 (tasks_found < thread_list_size)) {
329 /* Get the location of the thread structure. */
330 rtos->thread_details[tasks_found].threadid = 0;
331 retval = target_read_u32(rtos->target,
332 list_elem_ptr + param->list_elem_content_offset,
333 &pointer_casts_are_bad);
334 if (retval != ERROR_OK) {
335 LOG_ERROR("Error reading thread list item object in FreeRTOS thread list");
339 rtos->thread_details[tasks_found].threadid = pointer_casts_are_bad;
340 LOG_DEBUG("FreeRTOS: Read Thread ID at 0x%" PRIx32 ", value 0x%" PRIx64,
341 list_elem_ptr + param->list_elem_content_offset,
342 rtos->thread_details[tasks_found].threadid);
344 /* get thread name */
346 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
347 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
349 /* Read the thread name */
350 retval = target_read_buffer(rtos->target,
351 rtos->thread_details[tasks_found].threadid + param->thread_name_offset,
352 FREERTOS_THREAD_NAME_STR_SIZE,
353 (uint8_t *)&tmp_str);
354 if (retval != ERROR_OK) {
355 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
359 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
360 LOG_DEBUG("FreeRTOS: Read Thread Name at 0x%" PRIx64 ", value '%s'",
361 rtos->thread_details[tasks_found].threadid + param->thread_name_offset,
364 if (tmp_str[0] == '\x00')
365 strcpy(tmp_str, "No Name");
367 rtos->thread_details[tasks_found].thread_name_str =
368 malloc(strlen(tmp_str)+1);
369 strcpy(rtos->thread_details[tasks_found].thread_name_str, tmp_str);
370 rtos->thread_details[tasks_found].exists = true;
372 if (rtos->thread_details[tasks_found].threadid == rtos->current_thread) {
373 char running_str[] = "State: Running";
374 rtos->thread_details[tasks_found].extra_info_str = malloc(
375 sizeof(running_str));
376 strcpy(rtos->thread_details[tasks_found].extra_info_str,
379 rtos->thread_details[tasks_found].extra_info_str = NULL;
384 prev_list_elem_ptr = list_elem_ptr;
386 retval = target_read_u32(rtos->target,
387 prev_list_elem_ptr + param->list_elem_next_offset,
389 if (retval != ERROR_OK) {
390 LOG_ERROR("Error reading next thread item location in FreeRTOS thread list");
394 LOG_DEBUG("FreeRTOS: Read next thread location at 0x%" PRIx32 ", value 0x%" PRIx32,
395 prev_list_elem_ptr + param->list_elem_next_offset,
401 rtos->thread_count = tasks_found;
405 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
406 struct rtos_reg **reg_list, int *num_regs)
409 const struct FreeRTOS_params *param;
410 int64_t stack_ptr = 0;
418 if (rtos->rtos_specific_params == NULL)
421 param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
423 /* Read the stack pointer */
424 uint32_t pointer_casts_are_bad;
425 retval = target_read_u32(rtos->target,
426 thread_id + param->thread_stack_offset,
427 &pointer_casts_are_bad);
428 if (retval != ERROR_OK) {
429 LOG_ERROR("Error reading stack frame from FreeRTOS thread");
432 stack_ptr = pointer_casts_are_bad;
433 LOG_DEBUG("FreeRTOS: Read stack pointer at 0x%" PRIx64 ", value 0x%" PRIx64,
434 thread_id + param->thread_stack_offset,
437 /* Check for armv7m with *enabled* FPU, i.e. a Cortex-M4F */
438 int cm4_fpu_enabled = 0;
439 struct armv7m_common *armv7m_target = target_to_armv7m(rtos->target);
440 if (is_armv7m(armv7m_target)) {
441 if (armv7m_target->fp_feature == FPv4_SP) {
442 /* Found ARM v7m target which includes a FPU */
445 retval = target_read_u32(rtos->target, FPU_CPACR, &cpacr);
446 if (retval != ERROR_OK) {
447 LOG_ERROR("Could not read CPACR register to check FPU state");
451 /* Check if CP10 and CP11 are set to full access. */
452 if (cpacr & 0x00F00000) {
453 /* Found target with enabled FPU */
459 if (cm4_fpu_enabled == 1) {
460 /* Read the LR to decide between stacking with or without FPU */
462 retval = target_read_u32(rtos->target,
465 if (retval != ERROR_OK) {
466 LOG_OUTPUT("Error reading stack frame from FreeRTOS thread");
469 if ((LR_svc & 0x10) == 0)
470 return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f_fpu, stack_ptr, reg_list, num_regs);
472 return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f, stack_ptr, reg_list, num_regs);
474 return rtos_generic_stack_read(rtos->target, param->stacking_info_cm3, stack_ptr, reg_list, num_regs);
477 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[])
480 *symbol_list = calloc(
481 ARRAY_SIZE(FreeRTOS_symbol_list), sizeof(symbol_table_elem_t));
483 for (i = 0; i < ARRAY_SIZE(FreeRTOS_symbol_list); i++) {
484 (*symbol_list)[i].symbol_name = FreeRTOS_symbol_list[i].name;
485 (*symbol_list)[i].optional = FreeRTOS_symbol_list[i].optional;
493 static int FreeRTOS_set_current_thread(struct rtos *rtos, threadid_t thread_id)
498 static int FreeRTOS_get_thread_ascii_info(struct rtos *rtos, threadid_t thread_id, char **info)
501 const struct FreeRTOS_params *param;
509 if (rtos->rtos_specific_params == NULL)
512 param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
514 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
515 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
517 /* Read the thread name */
518 retval = target_read_buffer(rtos->target,
519 thread_id + param->thread_name_offset,
520 FREERTOS_THREAD_NAME_STR_SIZE,
521 (uint8_t *)&tmp_str);
522 if (retval != ERROR_OK) {
523 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
526 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
528 if (tmp_str[0] == '\x00')
529 strcpy(tmp_str, "No Name");
531 *info = malloc(strlen(tmp_str)+1);
532 strcpy(*info, tmp_str);
538 static bool FreeRTOS_detect_rtos(struct target *target)
540 if ((target->rtos->symbols != NULL) &&
541 (target->rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address != 0)) {
542 /* looks like FreeRTOS */
548 static int FreeRTOS_create(struct target *target)
551 while ((i < FREERTOS_NUM_PARAMS) &&
552 (0 != strcmp(FreeRTOS_params_list[i].target_name, target->type->name))) {
555 if (i >= FREERTOS_NUM_PARAMS) {
556 LOG_ERROR("Could not find target in FreeRTOS compatibility list");
560 target->rtos->rtos_specific_params = (void *) &FreeRTOS_params_list[i];