|
9 | 9 | #include "rtos_nuttx_stackings.h" |
10 | 10 | #include "rtos_standard_stackings.h" |
11 | 11 | #include <target/riscv/riscv.h> |
| 12 | +#include <helper/bits.h> |
12 | 13 |
|
13 | | -/* see arch/arm/include/armv7-m/irq_cmnvector.h */ |
| 14 | +/* The cortex_m target uses nuttx_tcbinfo_stack_read which uses a symbol |
| 15 | + * provided by Nuttx to read the registers from memory and place them directly |
| 16 | + * in the order we need. This is because the register offsets change with |
| 17 | + * different versions of Nuttx, FPU vs non-FPU and ARMv7 vs ARMv8. |
| 18 | + * This allows a single function to work with many versions. |
| 19 | + */ |
14 | 20 | static const struct stack_register_offset nuttx_stack_offsets_cortex_m[] = { |
15 | | - { ARMV7M_R0, 0x28, 32 }, /* r0 */ |
16 | | - { ARMV7M_R1, 0x2c, 32 }, /* r1 */ |
17 | | - { ARMV7M_R2, 0x30, 32 }, /* r2 */ |
18 | | - { ARMV7M_R3, 0x34, 32 }, /* r3 */ |
19 | | - { ARMV7M_R4, 0x08, 32 }, /* r4 */ |
20 | | - { ARMV7M_R5, 0x0c, 32 }, /* r5 */ |
21 | | - { ARMV7M_R6, 0x10, 32 }, /* r6 */ |
22 | | - { ARMV7M_R7, 0x14, 32 }, /* r7 */ |
23 | | - { ARMV7M_R8, 0x18, 32 }, /* r8 */ |
24 | | - { ARMV7M_R9, 0x1c, 32 }, /* r9 */ |
25 | | - { ARMV7M_R10, 0x20, 32 }, /* r10 */ |
26 | | - { ARMV7M_R11, 0x24, 32 }, /* r11 */ |
27 | | - { ARMV7M_R12, 0x38, 32 }, /* r12 */ |
28 | | - { ARMV7M_R13, 0, 32 }, /* sp */ |
29 | | - { ARMV7M_R14, 0x3c, 32 }, /* lr */ |
30 | | - { ARMV7M_PC, 0x40, 32 }, /* pc */ |
31 | | - { ARMV7M_XPSR, 0x44, 32 }, /* xPSR */ |
| 21 | + { ARMV7M_R0, 0, 32 }, /* r0 */ |
| 22 | + { ARMV7M_R1, 4, 32 }, /* r1 */ |
| 23 | + { ARMV7M_R2, 8, 32 }, /* r2 */ |
| 24 | + { ARMV7M_R3, 12, 32 }, /* r3 */ |
| 25 | + { ARMV7M_R4, 16, 32 }, /* r4 */ |
| 26 | + { ARMV7M_R5, 20, 32 }, /* r5 */ |
| 27 | + { ARMV7M_R6, 24, 32 }, /* r6 */ |
| 28 | + { ARMV7M_R7, 28, 32 }, /* r7 */ |
| 29 | + { ARMV7M_R8, 32, 32 }, /* r8 */ |
| 30 | + { ARMV7M_R9, 36, 32 }, /* r9 */ |
| 31 | + { ARMV7M_R10, 40, 32 }, /* r10 */ |
| 32 | + { ARMV7M_R11, 44, 32 }, /* r11 */ |
| 33 | + { ARMV7M_R12, 48, 32 }, /* r12 */ |
| 34 | + { ARMV7M_R13, 52, 32 }, /* sp */ |
| 35 | + { ARMV7M_R14, 56, 32 }, /* lr */ |
| 36 | + { ARMV7M_PC, 60, 32 }, /* pc */ |
| 37 | + { ARMV7M_XPSR, 64, 32 }, /* xPSR */ |
32 | 38 | }; |
33 | 39 |
|
34 | | -const struct rtos_register_stacking nuttx_stacking_cortex_m = { |
35 | | - .stack_registers_size = 0x48, |
36 | | - .stack_growth_direction = -1, |
37 | | - .num_output_registers = 17, |
38 | | - .register_offsets = nuttx_stack_offsets_cortex_m, |
39 | | -}; |
| 40 | +/* The Nuttx stack frame for most architectures has some registers placed |
| 41 | + * by hardware and some by software. The hardware register order and number does not change |
| 42 | + * but the software registers may change with different versions of Nuttx. |
| 43 | + * For example with ARMv7, nuttx-12.3.0 added a new register which changed all |
| 44 | + * the offsets. We can either create separate offset tables for each version of Nuttx |
| 45 | + * which will break again in the future, or read the offsets from the TCB info. |
| 46 | + * Nuttx provides a symbol (g_reg_offs) which holds all the offsets for each stored register. |
| 47 | + * This offset table is stored in GDB org.gnu.gdb.xxx feature order. |
| 48 | + * The same order we need. |
| 49 | + * Please refer: |
| 50 | + * https://sourceware.org/gdb/current/onlinedocs/gdb/ARM-Features.html |
| 51 | + * https://sourceware.org/gdb/current/onlinedocs/gdb/RISC_002dV-Features.html |
| 52 | + */ |
| 53 | +static int nuttx_cortex_m_tcbinfo_stack_read(struct target *target, |
| 54 | + int64_t stack_ptr, const struct rtos_register_stacking *stacking, |
| 55 | + uint8_t *stack_data) |
| 56 | +{ |
| 57 | + struct rtos *rtos = target->rtos; |
| 58 | + target_addr_t xcpreg_off = rtos->symbols[NX_SYM_REG_OFFSETS].address; |
40 | 59 |
|
41 | | -static const struct stack_register_offset nuttx_stack_offsets_cortex_m_fpu[] = { |
42 | | - { ARMV7M_R0, 0x6c, 32 }, /* r0 */ |
43 | | - { ARMV7M_R1, 0x70, 32 }, /* r1 */ |
44 | | - { ARMV7M_R2, 0x74, 32 }, /* r2 */ |
45 | | - { ARMV7M_R3, 0x78, 32 }, /* r3 */ |
46 | | - { ARMV7M_R4, 0x08, 32 }, /* r4 */ |
47 | | - { ARMV7M_R5, 0x0c, 32 }, /* r5 */ |
48 | | - { ARMV7M_R6, 0x10, 32 }, /* r6 */ |
49 | | - { ARMV7M_R7, 0x14, 32 }, /* r7 */ |
50 | | - { ARMV7M_R8, 0x18, 32 }, /* r8 */ |
51 | | - { ARMV7M_R9, 0x1c, 32 }, /* r9 */ |
52 | | - { ARMV7M_R10, 0x20, 32 }, /* r10 */ |
53 | | - { ARMV7M_R11, 0x24, 32 }, /* r11 */ |
54 | | - { ARMV7M_R12, 0x7c, 32 }, /* r12 */ |
55 | | - { ARMV7M_R13, 0, 32 }, /* sp */ |
56 | | - { ARMV7M_R14, 0x80, 32 }, /* lr */ |
57 | | - { ARMV7M_PC, 0x84, 32 }, /* pc */ |
58 | | - { ARMV7M_XPSR, 0x88, 32 }, /* xPSR */ |
59 | | -}; |
| 60 | + for (int i = 0; i < stacking->num_output_registers; ++i) { |
| 61 | + uint16_t stack_reg_offset; |
| 62 | + int ret = target_read_u16(rtos->target, xcpreg_off + 2 * i, &stack_reg_offset); |
| 63 | + if (ret != ERROR_OK) { |
| 64 | + LOG_ERROR("Failed to read stack_reg_offset: ret = %d", ret); |
| 65 | + return ret; |
| 66 | + } |
| 67 | + if (stack_reg_offset != UINT16_MAX && stacking->register_offsets[i].offset >= 0) { |
| 68 | + ret = target_read_buffer(target, |
| 69 | + stack_ptr + stack_reg_offset, |
| 70 | + stacking->register_offsets[i].width_bits / 8, |
| 71 | + &stack_data[stacking->register_offsets[i].offset]); |
| 72 | + if (ret != ERROR_OK) { |
| 73 | + LOG_ERROR("Failed to read register: ret = %d", ret); |
| 74 | + return ret; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /* Offset match nuttx_stack_offsets_cortex_m */ |
| 80 | + const int XPSR_OFFSET = 64; |
| 81 | + const int SP_OFFSET = 52; |
| 82 | + /* Nuttx stack frames (produced in exception_common) store the SP of the ISR minus |
| 83 | + * the hardware stack frame size. This SP may include an additional 4 byte alignment |
| 84 | + * depending in xPSR[9]. The Nuttx stack frame stores post alignment since the |
| 85 | + * hardware will add/remove automatically on both enter/exit. |
| 86 | + * We need to adjust the SP to get the real SP of the stack. |
| 87 | + * See Arm Reference manual "Stack alignment on exception entry" |
| 88 | + */ |
| 89 | + uint32_t xpsr = target_buffer_get_u32(target, &stack_data[XPSR_OFFSET]); |
| 90 | + if (xpsr & BIT(9)) { |
| 91 | + uint32_t sp = target_buffer_get_u32(target, &stack_data[SP_OFFSET]); |
| 92 | + target_buffer_set_u32(target, &stack_data[SP_OFFSET], sp - 4 * stacking->stack_growth_direction); |
| 93 | + } |
60 | 94 |
|
61 | | -const struct rtos_register_stacking nuttx_stacking_cortex_m_fpu = { |
62 | | - .stack_registers_size = 0x8c, |
| 95 | + return ERROR_OK; |
| 96 | +} |
| 97 | + |
| 98 | +const struct rtos_register_stacking nuttx_stacking_cortex_m = { |
| 99 | + /* nuttx_tcbinfo_stack_read transforms the stack into just output registers */ |
| 100 | + .stack_registers_size = ARRAY_SIZE(nuttx_stack_offsets_cortex_m) * 4, |
63 | 101 | .stack_growth_direction = -1, |
64 | | - .num_output_registers = 17, |
65 | | - .register_offsets = nuttx_stack_offsets_cortex_m_fpu, |
| 102 | + .num_output_registers = ARRAY_SIZE(nuttx_stack_offsets_cortex_m), |
| 103 | + .read_stack = nuttx_cortex_m_tcbinfo_stack_read, |
| 104 | + .calculate_process_stack = NULL, /* Stack alignment done in nuttx_cortex_m_tcbinfo_stack_read */ |
| 105 | + .register_offsets = nuttx_stack_offsets_cortex_m, |
66 | 106 | }; |
67 | 107 |
|
68 | 108 | static const struct stack_register_offset nuttx_stack_offsets_riscv[] = { |
|
0 commit comments