rtos: removed chSysInit from detection of ChibiOS (#121)
[openocd.git] / src / rtos / ThreadX.c
index add34b4f4db8b2c2da8cb3dd0032e8d74317422c..229bf72e532de645c33068938475b503053a70e1 100644 (file)
@@ -13,9 +13,7 @@
  *   GNU General Public License for more details.                          *
  *                                                                         *
  *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 #include "helper/types.h"
 #include "rtos_standard_stackings.h"
 
+static const struct rtos_register_stacking *get_stacking_info(const struct rtos *rtos, int64_t stack_ptr);
+static const struct rtos_register_stacking *get_stacking_info_arm926ejs(const struct rtos *rtos, int64_t stack_ptr);
+
+static int is_thread_id_valid(const struct rtos *rtos, int64_t thread_id);
+static int is_thread_id_valid_arm926ejs(const struct rtos *rtos, int64_t thread_id);
+
 static int ThreadX_detect_rtos(struct target *target);
 static int ThreadX_create(struct target *target);
 static int ThreadX_update_threads(struct rtos *rtos);
 static int ThreadX_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char **hex_reg_list);
 static int ThreadX_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[]);
 
+
+
 struct ThreadX_thread_state {
        int value;
-       char *desc;
+       const char *desc;
 };
 
-struct ThreadX_thread_state ThreadX_thread_states[] = {
+static const struct ThreadX_thread_state ThreadX_thread_states[] = {
        { 0,  "Ready" },
        { 1,  "Completed" },
        { 2,  "Terminated" },
@@ -61,17 +67,80 @@ struct ThreadX_thread_state ThreadX_thread_states[] = {
 
 #define THREADX_NUM_STATES (sizeof(ThreadX_thread_states)/sizeof(struct ThreadX_thread_state))
 
+#define ARM926EJS_REGISTERS_SIZE_SOLICITED (11 * 4)
+static const struct stack_register_offset rtos_threadx_arm926ejs_stack_offsets_solicited[] = {
+       { -1,   32 },           /* r0        */
+       { -1,   32 },           /* r1        */
+       { -1,   32 },           /* r2        */
+       { -1,   32 },           /* r3        */
+       { 0x08, 32 },           /* r4        */
+       { 0x0C, 32 },           /* r5        */
+       { 0x10, 32 },           /* r6        */
+       { 0x14, 32 },           /* r7        */
+       { 0x18, 32 },           /* r8        */
+       { 0x1C, 32 },           /* r9        */
+       { 0x20, 32 },           /* r10       */
+       { 0x24, 32 },           /* r11       */
+       { -1,   32 },           /* r12       */
+       { -2,   32 },           /* sp (r13)  */
+       { 0x28, 32 },           /* lr (r14)  */
+       { -1,   32 },           /* pc (r15)  */
+       /*{ -1,   32 },*/               /* lr (r14)  */
+       /*{ 0x28, 32 },*/               /* pc (r15)  */
+       { 0x04, 32 },           /* xPSR      */
+};
+#define ARM926EJS_REGISTERS_SIZE_INTERRUPT (17 * 4)
+static const struct stack_register_offset rtos_threadx_arm926ejs_stack_offsets_interrupt[] = {
+       { 0x08, 32 },           /* r0        */
+       { 0x0C, 32 },           /* r1        */
+       { 0x10, 32 },           /* r2        */
+       { 0x14, 32 },           /* r3        */
+       { 0x18, 32 },           /* r4        */
+       { 0x1C, 32 },           /* r5        */
+       { 0x20, 32 },           /* r6        */
+       { 0x24, 32 },           /* r7        */
+       { 0x28, 32 },           /* r8        */
+       { 0x2C, 32 },           /* r9        */
+       { 0x30, 32 },           /* r10       */
+       { 0x34, 32 },           /* r11       */
+       { 0x38, 32 },           /* r12       */
+       { -2,   32 },           /* sp (r13)  */
+       { 0x3C, 32 },           /* lr (r14)  */
+       { 0x40, 32 },           /* pc (r15)  */
+       { 0x04, 32 },           /* xPSR      */
+};
+
+const struct rtos_register_stacking rtos_threadx_arm926ejs_stacking[] = {
+{
+       ARM926EJS_REGISTERS_SIZE_SOLICITED,     /* stack_registers_size */
+       -1,                                                                     /* stack_growth_direction */
+       17,                                                                     /* num_output_registers */
+       NULL,                                                           /* stack_alignment */
+       rtos_threadx_arm926ejs_stack_offsets_solicited  /* register_offsets */
+},
+{
+       ARM926EJS_REGISTERS_SIZE_INTERRUPT,     /* stack_registers_size */
+       -1,                                                                     /* stack_growth_direction */
+       17,                                                                     /* num_output_registers */
+       NULL,                                                           /* stack_alignment */
+       rtos_threadx_arm926ejs_stack_offsets_interrupt  /* register_offsets */
+},
+};
+
 struct ThreadX_params {
-       char *target_name;
+       const char *target_name;
        unsigned char pointer_width;
        unsigned char thread_stack_offset;
        unsigned char thread_name_offset;
        unsigned char thread_state_offset;
        unsigned char thread_next_offset;
        const struct rtos_register_stacking *stacking_info;
+       size_t stacking_info_nb;
+       const struct rtos_register_stacking* (*fn_get_stacking_info)(const struct rtos *rtos, int64_t stack_ptr);
+       int (*fn_is_thread_id_valid)(const struct rtos *rtos, int64_t thread_id);
 };
 
-const struct ThreadX_params ThreadX_params_list[] = {
+static const struct ThreadX_params ThreadX_params_list[] = {
        {
        "cortex_m",                             /* target_name */
        4,                                                      /* pointer_width; */
@@ -80,6 +149,9 @@ const struct ThreadX_params ThreadX_params_list[] = {
        48,                                                     /* thread_state_offset; */
        136,                                            /* thread_next_offset */
        &rtos_standard_Cortex_M3_stacking,      /* stacking_info */
+       1,                                                      /* stacking_info_nb */
+       NULL,                                           /* fn_get_stacking_info */
+       NULL,                                           /* fn_is_thread_id_valid */
        },
        {
        "cortex_r4",                            /* target_name */
@@ -89,6 +161,21 @@ const struct ThreadX_params ThreadX_params_list[] = {
        48,                                                     /* thread_state_offset; */
        136,                                            /* thread_next_offset */
        &rtos_standard_Cortex_R4_stacking,      /* stacking_info */
+       1,                                                      /* stacking_info_nb */
+       NULL,                                           /* fn_get_stacking_info */
+       NULL,                                           /* fn_is_thread_id_valid */
+       },
+       {
+       "arm926ejs",                            /* target_name */
+       4,                                                      /* pointer_width; */
+       8,                                                      /* thread_stack_offset; */
+       40,                                                     /* thread_name_offset; */
+       48,                                                     /* thread_state_offset; */
+       136,                                            /* thread_next_offset */
+       rtos_threadx_arm926ejs_stacking,        /* stacking_info */
+       2,                                                                      /* stacking_info_nb */
+       get_stacking_info_arm926ejs,            /* fn_get_stacking_info */
+       is_thread_id_valid_arm926ejs,           /* fn_is_thread_id_valid */
        },
 };
 
@@ -100,7 +187,7 @@ enum ThreadX_symbol_values {
        ThreadX_VAL_tx_thread_created_count = 2,
 };
 
-static char *ThreadX_symbol_list[] = {
+static const char * const ThreadX_symbol_list[] = {
        "_tx_thread_current_ptr",
        "_tx_thread_created_ptr",
        "_tx_thread_created_count",
@@ -115,9 +202,62 @@ const struct rtos_type ThreadX_rtos = {
        .update_threads = ThreadX_update_threads,
        .get_thread_reg_list = ThreadX_get_thread_reg_list,
        .get_symbol_list_to_lookup = ThreadX_get_symbol_list_to_lookup,
-
 };
 
+static const struct rtos_register_stacking *get_stacking_info(const struct rtos *rtos, int64_t stack_ptr)
+{
+       const struct ThreadX_params *param = (const struct ThreadX_params *) rtos->rtos_specific_params;
+
+       if (param->fn_get_stacking_info != NULL)
+               return param->fn_get_stacking_info(rtos, stack_ptr);
+
+       return param->stacking_info + 0;
+}
+
+static int is_thread_id_valid(const struct rtos *rtos, int64_t thread_id)
+{
+       const struct ThreadX_params *param;
+
+       if (rtos->rtos_specific_params == NULL)
+               return 0; /* invalid */
+
+       param = (const struct ThreadX_params *) rtos->rtos_specific_params;
+
+       if (param->fn_is_thread_id_valid != NULL)
+               return param->fn_is_thread_id_valid(rtos, thread_id);
+
+       return (thread_id != 0);
+}
+
+static const struct rtos_register_stacking *get_stacking_info_arm926ejs(const struct rtos *rtos, int64_t stack_ptr)
+{
+       const struct ThreadX_params *param = (const struct ThreadX_params *) rtos->rtos_specific_params;
+       int     retval;
+       uint32_t flag;
+
+       retval = target_read_buffer(rtos->target,
+                       stack_ptr,
+                       sizeof(flag),
+                       (uint8_t *)&flag);
+       if (retval != ERROR_OK) {
+               LOG_ERROR("Error reading stack data from ThreadX thread: stack_ptr=0x%" PRIx64, stack_ptr);
+               return NULL;
+       }
+
+       if (flag == 0) {
+               LOG_DEBUG("  solicited stack");
+               return param->stacking_info + 0;
+       } else {
+               LOG_DEBUG("  interrupt stack: %u", flag);
+               return param->stacking_info + 1;
+       }
+}
+
+static int is_thread_id_valid_arm926ejs(const struct rtos *rtos, int64_t thread_id)
+{
+       return (thread_id != 0 && thread_id != 1);
+}
+
 static int ThreadX_update_threads(struct rtos *rtos)
 {
        int retval;
@@ -175,13 +315,13 @@ static int ThreadX_update_threads(struct rtos *rtos)
                char tmp_str[] = "Current Execution";
                thread_list_size++;
                tasks_found++;
-               rtos->thread_details = (struct thread_detail *) malloc(
+               rtos->thread_details = malloc(
                                sizeof(struct thread_detail) * thread_list_size);
                rtos->thread_details->threadid = 1;
                rtos->thread_details->exists = true;
                rtos->thread_details->display_str = NULL;
                rtos->thread_details->extra_info_str = NULL;
-               rtos->thread_details->thread_name_str = (char *) malloc(sizeof(tmp_str));
+               rtos->thread_details->thread_name_str = malloc(sizeof(tmp_str));
                strcpy(rtos->thread_details->thread_name_str, tmp_str);
 
                if (thread_list_size == 0) {
@@ -190,7 +330,7 @@ static int ThreadX_update_threads(struct rtos *rtos)
                }
        } else {
                /* create space for new thread details */
-               rtos->thread_details = (struct thread_detail *) malloc(
+               rtos->thread_details = malloc(
                                sizeof(struct thread_detail) * thread_list_size);
        }
 
@@ -243,7 +383,7 @@ static int ThreadX_update_threads(struct rtos *rtos)
                        strcpy(tmp_str, "No Name");
 
                rtos->thread_details[tasks_found].thread_name_str =
-                       (char *)malloc(strlen(tmp_str)+1);
+                       malloc(strlen(tmp_str)+1);
                strcpy(rtos->thread_details[tasks_found].thread_name_str, tmp_str);
 
                /* Read the thread status */
@@ -262,13 +402,13 @@ static int ThreadX_update_threads(struct rtos *rtos)
                        /* empty */
                }
 
-               char *state_desc;
+               const char *state_desc;
                if  (i < THREADX_NUM_STATES)
                        state_desc = ThreadX_thread_states[i].desc;
                else
                        state_desc = "Unknown state";
 
-               rtos->thread_details[tasks_found].extra_info_str = (char *)malloc(strlen(
+               rtos->thread_details[tasks_found].extra_info_str = malloc(strlen(
                                        state_desc)+1);
                strcpy(rtos->thread_details[tasks_found].extra_info_str, state_desc);
 
@@ -306,7 +446,7 @@ static int ThreadX_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, cha
        if (rtos == NULL)
                return -1;
 
-       if (thread_id == 0)
+       if (!is_thread_id_valid(rtos, thread_id))
                return -2;
 
        if (rtos->rtos_specific_params == NULL)
@@ -325,14 +465,29 @@ static int ThreadX_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, cha
                return retval;
        }
 
-       return rtos_generic_stack_read(rtos->target, param->stacking_info, stack_ptr, hex_reg_list);
+       LOG_INFO("thread: 0x%" PRIx64 ", stack_ptr=0x%" PRIx64, (uint64_t)thread_id, (uint64_t)stack_ptr);
+
+       if (stack_ptr == 0) {
+               LOG_ERROR("null stack pointer in thread");
+               return -5;
+       }
+
+       const struct rtos_register_stacking *stacking_info =
+                       get_stacking_info(rtos, stack_ptr);
+
+       if (stacking_info == NULL) {
+               LOG_ERROR("Unknown stacking info for thread id=0x%" PRIx64, (uint64_t)thread_id);
+               return -6;
+       }
+
+       return rtos_generic_stack_read(rtos->target, stacking_info, stack_ptr, hex_reg_list);
 }
 
 static int ThreadX_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[])
 {
        unsigned int i;
-       *symbol_list = (symbol_table_elem_t *) malloc(
-                       sizeof(symbol_table_elem_t) * ARRAY_SIZE(ThreadX_symbol_list));
+       *symbol_list = calloc(
+                       ARRAY_SIZE(ThreadX_symbol_list), sizeof(symbol_table_elem_t));
 
        for (i = 0; i < ARRAY_SIZE(ThreadX_symbol_list); i++)
                (*symbol_list)[i].symbol_name = ThreadX_symbol_list[i];
@@ -412,7 +567,7 @@ static int ThreadX_get_thread_detail(struct rtos *rtos,
        if (tmp_str[0] == '\x00')
                strcpy(tmp_str, "No Name");
 
-       detail->thread_name_str = (char *)malloc(strlen(tmp_str)+1);
+       detail->thread_name_str = malloc(strlen(tmp_str)+1);
 
        /* Read the thread status */
        int64_t thread_status = 0;
@@ -437,7 +592,7 @@ static int ThreadX_get_thread_detail(struct rtos *rtos,
        else
                state_desc = "Unknown state";
 
-       detail->extra_info_str = (char *)malloc(strlen(state_desc)+1);
+       detail->extra_info_str = malloc(strlen(state_desc)+1);
 
        detail->exists = true;
 

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)