openocd: src/rtos: replace the GPL-2.0-or-later license tag
[fw/openocd] / src / rtos / FreeRTOS.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4  *   Copyright (C) 2011 by Broadcom Corporation                            *
5  *   Evan Hunter - ehunter@broadcom.com                                    *
6  ***************************************************************************/
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <helper/time_support.h>
13 #include <jtag/jtag.h>
14 #include "target/target.h"
15 #include "target/target_type.h"
16 #include "rtos.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"
22
23 #define FREERTOS_MAX_PRIORITIES 63
24
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
27  * bit target!
28  */
29
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;
43 };
44
45 static const struct freertos_params freertos_params_list[] = {
46         {
47         "cortex_m",                     /* target_name */
48         4,                                              /* thread_count_width; */
49         4,                                              /* pointer_width; */
50         16,                                             /* list_next_offset; */
51         20,                                             /* list_width; */
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,
59         },
60         {
61         "hla_target",                   /* target_name */
62         4,                                              /* thread_count_width; */
63         4,                                              /* pointer_width; */
64         16,                                             /* list_next_offset; */
65         20,                                             /* list_width; */
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,
73         },
74         {
75         "nds32_v3",                     /* target_name */
76         4,                                              /* thread_count_width; */
77         4,                                              /* pointer_width; */
78         16,                                             /* list_next_offset; */
79         20,                                             /* list_width; */
80         8,                                              /* list_elem_next_offset; */
81         12,                                             /* list_elem_content_offset */
82         0,                                              /* thread_stack_offset; */
83         52,                                             /* thread_name_offset; */
84         &rtos_standard_nds32_n1068_stacking,    /* stacking_info */
85         &rtos_standard_cortex_m4f_stacking,
86         &rtos_standard_cortex_m4f_fpu_stacking,
87         },
88 };
89
90 static bool freertos_detect_rtos(struct target *target);
91 static int freertos_create(struct target *target);
92 static int freertos_update_threads(struct rtos *rtos);
93 static int freertos_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
94                 struct rtos_reg **reg_list, int *num_regs);
95 static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
96
97 struct rtos_type freertos_rtos = {
98         .name = "FreeRTOS",
99
100         .detect_rtos = freertos_detect_rtos,
101         .create = freertos_create,
102         .update_threads = freertos_update_threads,
103         .get_thread_reg_list = freertos_get_thread_reg_list,
104         .get_symbol_list_to_lookup = freertos_get_symbol_list_to_lookup,
105 };
106
107 enum freertos_symbol_values {
108         FREERTOS_VAL_PX_CURRENT_TCB = 0,
109         FREERTOS_VAL_PX_READY_TASKS_LISTS = 1,
110         FREERTOS_VAL_X_DELAYED_TASK_LIST1 = 2,
111         FREERTOS_VAL_X_DELAYED_TASK_LIST2 = 3,
112         FREERTOS_VAL_PX_DELAYED_TASK_LIST = 4,
113         FREERTOS_VAL_PX_OVERFLOW_DELAYED_TASK_LIST = 5,
114         FREERTOS_VAL_X_PENDING_READY_LIST = 6,
115         FREERTOS_VAL_X_TASKS_WAITING_TERMINATION = 7,
116         FREERTOS_VAL_X_SUSPENDED_TASK_LIST = 8,
117         FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS = 9,
118         FREERTOS_VAL_UX_TOP_USED_PRIORITY = 10,
119 };
120
121 struct symbols {
122         const char *name;
123         bool optional;
124 };
125
126 static const struct symbols freertos_symbol_list[] = {
127         { "pxCurrentTCB", false },
128         { "pxReadyTasksLists", false },
129         { "xDelayedTaskList1", false },
130         { "xDelayedTaskList2", false },
131         { "pxDelayedTaskList", false },
132         { "pxOverflowDelayedTaskList", false },
133         { "xPendingReadyList", false },
134         { "xTasksWaitingTermination", true }, /* Only if INCLUDE_vTaskDelete */
135         { "xSuspendedTaskList", true }, /* Only if INCLUDE_vTaskSuspend */
136         { "uxCurrentNumberOfTasks", false },
137         { "uxTopUsedPriority", true }, /* Unavailable since v7.5.3 */
138         { NULL, false }
139 };
140
141 /* TODO: */
142 /* this is not safe for little endian yet */
143 /* may be problems reading if sizes are not 32 bit long integers. */
144 /* test mallocs for failure */
145
146 static int freertos_update_threads(struct rtos *rtos)
147 {
148         int retval;
149         unsigned int tasks_found = 0;
150         const struct freertos_params *param;
151
152         if (!rtos->rtos_specific_params)
153                 return -1;
154
155         param = (const struct freertos_params *) rtos->rtos_specific_params;
156
157         if (!rtos->symbols) {
158                 LOG_ERROR("No symbols for FreeRTOS");
159                 return -3;
160         }
161
162         if (rtos->symbols[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS].address == 0) {
163                 LOG_ERROR("Don't have the number of threads in FreeRTOS");
164                 return -2;
165         }
166
167         uint32_t thread_list_size = 0;
168         retval = target_read_u32(rtos->target,
169                         rtos->symbols[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS].address,
170                         &thread_list_size);
171         LOG_DEBUG("FreeRTOS: Read uxCurrentNumberOfTasks at 0x%" PRIx64 ", value %" PRIu32,
172                                                                                 rtos->symbols[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS].address,
173                                                                                 thread_list_size);
174
175         if (retval != ERROR_OK) {
176                 LOG_ERROR("Could not read FreeRTOS thread count from target");
177                 return retval;
178         }
179
180         /* wipe out previous thread details if any */
181         rtos_free_threadlist(rtos);
182
183         /* read the current thread */
184         uint32_t pointer_casts_are_bad;
185         retval = target_read_u32(rtos->target,
186                         rtos->symbols[FREERTOS_VAL_PX_CURRENT_TCB].address,
187                         &pointer_casts_are_bad);
188         if (retval != ERROR_OK) {
189                 LOG_ERROR("Error reading current thread in FreeRTOS thread list");
190                 return retval;
191         }
192         rtos->current_thread = pointer_casts_are_bad;
193         LOG_DEBUG("FreeRTOS: Read pxCurrentTCB at 0x%" PRIx64 ", value 0x%" PRIx64,
194                                                                                 rtos->symbols[FREERTOS_VAL_PX_CURRENT_TCB].address,
195                                                                                 rtos->current_thread);
196
197         if ((thread_list_size  == 0) || (rtos->current_thread == 0)) {
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
200                  * of idling */
201                 char tmp_str[] = "Current Execution";
202                 thread_list_size++;
203                 tasks_found++;
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);
208                         return ERROR_FAIL;
209                 }
210                 rtos->thread_details->threadid = 1;
211                 rtos->thread_details->exists = true;
212                 rtos->thread_details->extra_info_str = NULL;
213                 rtos->thread_details->thread_name_str = malloc(sizeof(tmp_str));
214                 strcpy(rtos->thread_details->thread_name_str, tmp_str);
215
216                 if (thread_list_size == 1) {
217                         rtos->thread_count = 1;
218                         return ERROR_OK;
219                 }
220         } else {
221                 /* create space for new thread details */
222                 rtos->thread_details = malloc(
223                                 sizeof(struct thread_detail) * thread_list_size);
224                 if (!rtos->thread_details) {
225                         LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
226                         return ERROR_FAIL;
227                 }
228         }
229
230         /* Find out how many lists are needed to be read from pxReadyTasksLists, */
231         if (rtos->symbols[FREERTOS_VAL_UX_TOP_USED_PRIORITY].address == 0) {
232                 LOG_ERROR("FreeRTOS: uxTopUsedPriority is not defined, consult the OpenOCD manual for a work-around");
233                 return ERROR_FAIL;
234         }
235         uint32_t top_used_priority = 0;
236         retval = target_read_u32(rtos->target,
237                         rtos->symbols[FREERTOS_VAL_UX_TOP_USED_PRIORITY].address,
238                         &top_used_priority);
239         if (retval != ERROR_OK)
240                 return retval;
241         LOG_DEBUG("FreeRTOS: Read uxTopUsedPriority at 0x%" PRIx64 ", value %" PRIu32,
242                                                                                 rtos->symbols[FREERTOS_VAL_UX_TOP_USED_PRIORITY].address,
243                                                                                 top_used_priority);
244         if (top_used_priority > FREERTOS_MAX_PRIORITIES) {
245                 LOG_ERROR("FreeRTOS top used priority is unreasonably big, not proceeding: %" PRIu32,
246                         top_used_priority);
247                 return ERROR_FAIL;
248         }
249
250         /* uxTopUsedPriority was defined as configMAX_PRIORITIES - 1
251          * in old FreeRTOS versions (before V7.5.3)
252          * Use contrib/rtos-helpers/FreeRTOS-openocd.c to get compatible symbol
253          * in newer FreeRTOS versions.
254          * Here we restore the original configMAX_PRIORITIES value */
255         unsigned int config_max_priorities = top_used_priority + 1;
256
257         symbol_address_t *list_of_lists =
258                 malloc(sizeof(symbol_address_t) * (config_max_priorities + 5));
259         if (!list_of_lists) {
260                 LOG_ERROR("Error allocating memory for %u priorities", config_max_priorities);
261                 return ERROR_FAIL;
262         }
263
264         unsigned int num_lists;
265         for (num_lists = 0; num_lists < config_max_priorities; num_lists++)
266                 list_of_lists[num_lists] = rtos->symbols[FREERTOS_VAL_PX_READY_TASKS_LISTS].address +
267                         num_lists * param->list_width;
268
269         list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_DELAYED_TASK_LIST1].address;
270         list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_DELAYED_TASK_LIST2].address;
271         list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_PENDING_READY_LIST].address;
272         list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_SUSPENDED_TASK_LIST].address;
273         list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_TASKS_WAITING_TERMINATION].address;
274
275         for (unsigned int i = 0; i < num_lists; i++) {
276                 if (list_of_lists[i] == 0)
277                         continue;
278
279                 /* Read the number of threads in this list */
280                 uint32_t list_thread_count = 0;
281                 retval = target_read_u32(rtos->target,
282                                 list_of_lists[i],
283                                 &list_thread_count);
284                 if (retval != ERROR_OK) {
285                         LOG_ERROR("Error reading number of threads in FreeRTOS thread list");
286                         free(list_of_lists);
287                         return retval;
288                 }
289                 LOG_DEBUG("FreeRTOS: Read thread count for list %u at 0x%" PRIx64 ", value %" PRIu32,
290                                                                                 i, list_of_lists[i], list_thread_count);
291
292                 if (list_thread_count == 0)
293                         continue;
294
295                 /* Read the location of first list item */
296                 uint32_t prev_list_elem_ptr = -1;
297                 uint32_t list_elem_ptr = 0;
298                 retval = target_read_u32(rtos->target,
299                                 list_of_lists[i] + param->list_next_offset,
300                                 &list_elem_ptr);
301                 if (retval != ERROR_OK) {
302                         LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
303                         free(list_of_lists);
304                         return retval;
305                 }
306                 LOG_DEBUG("FreeRTOS: Read first item for list %u at 0x%" PRIx64 ", value 0x%" PRIx32,
307                                                                                 i, list_of_lists[i] + param->list_next_offset, list_elem_ptr);
308
309                 while ((list_thread_count > 0) && (list_elem_ptr != 0) &&
310                                 (list_elem_ptr != prev_list_elem_ptr) &&
311                                 (tasks_found < thread_list_size)) {
312                         /* Get the location of the thread structure. */
313                         rtos->thread_details[tasks_found].threadid = 0;
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");
319                                 free(list_of_lists);
320                                 return retval;
321                         }
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);
326
327                         /* get thread name */
328
329                         #define FREERTOS_THREAD_NAME_STR_SIZE (200)
330                         char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
331
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");
339                                 free(list_of_lists);
340                                 return retval;
341                         }
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,
345                                                                                 tmp_str);
346
347                         if (tmp_str[0] == '\x00')
348                                 strcpy(tmp_str, "No Name");
349
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;
354
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,
360                                         running_str);
361                         } else
362                                 rtos->thread_details[tasks_found].extra_info_str = NULL;
363
364                         tasks_found++;
365                         list_thread_count--;
366
367                         prev_list_elem_ptr = list_elem_ptr;
368                         list_elem_ptr = 0;
369                         retval = target_read_u32(rtos->target,
370                                         prev_list_elem_ptr + param->list_elem_next_offset,
371                                         &list_elem_ptr);
372                         if (retval != ERROR_OK) {
373                                 LOG_ERROR("Error reading next thread item location in FreeRTOS thread list");
374                                 free(list_of_lists);
375                                 return retval;
376                         }
377                         LOG_DEBUG("FreeRTOS: Read next thread location at 0x%" PRIx32 ", value 0x%" PRIx32,
378                                                                                 prev_list_elem_ptr + param->list_elem_next_offset,
379                                                                                 list_elem_ptr);
380                 }
381         }
382
383         free(list_of_lists);
384         rtos->thread_count = tasks_found;
385         return 0;
386 }
387
388 static int freertos_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
389                 struct rtos_reg **reg_list, int *num_regs)
390 {
391         int retval;
392         const struct freertos_params *param;
393         int64_t stack_ptr = 0;
394
395         if (!rtos)
396                 return -1;
397
398         if (thread_id == 0)
399                 return -2;
400
401         if (!rtos->rtos_specific_params)
402                 return -1;
403
404         param = (const struct freertos_params *) rtos->rtos_specific_params;
405
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");
413                 return retval;
414         }
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,
418                                                                                 stack_ptr);
419
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) {
425                         /* Found ARM v7m target which includes a FPU */
426                         uint32_t cpacr;
427
428                         retval = target_read_u32(rtos->target, FPU_CPACR, &cpacr);
429                         if (retval != ERROR_OK) {
430                                 LOG_ERROR("Could not read CPACR register to check FPU state");
431                                 return -1;
432                         }
433
434                         /* Check if CP10 and CP11 are set to full access. */
435                         if (cpacr & 0x00F00000) {
436                                 /* Found target with enabled FPU */
437                                 cm4_fpu_enabled = 1;
438                         }
439                 }
440         }
441
442         if (cm4_fpu_enabled == 1) {
443                 /* Read the LR to decide between stacking with or without FPU */
444                 uint32_t lr_svc = 0;
445                 retval = target_read_u32(rtos->target,
446                                 stack_ptr + 0x20,
447                                 &lr_svc);
448                 if (retval != ERROR_OK) {
449                         LOG_OUTPUT("Error reading stack frame from FreeRTOS thread");
450                         return retval;
451                 }
452                 if ((lr_svc & 0x10) == 0)
453                         return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f_fpu, stack_ptr, reg_list, num_regs);
454                 else
455                         return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f, stack_ptr, reg_list, num_regs);
456         } else
457                 return rtos_generic_stack_read(rtos->target, param->stacking_info_cm3, stack_ptr, reg_list, num_regs);
458 }
459
460 static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
461 {
462         unsigned int i;
463         *symbol_list = calloc(
464                         ARRAY_SIZE(freertos_symbol_list), sizeof(struct symbol_table_elem));
465
466         for (i = 0; i < ARRAY_SIZE(freertos_symbol_list); i++) {
467                 (*symbol_list)[i].symbol_name = freertos_symbol_list[i].name;
468                 (*symbol_list)[i].optional = freertos_symbol_list[i].optional;
469         }
470
471         return 0;
472 }
473
474 #if 0
475
476 static int freertos_set_current_thread(struct rtos *rtos, threadid_t thread_id)
477 {
478         return 0;
479 }
480
481 static int freertos_get_thread_ascii_info(struct rtos *rtos, threadid_t thread_id, char **info)
482 {
483         int retval;
484         const struct freertos_params *param;
485
486         if (!rtos)
487                 return -1;
488
489         if (thread_id == 0)
490                 return -2;
491
492         if (!rtos->rtos_specific_params)
493                 return -3;
494
495         param = (const struct freertos_params *) rtos->rtos_specific_params;
496
497 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
498         char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
499
500         /* Read the thread name */
501         retval = target_read_buffer(rtos->target,
502                         thread_id + param->thread_name_offset,
503                         FREERTOS_THREAD_NAME_STR_SIZE,
504                         (uint8_t *)&tmp_str);
505         if (retval != ERROR_OK) {
506                 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
507                 return retval;
508         }
509         tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
510
511         if (tmp_str[0] == '\x00')
512                 strcpy(tmp_str, "No Name");
513
514         *info = malloc(strlen(tmp_str)+1);
515         strcpy(*info, tmp_str);
516         return 0;
517 }
518
519 #endif
520
521 static bool freertos_detect_rtos(struct target *target)
522 {
523         if ((target->rtos->symbols) &&
524                         (target->rtos->symbols[FREERTOS_VAL_PX_READY_TASKS_LISTS].address != 0)) {
525                 /* looks like FreeRTOS */
526                 return true;
527         }
528         return false;
529 }
530
531 static int freertos_create(struct target *target)
532 {
533         for (unsigned int i = 0; i < ARRAY_SIZE(freertos_params_list); i++)
534                 if (strcmp(freertos_params_list[i].target_name, target->type->name) == 0) {
535                         target->rtos->rtos_specific_params = (void *)&freertos_params_list[i];
536                         return 0;
537                 }
538
539         LOG_ERROR("Could not find target in FreeRTOS compatibility list");
540         return -1;
541 }