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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
123
124
125
126
127
128
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
169
170
171
172
173
174
180
181
182
183
189
190
191
192
198
199
200
207
208
215
216
223
224
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "rtos.h"
#include "target/armv7m.h"
#include "rtos_standard_stackings.h"
static const struct stack_register_offset rtos_standard_cortex_m3_stack_offsets[ARMV7M_NUM_CORE_REGS] = {
{ ARMV7M_R0, 0x20, 32 },
{ ARMV7M_R1, 0x24, 32 },
{ ARMV7M_R2, 0x28, 32 },
{ ARMV7M_R3, 0x2c, 32 },
{ ARMV7M_R4, 0x00, 32 },
{ ARMV7M_R5, 0x04, 32 },
{ ARMV7M_R6, 0x08, 32 },
{ ARMV7M_R7, 0x0c, 32 },
{ ARMV7M_R8, 0x10, 32 },
{ ARMV7M_R9, 0x14, 32 },
{ ARMV7M_R10, 0x18, 32 },
{ ARMV7M_R11, 0x1c, 32 },
{ ARMV7M_R12, 0x30, 32 },
{ ARMV7M_R13, -2, 32 },
{ ARMV7M_R14, 0x34, 32 },
{ ARMV7M_PC, 0x38, 32 },
{ ARMV7M_XPSR, 0x3c, 32 },
...};
static const struct stack_register_offset rtos_standard_cortex_m4f_stack_offsets[] = {
{ ARMV7M_R0, 0x24, 32 },
{ ARMV7M_R1, 0x28, 32 },
{ ARMV7M_R2, 0x2c, 32 },
{ ARMV7M_R3, 0x30, 32 },
{ ARMV7M_R4, 0x00, 32 },
{ ARMV7M_R5, 0x04, 32 },
{ ARMV7M_R6, 0x08, 32 },
{ ARMV7M_R7, 0x0c, 32 },
{ ARMV7M_R8, 0x10, 32 },
{ ARMV7M_R9, 0x14, 32 },
{ ARMV7M_R10, 0x18, 32 },
{ ARMV7M_R11, 0x1c, 32 },
{ ARMV7M_R12, 0x34, 32 },
{ ARMV7M_R13, -2, 32 },
{ ARMV7M_R14, 0x38, 32 },
{ ARMV7M_PC, 0x3c, 32 },
{ ARMV7M_XPSR, 0x40, 32 },
...};
static const struct stack_register_offset rtos_standard_cortex_m4f_fpu_stack_offsets[] = {
{ ARMV7M_R0, 0x64, 32 },
{ ARMV7M_R1, 0x68, 32 },
{ ARMV7M_R2, 0x6c, 32 },
{ ARMV7M_R3, 0x70, 32 },
{ ARMV7M_R4, 0x00, 32 },
{ ARMV7M_R5, 0x04, 32 },
{ ARMV7M_R6, 0x08, 32 },
{ ARMV7M_R7, 0x0c, 32 },
{ ARMV7M_R8, 0x10, 32 },
{ ARMV7M_R9, 0x14, 32 },
{ ARMV7M_R10, 0x18, 32 },
{ ARMV7M_R11, 0x1c, 32 },
{ ARMV7M_R12, 0x74, 32 },
{ ARMV7M_R13, -2, 32 },
{ ARMV7M_R14, 0x78, 32 },
{ ARMV7M_PC, 0x7c, 32 },
{ ARMV7M_XPSR, 0x80, 32 },
...};
static const struct stack_register_offset rtos_standard_cortex_r4_stack_offsets[] = {
{ 0, 0x08, 32 },
{ 1, 0x0c, 32 },
{ 2, 0x10, 32 },
{ 3, 0x14, 32 },
{ 4, 0x18, 32 },
{ 5, 0x1c, 32 },
{ 6, 0x20, 32 },
{ 7, 0x24, 32 },
{ 8, 0x28, 32 },
{ 10, 0x2c, 32 },
{ 11, 0x30, 32 },
{ 12, 0x34, 32 },
{ 13, 0x38, 32 },
{ 14, -2, 32 },
{ 15, 0x3c, 32 },
{ 16, 0x40, 32 },
{ 17, -1, 96 },
{ 18, -1, 96 },
{ 19, -1, 96 },
{ 20, -1, 96 },
{ 21, -1, 96 },
{ 22, -1, 96 },
{ 23, -1, 96 },
{ 24, -1, 96 },
{ 25, -1, 32 },
{ 26, 0x04, 32 },
...};
static target_addr_t rtos_generic_stack_align(struct target *target,
const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
target_addr_t stack_ptr, int align)
{
target_addr_t new_stack_ptr;
target_addr_t aligned_stack_ptr;
new_stack_ptr = stack_ptr - stacking->stack_growth_direction *
stacking->stack_registers_size;
aligned_stack_ptr = new_stack_ptr & ~((target_addr_t)align - 1);
if (aligned_stack_ptr != new_stack_ptr &&
stacking->stack_growth_direction == -1) {
/* ... */
aligned_stack_ptr += (target_addr_t)align;
}if (aligned_stack_ptr != new_stack_ptr && stacking->stack_growth_direction == -1) { ... }
return aligned_stack_ptr;
}{ ... }
target_addr_t rtos_generic_stack_align8(struct target *target,
const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
target_addr_t stack_ptr)
{
return rtos_generic_stack_align(target, stack_data,
stacking, stack_ptr, 8);
}{ ... }
/* ... */
target_addr_t rtos_cortex_m_stack_align(struct target *target,
const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
target_addr_t stack_ptr, size_t xpsr_offset)
{
const uint32_t ALIGN_NEEDED = (1 << 9);
uint32_t xpsr;
target_addr_t new_stack_ptr;
new_stack_ptr = stack_ptr - stacking->stack_growth_direction *
stacking->stack_registers_size;
xpsr = target_buffer_get_u32(target, &stack_data[xpsr_offset]);
if ((xpsr & ALIGN_NEEDED) != 0) {
LOG_DEBUG("XPSR(0x%08" PRIx32 ") indicated stack alignment was necessary\r\n",
xpsr);
new_stack_ptr -= (stacking->stack_growth_direction * 4);
}if ((xpsr & ALIGN_NEEDED) != 0) { ... }
return new_stack_ptr;
}{ ... }
static target_addr_t rtos_standard_cortex_m3_stack_align(struct target *target,
const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
target_addr_t stack_ptr)
{
const int XPSR_OFFSET = 0x3c;
return rtos_cortex_m_stack_align(target, stack_data, stacking,
stack_ptr, XPSR_OFFSET);
}{ ... }
static target_addr_t rtos_standard_cortex_m4f_stack_align(struct target *target,
const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
target_addr_t stack_ptr)
{
const int XPSR_OFFSET = 0x40;
return rtos_cortex_m_stack_align(target, stack_data, stacking,
stack_ptr, XPSR_OFFSET);
}{ ... }
static target_addr_t rtos_standard_cortex_m4f_fpu_stack_align(struct target *target,
const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
target_addr_t stack_ptr)
{
const int XPSR_OFFSET = 0x80;
return rtos_cortex_m_stack_align(target, stack_data, stacking,
stack_ptr, XPSR_OFFSET);
}{ ... }
const struct rtos_register_stacking rtos_standard_cortex_m3_stacking = {
.stack_registers_size = 0x40,
.stack_growth_direction = -1,
.num_output_registers = ARMV7M_NUM_CORE_REGS,
.calculate_process_stack = rtos_standard_cortex_m3_stack_align,
.register_offsets = rtos_standard_cortex_m3_stack_offsets
...};
const struct rtos_register_stacking rtos_standard_cortex_m4f_stacking = {
.stack_registers_size = 0x44,
.stack_growth_direction = -1,
.num_output_registers = ARMV7M_NUM_CORE_REGS,
.calculate_process_stack = rtos_standard_cortex_m4f_stack_align,
.register_offsets = rtos_standard_cortex_m4f_stack_offsets
...};
const struct rtos_register_stacking rtos_standard_cortex_m4f_fpu_stacking = {
.stack_registers_size = 0xcc,
.stack_growth_direction = -1,
.num_output_registers = ARMV7M_NUM_CORE_REGS,
.calculate_process_stack = rtos_standard_cortex_m4f_fpu_stack_align,
.register_offsets = rtos_standard_cortex_m4f_fpu_stack_offsets
...};
const struct rtos_register_stacking rtos_standard_cortex_r4_stacking = {
.stack_registers_size = 0x48,
.stack_growth_direction = -1,
.num_output_registers = 26,
.calculate_process_stack = rtos_generic_stack_align8,
.register_offsets = rtos_standard_cortex_r4_stack_offsets
...};