rtos: Fix wrong ReadyList lookup in ChibiOS
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "rtos.h"
33 #include "helper/log.h"
34 #include "helper/types.h"
35 #include "rtos_chibios_stackings.h"
36
37
38 /**
39 * @brief ChibiOS/RT memory signature record.
40 *
41 * @details Definition copied from os/kernel/include/chregistry.h of ChibiOS/RT.
42 */
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
57 field. */
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
62 field. */
63 uint8_t cf_off_time; /**< @brief Offset of @p p_time field. */
64 };
65
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)
69
70 /**
71 * @brief ChibiOS thread states.
72 */
73 const char *ChibiOS_thread_states[] = {
74 "READY", "CURRENT", "SUSPENDED", "WTSEM", "WTMTX", "WTCOND", "SLEEPING",
75 "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", "SNDMSG", "WTMSG", "WTQUEUE",
76 "FINAL"
77 };
78
79 #define CHIBIOS_NUM_STATES (sizeof(ChibiOS_thread_states)/sizeof(char *))
80
81 /* Maximum ChibiOS thread name. There is no real limit set by ChibiOS but 64
82 * chars ought to be enough.
83 */
84 #define CHIBIOS_THREAD_NAME_STR_SIZE (64)
85
86 struct ChibiOS_params {
87 const char *target_name;
88
89 struct ChibiOS_chdebug *signature;
90 const struct rtos_register_stacking *stacking_info;
91 };
92
93 struct ChibiOS_params ChibiOS_params_list[] = {
94 {
95 "cortex_m3", /* target_name */
96 0,
97 &rtos_chibios_arm_v7m_stacking, /* stacking_info */
98 },
99 {
100 "stm32_stlink", /* target_name */
101 0,
102 &rtos_chibios_arm_v7m_stacking, /* stacking_info */
103 }
104 };
105 #define CHIBIOS_NUM_PARAMS ((int)(sizeof(ChibiOS_params_list)/sizeof(struct ChibiOS_params)))
106
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[]);
112
113 struct rtos_type ChibiOS_rtos = {
114 .name = "ChibiOS",
115
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,
121 };
122
123 enum ChibiOS_symbol_values {
124 ChibiOS_VAL_rlist = 0,
125 ChibiOS_VAL_ch_debug = 1,
126 ChibiOS_VAL_chSysInit = 2
127 };
128
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*/
133 NULL
134 };
135
136 static int ChibiOS_update_memory_signature(struct rtos *rtos)
137 {
138 int retval;
139 struct ChibiOS_params *param;
140 struct ChibiOS_chdebug *signature;
141
142 param = (struct ChibiOS_params *) rtos->rtos_specific_params;
143
144 /* Free existing memory description.*/
145 if (param->signature) {
146 free(param->signature);
147 param->signature = 0;
148 }
149
150 signature = malloc(sizeof(*signature));
151 if (!signature) {
152 LOG_ERROR("Could not allocate space for ChibiOS/RT memory signature");
153 return -1;
154 }
155
156 retval = target_read_buffer(rtos->target,
157 rtos->symbols[ChibiOS_VAL_ch_debug].address,
158 sizeof(*signature),
159 (uint8_t *) signature);
160 if (retval != ERROR_OK) {
161 LOG_ERROR("Could not read ChibiOS/RT memory signature from target");
162 goto errfree;
163 }
164
165 if (strncmp(signature->ch_identifier, "main", 4) != 0) {
166 LOG_ERROR("Memory signature identifier does not contain magic bytes.");
167 goto errfree;
168 }
169
170 if (signature->ch_size < sizeof(*signature)) {
171 LOG_ERROR("ChibiOS/RT memory signature claims to be smaller "
172 "than expected");
173 goto errfree;
174 }
175
176 if (signature->ch_size > sizeof(*signature)) {
177 LOG_WARNING("ChibiOS/RT memory signature claims to be bigger than"
178 " expected. Assuming compatibility...");
179 }
180
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);
186
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));
191
192 param->signature = signature;
193 return 0;
194
195 errfree:
196 /* Error reading the ChibiOS memory structure */
197 free(signature);
198 param->signature = 0;
199 return -1;
200 }
201
202
203 static int ChibiOS_update_stacking(struct rtos *rtos)
204 {
205 /* Sometimes the stacking can not be determined only by looking at the
206 * target name but only a runtime.
207 *
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.
210 *
211 * Terminating which stacking is used is target depending.
212 *
213 * Assumptions:
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.
221 */
222
223 /* TODO: Add actual detection, currently it will not work with FPU enabled.*/
224 return -1;
225 }
226
227 static int ChibiOS_update_threads(struct rtos *rtos)
228 {
229 int retval;
230 const struct ChibiOS_params *param;
231 int tasks_found = 0;
232 int rtos_valid = -1;
233
234 if (!rtos->rtos_specific_params)
235 return -1;
236
237 if (!rtos->symbols) {
238 LOG_ERROR("No symbols for ChibiOS");
239 return -3;
240 }
241
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");
248 return retval;
249 }
250 }
251
252 /* wipe out previous thread details if any */
253 int j;
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);
263 }
264 free(rtos->thread_details);
265 rtos->thread_details = NULL;
266 rtos->thread_count = 0;
267 }
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
270 * threads. */
271 const uint32_t rlist = rtos->symbols[ChibiOS_VAL_rlist].address;
272 uint32_t current;
273 uint32_t previous;
274 uint32_t older;
275
276 current = rlist;
277 previous = rlist;
278 while (1) {
279 retval = target_read_buffer(rtos->target,
280 current + param->signature->cf_off_newer,
281 param->signature->ch_ptrsize,
282 (uint8_t *)&current);
283 if (retval != ERROR_OK) {
284 LOG_ERROR("Could not read next ChibiOS thread");
285 return retval;
286 }
287 /* Could be NULL if the kernel is not initialized yet or if the
288 * registry is corrupted. */
289 if (current == 0) {
290 LOG_ERROR("ChibiOS registry integrity check failed, NULL pointer");
291
292 rtos_valid = 0;
293 break;
294 }
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,
299 (uint8_t *)&older);
300 if ((retval != ERROR_OK) || (older == 0) || (older != previous)) {
301 LOG_ERROR("ChibiOS registry integrity check failed, "
302 "double linked list violation");
303 rtos_valid = 0;
304 break;
305 }
306 /* Check for full iteration of the linked list. */
307 if (current == rlist)
308 break;
309 tasks_found++;
310 previous = current;
311 }
312 if (!rtos_valid) {
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.");
316
317 const char tmp_thread_name[] = "Current Execution";
318 const char tmp_thread_extra_info[] = "No RTOS thread";
319
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;
325
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);
329
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);
333
334 rtos->current_thread = 1;
335 rtos->thread_count = 1;
336 return ERROR_OK;
337 }
338
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");
344 return -1;
345 }
346
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];
353
354 retval = target_read_buffer(rtos->target,
355 current + param->signature->cf_off_newer,
356 param->signature->ch_ptrsize,
357 (uint8_t *)&current);
358 if (retval != ERROR_OK) {
359 LOG_ERROR("Could not read next ChibiOS thread");
360 return -6;
361 }
362
363 /* Check for full iteration of the linked list. */
364 if (current == rlist)
365 break;
366
367 /* Save the thread pointer */
368 curr_thrd_details->threadid = current;
369
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");
377 return retval;
378 }
379
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");
386 return retval;
387 }
388 tmp_str[CHIBIOS_THREAD_NAME_STR_SIZE - 1] = '\x00';
389
390 if (tmp_str[0] == '\x00')
391 strcpy(tmp_str, "No Name");
392
393 curr_thrd_details->thread_name_str = (char *)malloc(
394 strlen(tmp_str) + 1);
395 strcpy(curr_thrd_details->thread_name_str, tmp_str);
396
397 /* State info */
398 uint8_t threadState;
399 const char *state_desc;
400
401 retval = target_read_buffer(rtos->target,
402 current + param->signature->cf_off_state,
403 1, &threadState);
404 if (retval != ERROR_OK) {
405 LOG_ERROR("Error reading thread state from ChibiOS target");
406 return retval;
407 }
408
409
410 if (threadState < CHIBIOS_NUM_STATES)
411 state_desc = ChibiOS_thread_states[threadState];
412 else
413 state_desc = "Unknown state";
414
415 curr_thrd_details->extra_info_str = (char *)malloc(strlen(
416 state_desc)+1);
417 strcpy(curr_thrd_details->extra_info_str, state_desc);
418
419 curr_thrd_details->exists = true;
420 curr_thrd_details->display_str = NULL;
421
422 curr_thrd_details++;
423 }
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");
431 return retval;
432 }
433
434 return 0;
435 }
436
437 static int ChibiOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char **hex_reg_list)
438 {
439 int retval;
440 const struct ChibiOS_params *param;
441 int64_t stack_ptr = 0;
442
443 *hex_reg_list = NULL;
444 if ((rtos == NULL) || (thread_id == 0) ||
445 (rtos->rtos_specific_params == NULL))
446 return -1;
447
448 param = (const struct ChibiOS_params *) rtos->rtos_specific_params;
449
450 if (!param->signature)
451 return -1;
452
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);
457 return -1;
458 }
459
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");
467 return retval;
468 }
469
470 return rtos_generic_stack_read(rtos->target, param->stacking_info, stack_ptr, hex_reg_list);
471 }
472
473 static int ChibiOS_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[])
474 {
475 unsigned int i;
476 *symbol_list = (symbol_table_elem_t *) malloc(
477 sizeof(symbol_table_elem_t) * ARRAY_SIZE(ChibiOS_symbol_list));
478
479 for (i = 0; i < ARRAY_SIZE(ChibiOS_symbol_list); i++)
480 (*symbol_list)[i].symbol_name = ChibiOS_symbol_list[i];
481
482 return 0;
483 }
484
485 static int ChibiOS_detect_rtos(struct target *target)
486 {
487 if ((target->rtos->symbols != NULL) &&
488 (target->rtos->symbols[ChibiOS_VAL_rlist].address != 0) &&
489 (target->rtos->symbols[ChibiOS_VAL_chSysInit].address != 0)) {
490
491 if (target->rtos->symbols[ChibiOS_VAL_ch_debug].address == 0) {
492 LOG_INFO("It looks like the target is running ChibiOS without "
493 "ch_debug.");
494 return 0;
495 }
496
497 /* looks like ChibiOS with memory map enabled.*/
498 return 1;
499 }
500
501 return 0;
502 }
503
504 static int ChibiOS_create(struct target *target)
505 {
506 int i = 0;
507 while ((i < CHIBIOS_NUM_PARAMS) &&
508 (0 != strcmp(ChibiOS_params_list[i].target_name, target->type->name))) {
509 i++;
510 }
511 if (i >= CHIBIOS_NUM_PARAMS) {
512 LOG_WARNING("Could not find target \"%s\" in ChibiOS compatibility "
513 "list", target->type->name);
514 return -1;
515 }
516
517 target->rtos->rtos_specific_params = (void *) &ChibiOS_params_list[i];
518 return 0;
519 }

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)