]> git.sur5r.net Git - openocd/blob - src/rtos/FreeRTOS.c
rtos: support gdb_get_register_packet
[openocd] / src / rtos / FreeRTOS.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Broadcom Corporation                            *
3  *   Evan Hunter - ehunter@broadcom.com                                    *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <helper/time_support.h>
24 #include <jtag/jtag.h>
25 #include "target/target.h"
26 #include "target/target_type.h"
27 #include "rtos.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"
33
34
35
36 #define FREERTOS_MAX_PRIORITIES 63
37
38 #define FreeRTOS_STRUCT(int_type, ptr_type, list_prev_offset)
39
40 struct FreeRTOS_params {
41         const char *target_name;
42         const unsigned char thread_count_width;
43         const unsigned char pointer_width;
44         const unsigned char list_next_offset;
45         const unsigned char list_width;
46         const unsigned char list_elem_next_offset;
47         const unsigned char list_elem_content_offset;
48         const unsigned char thread_stack_offset;
49         const unsigned char thread_name_offset;
50         const struct rtos_register_stacking *stacking_info_cm3;
51         const struct rtos_register_stacking *stacking_info_cm4f;
52         const struct rtos_register_stacking *stacking_info_cm4f_fpu;
53 };
54
55 static const struct FreeRTOS_params FreeRTOS_params_list[] = {
56         {
57         "cortex_m",                     /* target_name */
58         4,                                              /* thread_count_width; */
59         4,                                              /* pointer_width; */
60         16,                                             /* list_next_offset; */
61         20,                                             /* list_width; */
62         8,                                              /* list_elem_next_offset; */
63         12,                                             /* list_elem_content_offset */
64         0,                                              /* thread_stack_offset; */
65         52,                                             /* thread_name_offset; */
66         &rtos_standard_Cortex_M3_stacking,      /* stacking_info */
67         &rtos_standard_Cortex_M4F_stacking,
68         &rtos_standard_Cortex_M4F_FPU_stacking,
69         },
70         {
71         "hla_target",                   /* target_name */
72         4,                                              /* thread_count_width; */
73         4,                                              /* pointer_width; */
74         16,                                             /* list_next_offset; */
75         20,                                             /* list_width; */
76         8,                                              /* list_elem_next_offset; */
77         12,                                             /* list_elem_content_offset */
78         0,                                              /* thread_stack_offset; */
79         52,                                             /* thread_name_offset; */
80         &rtos_standard_Cortex_M3_stacking,      /* stacking_info */
81         &rtos_standard_Cortex_M4F_stacking,
82         &rtos_standard_Cortex_M4F_FPU_stacking,
83         },
84         {
85         "nds32_v3",                     /* target_name */
86         4,                                              /* thread_count_width; */
87         4,                                              /* pointer_width; */
88         16,                                             /* list_next_offset; */
89         20,                                             /* list_width; */
90         8,                                              /* list_elem_next_offset; */
91         12,                                             /* list_elem_content_offset */
92         0,                                              /* thread_stack_offset; */
93         52,                                             /* thread_name_offset; */
94         &rtos_standard_NDS32_N1068_stacking,    /* stacking_info */
95         &rtos_standard_Cortex_M4F_stacking,
96         &rtos_standard_Cortex_M4F_FPU_stacking,
97         },
98 };
99
100 #define FREERTOS_NUM_PARAMS ((int)(sizeof(FreeRTOS_params_list)/sizeof(struct FreeRTOS_params)))
101
102 static bool FreeRTOS_detect_rtos(struct target *target);
103 static int FreeRTOS_create(struct target *target);
104 static int FreeRTOS_update_threads(struct rtos *rtos);
105 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
106                 struct rtos_reg **reg_list, int *num_regs);
107 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[]);
108
109 struct rtos_type FreeRTOS_rtos = {
110         .name = "FreeRTOS",
111
112         .detect_rtos = FreeRTOS_detect_rtos,
113         .create = FreeRTOS_create,
114         .update_threads = FreeRTOS_update_threads,
115         .get_thread_reg_list = FreeRTOS_get_thread_reg_list,
116         .get_symbol_list_to_lookup = FreeRTOS_get_symbol_list_to_lookup,
117 };
118
119 enum FreeRTOS_symbol_values {
120         FreeRTOS_VAL_pxCurrentTCB = 0,
121         FreeRTOS_VAL_pxReadyTasksLists = 1,
122         FreeRTOS_VAL_xDelayedTaskList1 = 2,
123         FreeRTOS_VAL_xDelayedTaskList2 = 3,
124         FreeRTOS_VAL_pxDelayedTaskList = 4,
125         FreeRTOS_VAL_pxOverflowDelayedTaskList = 5,
126         FreeRTOS_VAL_xPendingReadyList = 6,
127         FreeRTOS_VAL_xTasksWaitingTermination = 7,
128         FreeRTOS_VAL_xSuspendedTaskList = 8,
129         FreeRTOS_VAL_uxCurrentNumberOfTasks = 9,
130         FreeRTOS_VAL_uxTopUsedPriority = 10,
131 };
132
133 struct symbols {
134         const char *name;
135         bool optional;
136 };
137
138 static const struct symbols FreeRTOS_symbol_list[] = {
139         { "pxCurrentTCB", false },
140         { "pxReadyTasksLists", false },
141         { "xDelayedTaskList1", false },
142         { "xDelayedTaskList2", false },
143         { "pxDelayedTaskList", false },
144         { "pxOverflowDelayedTaskList", false },
145         { "xPendingReadyList", false },
146         { "xTasksWaitingTermination", true }, /* Only if INCLUDE_vTaskDelete */
147         { "xSuspendedTaskList", true }, /* Only if INCLUDE_vTaskSuspend */
148         { "uxCurrentNumberOfTasks", false },
149         { "uxTopUsedPriority", true }, /* Unavailable since v7.5.3 */
150         { NULL, false }
151 };
152
153 /* TODO: */
154 /* this is not safe for little endian yet */
155 /* may be problems reading if sizes are not 32 bit long integers. */
156 /* test mallocs for failure */
157
158 static int FreeRTOS_update_threads(struct rtos *rtos)
159 {
160         int i = 0;
161         int retval;
162         int tasks_found = 0;
163         const struct FreeRTOS_params *param;
164
165         if (rtos->rtos_specific_params == NULL)
166                 return -1;
167
168         param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
169
170         if (rtos->symbols == NULL) {
171                 LOG_ERROR("No symbols for FreeRTOS");
172                 return -3;
173         }
174
175         if (rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address == 0) {
176                 LOG_ERROR("Don't have the number of threads in FreeRTOS");
177                 return -2;
178         }
179
180         int thread_list_size = 0;
181         retval = target_read_buffer(rtos->target,
182                         rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address,
183                         param->thread_count_width,
184                         (uint8_t *)&thread_list_size);
185         LOG_DEBUG("FreeRTOS: Read uxCurrentNumberOfTasks at 0x%" PRIx64 ", value %d\r\n",
186                                                                                 rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address,
187                                                                                 thread_list_size);
188
189         if (retval != ERROR_OK) {
190                 LOG_ERROR("Could not read FreeRTOS thread count from target");
191                 return retval;
192         }
193
194         /* wipe out previous thread details if any */
195         rtos_free_threadlist(rtos);
196
197         /* read the current thread */
198         retval = target_read_buffer(rtos->target,
199                         rtos->symbols[FreeRTOS_VAL_pxCurrentTCB].address,
200                         param->pointer_width,
201                         (uint8_t *)&rtos->current_thread);
202         if (retval != ERROR_OK) {
203                 LOG_ERROR("Error reading current thread in FreeRTOS thread list");
204                 return retval;
205         }
206         LOG_DEBUG("FreeRTOS: Read pxCurrentTCB at 0x%" PRIx64 ", value 0x%" PRIx64 "\r\n",
207                                                                                 rtos->symbols[FreeRTOS_VAL_pxCurrentTCB].address,
208                                                                                 rtos->current_thread);
209
210         if ((thread_list_size  == 0) || (rtos->current_thread == 0)) {
211                 /* Either : No RTOS threads - there is always at least the current execution though */
212                 /* OR     : No current thread - all threads suspended - show the current execution
213                  * of idling */
214                 char tmp_str[] = "Current Execution";
215                 thread_list_size++;
216                 tasks_found++;
217                 rtos->thread_details = malloc(
218                                 sizeof(struct thread_detail) * thread_list_size);
219                 if (!rtos->thread_details) {
220                         LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
221                         return ERROR_FAIL;
222                 }
223                 rtos->thread_details->threadid = 1;
224                 rtos->thread_details->exists = true;
225                 rtos->thread_details->extra_info_str = NULL;
226                 rtos->thread_details->thread_name_str = malloc(sizeof(tmp_str));
227                 strcpy(rtos->thread_details->thread_name_str, tmp_str);
228
229                 if (thread_list_size == 1) {
230                         rtos->thread_count = 1;
231                         return ERROR_OK;
232                 }
233         } else {
234                 /* create space for new thread details */
235                 rtos->thread_details = malloc(
236                                 sizeof(struct thread_detail) * thread_list_size);
237                 if (!rtos->thread_details) {
238                         LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
239                         return ERROR_FAIL;
240                 }
241         }
242
243         /* Find out how many lists are needed to be read from pxReadyTasksLists, */
244         if (rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address == 0) {
245                 LOG_ERROR("FreeRTOS: uxTopUsedPriority is not defined, consult the OpenOCD manual for a work-around");
246                 return ERROR_FAIL;
247         }
248         int64_t max_used_priority = 0;
249         retval = target_read_buffer(rtos->target,
250                         rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address,
251                         param->pointer_width,
252                         (uint8_t *)&max_used_priority);
253         if (retval != ERROR_OK)
254                 return retval;
255         LOG_DEBUG("FreeRTOS: Read uxTopUsedPriority at 0x%" PRIx64 ", value %" PRId64 "\r\n",
256                                                                                 rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address,
257                                                                                 max_used_priority);
258         if (max_used_priority > FREERTOS_MAX_PRIORITIES) {
259                 LOG_ERROR("FreeRTOS maximum used priority is unreasonably big, not proceeding: %" PRId64 "",
260                         max_used_priority);
261                 return ERROR_FAIL;
262         }
263
264         symbol_address_t *list_of_lists =
265                 malloc(sizeof(symbol_address_t) *
266                         (max_used_priority+1 + 5));
267         if (!list_of_lists) {
268                 LOG_ERROR("Error allocating memory for %" PRId64 " priorities", max_used_priority);
269                 return ERROR_FAIL;
270         }
271
272         int num_lists;
273         for (num_lists = 0; num_lists <= max_used_priority; num_lists++)
274                 list_of_lists[num_lists] = rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address +
275                         num_lists * param->list_width;
276
277         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList1].address;
278         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList2].address;
279         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xPendingReadyList].address;
280         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xSuspendedTaskList].address;
281         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xTasksWaitingTermination].address;
282
283         for (i = 0; i < num_lists; i++) {
284                 if (list_of_lists[i] == 0)
285                         continue;
286
287                 /* Read the number of threads in this list */
288                 int64_t list_thread_count = 0;
289                 retval = target_read_buffer(rtos->target,
290                                 list_of_lists[i],
291                                 param->thread_count_width,
292                                 (uint8_t *)&list_thread_count);
293                 if (retval != ERROR_OK) {
294                         LOG_ERROR("Error reading number of threads in FreeRTOS thread list");
295                         free(list_of_lists);
296                         return retval;
297                 }
298                 LOG_DEBUG("FreeRTOS: Read thread count for list %d at 0x%" PRIx64 ", value %" PRId64 "\r\n",
299                                                                                 i, list_of_lists[i], list_thread_count);
300
301                 if (list_thread_count == 0)
302                         continue;
303
304                 /* Read the location of first list item */
305                 uint64_t prev_list_elem_ptr = -1;
306                 uint64_t list_elem_ptr = 0;
307                 retval = target_read_buffer(rtos->target,
308                                 list_of_lists[i] + param->list_next_offset,
309                                 param->pointer_width,
310                                 (uint8_t *)&list_elem_ptr);
311                 if (retval != ERROR_OK) {
312                         LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
313                         free(list_of_lists);
314                         return retval;
315                 }
316                 LOG_DEBUG("FreeRTOS: Read first item for list %d at 0x%" PRIx64 ", value 0x%" PRIx64 "\r\n",
317                                                                                 i, list_of_lists[i] + param->list_next_offset, list_elem_ptr);
318
319                 while ((list_thread_count > 0) && (list_elem_ptr != 0) &&
320                                 (list_elem_ptr != prev_list_elem_ptr) &&
321                                 (tasks_found < thread_list_size)) {
322                         /* Get the location of the thread structure. */
323                         rtos->thread_details[tasks_found].threadid = 0;
324                         retval = target_read_buffer(rtos->target,
325                                         list_elem_ptr + param->list_elem_content_offset,
326                                         param->pointer_width,
327                                         (uint8_t *)&(rtos->thread_details[tasks_found].threadid));
328                         if (retval != ERROR_OK) {
329                                 LOG_ERROR("Error reading thread list item object in FreeRTOS thread list");
330                                 free(list_of_lists);
331                                 return retval;
332                         }
333                         LOG_DEBUG("FreeRTOS: Read Thread ID at 0x%" PRIx64 ", value 0x%" PRIx64 "\r\n",
334                                                                                 list_elem_ptr + param->list_elem_content_offset,
335                                                                                 rtos->thread_details[tasks_found].threadid);
336
337                         /* get thread name */
338
339                         #define FREERTOS_THREAD_NAME_STR_SIZE (200)
340                         char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
341
342                         /* Read the thread name */
343                         retval = target_read_buffer(rtos->target,
344                                         rtos->thread_details[tasks_found].threadid + param->thread_name_offset,
345                                         FREERTOS_THREAD_NAME_STR_SIZE,
346                                         (uint8_t *)&tmp_str);
347                         if (retval != ERROR_OK) {
348                                 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
349                                 free(list_of_lists);
350                                 return retval;
351                         }
352                         tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
353                         LOG_DEBUG("FreeRTOS: Read Thread Name at 0x%" PRIx64 ", value \"%s\"\r\n",
354                                                                                 rtos->thread_details[tasks_found].threadid + param->thread_name_offset,
355                                                                                 tmp_str);
356
357                         if (tmp_str[0] == '\x00')
358                                 strcpy(tmp_str, "No Name");
359
360                         rtos->thread_details[tasks_found].thread_name_str =
361                                 malloc(strlen(tmp_str)+1);
362                         strcpy(rtos->thread_details[tasks_found].thread_name_str, tmp_str);
363                         rtos->thread_details[tasks_found].exists = true;
364
365                         if (rtos->thread_details[tasks_found].threadid == rtos->current_thread) {
366                                 char running_str[] = "State: Running";
367                                 rtos->thread_details[tasks_found].extra_info_str = malloc(
368                                                 sizeof(running_str));
369                                 strcpy(rtos->thread_details[tasks_found].extra_info_str,
370                                         running_str);
371                         } else
372                                 rtos->thread_details[tasks_found].extra_info_str = NULL;
373
374                         tasks_found++;
375                         list_thread_count--;
376
377                         prev_list_elem_ptr = list_elem_ptr;
378                         list_elem_ptr = 0;
379                         retval = target_read_buffer(rtos->target,
380                                         prev_list_elem_ptr + param->list_elem_next_offset,
381                                         param->pointer_width,
382                                         (uint8_t *)&list_elem_ptr);
383                         if (retval != ERROR_OK) {
384                                 LOG_ERROR("Error reading next thread item location in FreeRTOS thread list");
385                                 free(list_of_lists);
386                                 return retval;
387                         }
388                         LOG_DEBUG("FreeRTOS: Read next thread location at 0x%" PRIx64 ", value 0x%" PRIx64 "\r\n",
389                                                                                 prev_list_elem_ptr + param->list_elem_next_offset,
390                                                                                 list_elem_ptr);
391                 }
392         }
393
394         free(list_of_lists);
395         rtos->thread_count = tasks_found;
396         return 0;
397 }
398
399 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
400                 struct rtos_reg **reg_list, int *num_regs)
401 {
402         int retval;
403         const struct FreeRTOS_params *param;
404         int64_t stack_ptr = 0;
405
406         if (rtos == NULL)
407                 return -1;
408
409         if (thread_id == 0)
410                 return -2;
411
412         if (rtos->rtos_specific_params == NULL)
413                 return -1;
414
415         param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
416
417         /* Read the stack pointer */
418         retval = target_read_buffer(rtos->target,
419                         thread_id + param->thread_stack_offset,
420                         param->pointer_width,
421                         (uint8_t *)&stack_ptr);
422         if (retval != ERROR_OK) {
423                 LOG_ERROR("Error reading stack frame from FreeRTOS thread");
424                 return retval;
425         }
426         LOG_DEBUG("FreeRTOS: Read stack pointer at 0x%" PRIx64 ", value 0x%" PRIx64 "\r\n",
427                                                                                 thread_id + param->thread_stack_offset,
428                                                                                 stack_ptr);
429
430         /* Check for armv7m with *enabled* FPU, i.e. a Cortex-M4F */
431         int cm4_fpu_enabled = 0;
432         struct armv7m_common *armv7m_target = target_to_armv7m(rtos->target);
433         if (is_armv7m(armv7m_target)) {
434                 if (armv7m_target->fp_feature == FPv4_SP) {
435                         /* Found ARM v7m target which includes a FPU */
436                         uint32_t cpacr;
437
438                         retval = target_read_u32(rtos->target, FPU_CPACR, &cpacr);
439                         if (retval != ERROR_OK) {
440                                 LOG_ERROR("Could not read CPACR register to check FPU state");
441                                 return -1;
442                         }
443
444                         /* Check if CP10 and CP11 are set to full access. */
445                         if (cpacr & 0x00F00000) {
446                                 /* Found target with enabled FPU */
447                                 cm4_fpu_enabled = 1;
448                         }
449                 }
450         }
451
452         if (cm4_fpu_enabled == 1) {
453                 /* Read the LR to decide between stacking with or without FPU */
454                 uint32_t LR_svc = 0;
455                 retval = target_read_buffer(rtos->target,
456                                 stack_ptr + 0x20,
457                                 param->pointer_width,
458                                 (uint8_t *)&LR_svc);
459                 if (retval != ERROR_OK) {
460                         LOG_OUTPUT("Error reading stack frame from FreeRTOS thread\r\n");
461                         return retval;
462                 }
463                 if ((LR_svc & 0x10) == 0)
464                         return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f_fpu, stack_ptr, reg_list, num_regs);
465                 else
466                         return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f, stack_ptr, reg_list, num_regs);
467         } else
468                 return rtos_generic_stack_read(rtos->target, param->stacking_info_cm3, stack_ptr, reg_list, num_regs);
469 }
470
471 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[])
472 {
473         unsigned int i;
474         *symbol_list = calloc(
475                         ARRAY_SIZE(FreeRTOS_symbol_list), sizeof(symbol_table_elem_t));
476
477         for (i = 0; i < ARRAY_SIZE(FreeRTOS_symbol_list); i++) {
478                 (*symbol_list)[i].symbol_name = FreeRTOS_symbol_list[i].name;
479                 (*symbol_list)[i].optional = FreeRTOS_symbol_list[i].optional;
480         }
481
482         return 0;
483 }
484
485 #if 0
486
487 static int FreeRTOS_set_current_thread(struct rtos *rtos, threadid_t thread_id)
488 {
489         return 0;
490 }
491
492 static int FreeRTOS_get_thread_ascii_info(struct rtos *rtos, threadid_t thread_id, char **info)
493 {
494         int retval;
495         const struct FreeRTOS_params *param;
496
497         if (rtos == NULL)
498                 return -1;
499
500         if (thread_id == 0)
501                 return -2;
502
503         if (rtos->rtos_specific_params == NULL)
504                 return -3;
505
506         param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
507
508 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
509         char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
510
511         /* Read the thread name */
512         retval = target_read_buffer(rtos->target,
513                         thread_id + param->thread_name_offset,
514                         FREERTOS_THREAD_NAME_STR_SIZE,
515                         (uint8_t *)&tmp_str);
516         if (retval != ERROR_OK) {
517                 LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
518                 return retval;
519         }
520         tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
521
522         if (tmp_str[0] == '\x00')
523                 strcpy(tmp_str, "No Name");
524
525         *info = malloc(strlen(tmp_str)+1);
526         strcpy(*info, tmp_str);
527         return 0;
528 }
529
530 #endif
531
532 static bool FreeRTOS_detect_rtos(struct target *target)
533 {
534         if ((target->rtos->symbols != NULL) &&
535                         (target->rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address != 0)) {
536                 /* looks like FreeRTOS */
537                 return true;
538         }
539         return false;
540 }
541
542 static int FreeRTOS_create(struct target *target)
543 {
544         int i = 0;
545         while ((i < FREERTOS_NUM_PARAMS) &&
546                         (0 != strcmp(FreeRTOS_params_list[i].target_name, target->type->name))) {
547                 i++;
548         }
549         if (i >= FREERTOS_NUM_PARAMS) {
550                 LOG_ERROR("Could not find target in FreeRTOS compatibility list");
551                 return -1;
552         }
553
554         target->rtos->rtos_specific_params = (void *) &FreeRTOS_params_list[i];
555         return 0;
556 }