1
2
3
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
43
44
58
59
60
61
62
63
64
65
66
67
68
69
70
71
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
100
101
102
103
104
105
106
107
108
112
113
114
115
116
117
118
119
120
124
125
126
130
131
132
133
134
135
136
137
138
139
140
141
145
146
147
148
149
150
151
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
199
203
204
205
206
207
208
209
210
211
212
213
217
218
219
220
221
222
223
227
228
229
230
234
235
236
237
238
239
240
241
242
246
247
251
252
253
254
255
259
260
261
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
311
312
313
317
318
319
320
324
325
326
327
328
332
333
337
338
339
340
341
342
343
344
345
346
347
348
352
353
354
355
356
361
362
374
375
376
377
378
379
380
381
382
383
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#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_standard_stackings.h"
#include "target/armv7m.h"
#include "target/cortex_m.h"
9 includes
#define ST_DEAD BIT(0)
#define ST_WAIT BIT(1)
#define ST_SEM BIT(2)
#define ST_MTX BIT(3)
#define ST_SIG BIT(4)
#define ST_DLY BIT(5)
#define ST_FLAG BIT(6)
#define ST_FLAG_ALL BIT(7)
#define ST_MBOX BIT(8)
#define ST_STP BIT(9)
#define ST_SUSPEND BIT(10)
#define ST_TT BIT(11)
#define ST_TT_YIELD BIT(12)
#define ST_CREATE BIT(13)
14 defines
struct rtkernel_params {
const char *target_name;
const struct rtos_register_stacking *stacking_info_cm3;
const struct rtos_register_stacking *stacking_info_cm4f;
const struct rtos_register_stacking *stacking_info_cm4f_fpu;
...};
static const struct rtkernel_params rtkernel_params_list[] = {
{
"cortex_m",
&rtos_standard_cortex_m3_stacking,
&rtos_standard_cortex_m4f_stacking,
&rtos_standard_cortex_m4f_fpu_stacking,
...},
{
"hla_target",
&rtos_standard_cortex_m3_stacking,
&rtos_standard_cortex_m4f_stacking,
&rtos_standard_cortex_m4f_fpu_stacking,
...},
...};
enum rtkernel_symbol_values {
sym_os_state = 0,
sym___off_os_state2chain = 1,
sym___off_os_state2current = 2,
sym___off_task2chain = 3,
sym___off_task2magic = 4,
sym___off_task2stack = 5,
sym___off_task2state = 6,
sym___off_task2name = 7,
sym___val_task_magic = 8,
...};
struct symbols {
const char *name;
bool optional;
...};
static const struct symbols rtkernel_symbol_list[] = {
{ "os_state", false },
{ "__off_os_state2chain", false },
{ "__off_os_state2current", false },
{ "__off_task2chain", false },
{ "__off_task2magic", false },
{ "__off_task2stack", false },
{ "__off_task2state", false },
{ "__off_task2name", false },
{ "__val_task_magic", false },
{ NULL, false }
...};
static void *realloc_preserve(void *ptr, size_t old_size, size_t new_size)
{
void *new_ptr = malloc(new_size);
if (new_ptr) {
memcpy(new_ptr, ptr, MIN(old_size, new_size));
free(ptr);
}if (new_ptr) { ... }
return new_ptr;
}{ ... }
static int rtkernel_add_task(struct rtos *rtos, uint32_t task, uint32_t current_task)
{
int retval;
int new_thread_count = rtos->thread_count + 1;
struct thread_detail *new_thread_details = realloc_preserve(rtos->thread_details,
rtos->thread_count * sizeof(struct thread_detail),
new_thread_count * sizeof(struct thread_detail));
if (!new_thread_details) {
LOG_ERROR("Error growing memory to %d threads", new_thread_count);
return ERROR_FAIL;
}if (!new_thread_details) { ... }
rtos->thread_details = new_thread_details;
struct thread_detail *thread = &new_thread_details[rtos->thread_count];
*thread = (struct thread_detail){ .threadid = task, .exists = true };
uint32_t name;
retval = target_read_u32(rtos->target, task + rtos->symbols[sym___off_task2name].address, &name);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read task name pointer from target");
return retval;
}if (retval != ERROR_OK) { ... }
uint8_t tmp_str[33];
retval = target_read_buffer(rtos->target, name, sizeof(tmp_str) - 1, tmp_str);
if (retval != ERROR_OK) {
LOG_ERROR("Error reading task name from target");
return retval;
}if (retval != ERROR_OK) { ... }
tmp_str[sizeof(tmp_str) - 1] = '\0';
LOG_DEBUG("task name at 0x%" PRIx32 ", value \"%s\"", name, tmp_str);
if (tmp_str[0] != '\0')
thread->thread_name_str = strdup((char *)tmp_str);
else
thread->thread_name_str = strdup("No Name");
uint16_t state;
retval = target_read_u16(rtos->target, task + rtos->symbols[sym___off_task2state].address, &state);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read task state from target");
return retval;
}if (retval != ERROR_OK) { ... }
LOG_DEBUG("task state 0x%" PRIx16, state);
char state_str[64] = "";
if (state & ST_TT)
strcat(state_str, "TT|");
if (task == current_task) {
strcat(state_str, "RUN");
}if (task == current_task) { ... } else {
if (state & (ST_TT | ST_TT_YIELD))
strcat(state_str, "YIELD");
else if (state & ST_DEAD)
strcat(state_str, "DEAD");
else if (state & ST_WAIT)
strcat(state_str, "WAIT");
else if (state & ST_SUSPEND)
strcat(state_str, "SUSP");
else
strcat(state_str, "READY");
}else { ... }
if (state & ST_SEM)
strcat(state_str, "|SEM");
if (state & ST_MTX)
strcat(state_str, "|MTX");
if (state & ST_SIG)
strcat(state_str, "|SIG");
if (state & ST_DLY)
strcat(state_str, "|DLY");
if ((state & ST_FLAG) || (state & ST_FLAG_ALL))
strcat(state_str, "|FLAG");
if (state & ST_FLAG_ALL)
strcat(state_str, "_ALL");
if (state & ST_MBOX)
strcat(state_str, "|MBOX");
if (state & ST_STP)
strcat(state_str, "|STP");
thread->extra_info_str = strdup(state_str);
rtos->thread_count = new_thread_count;
if (task == current_task)
rtos->current_thread = task;
return ERROR_OK;
}{ ... }
static int rtkernel_verify_task(struct rtos *rtos, uint32_t task)
{
int retval;
uint32_t magic;
retval = target_read_u32(rtos->target, task + rtos->symbols[sym___off_task2magic].address, &magic);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read task magic from target");
return retval;
}if (retval != ERROR_OK) { ... }
if (magic != rtos->symbols[sym___val_task_magic].address) {
LOG_ERROR("Invalid task found (magic=0x%" PRIx32 ")", magic);
return ERROR_FAIL;
}if (magic != rtos->symbols[sym___val_task_magic].address) { ... }
return retval;
}{ ... }
static int rtkernel_update_threads(struct rtos *rtos)
{
rtos_free_threadlist(rtos);
rtos->current_thread = 0;
if (!rtos->symbols) {
LOG_ERROR("No symbols for rt-kernel");
return -3;
}if (!rtos->symbols) { ... }
uint32_t current_task;
int retval = target_read_u32(rtos->target,
rtos->symbols[sym_os_state].address + rtos->symbols[sym___off_os_state2current].address,
¤t_task);
if (retval != ERROR_OK) {
LOG_ERROR("Error reading current task");
return retval;
}if (retval != ERROR_OK) { ... }
LOG_DEBUG("current task is 0x%" PRIx32, current_task);
retval = rtkernel_verify_task(rtos, current_task);
if (retval != ERROR_OK) {
LOG_ERROR("Current task is invalid");
return retval;
}if (retval != ERROR_OK) { ... }
uint32_t chain = rtos->symbols[sym_os_state].address + rtos->symbols[sym___off_os_state2chain].address;
LOG_DEBUG("chain start at 0x%" PRIx32, chain);
uint32_t next = chain;
for (;;) {
retval = target_read_u32(rtos->target, next, &next);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read rt-kernel data structure from target");
return retval;
}if (retval != ERROR_OK) { ... }
LOG_DEBUG("next entry at 0x%" PRIx32, next);
if (next == chain) {
LOG_DEBUG("end of chain detected");
break;
}if (next == chain) { ... }
uint32_t task = next - rtos->symbols[sym___off_task2chain].address;
LOG_DEBUG("found task at 0x%" PRIx32, task);
retval = rtkernel_verify_task(rtos, task);
if (retval != ERROR_OK) {
LOG_ERROR("Invalid task found");
return retval;
}if (retval != ERROR_OK) { ... }
retval = rtkernel_add_task(rtos, task, current_task);
if (retval != ERROR_OK) {
LOG_ERROR("Could not add task to rtos system");
return retval;
}if (retval != ERROR_OK) { ... }
}for (;;) { ... }
return ERROR_OK;
}{ ... }
static int rtkernel_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
struct rtos_reg **reg_list, int *num_regs)
{
uint32_t stack_ptr = 0;
if (!rtos)
return -1;
if (thread_id == 0)
return -2;
if (!rtos->rtos_specific_params)
return -1;
const struct rtkernel_params *param = rtos->rtos_specific_params;
int retval = target_read_u32(rtos->target, thread_id + rtos->symbols[sym___off_task2stack].address, &stack_ptr);
if (retval != ERROR_OK) {
LOG_ERROR("Error reading stack pointer from rtkernel thread");
return retval;
}if (retval != ERROR_OK) { ... }
LOG_DEBUG("stack pointer at 0x%" PRIx64 ", value 0x%" PRIx32,
thread_id + rtos->symbols[sym___off_task2stack].address,
stack_ptr);
stack_ptr += 4;
bool cm4_fpu_enabled = false;
struct armv7m_common *armv7m_target = target_to_armv7m(rtos->target);
if (is_armv7m(armv7m_target)) {
if (armv7m_target->fp_feature != FP_NONE) {
uint32_t cpacr;
retval = target_read_u32(rtos->target, FPU_CPACR, &cpacr);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read CPACR register to check FPU state");
return -1;
}if (retval != ERROR_OK) { ... }
if (cpacr & 0x00F00000) {
cm4_fpu_enabled = true;
}if (cpacr & 0x00F00000) { ... }
}if (armv7m_target->fp_feature != FP_NONE) { ... }
}if (is_armv7m(armv7m_target)) { ... }
if (!cm4_fpu_enabled) {
LOG_DEBUG("cm3 stacking");
return rtos_generic_stack_read(rtos->target, param->stacking_info_cm3, stack_ptr, reg_list, num_regs);
}if (!cm4_fpu_enabled) { ... }
uint32_t lr_svc;
retval = target_read_u32(rtos->target, stack_ptr + 0x20, &lr_svc);
if (retval != ERROR_OK) {
LOG_OUTPUT("Error reading stack frame from rtkernel thread\r\n");
return retval;
}if (retval != ERROR_OK) { ... }
if ((lr_svc & 0x10) == 0) {
LOG_DEBUG("cm4f_fpu stacking");
return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f_fpu, stack_ptr, reg_list, num_regs);
}if ((lr_svc & 0x10) == 0) { ... }
LOG_DEBUG("cm4f stacking");
return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f, stack_ptr, reg_list, num_regs);
}{ ... }
static int rtkernel_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
{
*symbol_list = calloc(ARRAY_SIZE(rtkernel_symbol_list), sizeof(struct symbol_table_elem));
if (!*symbol_list)
return ERROR_FAIL;
for (size_t i = 0; i < ARRAY_SIZE(rtkernel_symbol_list); i++) {
(*symbol_list)[i].symbol_name = rtkernel_symbol_list[i].name;
(*symbol_list)[i].optional = rtkernel_symbol_list[i].optional;
}for (size_t i = 0; i < ARRAY_SIZE(rtkernel_symbol_list); i++) { ... }
return ERROR_OK;
}{ ... }
static bool rtkernel_detect_rtos(struct target *target)
{
return (target->rtos->symbols) &&
(target->rtos->symbols[sym___off_os_state2chain].address != 0);
}{ ... }
static int rtkernel_create(struct target *target)
{
for (size_t i = 0; i < ARRAY_SIZE(rtkernel_params_list); i++) {
if (strcmp(rtkernel_params_list[i].target_name, target_type_name(target)) == 0) {
target->rtos->rtos_specific_params = (void *)&rtkernel_params_list[i];
return 0;
}if (strcmp(rtkernel_params_list[i].target_name, target_type_name(target)) == 0) { ... }
}for (size_t i = 0; i < ARRAY_SIZE(rtkernel_params_list); i++) { ... }
LOG_ERROR("Could not find target in rt-kernel compatibility list");
return -1;
}{ ... }
const struct rtos_type rtkernel_rtos = {
.name = "rtkernel",
.detect_rtos = rtkernel_detect_rtos,
.create = rtkernel_create,
.update_threads = rtkernel_update_threads,
.get_thread_reg_list = rtkernel_get_thread_reg_list,
.get_symbol_list_to_lookup = rtkernel_get_symbol_list_to_lookup,
...};