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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
22 ***************************************************************************/
28 #include <helper/time_support.h>
29 #include <jtag/jtag.h>
30 #include "target/target.h"
31 #include "target/target_type.h"
32 #include "target/armv7m.h"
33 #include "target/cortex_m.h"
35 #include "helper/log.h"
36 #include "helper/types.h"
37 #include "rtos_chibios_stackings.h"
40 * @brief ChibiOS/RT memory signature record.
42 * @details Definition copied from os/kernel/include/chregistry.h of ChibiOS/RT.
44 struct ChibiOS_chdebug
{
45 char ch_identifier
[4]; /**< @brief Always set to "main". */
46 uint8_t ch_zero
; /**< @brief Must be zero. */
47 uint8_t ch_size
; /**< @brief Size of this structure. */
48 uint16_t ch_version
; /**< @brief Encoded ChibiOS/RT version. */
49 uint8_t ch_ptrsize
; /**< @brief Size of a pointer. */
50 uint8_t ch_timesize
; /**< @brief Size of a @p systime_t. */
51 uint8_t ch_threadsize
; /**< @brief Size of a @p Thread struct. */
52 uint8_t cf_off_prio
; /**< @brief Offset of @p p_prio field. */
53 uint8_t cf_off_ctx
; /**< @brief Offset of @p p_ctx field. */
54 uint8_t cf_off_newer
; /**< @brief Offset of @p p_newer field. */
55 uint8_t cf_off_older
; /**< @brief Offset of @p p_older field. */
56 uint8_t cf_off_name
; /**< @brief Offset of @p p_name field. */
57 uint8_t cf_off_stklimit
; /**< @brief Offset of @p p_stklimit
59 uint8_t cf_off_state
; /**< @brief Offset of @p p_state field. */
60 uint8_t cf_off_flags
; /**< @brief Offset of @p p_flags field. */
61 uint8_t cf_off_refs
; /**< @brief Offset of @p p_refs field. */
62 uint8_t cf_off_preempt
; /**< @brief Offset of @p p_preempt
64 uint8_t cf_off_time
; /**< @brief Offset of @p p_time field. */
67 #define GET_CH_KERNEL_MAJOR(codedVersion) ((codedVersion >> 11) & 0x1f)
68 #define GET_CH_KERNEL_MINOR(codedVersion) ((codedVersion >> 6) & 0x1f)
69 #define GET_CH_KERNEL_PATCH(codedVersion) ((codedVersion >> 0) & 0x3f)
72 * @brief ChibiOS thread states.
74 static const char * const ChibiOS_thread_states
[] = {
75 "READY", "CURRENT", "SUSPENDED", "WTSEM", "WTMTX", "WTCOND", "SLEEPING",
76 "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", "SNDMSG", "WTMSG", "WTQUEUE",
80 #define CHIBIOS_NUM_STATES (sizeof(ChibiOS_thread_states)/sizeof(char *))
82 /* Maximum ChibiOS thread name. There is no real limit set by ChibiOS but 64
83 * chars ought to be enough.
85 #define CHIBIOS_THREAD_NAME_STR_SIZE (64)
87 struct ChibiOS_params
{
88 const char *target_name
;
90 struct ChibiOS_chdebug
*signature
;
91 const struct rtos_register_stacking
*stacking_info
;
94 static const struct ChibiOS_params ChibiOS_params_list
[] = {
96 "cortex_m", /* target_name */
98 NULL
, /* stacking_info */
101 "hla_target", /* target_name */
103 NULL
, /* stacking_info */
106 #define CHIBIOS_NUM_PARAMS ((int)(sizeof(ChibiOS_params_list)/sizeof(struct ChibiOS_params)))
108 static int ChibiOS_detect_rtos(struct target
*target
);
109 static int ChibiOS_create(struct target
*target
);
110 static int ChibiOS_update_threads(struct rtos
*rtos
);
111 static int ChibiOS_get_thread_reg_list(struct rtos
*rtos
, int64_t thread_id
, char **hex_reg_list
);
112 static int ChibiOS_get_symbol_list_to_lookup(symbol_table_elem_t
*symbol_list
[]);
114 struct rtos_type ChibiOS_rtos
= {
117 .detect_rtos
= ChibiOS_detect_rtos
,
118 .create
= ChibiOS_create
,
119 .update_threads
= ChibiOS_update_threads
,
120 .get_thread_reg_list
= ChibiOS_get_thread_reg_list
,
121 .get_symbol_list_to_lookup
= ChibiOS_get_symbol_list_to_lookup
,
125 /* In ChibiOS/RT 3.0 the rlist structure has become part of a system
126 * data structure ch. We declare both symbols as optional and later
127 * use whatever is available.
130 enum ChibiOS_symbol_values
{
131 ChibiOS_VAL_rlist
= 0,
133 ChibiOS_VAL_ch_debug
= 2,
134 ChibiOS_VAL_chSysInit
= 3
137 static symbol_table_elem_t ChibiOS_symbol_list
[] = {
138 { "rlist", 0, true}, /* Thread ready list */
139 { "ch", 0, true}, /* System data structure */
140 { "ch_debug", 0, false}, /* Memory Signature containing offsets of fields in rlist */
141 { "chSysInit", 0, false}, /* Necessary part of API, used for ChibiOS detection */
145 /* Offset of the rlist structure within the system data structure (ch) */
146 #define CH_RLIST_OFFSET 0x00
148 static int ChibiOS_update_memory_signature(struct rtos
*rtos
)
151 struct ChibiOS_params
*param
;
152 struct ChibiOS_chdebug
*signature
;
154 param
= (struct ChibiOS_params
*) rtos
->rtos_specific_params
;
156 /* Free existing memory description.*/
157 if (param
->signature
) {
158 free(param
->signature
);
159 param
->signature
= 0;
162 signature
= malloc(sizeof(*signature
));
164 LOG_ERROR("Could not allocate space for ChibiOS/RT memory signature");
168 retval
= target_read_buffer(rtos
->target
,
169 rtos
->symbols
[ChibiOS_VAL_ch_debug
].address
,
171 (uint8_t *) signature
);
172 if (retval
!= ERROR_OK
) {
173 LOG_ERROR("Could not read ChibiOS/RT memory signature from target");
177 if (strncmp(signature
->ch_identifier
, "main", 4) != 0) {
178 LOG_ERROR("Memory signature identifier does not contain magic bytes.");
182 if (signature
->ch_size
< sizeof(*signature
)) {
183 LOG_ERROR("ChibiOS/RT memory signature claims to be smaller "
188 if (signature
->ch_size
> sizeof(*signature
)) {
189 LOG_WARNING("ChibiOS/RT memory signature claims to be bigger than"
190 " expected. Assuming compatibility...");
193 /* Convert endianness of version field */
194 const uint8_t *versionTarget
= (const uint8_t *)
195 &signature
->ch_version
;
196 signature
->ch_version
= rtos
->target
->endianness
== TARGET_LITTLE_ENDIAN
?
197 le_to_h_u32(versionTarget
) : be_to_h_u32(versionTarget
);
199 const uint16_t ch_version
= signature
->ch_version
;
200 LOG_INFO("Successfully loaded memory map of ChibiOS/RT target "
201 "running version %i.%i.%i", GET_CH_KERNEL_MAJOR(ch_version
),
202 GET_CH_KERNEL_MINOR(ch_version
), GET_CH_KERNEL_PATCH(ch_version
));
204 /* Currently, we have the inherent assumption that all address pointers
205 * are 32 bit wide. */
206 if (signature
->ch_ptrsize
!= sizeof(uint32_t)) {
207 LOG_ERROR("ChibiOS/RT target memory signature claims an address"
208 "width unequal to 32 bits!");
213 param
->signature
= signature
;
217 /* Error reading the ChibiOS memory structure */
219 param
->signature
= 0;
224 static int ChibiOS_update_stacking(struct rtos
*rtos
)
226 /* Sometimes the stacking can not be determined only by looking at the
227 * target name but only a runtime.
229 * For example, this is the case for cortex-m4 targets and ChibiOS which
230 * only stack the FPU registers if it is enabled during ChibiOS build.
232 * Terminating which stacking is used is target depending.
235 * - Once ChibiOS is actually initialized, the stacking is fixed.
236 * - During startup code, the FPU might not be initialized and the
237 * detection might fail.
238 * - Since no threads are running during startup, the problem is solved
239 * by delaying stacking detection until there are more threads
240 * available than the current execution. In which case
241 * ChibiOS_get_thread_reg_list is called.
245 if (!rtos
->rtos_specific_params
)
248 struct ChibiOS_params
*param
;
249 param
= (struct ChibiOS_params
*) rtos
->rtos_specific_params
;
251 /* Check for armv7m with *enabled* FPU, i.e. a Cortex M4 */
252 struct armv7m_common
*armv7m_target
= target_to_armv7m(rtos
->target
);
253 if (is_armv7m(armv7m_target
)) {
254 if (armv7m_target
->fp_feature
== FPv4_SP
) {
255 /* Found ARM v7m target which includes a FPU */
258 retval
= target_read_u32(rtos
->target
, FPU_CPACR
, &cpacr
);
259 if (retval
!= ERROR_OK
) {
260 LOG_ERROR("Could not read CPACR register to check FPU state");
264 /* Check if CP10 and CP11 are set to full access.
265 * In ChibiOS this is done in ResetHandler() in crt0.c */
266 if (cpacr
& 0x00F00000) {
267 /* Found target with enabled FPU */
268 /* FIXME: Need to figure out how to specify the FPU registers */
269 LOG_ERROR("ChibiOS ARM v7m targets with enabled FPU "
270 " are NOT supported");
275 /* Found ARM v7m target with no or disabled FPU */
276 param
->stacking_info
= &rtos_chibios_arm_v7m_stacking
;
283 static int ChibiOS_update_threads(struct rtos
*rtos
)
286 const struct ChibiOS_params
*param
;
290 if (!rtos
->rtos_specific_params
)
293 if (!rtos
->symbols
) {
294 LOG_ERROR("No symbols for ChibiOS");
298 param
= (const struct ChibiOS_params
*) rtos
->rtos_specific_params
;
299 /* Update the memory signature saved in the target memory */
300 if (!param
->signature
) {
301 retval
= ChibiOS_update_memory_signature(rtos
);
302 if (retval
!= ERROR_OK
) {
303 LOG_ERROR("Reading the memory signature of ChibiOS/RT failed");
308 /* wipe out previous thread details if any */
309 rtos_free_threadlist(rtos
);
311 /* ChibiOS does not save the current thread count. We have to first
312 * parse the double linked thread list to check for errors and the number of
314 const uint32_t rlist
= rtos
->symbols
[ChibiOS_VAL_rlist
].address
?
315 rtos
->symbols
[ChibiOS_VAL_rlist
].address
:
316 rtos
->symbols
[ChibiOS_VAL_ch
].address
+ CH_RLIST_OFFSET
/* ChibiOS3 */;
317 const struct ChibiOS_chdebug
*signature
= param
->signature
;
325 retval
= target_read_u32(rtos
->target
,
326 current
+ signature
->cf_off_newer
, ¤t
);
327 if (retval
!= ERROR_OK
) {
328 LOG_ERROR("Could not read next ChibiOS thread");
331 /* Could be NULL if the kernel is not initialized yet or if the
332 * registry is corrupted. */
334 LOG_ERROR("ChibiOS registry integrity check failed, NULL pointer");
339 /* Fetch previous thread in the list as a integrity check. */
340 retval
= target_read_u32(rtos
->target
,
341 current
+ signature
->cf_off_older
, &older
);
342 if ((retval
!= ERROR_OK
) || (older
== 0) || (older
!= previous
)) {
343 LOG_ERROR("ChibiOS registry integrity check failed, "
344 "double linked list violation");
348 /* Check for full iteration of the linked list. */
349 if (current
== rlist
)
355 /* No RTOS, there is always at least the current execution, though */
356 LOG_INFO("Only showing current execution because of a broken "
357 "ChibiOS thread registry.");
359 const char tmp_thread_name
[] = "Current Execution";
360 const char tmp_thread_extra_info
[] = "No RTOS thread";
362 rtos
->thread_details
= malloc(
363 sizeof(struct thread_detail
));
364 rtos
->thread_details
->threadid
= 1;
365 rtos
->thread_details
->exists
= true;
366 rtos
->thread_details
->display_str
= NULL
;
368 rtos
->thread_details
->extra_info_str
= malloc(
369 sizeof(tmp_thread_extra_info
));
370 strcpy(rtos
->thread_details
->extra_info_str
, tmp_thread_extra_info
);
372 rtos
->thread_details
->thread_name_str
= malloc(
373 sizeof(tmp_thread_name
));
374 strcpy(rtos
->thread_details
->thread_name_str
, tmp_thread_name
);
376 rtos
->current_thread
= 1;
377 rtos
->thread_count
= 1;
381 /* create space for new thread details */
382 rtos
->thread_details
= malloc(
383 sizeof(struct thread_detail
) * tasks_found
);
384 if (!rtos
->thread_details
) {
385 LOG_ERROR("Could not allocate space for thread details");
389 rtos
->thread_count
= tasks_found
;
390 /* Loop through linked list. */
391 struct thread_detail
*curr_thrd_details
= rtos
->thread_details
;
392 while (curr_thrd_details
< rtos
->thread_details
+ tasks_found
) {
393 uint32_t name_ptr
= 0;
394 char tmp_str
[CHIBIOS_THREAD_NAME_STR_SIZE
];
396 retval
= target_read_u32(rtos
->target
,
397 current
+ signature
->cf_off_newer
, ¤t
);
398 if (retval
!= ERROR_OK
) {
399 LOG_ERROR("Could not read next ChibiOS thread");
403 /* Check for full iteration of the linked list. */
404 if (current
== rlist
)
407 /* Save the thread pointer */
408 curr_thrd_details
->threadid
= current
;
410 /* read the name pointer */
411 retval
= target_read_u32(rtos
->target
,
412 current
+ signature
->cf_off_name
, &name_ptr
);
413 if (retval
!= ERROR_OK
) {
414 LOG_ERROR("Could not read ChibiOS thread name pointer from target");
418 /* Read the thread name */
419 retval
= target_read_buffer(rtos
->target
, name_ptr
,
420 CHIBIOS_THREAD_NAME_STR_SIZE
,
421 (uint8_t *)&tmp_str
);
422 if (retval
!= ERROR_OK
) {
423 LOG_ERROR("Error reading thread name from ChibiOS target");
426 tmp_str
[CHIBIOS_THREAD_NAME_STR_SIZE
- 1] = '\x00';
428 if (tmp_str
[0] == '\x00')
429 strcpy(tmp_str
, "No Name");
431 curr_thrd_details
->thread_name_str
= malloc(
432 strlen(tmp_str
) + 1);
433 strcpy(curr_thrd_details
->thread_name_str
, tmp_str
);
437 const char *state_desc
;
439 retval
= target_read_u8(rtos
->target
,
440 current
+ signature
->cf_off_state
, &threadState
);
441 if (retval
!= ERROR_OK
) {
442 LOG_ERROR("Error reading thread state from ChibiOS target");
447 if (threadState
< CHIBIOS_NUM_STATES
)
448 state_desc
= ChibiOS_thread_states
[threadState
];
450 state_desc
= "Unknown state";
452 curr_thrd_details
->extra_info_str
= malloc(strlen(
454 strcpy(curr_thrd_details
->extra_info_str
, state_desc
);
456 curr_thrd_details
->exists
= true;
457 curr_thrd_details
->display_str
= NULL
;
462 uint32_t current_thrd
;
463 /* NOTE: By design, cf_off_name equals readylist_current_offset */
464 retval
= target_read_u32(rtos
->target
,
465 rlist
+ signature
->cf_off_name
,
467 if (retval
!= ERROR_OK
) {
468 LOG_ERROR("Could not read current Thread from ChibiOS target");
471 rtos
->current_thread
= current_thrd
;
476 static int ChibiOS_get_thread_reg_list(struct rtos
*rtos
, int64_t thread_id
, char **hex_reg_list
)
479 const struct ChibiOS_params
*param
;
480 uint32_t stack_ptr
= 0;
482 *hex_reg_list
= NULL
;
483 if ((rtos
== NULL
) || (thread_id
== 0) ||
484 (rtos
->rtos_specific_params
== NULL
))
487 param
= (const struct ChibiOS_params
*) rtos
->rtos_specific_params
;
489 if (!param
->signature
)
492 /* Update stacking if it can only be determined from runtime information */
493 if ((param
->stacking_info
== 0) &&
494 (ChibiOS_update_stacking(rtos
) != ERROR_OK
)) {
495 LOG_ERROR("Failed to determine exact stacking for the target type %s", rtos
->target
->type
->name
);
499 /* Read the stack pointer */
500 retval
= target_read_u32(rtos
->target
,
501 thread_id
+ param
->signature
->cf_off_ctx
, &stack_ptr
);
502 if (retval
!= ERROR_OK
) {
503 LOG_ERROR("Error reading stack frame from ChibiOS thread");
507 return rtos_generic_stack_read(rtos
->target
, param
->stacking_info
, stack_ptr
, hex_reg_list
);
510 static int ChibiOS_get_symbol_list_to_lookup(symbol_table_elem_t
*symbol_list
[])
512 *symbol_list
= ChibiOS_symbol_list
;
516 static int ChibiOS_detect_rtos(struct target
*target
)
518 if ((target
->rtos
->symbols
!= NULL
) &&
519 ((target
->rtos
->symbols
[ChibiOS_VAL_rlist
].address
!= 0) ||
520 (target
->rtos
->symbols
[ChibiOS_VAL_ch
].address
!= 0)) &&
521 (target
->rtos
->symbols
[ChibiOS_VAL_chSysInit
].address
!= 0)) {
523 if (target
->rtos
->symbols
[ChibiOS_VAL_ch_debug
].address
== 0) {
524 LOG_INFO("It looks like the target is running ChibiOS without "
529 /* looks like ChibiOS with memory map enabled.*/
536 static int ChibiOS_create(struct target
*target
)
539 while ((i
< CHIBIOS_NUM_PARAMS
) &&
540 (0 != strcmp(ChibiOS_params_list
[i
].target_name
, target
->type
->name
))) {
543 if (i
>= CHIBIOS_NUM_PARAMS
) {
544 LOG_WARNING("Could not find target \"%s\" in ChibiOS compatibility "
545 "list", target
->type
->name
);
549 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)