OpenOCD
rtos_standard_stackings.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2011 by Broadcom Corporation *
5  * Evan Hunter - ehunter@broadcom.com *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "rtos.h"
13 #include "target/armv7m.h"
15 
17  { ARMV7M_R0, 0x20, 32 }, /* r0 */
18  { ARMV7M_R1, 0x24, 32 }, /* r1 */
19  { ARMV7M_R2, 0x28, 32 }, /* r2 */
20  { ARMV7M_R3, 0x2c, 32 }, /* r3 */
21  { ARMV7M_R4, 0x00, 32 }, /* r4 */
22  { ARMV7M_R5, 0x04, 32 }, /* r5 */
23  { ARMV7M_R6, 0x08, 32 }, /* r6 */
24  { ARMV7M_R7, 0x0c, 32 }, /* r7 */
25  { ARMV7M_R8, 0x10, 32 }, /* r8 */
26  { ARMV7M_R9, 0x14, 32 }, /* r9 */
27  { ARMV7M_R10, 0x18, 32 }, /* r10 */
28  { ARMV7M_R11, 0x1c, 32 }, /* r11 */
29  { ARMV7M_R12, 0x30, 32 }, /* r12 */
30  { ARMV7M_R13, -2, 32 }, /* sp */
31  { ARMV7M_R14, 0x34, 32 }, /* lr */
32  { ARMV7M_PC, 0x38, 32 }, /* pc */
33  { ARMV7M_XPSR, 0x3c, 32 }, /* xPSR */
34 };
35 
37  { ARMV7M_R0, 0x24, 32 }, /* r0 */
38  { ARMV7M_R1, 0x28, 32 }, /* r1 */
39  { ARMV7M_R2, 0x2c, 32 }, /* r2 */
40  { ARMV7M_R3, 0x30, 32 }, /* r3 */
41  { ARMV7M_R4, 0x00, 32 }, /* r4 */
42  { ARMV7M_R5, 0x04, 32 }, /* r5 */
43  { ARMV7M_R6, 0x08, 32 }, /* r6 */
44  { ARMV7M_R7, 0x0c, 32 }, /* r7 */
45  { ARMV7M_R8, 0x10, 32 }, /* r8 */
46  { ARMV7M_R9, 0x14, 32 }, /* r9 */
47  { ARMV7M_R10, 0x18, 32 }, /* r10 */
48  { ARMV7M_R11, 0x1c, 32 }, /* r11 */
49  { ARMV7M_R12, 0x34, 32 }, /* r12 */
50  { ARMV7M_R13, -2, 32 }, /* sp */
51  { ARMV7M_R14, 0x38, 32 }, /* lr */
52  { ARMV7M_PC, 0x3c, 32 }, /* pc */
53  { ARMV7M_XPSR, 0x40, 32 }, /* xPSR */
54 };
55 
57  { ARMV7M_R0, 0x64, 32 }, /* r0 */
58  { ARMV7M_R1, 0x68, 32 }, /* r1 */
59  { ARMV7M_R2, 0x6c, 32 }, /* r2 */
60  { ARMV7M_R3, 0x70, 32 }, /* r3 */
61  { ARMV7M_R4, 0x00, 32 }, /* r4 */
62  { ARMV7M_R5, 0x04, 32 }, /* r5 */
63  { ARMV7M_R6, 0x08, 32 }, /* r6 */
64  { ARMV7M_R7, 0x0c, 32 }, /* r7 */
65  { ARMV7M_R8, 0x10, 32 }, /* r8 */
66  { ARMV7M_R9, 0x14, 32 }, /* r9 */
67  { ARMV7M_R10, 0x18, 32 }, /* r10 */
68  { ARMV7M_R11, 0x1c, 32 }, /* r11 */
69  { ARMV7M_R12, 0x74, 32 }, /* r12 */
70  { ARMV7M_R13, -2, 32 }, /* sp */
71  { ARMV7M_R14, 0x78, 32 }, /* lr */
72  { ARMV7M_PC, 0x7c, 32 }, /* pc */
73  { ARMV7M_XPSR, 0x80, 32 }, /* xPSR */
74 };
75 
76 
78  { 0, 0x08, 32 }, /* r0 (a1) */
79  { 1, 0x0c, 32 }, /* r1 (a2) */
80  { 2, 0x10, 32 }, /* r2 (a3) */
81  { 3, 0x14, 32 }, /* r3 (a4) */
82  { 4, 0x18, 32 }, /* r4 (v1) */
83  { 5, 0x1c, 32 }, /* r5 (v2) */
84  { 6, 0x20, 32 }, /* r6 (v3) */
85  { 7, 0x24, 32 }, /* r7 (v4) */
86  { 8, 0x28, 32 }, /* r8 (a1) */
87  { 10, 0x2c, 32 }, /* r9 (sb) */
88  { 11, 0x30, 32 }, /* r10 (sl) */
89  { 12, 0x34, 32 }, /* r11 (fp) */
90  { 13, 0x38, 32 }, /* r12 (ip) */
91  { 14, -2, 32 }, /* sp */
92  { 15, 0x3c, 32 }, /* lr */
93  { 16, 0x40, 32 }, /* pc */
94  { 17, -1, 96 }, /* FPA1 */
95  { 18, -1, 96 }, /* FPA2 */
96  { 19, -1, 96 }, /* FPA3 */
97  { 20, -1, 96 }, /* FPA4 */
98  { 21, -1, 96 }, /* FPA5 */
99  { 22, -1, 96 }, /* FPA6 */
100  { 23, -1, 96 }, /* FPA7 */
101  { 24, -1, 96 }, /* FPA8 */
102  { 25, -1, 32 }, /* FPS */
103  { 26, 0x04, 32 }, /* CSPR */
104 };
105 
107  const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
108  target_addr_t stack_ptr, int align)
109 {
110  target_addr_t new_stack_ptr;
111  target_addr_t aligned_stack_ptr;
112  new_stack_ptr = stack_ptr - stacking->stack_growth_direction *
113  stacking->stack_registers_size;
114  aligned_stack_ptr = new_stack_ptr & ~((target_addr_t)align - 1);
115  if (aligned_stack_ptr != new_stack_ptr &&
116  stacking->stack_growth_direction == -1) {
117  /* If we have a downward growing stack, the simple alignment code
118  * above results in a wrong result (since it rounds down to nearest
119  * alignment). We want to round up so add an extra align.
120  */
121  aligned_stack_ptr += (target_addr_t)align;
122  }
123  return aligned_stack_ptr;
124 }
125 
127  const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
128  target_addr_t stack_ptr)
129 {
130  return rtos_generic_stack_align(target, stack_data,
131  stacking, stack_ptr, 8);
132 }
133 
134 /* The Cortex-M3 will indicate that an alignment adjustment
135  * has been done on the stack by setting bit 9 of the stacked xPSR
136  * register. In this case, we can just add an extra 4 bytes to get
137  * to the program stack. Note that some places in the ARM documentation
138  * make this a little unclear but the padding takes place before the
139  * normal exception stacking - so xPSR is always available at a fixed
140  * location.
141  *
142  * Relevant documentation:
143  * Cortex-M series processors -> Cortex-M3 -> Revision: xxx ->
144  * Cortex-M3 Devices Generic User Guide -> The Cortex-M3 Processor ->
145  * Exception Model -> Exception entry and return -> Exception entry
146  * Cortex-M series processors -> Cortex-M3 -> Revision: xxx ->
147  * Cortex-M3 Devices Generic User Guide -> Cortex-M3 Peripherals ->
148  * System control block -> Configuration and Control Register (STKALIGN)
149  *
150  * This is just a helper function for use in the calculate_process_stack
151  * function for a given architecture/rtos.
152  */
154  const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
155  target_addr_t stack_ptr, size_t xpsr_offset)
156 {
157  const uint32_t ALIGN_NEEDED = (1 << 9);
158  uint32_t xpsr;
159  target_addr_t new_stack_ptr;
160 
161  new_stack_ptr = stack_ptr - stacking->stack_growth_direction *
162  stacking->stack_registers_size;
163  xpsr = target_buffer_get_u32(target, &stack_data[xpsr_offset]);
164  if ((xpsr & ALIGN_NEEDED) != 0) {
165  LOG_DEBUG("XPSR(0x%08" PRIx32 ") indicated stack alignment was necessary\r\n",
166  xpsr);
167  new_stack_ptr -= (stacking->stack_growth_direction * 4);
168  }
169  return new_stack_ptr;
170 }
171 
173  const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
174  target_addr_t stack_ptr)
175 {
176  const int XPSR_OFFSET = 0x3c;
177  return rtos_cortex_m_stack_align(target, stack_data, stacking,
178  stack_ptr, XPSR_OFFSET);
179 }
180 
182  const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
183  target_addr_t stack_ptr)
184 {
185  const int XPSR_OFFSET = 0x40;
186  return rtos_cortex_m_stack_align(target, stack_data, stacking,
187  stack_ptr, XPSR_OFFSET);
188 }
189 
191  const uint8_t *stack_data, const struct rtos_register_stacking *stacking,
192  target_addr_t stack_ptr)
193 {
194  const int XPSR_OFFSET = 0x80;
195  return rtos_cortex_m_stack_align(target, stack_data, stacking,
196  stack_ptr, XPSR_OFFSET);
197 }
198 
199 
201  .stack_registers_size = 0x40,
202  .stack_growth_direction = -1,
203  .num_output_registers = ARMV7M_NUM_CORE_REGS,
204  .calculate_process_stack = rtos_standard_cortex_m3_stack_align,
205  .register_offsets = rtos_standard_cortex_m3_stack_offsets
206 };
207 
209  .stack_registers_size = 0x44,
210  .stack_growth_direction = -1,
211  .num_output_registers = ARMV7M_NUM_CORE_REGS,
212  .calculate_process_stack = rtos_standard_cortex_m4f_stack_align,
213  .register_offsets = rtos_standard_cortex_m4f_stack_offsets
214 };
215 
217  .stack_registers_size = 0xcc,
218  .stack_growth_direction = -1,
219  .num_output_registers = ARMV7M_NUM_CORE_REGS,
220  .calculate_process_stack = rtos_standard_cortex_m4f_fpu_stack_align,
222 };
223 
225  .stack_registers_size = 0x48,
226  .stack_growth_direction = -1,
227  .num_output_registers = 26,
228  .calculate_process_stack = rtos_generic_stack_align8,
229  .register_offsets = rtos_standard_cortex_r4_stack_offsets
230 };
@ ARMV7M_R1
Definition: armv7m.h:108
@ ARMV7M_R6
Definition: armv7m.h:114
@ ARMV7M_R2
Definition: armv7m.h:109
@ ARMV7M_R3
Definition: armv7m.h:110
@ ARMV7M_R14
Definition: armv7m.h:124
@ ARMV7M_R9
Definition: armv7m.h:118
@ ARMV7M_R12
Definition: armv7m.h:122
@ ARMV7M_R0
Definition: armv7m.h:107
@ ARMV7M_R13
Definition: armv7m.h:123
@ ARMV7M_PC
Definition: armv7m.h:125
@ ARMV7M_R7
Definition: armv7m.h:115
@ ARMV7M_R4
Definition: armv7m.h:112
@ ARMV7M_XPSR
Definition: armv7m.h:127
@ ARMV7M_R8
Definition: armv7m.h:117
@ ARMV7M_R11
Definition: armv7m.h:120
@ ARMV7M_R10
Definition: armv7m.h:119
@ ARMV7M_R5
Definition: armv7m.h:113
#define ARMV7M_NUM_CORE_REGS
Definition: armv7m.h:218
#define LOG_DEBUG(expr ...)
Definition: log.h:109
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)
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)
static const struct stack_register_offset rtos_standard_cortex_m4f_stack_offsets[]
static const struct stack_register_offset rtos_standard_cortex_m3_stack_offsets[ARMV7M_NUM_CORE_REGS]
const struct rtos_register_stacking rtos_standard_cortex_m3_stacking
static const struct stack_register_offset rtos_standard_cortex_m4f_fpu_stack_offsets[]
const struct rtos_register_stacking rtos_standard_cortex_m4f_fpu_stacking
const struct rtos_register_stacking rtos_standard_cortex_m4f_stacking
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 struct rtos_register_stacking rtos_standard_cortex_r4_stacking
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)
static const struct stack_register_offset rtos_standard_cortex_r4_stack_offsets[]
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)
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)
unsigned char stack_registers_size
Definition: rtos.h:92
signed char stack_growth_direction
Definition: rtos.h:93
Definition: target.h:116
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:316
uint64_t target_addr_t
Definition: types.h:335