1 /***************************************************************************
2 * Copyright (C) 2012 by Matthias Blaicher *
3 * Matthias Blaicher - matthias@blaicher.com *
5 * Copyright (C) 2011 by Broadcom Corporation *
6 * Evan Hunter - ehunter@broadcom.com *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
28 #include <helper/time_support.h>
29 #include <jtag/jtag.h>
30 #include "target/target.h"
31 #include "target/target_type.h"
33 #include "helper/log.h"
34 #include "helper/types.h"
35 #include "rtos_chibios_stackings.h"
39 * @brief ChibiOS/RT memory signature record.
41 * @details Definition copied from os/kernel/include/chregistry.h of ChibiOS/RT.
43 struct ChibiOS_chdebug
{
44 char ch_identifier
[4]; /**< @brief Always set to "main". */
45 uint8_t ch_zero
; /**< @brief Must be zero. */
46 uint8_t ch_size
; /**< @brief Size of this structure. */
47 uint16_t ch_version
; /**< @brief Encoded ChibiOS/RT version. */
48 uint8_t ch_ptrsize
; /**< @brief Size of a pointer. */
49 uint8_t ch_timesize
; /**< @brief Size of a @p systime_t. */
50 uint8_t ch_threadsize
; /**< @brief Size of a @p Thread struct. */
51 uint8_t cf_off_prio
; /**< @brief Offset of @p p_prio field. */
52 uint8_t cf_off_ctx
; /**< @brief Offset of @p p_ctx field. */
53 uint8_t cf_off_newer
; /**< @brief Offset of @p p_newer field. */
54 uint8_t cf_off_older
; /**< @brief Offset of @p p_older field. */
55 uint8_t cf_off_name
; /**< @brief Offset of @p p_name field. */
56 uint8_t cf_off_stklimit
; /**< @brief Offset of @p p_stklimit
58 uint8_t cf_off_state
; /**< @brief Offset of @p p_state field. */
59 uint8_t cf_off_flags
; /**< @brief Offset of @p p_flags field. */
60 uint8_t cf_off_refs
; /**< @brief Offset of @p p_refs field. */
61 uint8_t cf_off_preempt
; /**< @brief Offset of @p p_preempt
63 uint8_t cf_off_time
; /**< @brief Offset of @p p_time field. */
66 #define GET_CH_KERNEL_MAJOR(codedVersion) ((codedVersion >> 11) & 0x1f)
67 #define GET_CH_KERNEL_MINOR(codedVersion) ((codedVersion >> 6) & 0x1f)
68 #define GET_CH_KERNEL_PATCH(codedVersion) ((codedVersion >> 0) & 0x3f)
71 * @brief ChibiOS thread states.
73 const char *ChibiOS_thread_states
[] = {
74 "READY", "CURRENT", "SUSPENDED", "WTSEM", "WTMTX", "WTCOND", "SLEEPING",
75 "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", "SNDMSG", "WTMSG", "WTQUEUE",
79 #define CHIBIOS_NUM_STATES (sizeof(ChibiOS_thread_states)/sizeof(char *))
81 /* Maximum ChibiOS thread name. There is no real limit set by ChibiOS but 64
82 * chars ought to be enough.
84 #define CHIBIOS_THREAD_NAME_STR_SIZE (64)
86 struct ChibiOS_params
{
87 const char *target_name
;
89 struct ChibiOS_chdebug
*signature
;
90 const struct rtos_register_stacking
*stacking_info
;
93 struct ChibiOS_params ChibiOS_params_list
[] = {
95 "cortex_m3", /* target_name */
97 &rtos_chibios_arm_v7m_stacking
, /* stacking_info */
100 "stm32_stlink", /* target_name */
102 &rtos_chibios_arm_v7m_stacking
, /* stacking_info */
105 #define CHIBIOS_NUM_PARAMS ((int)(sizeof(ChibiOS_params_list)/sizeof(struct ChibiOS_params)))
107 static int ChibiOS_detect_rtos(struct target
*target
);
108 static int ChibiOS_create(struct target
*target
);
109 static int ChibiOS_update_threads(struct rtos
*rtos
);
110 static int ChibiOS_get_thread_reg_list(struct rtos
*rtos
, int64_t thread_id
, char **hex_reg_list
);
111 static int ChibiOS_get_symbol_list_to_lookup(symbol_table_elem_t
*symbol_list
[]);
113 struct rtos_type ChibiOS_rtos
= {
116 .detect_rtos
= ChibiOS_detect_rtos
,
117 .create
= ChibiOS_create
,
118 .update_threads
= ChibiOS_update_threads
,
119 .get_thread_reg_list
= ChibiOS_get_thread_reg_list
,
120 .get_symbol_list_to_lookup
= ChibiOS_get_symbol_list_to_lookup
,
123 enum ChibiOS_symbol_values
{
124 ChibiOS_VAL_rlist
= 0,
125 ChibiOS_VAL_ch_debug
= 1,
126 ChibiOS_VAL_chSysInit
= 2
129 static char *ChibiOS_symbol_list
[] = {
130 "rlist", /* Thread ready list*/
131 "ch_debug", /* Memory Signatur containing offsets of fields in rlist*/
132 "chSysInit", /* Necessary part of API, used for ChibiOS detection*/
136 static int ChibiOS_update_memory_signature(struct rtos
*rtos
)
139 struct ChibiOS_params
*param
;
140 struct ChibiOS_chdebug
*signature
;
142 param
= (struct ChibiOS_params
*) rtos
->rtos_specific_params
;
144 /* Free existing memory description.*/
145 if (param
->signature
) {
146 free(param
->signature
);
147 param
->signature
= 0;
150 signature
= malloc(sizeof(*signature
));
152 LOG_ERROR("Could not allocate space for ChibiOS/RT memory signature");
156 retval
= target_read_buffer(rtos
->target
,
157 rtos
->symbols
[ChibiOS_VAL_ch_debug
].address
,
159 (uint8_t *) signature
);
160 if (retval
!= ERROR_OK
) {
161 LOG_ERROR("Could not read ChibiOS/RT memory signature from target");
165 if (strncmp(signature
->ch_identifier
, "main", 4) != 0) {
166 LOG_ERROR("Memory signature identifier does not contain magic bytes.");
170 if (signature
->ch_size
< sizeof(*signature
)) {
171 LOG_ERROR("ChibiOS/RT memory signature claims to be smaller "
176 if (signature
->ch_size
> sizeof(*signature
)) {
177 LOG_WARNING("ChibiOS/RT memory signature claims to be bigger than"
178 " expected. Assuming compatibility...");
181 /* Convert endianness of version field */
182 const uint8_t *versionTarget
= (const uint8_t *)
183 &signature
->ch_version
;
184 signature
->ch_version
= rtos
->target
->endianness
== TARGET_LITTLE_ENDIAN
?
185 le_to_h_u32(versionTarget
) : be_to_h_u32(versionTarget
);
187 const uint16_t ch_version
= signature
->ch_version
;
188 LOG_INFO("Successfully loaded memory map of ChibiOS/RT target "
189 "running version %i.%i.%i", GET_CH_KERNEL_MAJOR(ch_version
),
190 GET_CH_KERNEL_MINOR(ch_version
), GET_CH_KERNEL_PATCH(ch_version
));
192 param
->signature
= signature
;
196 /* Error reading the ChibiOS memory structure */
198 param
->signature
= 0;
203 static int ChibiOS_update_stacking(struct rtos
*rtos
)
205 /* Sometimes the stacking can not be determined only by looking at the
206 * target name but only a runtime.
208 * For example, this is the case for cortex-m4 targets and ChibiOS which
209 * only stack the FPU registers if it is enabled during ChibiOS build.
211 * Terminating which stacking is used is target depending.
214 * - Once ChibiOS is actually initialized, the stacking is fixed.
215 * - During startup code, the FPU might not be initialized and the
216 * detection might fail.
217 * - Since no threads are running during startup, the problem is solved
218 * by delaying stacking detection until there are more threads
219 * available than the current execution. In which case
220 * ChibiOS_get_thread_reg_list is called.
223 /* TODO: Add actual detection, currently it will not work with FPU enabled.*/
227 static int ChibiOS_update_threads(struct rtos
*rtos
)
230 const struct ChibiOS_params
*param
;
234 if (!rtos
->rtos_specific_params
)
237 if (!rtos
->symbols
) {
238 LOG_ERROR("No symbols for ChibiOS");
242 param
= (const struct ChibiOS_params
*) rtos
->rtos_specific_params
;
243 /* Update the memory signature saved in the target memory */
244 if (!param
->signature
) {
245 retval
= ChibiOS_update_memory_signature(rtos
);
246 if (retval
!= ERROR_OK
) {
247 LOG_ERROR("Reading the memory signature of ChibiOS/RT failed");
252 /* wipe out previous thread details if any */
254 if (rtos
->thread_details
) {
255 for (j
= 0; j
< rtos
->thread_count
; j
++) {
256 struct thread_detail
*current_thread
= &rtos
->thread_details
[j
];
257 if (current_thread
->display_str
!= NULL
)
258 free(current_thread
->display_str
);
259 if (current_thread
->thread_name_str
!= NULL
)
260 free(current_thread
->thread_name_str
);
261 if (current_thread
->extra_info_str
!= NULL
)
262 free(current_thread
->extra_info_str
);
264 free(rtos
->thread_details
);
265 rtos
->thread_details
= NULL
;
266 rtos
->thread_count
= 0;
268 /* ChibiOS does not save the current thread count. We have to first
269 * parse the double linked thread list to check for errors and the number of
271 const uint32_t rlist
= rtos
->symbols
[ChibiOS_VAL_rlist
].address
;
279 retval
= target_read_buffer(rtos
->target
,
280 current
+ param
->signature
->cf_off_newer
,
281 param
->signature
->ch_ptrsize
,
282 (uint8_t *)¤t
);
283 if (retval
!= ERROR_OK
) {
284 LOG_ERROR("Could not read next ChibiOS thread");
287 /* Could be NULL if the kernel is not initialized yet or if the
288 * registry is corrupted. */
290 LOG_ERROR("ChibiOS registry integrity check failed, NULL pointer");
295 /* Fetch previous thread in the list as a integrity check. */
296 retval
= target_read_buffer(rtos
->target
,
297 current
+ param
->signature
->cf_off_older
,
298 param
->signature
->ch_ptrsize
,
300 if ((retval
!= ERROR_OK
) || (older
== 0) || (older
!= previous
)) {
301 LOG_ERROR("ChibiOS registry integrity check failed, "
302 "double linked list violation");
306 /* Check for full iteration of the linked list. */
307 if (current
== rlist
)
313 /* No RTOS, there is always at least the current execution, though */
314 LOG_INFO("Only showing current execution because of a broken "
315 "ChibiOS thread registry.");
317 const char tmp_thread_name
[] = "Current Execution";
318 const char tmp_thread_extra_info
[] = "No RTOS thread";
320 rtos
->thread_details
= (struct thread_detail
*) malloc(
321 sizeof(struct thread_detail
));
322 rtos
->thread_details
->threadid
= 1;
323 rtos
->thread_details
->exists
= true;
324 rtos
->thread_details
->display_str
= NULL
;
326 rtos
->thread_details
->extra_info_str
= (char *) malloc(
327 sizeof(tmp_thread_extra_info
));
328 strcpy(rtos
->thread_details
->extra_info_str
, tmp_thread_extra_info
);
330 rtos
->thread_details
->thread_name_str
= (char *) malloc(
331 sizeof(tmp_thread_name
));
332 strcpy(rtos
->thread_details
->thread_name_str
, tmp_thread_name
);
334 rtos
->current_thread
= 1;
335 rtos
->thread_count
= 1;
339 /* create space for new thread details */
340 rtos
->thread_details
= (struct thread_detail
*) malloc(
341 sizeof(struct thread_detail
) * tasks_found
);
342 if (!rtos
->thread_details
) {
343 LOG_ERROR("Could not allocate space for thread details");
347 rtos
->thread_count
= tasks_found
;
348 /* Loop through linked list. */
349 struct thread_detail
*curr_thrd_details
= rtos
->thread_details
;
350 while (curr_thrd_details
< rtos
->thread_details
+ tasks_found
) {
351 uint32_t name_ptr
= 0;
352 char tmp_str
[CHIBIOS_THREAD_NAME_STR_SIZE
];
354 retval
= target_read_buffer(rtos
->target
,
355 current
+ param
->signature
->cf_off_newer
,
356 param
->signature
->ch_ptrsize
,
357 (uint8_t *)¤t
);
358 if (retval
!= ERROR_OK
) {
359 LOG_ERROR("Could not read next ChibiOS thread");
363 /* Check for full iteration of the linked list. */
364 if (current
== rlist
)
367 /* Save the thread pointer */
368 curr_thrd_details
->threadid
= current
;
370 /* read the name pointer */
371 retval
= target_read_buffer(rtos
->target
,
372 current
+ param
->signature
->cf_off_name
,
373 param
->signature
->ch_ptrsize
,
374 (uint8_t *)&name_ptr
);
375 if (retval
!= ERROR_OK
) {
376 LOG_ERROR("Could not read ChibiOS thread name pointer from target");
380 /* Read the thread name */
381 retval
= target_read_buffer(rtos
->target
, name_ptr
,
382 CHIBIOS_THREAD_NAME_STR_SIZE
,
383 (uint8_t *)&tmp_str
);
384 if (retval
!= ERROR_OK
) {
385 LOG_ERROR("Error reading thread name from ChibiOS target");
388 tmp_str
[CHIBIOS_THREAD_NAME_STR_SIZE
- 1] = '\x00';
390 if (tmp_str
[0] == '\x00')
391 strcpy(tmp_str
, "No Name");
393 curr_thrd_details
->thread_name_str
= (char *)malloc(
394 strlen(tmp_str
) + 1);
395 strcpy(curr_thrd_details
->thread_name_str
, tmp_str
);
399 const char *state_desc
;
401 retval
= target_read_buffer(rtos
->target
,
402 current
+ param
->signature
->cf_off_state
,
404 if (retval
!= ERROR_OK
) {
405 LOG_ERROR("Error reading thread state from ChibiOS target");
410 if (threadState
< CHIBIOS_NUM_STATES
)
411 state_desc
= ChibiOS_thread_states
[threadState
];
413 state_desc
= "Unknown state";
415 curr_thrd_details
->extra_info_str
= (char *)malloc(strlen(
417 strcpy(curr_thrd_details
->extra_info_str
, state_desc
);
419 curr_thrd_details
->exists
= true;
420 curr_thrd_details
->display_str
= NULL
;
424 /* NOTE: By design, cf_off_name equals readylist_current_offset */
425 retval
= target_read_buffer(rtos
->target
,
426 rlist
+ param
->signature
->cf_off_name
,
427 param
->signature
->ch_ptrsize
,
428 (uint8_t *)&rtos
->current_thread
);
429 if (retval
!= ERROR_OK
) {
430 LOG_ERROR("Could not read current Thread from ChibiOS target");
437 static int ChibiOS_get_thread_reg_list(struct rtos
*rtos
, int64_t thread_id
, char **hex_reg_list
)
440 const struct ChibiOS_params
*param
;
441 int64_t stack_ptr
= 0;
443 *hex_reg_list
= NULL
;
444 if ((rtos
== NULL
) || (thread_id
== 0) ||
445 (rtos
->rtos_specific_params
== NULL
))
448 param
= (const struct ChibiOS_params
*) rtos
->rtos_specific_params
;
450 if (!param
->signature
)
453 /* Update stacking if it can only be determined from runtime information */
454 if ((param
->stacking_info
== 0) &&
455 (ChibiOS_update_stacking(rtos
) != ERROR_OK
)) {
456 LOG_ERROR("Failed to determine exact stacking for the target type %s", rtos
->target
->type
->name
);
460 /* Read the stack pointer */
461 retval
= target_read_buffer(rtos
->target
,
462 thread_id
+ param
->signature
->cf_off_ctx
,
463 param
->signature
->ch_ptrsize
,
464 (uint8_t *)&stack_ptr
);
465 if (retval
!= ERROR_OK
) {
466 LOG_ERROR("Error reading stack frame from ChibiOS thread");
470 return rtos_generic_stack_read(rtos
->target
, param
->stacking_info
, stack_ptr
, hex_reg_list
);
473 static int ChibiOS_get_symbol_list_to_lookup(symbol_table_elem_t
*symbol_list
[])
476 *symbol_list
= (symbol_table_elem_t
*) malloc(
477 sizeof(symbol_table_elem_t
) * ARRAY_SIZE(ChibiOS_symbol_list
));
479 for (i
= 0; i
< ARRAY_SIZE(ChibiOS_symbol_list
); i
++)
480 (*symbol_list
)[i
].symbol_name
= ChibiOS_symbol_list
[i
];
485 static int ChibiOS_detect_rtos(struct target
*target
)
487 if ((target
->rtos
->symbols
!= NULL
) &&
488 (target
->rtos
->symbols
[ChibiOS_VAL_rlist
].address
!= 0) &&
489 (target
->rtos
->symbols
[ChibiOS_VAL_chSysInit
].address
!= 0)) {
491 if (target
->rtos
->symbols
[ChibiOS_VAL_ch_debug
].address
== 0) {
492 LOG_INFO("It looks like the target is running ChibiOS without "
497 /* looks like ChibiOS with memory map enabled.*/
504 static int ChibiOS_create(struct target
*target
)
507 while ((i
< CHIBIOS_NUM_PARAMS
) &&
508 (0 != strcmp(ChibiOS_params_list
[i
].target_name
, target
->type
->name
))) {
511 if (i
>= CHIBIOS_NUM_PARAMS
) {
512 LOG_WARNING("Could not find target \"%s\" in ChibiOS compatibility "
513 "list", target
->type
->name
);
517 target
->rtos
->rtos_specific_params
= (void *) &ChibiOS_params_list
[i
];
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)