rtos: add sanity checking for FreeRTOS's quantity of priorities
[fw/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, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <helper/time_support.h>
26 #include <jtag/jtag.h>
27 #include "target/target.h"
28 #include "target/target_type.h"
29 #include "rtos.h"
30 #include "helper/log.h"
31 #include "rtos_standard_stackings.h"
32
33 #define FREERTOS_MAX_PRIORITIES 63
34
35 #define FreeRTOS_STRUCT(int_type, ptr_type, list_prev_offset)
36
37 struct FreeRTOS_params {
38         const char *target_name;
39         const unsigned char thread_count_width;
40         const unsigned char pointer_width;
41         const unsigned char list_next_offset;
42         const unsigned char list_width;
43         const unsigned char list_elem_next_offset;
44         const unsigned char list_elem_content_offset;
45         const unsigned char thread_stack_offset;
46         const unsigned char thread_name_offset;
47         const struct rtos_register_stacking *stacking_info;
48 };
49
50 const struct FreeRTOS_params FreeRTOS_params_list[] = {
51         {
52         "cortex_m3",                    /* target_name */
53         4,                                              /* thread_count_width; */
54         4,                                              /* pointer_width; */
55         16,                                             /* list_next_offset; */
56         20,                                             /* list_width; */
57         8,                                              /* list_elem_next_offset; */
58         12,                                             /* list_elem_content_offset */
59         0,                                              /* thread_stack_offset; */
60         52,                                             /* thread_name_offset; */
61         &rtos_standard_Cortex_M3_stacking,      /* stacking_info */
62         }
63 };
64
65 #define FREERTOS_NUM_PARAMS ((int)(sizeof(FreeRTOS_params_list)/sizeof(struct FreeRTOS_params)))
66
67 static int FreeRTOS_detect_rtos(struct target *target);
68 static int FreeRTOS_create(struct target *target);
69 static int FreeRTOS_update_threads(struct rtos *rtos);
70 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char **hex_reg_list);
71 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[]);
72
73 struct rtos_type FreeRTOS_rtos = {
74         .name = "FreeRTOS",
75
76         .detect_rtos = FreeRTOS_detect_rtos,
77         .create = FreeRTOS_create,
78         .update_threads = FreeRTOS_update_threads,
79         .get_thread_reg_list = FreeRTOS_get_thread_reg_list,
80         .get_symbol_list_to_lookup = FreeRTOS_get_symbol_list_to_lookup,
81 };
82
83 enum FreeRTOS_symbol_values {
84         FreeRTOS_VAL_pxCurrentTCB = 0,
85         FreeRTOS_VAL_pxReadyTasksLists = 1,
86         FreeRTOS_VAL_xDelayedTaskList1 = 2,
87         FreeRTOS_VAL_xDelayedTaskList2 = 3,
88         FreeRTOS_VAL_pxDelayedTaskList = 4,
89         FreeRTOS_VAL_pxOverflowDelayedTaskList = 5,
90         FreeRTOS_VAL_xPendingReadyList = 6,
91         FreeRTOS_VAL_xTasksWaitingTermination = 7,
92         FreeRTOS_VAL_xSuspendedTaskList = 8,
93         FreeRTOS_VAL_uxCurrentNumberOfTasks = 9,
94         FreeRTOS_VAL_uxTopUsedPriority = 10,
95 };
96
97 static char *FreeRTOS_symbol_list[] = {
98         "pxCurrentTCB",
99         "pxReadyTasksLists",
100         "xDelayedTaskList1",
101         "xDelayedTaskList2",
102         "pxDelayedTaskList",
103         "pxOverflowDelayedTaskList",
104         "xPendingReadyList",
105         "xTasksWaitingTermination",
106         "xSuspendedTaskList",
107         "uxCurrentNumberOfTasks",
108         "uxTopUsedPriority",
109         NULL
110 };
111
112 #define FREERTOS_NUM_SYMBOLS (sizeof(FreeRTOS_symbol_list)/sizeof(char *))
113
114 /* TODO: */
115 /* this is not safe for little endian yet */
116 /* may be problems reading if sizes are not 32 bit long integers. */
117 /* test mallocs for failure */
118
119 static int FreeRTOS_update_threads(struct rtos *rtos)
120 {
121         int i = 0;
122         int retval;
123         int tasks_found = 0;
124         const struct FreeRTOS_params *param;
125
126         if (rtos->rtos_specific_params == NULL)
127                 return -1;
128
129         param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
130
131         if (rtos->symbols == NULL) {
132                 LOG_OUTPUT("No symbols for FreeRTOS\r\n");
133                 return -3;
134         }
135
136         if (rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address == 0) {
137                 LOG_OUTPUT("Don't have the number of threads in FreeRTOS \r\n");
138                 return -2;
139         }
140
141         int thread_list_size = 0;
142         retval = target_read_buffer(rtos->target,
143                         rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address,
144                         param->thread_count_width,
145                         (uint8_t *)&thread_list_size);
146
147         if (retval != ERROR_OK) {
148                 LOG_OUTPUT("Could not read FreeRTOS thread count from target\r\n");
149                 return retval;
150         }
151
152         /* wipe out previous thread details if any */
153         if (rtos->thread_details != NULL) {
154                 int j;
155                 for (j = 0; j < rtos->thread_count; j++) {
156                         if (rtos->thread_details[j].display_str != NULL) {
157                                 free(rtos->thread_details[j].display_str);
158                                 rtos->thread_details[j].display_str = NULL;
159                         }
160                         if (rtos->thread_details[j].thread_name_str != NULL) {
161                                 free(rtos->thread_details[j].thread_name_str);
162                                 rtos->thread_details[j].thread_name_str = NULL;
163                         }
164                         if (rtos->thread_details[j].extra_info_str != NULL) {
165                                 free(rtos->thread_details[j].extra_info_str);
166                                 rtos->thread_details[j].extra_info_str = NULL;
167                         }
168                 }
169                 free(rtos->thread_details);
170                 rtos->thread_details = NULL;
171         }
172
173         /* read the current thread */
174         retval = target_read_buffer(rtos->target,
175                         rtos->symbols[FreeRTOS_VAL_pxCurrentTCB].address,
176                         param->pointer_width,
177                         (uint8_t *)&rtos->current_thread);
178         if (retval != ERROR_OK) {
179                 LOG_OUTPUT("Error reading current thread in FreeRTOS thread list\r\n");
180                 return retval;
181         }
182
183         if ((thread_list_size  == 0) || (rtos->current_thread == 0)) {
184                 /* Either : No RTOS threads - there is always at least the current execution though */
185                 /* OR     : No current thread - all threads suspended - show the current execution
186                  * of idling */
187                 char tmp_str[] = "Current Execution";
188                 thread_list_size++;
189                 tasks_found++;
190                 rtos->thread_details = (struct thread_detail *) malloc(
191                                 sizeof(struct thread_detail) * thread_list_size);
192                 if (!rtos->thread_details) {
193                         LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
194                         return ERROR_FAIL;
195                 }
196                 rtos->thread_details->threadid = 1;
197                 rtos->thread_details->exists = true;
198                 rtos->thread_details->display_str = NULL;
199                 rtos->thread_details->extra_info_str = NULL;
200                 rtos->thread_details->thread_name_str = (char *) malloc(sizeof(tmp_str));
201                 strcpy(rtos->thread_details->thread_name_str, tmp_str);
202
203                 if (thread_list_size == 1) {
204                         rtos->thread_count = 1;
205                         return ERROR_OK;
206                 }
207         } else {
208                 /* create space for new thread details */
209                 rtos->thread_details = (struct thread_detail *) malloc(
210                                 sizeof(struct thread_detail) * thread_list_size);
211                 if (!rtos->thread_details) {
212                         LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
213                         return ERROR_FAIL;
214                 }
215         }
216
217         /* Find out how many lists are needed to be read from pxReadyTasksLists, */
218         int64_t max_used_priority = 0;
219         retval = target_read_buffer(rtos->target,
220                         rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address,
221                         param->pointer_width,
222                         (uint8_t *)&max_used_priority);
223         if (retval != ERROR_OK)
224                 return retval;
225         if (max_used_priority > FREERTOS_MAX_PRIORITIES) {
226                 LOG_ERROR("FreeRTOS maximum used priority is unreasonably big, not proceeding: %" PRId64 "",
227                         max_used_priority);
228                 return ERROR_FAIL;
229         }
230
231         symbol_address_t *list_of_lists =
232                 (symbol_address_t *)malloc(sizeof(symbol_address_t) *
233                         (max_used_priority+1 + 5));
234         if (!list_of_lists) {
235                 LOG_ERROR("Error allocating memory for %" PRId64 " priorities", max_used_priority);
236                 return ERROR_FAIL;
237         }
238
239         int num_lists;
240         for (num_lists = 0; num_lists <= max_used_priority; num_lists++)
241                 list_of_lists[num_lists] = rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address +
242                         num_lists * param->list_width;
243
244         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList1].address;
245         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList2].address;
246         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xPendingReadyList].address;
247         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xSuspendedTaskList].address;
248         list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xTasksWaitingTermination].address;
249
250         for (i = 0; i < num_lists; i++) {
251                 if (list_of_lists[i] == 0)
252                         continue;
253
254                 /* Read the number of threads in this list */
255                 int64_t list_thread_count = 0;
256                 retval = target_read_buffer(rtos->target,
257                                 list_of_lists[i],
258                                 param->thread_count_width,
259                                 (uint8_t *)&list_thread_count);
260                 if (retval != ERROR_OK) {
261                         LOG_OUTPUT("Error reading number of threads in FreeRTOS thread list\r\n");
262                         return retval;
263                 }
264
265                 if (list_thread_count == 0)
266                         continue;
267
268                 /* Read the location of first list item */
269                 uint64_t prev_list_elem_ptr = -1;
270                 uint64_t list_elem_ptr = 0;
271                 retval = target_read_buffer(rtos->target,
272                                 list_of_lists[i] + param->list_next_offset,
273                                 param->pointer_width,
274                                 (uint8_t *)&list_elem_ptr);
275                 if (retval != ERROR_OK) {
276                         LOG_OUTPUT(
277                                 "Error reading first thread item location in FreeRTOS thread list\r\n");
278                         return retval;
279                 }
280
281                 while ((list_thread_count > 0) && (list_elem_ptr != 0) &&
282                                 (list_elem_ptr != prev_list_elem_ptr) &&
283                                 (tasks_found < thread_list_size)) {
284                         /* Get the location of the thread structure. */
285                         rtos->thread_details[tasks_found].threadid = 0;
286                         retval = target_read_buffer(rtos->target,
287                                         list_elem_ptr + param->list_elem_content_offset,
288                                         param->pointer_width,
289                                         (uint8_t *)&(rtos->thread_details[tasks_found].threadid));
290                         if (retval != ERROR_OK) {
291                                 LOG_OUTPUT(
292                                         "Error reading thread list item object in FreeRTOS thread list\r\n");
293                                 return retval;
294                         }
295
296                         /* get thread name */
297
298                         #define FREERTOS_THREAD_NAME_STR_SIZE (200)
299                         char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
300
301                         /* Read the thread name */
302                         retval = target_read_buffer(rtos->target,
303                                         rtos->thread_details[tasks_found].threadid + param->thread_name_offset,
304                                         FREERTOS_THREAD_NAME_STR_SIZE,
305                                         (uint8_t *)&tmp_str);
306                         if (retval != ERROR_OK) {
307                                 LOG_OUTPUT(
308                                         "Error reading first thread item location in FreeRTOS thread list\r\n");
309                                 return retval;
310                         }
311                         tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
312
313                         if (tmp_str[0] == '\x00')
314                                 strcpy(tmp_str, "No Name");
315
316                         rtos->thread_details[tasks_found].thread_name_str =
317                                 (char *)malloc(strlen(tmp_str)+1);
318                         strcpy(rtos->thread_details[tasks_found].thread_name_str, tmp_str);
319                         rtos->thread_details[tasks_found].display_str = NULL;
320                         rtos->thread_details[tasks_found].exists = true;
321
322                         if (rtos->thread_details[tasks_found].threadid == rtos->current_thread) {
323                                 char running_str[] = "Running";
324                                 rtos->thread_details[tasks_found].extra_info_str = (char *) malloc(
325                                                 sizeof(running_str));
326                                 strcpy(rtos->thread_details[tasks_found].extra_info_str,
327                                         running_str);
328                         } else
329                                 rtos->thread_details[tasks_found].extra_info_str = NULL;
330
331                         tasks_found++;
332                         list_thread_count--;
333
334                         prev_list_elem_ptr = list_elem_ptr;
335                         list_elem_ptr = 0;
336                         retval = target_read_buffer(rtos->target,
337                                         prev_list_elem_ptr + param->list_elem_next_offset,
338                                         param->pointer_width,
339                                         (uint8_t *)&list_elem_ptr);
340                         if (retval != ERROR_OK) {
341                                 LOG_OUTPUT(
342                                         "Error reading next thread item location in FreeRTOS thread list\r\n");
343                                 return retval;
344                         }
345                 }
346         }
347         free(list_of_lists);
348         rtos->thread_count = tasks_found;
349         return 0;
350 }
351
352 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char **hex_reg_list)
353 {
354         int retval;
355         const struct FreeRTOS_params *param;
356         int64_t stack_ptr = 0;
357
358         *hex_reg_list = NULL;
359         if (rtos == NULL)
360                 return -1;
361
362         if (thread_id == 0)
363                 return -2;
364
365         if (rtos->rtos_specific_params == NULL)
366                 return -1;
367
368         param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
369
370         /* Read the stack pointer */
371         retval = target_read_buffer(rtos->target,
372                         thread_id + param->thread_stack_offset,
373                         param->pointer_width,
374                         (uint8_t *)&stack_ptr);
375         if (retval != ERROR_OK) {
376                 LOG_OUTPUT("Error reading stack frame from FreeRTOS thread\r\n");
377                 return retval;
378         }
379
380         return rtos_generic_stack_read(rtos->target, param->stacking_info, stack_ptr, hex_reg_list);
381
382 }
383
384 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[])
385 {
386         unsigned int i;
387         *symbol_list = (symbol_table_elem_t *) malloc(
388                         sizeof(symbol_table_elem_t) * FREERTOS_NUM_SYMBOLS);
389
390         for (i = 0; i < FREERTOS_NUM_SYMBOLS; i++)
391                 (*symbol_list)[i].symbol_name = FreeRTOS_symbol_list[i];
392
393         return 0;
394 }
395
396 #if 0
397
398 static int FreeRTOS_set_current_thread(struct rtos *rtos, threadid_t thread_id)
399 {
400         return 0;
401 }
402
403 static int FreeRTOS_get_thread_ascii_info(struct rtos *rtos, threadid_t thread_id, char **info)
404 {
405         int retval;
406         const struct FreeRTOS_params *param;
407
408         if (rtos == NULL)
409                 return -1;
410
411         if (thread_id == 0)
412                 return -2;
413
414         if (rtos->rtos_specific_params == NULL)
415                 return -3;
416
417         param = (const struct FreeRTOS_params *) rtos->rtos_specific_params;
418
419 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
420         char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
421
422         /* Read the thread name */
423         retval = target_read_buffer(rtos->target,
424                         thread_id + param->thread_name_offset,
425                         FREERTOS_THREAD_NAME_STR_SIZE,
426                         (uint8_t *)&tmp_str);
427         if (retval != ERROR_OK) {
428                 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
429                 return retval;
430         }
431         tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
432
433         if (tmp_str[0] == '\x00')
434                 strcpy(tmp_str, "No Name");
435
436         *info = (char *)malloc(strlen(tmp_str)+1);
437         strcpy(*info, tmp_str);
438         return 0;
439 }
440
441 #endif
442
443 static int FreeRTOS_detect_rtos(struct target *target)
444 {
445         if ((target->rtos->symbols != NULL) &&
446                         (target->rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address != 0)) {
447                 /* looks like FreeRTOS */
448                 return 1;
449         }
450         return 0;
451 }
452
453 static int FreeRTOS_create(struct target *target)
454 {
455         int i = 0;
456         while ((i < FREERTOS_NUM_PARAMS) &&
457                         (0 != strcmp(FreeRTOS_params_list[i].target_name, target->type->name))) {
458                 i++;
459         }
460         if (i >= FREERTOS_NUM_PARAMS) {
461                 LOG_OUTPUT("Could not find target in FreeRTOS compatibility list\r\n");
462                 return -1;
463         }
464
465         target->rtos->rtos_specific_params = (void *) &FreeRTOS_params_list[i];
466         return 0;
467 }