46fdca323fcdf7ec1bb3bb80288a0d4c427931b4
[openocd.git] / src / rtos / ChibiOS.c
1 /***************************************************************************
2 * Copyright (C) 2012 by Matthias Blaicher *
3 * Matthias Blaicher - matthias@blaicher.com *
4 * *
5 * Copyright (C) 2011 by Broadcom Corporation *
6 * Evan Hunter - ehunter@broadcom.com *
7 * *
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. *
12 * *
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. *
17 * *
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 ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
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"
34 #include "rtos.h"
35 #include "helper/log.h"
36 #include "helper/types.h"
37 #include "rtos_chibios_stackings.h"
38
39 /**
40 * @brief ChibiOS/RT memory signature record.
41 *
42 * @details Definition copied from os/kernel/include/chregistry.h of ChibiOS/RT.
43 */
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
58 field. */
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
63 field. */
64 uint8_t cf_off_time; /**< @brief Offset of @p p_time field. */
65 };
66
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)
70
71 /**
72 * @brief ChibiOS thread states.
73 */
74 static const char * const ChibiOS_thread_states[] = {
75 "READY", "CURRENT", "SUSPENDED", "WTSEM", "WTMTX", "WTCOND", "SLEEPING",
76 "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", "SNDMSG", "WTMSG", "WTQUEUE",
77 "FINAL"
78 };
79
80 #define CHIBIOS_NUM_STATES (sizeof(ChibiOS_thread_states)/sizeof(char *))
81
82 /* Maximum ChibiOS thread name. There is no real limit set by ChibiOS but 64
83 * chars ought to be enough.
84 */
85 #define CHIBIOS_THREAD_NAME_STR_SIZE (64)
86
87 struct ChibiOS_params {
88 const char *target_name;
89
90 struct ChibiOS_chdebug *signature;
91 const struct rtos_register_stacking *stacking_info;
92 };
93
94 static const struct ChibiOS_params ChibiOS_params_list[] = {
95 {
96 "cortex_m", /* target_name */
97 0,
98 NULL, /* stacking_info */
99 },
100 {
101 "hla_target", /* target_name */
102 0,
103 NULL, /* stacking_info */
104 }
105 };
106 #define CHIBIOS_NUM_PARAMS ((int)(sizeof(ChibiOS_params_list)/sizeof(struct ChibiOS_params)))
107
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[]);
113
114 struct rtos_type ChibiOS_rtos = {
115 .name = "ChibiOS",
116
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,
122 };
123
124 enum ChibiOS_symbol_values {
125 ChibiOS_VAL_rlist = 0,
126 ChibiOS_VAL_ch_debug = 1,
127 ChibiOS_VAL_chSysInit = 2
128 };
129
130 static const char * const ChibiOS_symbol_list[] = {
131 "rlist", /* Thread ready list*/
132 "ch_debug", /* Memory Signatur containing offsets of fields in rlist*/
133 "chSysInit", /* Necessary part of API, used for ChibiOS detection*/
134 NULL
135 };
136
137 static int ChibiOS_update_memory_signature(struct rtos *rtos)
138 {
139 int retval;
140 struct ChibiOS_params *param;
141 struct ChibiOS_chdebug *signature;
142
143 param = (struct ChibiOS_params *) rtos->rtos_specific_params;
144
145 /* Free existing memory description.*/
146 if (param->signature) {
147 free(param->signature);
148 param->signature = 0;
149 }
150
151 signature = malloc(sizeof(*signature));
152 if (!signature) {
153 LOG_ERROR("Could not allocate space for ChibiOS/RT memory signature");
154 return -1;
155 }
156
157 retval = target_read_buffer(rtos->target,
158 rtos->symbols[ChibiOS_VAL_ch_debug].address,
159 sizeof(*signature),
160 (uint8_t *) signature);
161 if (retval != ERROR_OK) {
162 LOG_ERROR("Could not read ChibiOS/RT memory signature from target");
163 goto errfree;
164 }
165
166 if (strncmp(signature->ch_identifier, "main", 4) != 0) {
167 LOG_ERROR("Memory signature identifier does not contain magic bytes.");
168 goto errfree;
169 }
170
171 if (signature->ch_size < sizeof(*signature)) {
172 LOG_ERROR("ChibiOS/RT memory signature claims to be smaller "
173 "than expected");
174 goto errfree;
175 }
176
177 if (signature->ch_size > sizeof(*signature)) {
178 LOG_WARNING("ChibiOS/RT memory signature claims to be bigger than"
179 " expected. Assuming compatibility...");
180 }
181
182 /* Convert endianness of version field */
183 const uint8_t *versionTarget = (const uint8_t *)
184 &signature->ch_version;
185 signature->ch_version = rtos->target->endianness == TARGET_LITTLE_ENDIAN ?
186 le_to_h_u32(versionTarget) : be_to_h_u32(versionTarget);
187
188 const uint16_t ch_version = signature->ch_version;
189 LOG_INFO("Successfully loaded memory map of ChibiOS/RT target "
190 "running version %i.%i.%i", GET_CH_KERNEL_MAJOR(ch_version),
191 GET_CH_KERNEL_MINOR(ch_version), GET_CH_KERNEL_PATCH(ch_version));
192
193 /* Currently, we have the inherent assumption that all address pointers
194 * are 32 bit wide. */
195 if (signature->ch_ptrsize != sizeof(uint32_t)) {
196 LOG_ERROR("ChibiOS/RT target memory signature claims an address"
197 "width unequal to 32 bits!");
198 free(signature);
199 return -1;
200 }
201
202 param->signature = signature;
203 return 0;
204
205 errfree:
206 /* Error reading the ChibiOS memory structure */
207 free(signature);
208 param->signature = 0;
209 return -1;
210 }
211
212
213 static int ChibiOS_update_stacking(struct rtos *rtos)
214 {
215 /* Sometimes the stacking can not be determined only by looking at the
216 * target name but only a runtime.
217 *
218 * For example, this is the case for cortex-m4 targets and ChibiOS which
219 * only stack the FPU registers if it is enabled during ChibiOS build.
220 *
221 * Terminating which stacking is used is target depending.
222 *
223 * Assumptions:
224 * - Once ChibiOS is actually initialized, the stacking is fixed.
225 * - During startup code, the FPU might not be initialized and the
226 * detection might fail.
227 * - Since no threads are running during startup, the problem is solved
228 * by delaying stacking detection until there are more threads
229 * available than the current execution. In which case
230 * ChibiOS_get_thread_reg_list is called.
231 */
232 int retval;
233
234 if (!rtos->rtos_specific_params)
235 return -1;
236
237 struct ChibiOS_params *param;
238 param = (struct ChibiOS_params *) rtos->rtos_specific_params;
239
240 /* Check for armv7m with *enabled* FPU, i.e. a Cortex M4 */
241 struct armv7m_common *armv7m_target = target_to_armv7m(rtos->target);
242 if (is_armv7m(armv7m_target)) {
243 if (armv7m_target->fp_feature == FPv4_SP) {
244 /* Found ARM v7m target which includes a FPU */
245 uint32_t cpacr;
246
247 retval = target_read_u32(rtos->target, FPU_CPACR, &cpacr);
248 if (retval != ERROR_OK) {
249 LOG_ERROR("Could not read CPACR register to check FPU state");
250 return -1;
251 }
252
253 /* Check if CP10 and CP11 are set to full access.
254 * In ChibiOS this is done in ResetHandler() in crt0.c */
255 if (cpacr & 0x00F00000) {
256 /* Found target with enabled FPU */
257 /* FIXME: Need to figure out how to specify the FPU registers */
258 LOG_ERROR("ChibiOS ARM v7m targets with enabled FPU "
259 " are NOT supported");
260 return -1;
261 }
262 }
263
264 /* Found ARM v7m target with no or disabled FPU */
265 param->stacking_info = &rtos_chibios_arm_v7m_stacking;
266 return 0;
267 }
268
269 return -1;
270 }
271
272 static int ChibiOS_update_threads(struct rtos *rtos)
273 {
274 int retval;
275 const struct ChibiOS_params *param;
276 int tasks_found = 0;
277 int rtos_valid = -1;
278
279 if (!rtos->rtos_specific_params)
280 return -1;
281
282 if (!rtos->symbols) {
283 LOG_ERROR("No symbols for ChibiOS");
284 return -3;
285 }
286
287 param = (const struct ChibiOS_params *) rtos->rtos_specific_params;
288 /* Update the memory signature saved in the target memory */
289 if (!param->signature) {
290 retval = ChibiOS_update_memory_signature(rtos);
291 if (retval != ERROR_OK) {
292 LOG_ERROR("Reading the memory signature of ChibiOS/RT failed");
293 return retval;
294 }
295 }
296
297 /* wipe out previous thread details if any */
298 rtos_free_threadlist(rtos);
299
300 /* ChibiOS does not save the current thread count. We have to first
301 * parse the double linked thread list to check for errors and the number of
302 * threads. */
303 const uint32_t rlist = rtos->symbols[ChibiOS_VAL_rlist].address;
304 const struct ChibiOS_chdebug *signature = param->signature;
305 uint32_t current;
306 uint32_t previous;
307 uint32_t older;
308
309 current = rlist;
310 previous = rlist;
311 while (1) {
312 retval = target_read_u32(rtos->target,
313 current + signature->cf_off_newer, &current);
314 if (retval != ERROR_OK) {
315 LOG_ERROR("Could not read next ChibiOS thread");
316 return retval;
317 }
318 /* Could be NULL if the kernel is not initialized yet or if the
319 * registry is corrupted. */
320 if (current == 0) {
321 LOG_ERROR("ChibiOS registry integrity check failed, NULL pointer");
322
323 rtos_valid = 0;
324 break;
325 }
326 /* Fetch previous thread in the list as a integrity check. */
327 retval = target_read_u32(rtos->target,
328 current + signature->cf_off_older, &older);
329 if ((retval != ERROR_OK) || (older == 0) || (older != previous)) {
330 LOG_ERROR("ChibiOS registry integrity check failed, "
331 "double linked list violation");
332 rtos_valid = 0;
333 break;
334 }
335 /* Check for full iteration of the linked list. */
336 if (current == rlist)
337 break;
338 tasks_found++;
339 previous = current;
340 }
341 if (!rtos_valid) {
342 /* No RTOS, there is always at least the current execution, though */
343 LOG_INFO("Only showing current execution because of a broken "
344 "ChibiOS thread registry.");
345
346 const char tmp_thread_name[] = "Current Execution";
347 const char tmp_thread_extra_info[] = "No RTOS thread";
348
349 rtos->thread_details = malloc(
350 sizeof(struct thread_detail));
351 rtos->thread_details->threadid = 1;
352 rtos->thread_details->exists = true;
353 rtos->thread_details->display_str = NULL;
354
355 rtos->thread_details->extra_info_str = malloc(
356 sizeof(tmp_thread_extra_info));
357 strcpy(rtos->thread_details->extra_info_str, tmp_thread_extra_info);
358
359 rtos->thread_details->thread_name_str = malloc(
360 sizeof(tmp_thread_name));
361 strcpy(rtos->thread_details->thread_name_str, tmp_thread_name);
362
363 rtos->current_thread = 1;
364 rtos->thread_count = 1;
365 return ERROR_OK;
366 }
367
368 /* create space for new thread details */
369 rtos->thread_details = malloc(
370 sizeof(struct thread_detail) * tasks_found);
371 if (!rtos->thread_details) {
372 LOG_ERROR("Could not allocate space for thread details");
373 return -1;
374 }
375
376 rtos->thread_count = tasks_found;
377 /* Loop through linked list. */
378 struct thread_detail *curr_thrd_details = rtos->thread_details;
379 while (curr_thrd_details < rtos->thread_details + tasks_found) {
380 uint32_t name_ptr = 0;
381 char tmp_str[CHIBIOS_THREAD_NAME_STR_SIZE];
382
383 retval = target_read_u32(rtos->target,
384 current + signature->cf_off_newer, &current);
385 if (retval != ERROR_OK) {
386 LOG_ERROR("Could not read next ChibiOS thread");
387 return -6;
388 }
389
390 /* Check for full iteration of the linked list. */
391 if (current == rlist)
392 break;
393
394 /* Save the thread pointer */
395 curr_thrd_details->threadid = current;
396
397 /* read the name pointer */
398 retval = target_read_u32(rtos->target,
399 current + signature->cf_off_name, &name_ptr);
400 if (retval != ERROR_OK) {
401 LOG_ERROR("Could not read ChibiOS thread name pointer from target");
402 return retval;
403 }
404
405 /* Read the thread name */
406 retval = target_read_buffer(rtos->target, name_ptr,
407 CHIBIOS_THREAD_NAME_STR_SIZE,
408 (uint8_t *)&tmp_str);
409 if (retval != ERROR_OK) {
410 LOG_ERROR("Error reading thread name from ChibiOS target");
411 return retval;
412 }
413 tmp_str[CHIBIOS_THREAD_NAME_STR_SIZE - 1] = '\x00';
414
415 if (tmp_str[0] == '\x00')
416 strcpy(tmp_str, "No Name");
417
418 curr_thrd_details->thread_name_str = malloc(
419 strlen(tmp_str) + 1);
420 strcpy(curr_thrd_details->thread_name_str, tmp_str);
421
422 /* State info */
423 uint8_t threadState;
424 const char *state_desc;
425
426 retval = target_read_u8(rtos->target,
427 current + signature->cf_off_state, &threadState);
428 if (retval != ERROR_OK) {
429 LOG_ERROR("Error reading thread state from ChibiOS target");
430 return retval;
431 }
432
433
434 if (threadState < CHIBIOS_NUM_STATES)
435 state_desc = ChibiOS_thread_states[threadState];
436 else
437 state_desc = "Unknown state";
438
439 curr_thrd_details->extra_info_str = malloc(strlen(
440 state_desc)+1);
441 strcpy(curr_thrd_details->extra_info_str, state_desc);
442
443 curr_thrd_details->exists = true;
444 curr_thrd_details->display_str = NULL;
445
446 curr_thrd_details++;
447 }
448
449 uint32_t current_thrd;
450 /* NOTE: By design, cf_off_name equals readylist_current_offset */
451 retval = target_read_u32(rtos->target,
452 rlist + signature->cf_off_name,
453 &current_thrd);
454 if (retval != ERROR_OK) {
455 LOG_ERROR("Could not read current Thread from ChibiOS target");
456 return retval;
457 }
458 rtos->current_thread = current_thrd;
459
460 return 0;
461 }
462
463 static int ChibiOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char **hex_reg_list)
464 {
465 int retval;
466 const struct ChibiOS_params *param;
467 uint32_t stack_ptr = 0;
468
469 *hex_reg_list = NULL;
470 if ((rtos == NULL) || (thread_id == 0) ||
471 (rtos->rtos_specific_params == NULL))
472 return -1;
473
474 param = (const struct ChibiOS_params *) rtos->rtos_specific_params;
475
476 if (!param->signature)
477 return -1;
478
479 /* Update stacking if it can only be determined from runtime information */
480 if ((param->stacking_info == 0) &&
481 (ChibiOS_update_stacking(rtos) != ERROR_OK)) {
482 LOG_ERROR("Failed to determine exact stacking for the target type %s", rtos->target->type->name);
483 return -1;
484 }
485
486 /* Read the stack pointer */
487 retval = target_read_u32(rtos->target,
488 thread_id + param->signature->cf_off_ctx, &stack_ptr);
489 if (retval != ERROR_OK) {
490 LOG_ERROR("Error reading stack frame from ChibiOS thread");
491 return retval;
492 }
493
494 return rtos_generic_stack_read(rtos->target, param->stacking_info, stack_ptr, hex_reg_list);
495 }
496
497 static int ChibiOS_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[])
498 {
499 unsigned int i;
500 *symbol_list = malloc(
501 sizeof(symbol_table_elem_t) * ARRAY_SIZE(ChibiOS_symbol_list));
502
503 for (i = 0; i < ARRAY_SIZE(ChibiOS_symbol_list); i++)
504 (*symbol_list)[i].symbol_name = ChibiOS_symbol_list[i];
505
506 return 0;
507 }
508
509 static int ChibiOS_detect_rtos(struct target *target)
510 {
511 if ((target->rtos->symbols != NULL) &&
512 (target->rtos->symbols[ChibiOS_VAL_rlist].address != 0) &&
513 (target->rtos->symbols[ChibiOS_VAL_chSysInit].address != 0)) {
514
515 if (target->rtos->symbols[ChibiOS_VAL_ch_debug].address == 0) {
516 LOG_INFO("It looks like the target is running ChibiOS without "
517 "ch_debug.");
518 return 0;
519 }
520
521 /* looks like ChibiOS with memory map enabled.*/
522 return 1;
523 }
524
525 return 0;
526 }
527
528 static int ChibiOS_create(struct target *target)
529 {
530 int i = 0;
531 while ((i < CHIBIOS_NUM_PARAMS) &&
532 (0 != strcmp(ChibiOS_params_list[i].target_name, target->type->name))) {
533 i++;
534 }
535 if (i >= CHIBIOS_NUM_PARAMS) {
536 LOG_WARNING("Could not find target \"%s\" in ChibiOS compatibility "
537 "list", target->type->name);
538 return -1;
539 }
540
541 target->rtos->rtos_specific_params = (void *) &ChibiOS_params_list[i];
542 return 0;
543 }

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)