1
2
3
7
8
9
10
11
12
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
43
44
47
48
53
54
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
83
84
87
88
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
119
120
121
122
123
124
125
126
127
131
132
136
137
138
139
142
143
144
145
146
152
153
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
208
212
213
214
215
216
217
218
219
223
224
225
226
229
230
231
241
242
245
246
247
260
261
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
378
379
380
381
382
383
384
385
386
387
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
412
413
414
415
416
417
418
419
420
421
422
423
424
425
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
473
474
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
#include <helper/time_support.h>
#include <jtag/jtag.h>
#include "target/target.h"
#include "rtos.h"
#include "helper/log.h"
#include "helper/types.h"
#include "rtos_mqx_stackings.h"
8 includes
#define MQX_THREAD_NAME_LENGTH (255)
#define MQX_KERNEL_OFFSET_TDLIST (0x0108)
#define MQX_KERNEL_OFFSET_SYSTEM_TASK (0x0050)
#define MQX_KERNEL_OFFSET_ACTIVE_TASK (0x001C)
#define MQX_KERNEL_OFFSET_CAPABILITY (0x0000)
#define MQX_QUEUE_OFFSET_SIZE (0x0008)
#define MQX_TASK_OFFSET_STATE (0x0008)
#define MQX_TASK_OFFSET_ID (0x000c)
#define MQX_TASK_OFFSET_TEMPLATE (0x0068)
#define MQX_TASK_OFFSET_STACK (0x0014)
#define MQX_TASK_OFFSET_TDLIST (0x006C)
#define MQX_TASK_OFFSET_NEXT (0x0000)
#define MQX_TASK_TEMPLATE_OFFSET_NAME (0x0010)
#define MQX_TASK_OFFSET_ERROR_CODE (0x005C)
#define MQX_TASK_STATE_MASK (0xFFF)
15 defines
enum mqx_symbols {
MQX_VAL_MQX_KERNEL_DATA,
MQX_VAL_MQX_INIT_STRUCT,
...};
enum mqx_arch {
mqx_arch_cortexm,
...};
struct mqx_params {
const char *target_name;
const enum mqx_arch target_arch;
const struct rtos_register_stacking *stacking_info;
...};
struct mqx_state {
uint32_t state;
char *name;
...};
static const struct mqx_state mqx_states[] = {
{ 0x0002, "READY" },
{ 0x0003, "BLOCKED" },
{ 0x0005, "RCV_SPECIFIC_BLOCKED" },
{ 0x0007, "RCV_ANY_BLOCKED" },
{ 0x0009, "DYING" },
{ 0x000B, "UNHANDLED_INT_BLOCKED" },
{ 0x000D, "SEND_BLOCKED" },
{ 0x000F, "BREAKPOINT_BLOCKED" },
{ 0x0211, "IO_BLOCKED" },
{ 0x0021, "SEM_BLOCKED" },
{ 0x0223, "MUTEX_BLOCKED" },
{ 0x0025, "EVENT_BLOCKED" },
{ 0x0229, "TASK_QUEUE_BLOCKED" },
{ 0x042B, "LWSEM_BLOCKED" },
{ 0x042D, "LWEVENT_BLOCKED" },
...};
static const char * const mqx_symbol_list[] = {
"_mqx_kernel_data",
"MQX_init_struct",
NULL
...};
static const struct mqx_params mqx_params_list[] = {
{ "cortex_m", mqx_arch_cortexm, &rtos_mqx_arm_v7m_stacking },
...};
/* ... */
static int mqx_valid_address_check(
struct rtos *rtos,
uint32_t address
)
{
enum mqx_arch arch_type = ((struct mqx_params *)rtos->rtos_specific_params)->target_arch;
const char *targetname = ((struct mqx_params *)rtos->rtos_specific_params)->target_name;
if (arch_type == mqx_arch_cortexm) {
if (
(address && address <= 0x3FFFFFFFu) ||
(address >= 0x6000000u && address <= 0x9FFFFFFFu)
) {
return ERROR_OK;
}if (/* code and sram area */ (address && address <= 0x3FFFFFFFu) || /* external ram area*/ (address >= 0x6000000u && address <= 0x9FFFFFFFu)) { ... }
return ERROR_FAIL;
}if (arch_type == mqx_arch_cortexm) { ... }
LOG_ERROR("MQX RTOS - unknown architecture %s", targetname);
return ERROR_FAIL;
}{ ... }
/* ... */
static int mqx_target_read_buffer(
struct target *target,
uint32_t address,
uint32_t size,
uint8_t *buffer
)
{
int status = mqx_valid_address_check(target->rtos, address);
if (status != ERROR_OK) {
LOG_WARNING("MQX RTOS - target address 0x%" PRIx32 " is not allowed to read", address);
return status;
}if (status != ERROR_OK) { ... }
status = target_read_buffer(target, address, size, buffer);
if (status != ERROR_OK) {
LOG_ERROR("MQX RTOS - reading target address 0x%" PRIx32" failed", address);
return status;
}if (status != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int mqx_get_symbol(
struct rtos *rtos,
enum mqx_symbols symbol,
void *result
)
{
(*(int *)result) = (uint32_t)rtos->symbols[symbol].address;
return ERROR_OK;
}{ ... }
/* ... */
static int mqx_get_member(
struct rtos *rtos,
const uint32_t base_address,
int32_t member_offset,
int32_t member_width,
const char *member_name,
void *result
)
{
int status = ERROR_FAIL;
status = mqx_target_read_buffer(
rtos->target, base_address + member_offset, member_width, result
);
if (status != ERROR_OK)
LOG_WARNING("MQX RTOS - cannot read \"%s\" at address 0x%" PRIx32,
member_name, (uint32_t)(base_address + member_offset));
return status;
}{ ... }
/* ... */
static int mqx_is_scheduler_running(
struct rtos *rtos
)
{
uint32_t kernel_data_symbol = 0;
uint32_t kernel_data_addr = 0;
uint32_t system_td_addr = 0;
uint32_t active_td_addr = 0;
uint32_t capability_value = 0;
if (mqx_get_symbol(rtos, MQX_VAL_MQX_KERNEL_DATA, &kernel_data_symbol) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_member(rtos, kernel_data_symbol, 0, 4,
"_mqx_kernel_data", &kernel_data_addr) != ERROR_OK)
return ERROR_FAIL;
if (kernel_data_addr == 0 || kernel_data_addr == (uint32_t)(-1))
return ERROR_FAIL;
if (mqx_get_member(rtos, kernel_data_addr, MQX_KERNEL_OFFSET_CAPABILITY, 4,
"kernel_data->ADDRESSING_CAPABILITY", (void *)&capability_value) != ERROR_OK)
return ERROR_FAIL;
/* ... */
if (capability_value != 8) {
LOG_WARNING("MQX RTOS - value of '_mqx_kernel_data->ADDRESSING_CAPABILITY' contains invalid value");
return ERROR_FAIL;
}if (capability_value != 8) { ... }
if (mqx_get_member(rtos, kernel_data_addr, MQX_KERNEL_OFFSET_ACTIVE_TASK, 4,
"kernel_data->ACTIVE_PTR", (void *)&active_td_addr) != ERROR_OK)
return ERROR_FAIL;
system_td_addr = kernel_data_addr + MQX_KERNEL_OFFSET_SYSTEM_TASK;
if (active_td_addr == system_td_addr) {
LOG_WARNING("MQX RTOS - scheduler does not run");
return ERROR_FAIL;
}if (active_td_addr == system_td_addr) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static bool mqx_detect_rtos(
struct target *target
)
{
if (
(target->rtos->symbols) &&
(target->rtos->symbols[MQX_VAL_MQX_KERNEL_DATA].address != 0)
) {
return true;
}if ((target->rtos->symbols) && (target->rtos->symbols[MQX_VAL_MQX_KERNEL_DATA].address != 0)) { ... }
return false;
}{ ... }
/* ... */
static int mqx_create(
struct target *target
)
{
for (unsigned int i = 0; i < ARRAY_SIZE(mqx_params_list); i++) {
if (strcmp(mqx_params_list[i].target_name, target_type_name(target)) == 0) {
target->rtos->rtos_specific_params = (void *)&mqx_params_list[i];
return 0;
}if (strcmp(mqx_params_list[i].target_name, target_type_name(target)) == 0) { ... }
}for (unsigned int i = 0; i < ARRAY_SIZE(mqx_params_list); i++) { ... }
LOG_ERROR("MQX RTOS - could not find target \"%s\" in MQX compatibility list", target_type_name(target));
return -1;
}{ ... }
/* ... */
static int mqx_update_threads(
struct rtos *rtos
)
{
uint32_t task_queue_addr = 0;
uint32_t kernel_data_addr = 0;
uint16_t task_queue_size = 0;
uint32_t active_td_addr = 0;
if (!rtos->rtos_specific_params)
return -3;
if (!rtos->symbols)
return -4;
rtos_free_threadlist(rtos);
if (mqx_is_scheduler_running(rtos) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_symbol(rtos, MQX_VAL_MQX_KERNEL_DATA, &kernel_data_addr) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_member(rtos, kernel_data_addr, 0, 4,
"_mqx_kernel_data", &kernel_data_addr) != ERROR_OK)
return ERROR_FAIL;
task_queue_addr = kernel_data_addr + MQX_KERNEL_OFFSET_TDLIST;
if (mqx_get_member(rtos, task_queue_addr, MQX_QUEUE_OFFSET_SIZE, 2,
"kernel_data->TD_LIST.SIZE", &task_queue_size) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_member(rtos, kernel_data_addr, MQX_KERNEL_OFFSET_ACTIVE_TASK, 4,
"kernel_data->ACTIVE_PTR", (void *)&active_td_addr) != ERROR_OK)
return ERROR_FAIL;
rtos->thread_count = task_queue_size;
rtos->current_thread = 0;
rtos->thread_details = calloc(rtos->thread_count, sizeof(struct thread_detail));
if (!rtos->thread_details)
return ERROR_FAIL;
/* ... */
for (
uint32_t i = 0, taskpool_addr = task_queue_addr;
i < (uint32_t)rtos->thread_count;
i++
) {
uint8_t task_name[MQX_THREAD_NAME_LENGTH + 1];
uint32_t task_addr = 0, task_template = 0, task_state = 0;
uint32_t task_name_addr = 0, task_id = 0, task_errno = 0;
uint32_t state_index = 0;
uint32_t extra_info_length = 0;
char *state_name = "Unknown";
if (mqx_get_member(rtos, taskpool_addr, MQX_TASK_OFFSET_NEXT, 4,
"td_struct_ptr->NEXT", &taskpool_addr) != ERROR_OK)
return ERROR_FAIL;
task_addr = taskpool_addr - MQX_TASK_OFFSET_TDLIST;
if (mqx_get_member(rtos, task_addr, MQX_TASK_OFFSET_TEMPLATE, 4,
"td_struct_ptr->TEMPLATE_LIST_PTR", &task_template) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_member(rtos, task_template, MQX_TASK_TEMPLATE_OFFSET_NAME, 4,
"td_struct_ptr->TEMPLATE_LIST_PTR->NAME", &task_name_addr) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_member(rtos, task_name_addr, 0, MQX_THREAD_NAME_LENGTH,
"*td_struct_ptr->TEMPLATE_LIST_PTR->NAME", task_name) != ERROR_OK)
return ERROR_FAIL;
/* ... */
task_name[MQX_THREAD_NAME_LENGTH] = '\0';
if (mqx_get_member(rtos, task_addr, MQX_TASK_OFFSET_ID, 4,
"td_struct_ptr->TASK_ID", &task_id) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_member(rtos, task_addr, MQX_TASK_OFFSET_ERROR_CODE, 4,
"td_struct_ptr->TASK_ERROR_CODE", &task_errno) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_member(rtos, task_addr, MQX_TASK_OFFSET_STATE, 4,
"td_struct_ptr->STATE", &task_state) != ERROR_OK)
return ERROR_FAIL;
task_state &= MQX_TASK_STATE_MASK;
for (state_index = 0; state_index < ARRAY_SIZE(mqx_states); state_index++) {
if (mqx_states[state_index].state == task_state) {
state_name = mqx_states[state_index].name;
break;
}if (mqx_states[state_index].state == task_state) { ... }
}for (state_index = 0; state_index < ARRAY_SIZE(mqx_states); state_index++) { ... }
rtos->thread_details[i].threadid = task_id;
rtos->thread_details[i].exists = true;
rtos->thread_details[i].thread_name_str = malloc(strlen((void *)task_name) + 1);
if (!rtos->thread_details[i].thread_name_str)
return ERROR_FAIL;
strcpy(rtos->thread_details[i].thread_name_str, (void *)task_name);
/* ... */
extra_info_length += strlen((void *)state_name) + 7 + 13 + 8 + 15 + 8;
rtos->thread_details[i].extra_info_str = malloc(extra_info_length + 1);
if (!rtos->thread_details[i].extra_info_str)
return ERROR_FAIL;
snprintf(rtos->thread_details[i].extra_info_str, extra_info_length,
"State: %s, Address: 0x%" PRIx32 ", Error Code: %" PRIu32,
state_name, task_addr, task_errno
);
if (active_td_addr == task_addr)
rtos->current_thread = task_id;
}for (uint32_t i = 0, taskpool_addr = task_queue_addr; i < (uint32_t)rtos->thread_count; i++) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int mqx_get_thread_reg_list(
struct rtos *rtos,
int64_t thread_id,
struct rtos_reg **reg_list,
int *num_regs
)
{
int64_t stack_ptr = 0;
uint32_t my_task_addr = 0;
uint32_t task_queue_addr = 0;
uint32_t task_queue_size = 0;
uint32_t kernel_data_addr = 0;
if (thread_id == 0) {
LOG_ERROR("MQX RTOS - invalid threadid: 0x%X", (int)thread_id);
return ERROR_FAIL;
}if (thread_id == 0) { ... }
if (mqx_is_scheduler_running(rtos) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_symbol(rtos, MQX_VAL_MQX_KERNEL_DATA, &kernel_data_addr) != ERROR_OK)
return ERROR_FAIL;
if (mqx_get_member(rtos, kernel_data_addr, 0, 4,
"_mqx_kernel_data", &kernel_data_addr) != ERROR_OK)
return ERROR_FAIL;
task_queue_addr = kernel_data_addr + MQX_KERNEL_OFFSET_TDLIST;
if (mqx_get_member(rtos, task_queue_addr, MQX_QUEUE_OFFSET_SIZE, 2,
"kernel_data->TD_LIST.SIZE", &task_queue_size) != ERROR_OK)
return ERROR_FAIL;
for (
uint32_t i = 0, taskpool_addr = task_queue_addr;
i < (uint32_t)rtos->thread_count;
i++
) {
uint32_t tmp_address = 0, task_addr = 0;
uint32_t task_id = 0;
tmp_address = taskpool_addr;
if (mqx_get_member(rtos, tmp_address, MQX_TASK_OFFSET_NEXT, 4,
"td_struct_ptr->NEXT", &taskpool_addr) != ERROR_OK)
return ERROR_FAIL;
task_addr = taskpool_addr - MQX_TASK_OFFSET_TDLIST;
if (mqx_get_member(rtos, task_addr, MQX_TASK_OFFSET_ID, 4,
"td_struct_ptr->TASK_ID", &task_id) != ERROR_OK)
return ERROR_FAIL;
if (task_id == thread_id) {
my_task_addr = task_addr;
break;
}if (task_id == thread_id) { ... }
}for (uint32_t i = 0, taskpool_addr = task_queue_addr; i < (uint32_t)rtos->thread_count; i++) { ... }
if (!my_task_addr) {
LOG_ERROR("MQX_RTOS - threadid %" PRId64 " does not match any task", thread_id);
return ERROR_FAIL;
}if (!my_task_addr) { ... }
if (mqx_get_member(rtos, my_task_addr, MQX_TASK_OFFSET_STACK, 4,
"task->STACK_PTR", &stack_ptr) != ERROR_OK)
return ERROR_FAIL;
return rtos_generic_stack_read(
rtos->target, ((struct mqx_params *)rtos->rtos_specific_params)->stacking_info, stack_ptr, reg_list, num_regs
);
}{ ... }
static int mqx_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
{
*symbol_list = calloc(ARRAY_SIZE(mqx_symbol_list), sizeof(struct symbol_table_elem));
if (!*symbol_list)
return ERROR_FAIL;
for (int i = 0; i < (int)(ARRAY_SIZE(mqx_symbol_list)); i++)
(*symbol_list)[i].symbol_name = mqx_symbol_list[i];
return ERROR_OK;
}{ ... }
const struct rtos_type mqx_rtos = {
.name = "mqx",
.detect_rtos = mqx_detect_rtos,
.create = mqx_create,
.update_threads = mqx_update_threads,
.get_thread_reg_list = mqx_get_thread_reg_list,
.get_symbol_list_to_lookup = mqx_get_symbol_list_to_lookup,
...};