|
| 1 | +// Copyright (c) 2026, Arm Limited and contributors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// ARM-mode replacements for NXP's RTD Thumb-encoded CP15 / VFP accessors, |
| 5 | +// hooked in via `-Wl,--wrap=read_CPACR,...` so callers reach these |
| 6 | +// instead of NXP's. NXP ships read_CPACR / write_CPACR / write_FPEXC |
| 7 | +// inside core_utility.s with `.thumb2`. Core_FPU_Init in core.c is |
| 8 | +// ARM-mode and reaches them via `blx`; the `blx` correctly switches to |
| 9 | +// Thumb on entry, but the Thumb `mov pc, lr` epilogue does *not* |
| 10 | +// interwork back to ARM on this Cortex-R52 — the T bit stays set, the |
| 11 | +// caller's ARM-encoded body is decoded as Thumb-2, and the CPU branches |
| 12 | +// off into the .intc_vector data area. Replacing the helpers with |
| 13 | +// ARM-mode versions that finish with `bx lr` keeps the LSB-clear LR set |
| 14 | +// by the caller's `blx`, so the return is interworked correctly and |
| 15 | +// control comes back to the caller in ARM state. |
| 16 | + |
| 17 | + .syntax unified |
| 18 | + .arch armv8-r |
| 19 | + .arm |
| 20 | + |
| 21 | + .section .sram_code, "ax" |
| 22 | + |
| 23 | + .global __wrap_read_CPACR |
| 24 | + .type __wrap_read_CPACR, %function |
| 25 | +__wrap_read_CPACR: |
| 26 | + mrc p15, 0, r0, c1, c0, 2 |
| 27 | + bx lr |
| 28 | + .size __wrap_read_CPACR, . - __wrap_read_CPACR |
| 29 | + |
| 30 | + .global __wrap_write_CPACR |
| 31 | + .type __wrap_write_CPACR, %function |
| 32 | +__wrap_write_CPACR: |
| 33 | + mcr p15, 0, r0, c1, c0, 2 |
| 34 | + isb sy |
| 35 | + bx lr |
| 36 | + .size __wrap_write_CPACR, . - __wrap_write_CPACR |
| 37 | + |
| 38 | + .global __wrap_write_FPEXC |
| 39 | + .type __wrap_write_FPEXC, %function |
| 40 | +__wrap_write_FPEXC: |
| 41 | + vmsr fpexc, r0 |
| 42 | + isb sy |
| 43 | + bx lr |
| 44 | + .size __wrap_write_FPEXC, . - __wrap_write_FPEXC |
| 45 | + |
| 46 | + .global __wrap_read_FPEXC |
| 47 | + .type __wrap_read_FPEXC, %function |
| 48 | +__wrap_read_FPEXC: |
| 49 | + vmrs r0, fpexc |
| 50 | + bx lr |
| 51 | + .size __wrap_read_FPEXC, . - __wrap_read_FPEXC |
| 52 | + |
| 53 | + // write_64bit(lo=r0, hi=r1, dest=r2) — 64-bit atomic store for ECC SRAM. |
| 54 | + // NXP's Thumb-encoded original has the same `mov pc, lr` non-interwork |
| 55 | + // problem as the CPACR helpers; init_data_bss → zero_copy64 → write_64bit |
| 56 | + // is the chain that derailed before this wrap was added. |
| 57 | + .global __wrap_write_64bit |
| 58 | + .type __wrap_write_64bit, %function |
| 59 | +__wrap_write_64bit: |
| 60 | + strd r0, r1, [r2] |
| 61 | + bx lr |
| 62 | + .size __wrap_write_64bit, . - __wrap_write_64bit |
| 63 | + |
| 64 | + // ===== CP15 / MPU / GIC helpers ===== |
| 65 | + // The same `mov pc, lr` interworking failure also affects every other |
| 66 | + // CP15 accessor NXP ships in core_utility.s. SystemInit → Core_IC_Init |
| 67 | + // and the GIC500 init path call these via `blx` from ARM C code, and |
| 68 | + // any non-interworked return derails into Thumb decode of ARM bytes. |
| 69 | + // Wrap every helper that is actually called via BLX in the current |
| 70 | + // build (15 total, identified by `objdump -d | grep 'blx 321012...'`). |
| 71 | + // |
| 72 | + // Encodings are byte-identical to NXP's originals at 0x32101276.. |
| 73 | + // 0x32101324, just re-emitted as ARM-mode with a `bx lr` epilogue. |
| 74 | + |
| 75 | + .global __wrap_read_SCTLR |
| 76 | + .type __wrap_read_SCTLR, %function |
| 77 | +__wrap_read_SCTLR: |
| 78 | + mrc p15, 0, r0, c1, c0, 0 |
| 79 | + bx lr |
| 80 | + .size __wrap_read_SCTLR, . - __wrap_read_SCTLR |
| 81 | + |
| 82 | + .global __wrap_write_SCTLR |
| 83 | + .type __wrap_write_SCTLR, %function |
| 84 | +__wrap_write_SCTLR: |
| 85 | + mcr p15, 0, r0, c1, c0, 0 |
| 86 | + isb sy |
| 87 | + bx lr |
| 88 | + .size __wrap_write_SCTLR, . - __wrap_write_SCTLR |
| 89 | + |
| 90 | + .global __wrap_write_PRSELR |
| 91 | + .type __wrap_write_PRSELR, %function |
| 92 | +__wrap_write_PRSELR: |
| 93 | + mcr p15, 0, r0, c6, c2, 1 |
| 94 | + isb sy |
| 95 | + bx lr |
| 96 | + .size __wrap_write_PRSELR, . - __wrap_write_PRSELR |
| 97 | + |
| 98 | + .global __wrap_write_PRBAR |
| 99 | + .type __wrap_write_PRBAR, %function |
| 100 | +__wrap_write_PRBAR: |
| 101 | + mcr p15, 0, r0, c6, c3, 0 |
| 102 | + bx lr |
| 103 | + .size __wrap_write_PRBAR, . - __wrap_write_PRBAR |
| 104 | + |
| 105 | + .global __wrap_write_PRLAR |
| 106 | + .type __wrap_write_PRLAR, %function |
| 107 | +__wrap_write_PRLAR: |
| 108 | + mcr p15, 0, r0, c6, c3, 1 |
| 109 | + bx lr |
| 110 | + .size __wrap_write_PRLAR, . - __wrap_write_PRLAR |
| 111 | + |
| 112 | + .global __wrap_write_MAIR0 |
| 113 | + .type __wrap_write_MAIR0, %function |
| 114 | +__wrap_write_MAIR0: |
| 115 | + mcr p15, 0, r0, c10, c2, 0 |
| 116 | + bx lr |
| 117 | + .size __wrap_write_MAIR0, . - __wrap_write_MAIR0 |
| 118 | + |
| 119 | + .global __wrap_write_MAIR1 |
| 120 | + .type __wrap_write_MAIR1, %function |
| 121 | +__wrap_write_MAIR1: |
| 122 | + mcr p15, 0, r0, c10, c2, 1 |
| 123 | + bx lr |
| 124 | + .size __wrap_write_MAIR1, . - __wrap_write_MAIR1 |
| 125 | + |
| 126 | + .global __wrap_read_MPIDR |
| 127 | + .type __wrap_read_MPIDR, %function |
| 128 | +__wrap_read_MPIDR: |
| 129 | + mrc p15, 0, r0, c0, c0, 5 |
| 130 | + bx lr |
| 131 | + .size __wrap_read_MPIDR, . - __wrap_read_MPIDR |
| 132 | + |
| 133 | + .global __wrap_write_ICC_SRE |
| 134 | + .type __wrap_write_ICC_SRE, %function |
| 135 | +__wrap_write_ICC_SRE: |
| 136 | + mcr p15, 0, r0, c12, c12, 5 |
| 137 | + isb sy |
| 138 | + bx lr |
| 139 | + .size __wrap_write_ICC_SRE, . - __wrap_write_ICC_SRE |
| 140 | + |
| 141 | + // gic500_enableCpuInterface reaches write_ICC_PMR via *plain* ARM `bl`, |
| 142 | + // not `blx` — the compiler/linker did not insert an interworking veneer. |
| 143 | + // Without a wrap the ARM caller jumps to a Thumb-encoded body and decodes |
| 144 | + // its first halfword as ARM (UndefInstr). All other ICC_* helpers in the |
| 145 | + // same call sequence happen to be reached via `blx` and are caught by |
| 146 | + // the BLX-only audit; PMR is the lone exception, so it needs a wrap too. |
| 147 | + .global __wrap_write_ICC_PMR |
| 148 | + .type __wrap_write_ICC_PMR, %function |
| 149 | +__wrap_write_ICC_PMR: |
| 150 | + mcr p15, 0, r0, c4, c6, 0 |
| 151 | + isb sy |
| 152 | + bx lr |
| 153 | + .size __wrap_write_ICC_PMR, . - __wrap_write_ICC_PMR |
| 154 | + |
| 155 | + .global __wrap_write_ICC_BPR0 |
| 156 | + .type __wrap_write_ICC_BPR0, %function |
| 157 | +__wrap_write_ICC_BPR0: |
| 158 | + mcr p15, 0, r0, c12, c8, 3 |
| 159 | + bx lr |
| 160 | + .size __wrap_write_ICC_BPR0, . - __wrap_write_ICC_BPR0 |
| 161 | + |
| 162 | + .global __wrap_write_ICC_BPR1 |
| 163 | + .type __wrap_write_ICC_BPR1, %function |
| 164 | +__wrap_write_ICC_BPR1: |
| 165 | + mcr p15, 0, r0, c12, c12, 3 |
| 166 | + bx lr |
| 167 | + .size __wrap_write_ICC_BPR1, . - __wrap_write_ICC_BPR1 |
| 168 | + |
| 169 | + .global __wrap_write_ICC_IGRPEN0 |
| 170 | + .type __wrap_write_ICC_IGRPEN0, %function |
| 171 | +__wrap_write_ICC_IGRPEN0: |
| 172 | + mcr p15, 0, r0, c12, c12, 6 |
| 173 | + isb sy |
| 174 | + bx lr |
| 175 | + .size __wrap_write_ICC_IGRPEN0, . - __wrap_write_ICC_IGRPEN0 |
| 176 | + |
| 177 | + .global __wrap_write_ICC_IGRPEN1 |
| 178 | + .type __wrap_write_ICC_IGRPEN1, %function |
| 179 | +__wrap_write_ICC_IGRPEN1: |
| 180 | + mcr p15, 0, r0, c12, c12, 7 |
| 181 | + isb sy |
| 182 | + bx lr |
| 183 | + .size __wrap_write_ICC_IGRPEN1, . - __wrap_write_ICC_IGRPEN1 |
| 184 | + |
| 185 | + .global __wrap_read_ICC_CTLR |
| 186 | + .type __wrap_read_ICC_CTLR, %function |
| 187 | +__wrap_read_ICC_CTLR: |
| 188 | + mrc p15, 0, r0, c12, c12, 4 |
| 189 | + bx lr |
| 190 | + .size __wrap_read_ICC_CTLR, . - __wrap_read_ICC_CTLR |
| 191 | + |
| 192 | + .global __wrap_write_ICC_CTLR |
| 193 | + .type __wrap_write_ICC_CTLR, %function |
| 194 | +__wrap_write_ICC_CTLR: |
| 195 | + mcr p15, 0, r0, c12, c12, 4 |
| 196 | + isb sy |
| 197 | + bx lr |
| 198 | + .size __wrap_write_ICC_CTLR, . - __wrap_write_ICC_CTLR |
| 199 | + |
| 200 | + // ===== Undefined-Instruction vector -> FreeRTOS lazy-FPU handler ===== |
| 201 | + // NXP's Vector_Table.s routes the undef vector to a .weak UndefInstr_Handler |
| 202 | + // (exceptions.c) that just spins. The ARM_CR52_GIC port instead ships its |
| 203 | + // own vPortUndefinedInstruction (port.c, built when configENABLE_FPU==1) |
| 204 | + // that lazily re-enables the FPU on a task's first VFP instruction and |
| 205 | + // manages per-task FP context -- but it is never wired up. The port clears |
| 206 | + // FPEXC.EN on every task switch and *relies* on this handler to re-enable |
| 207 | + // it, so without the wiring the first VFP op in any task (e.g. _vfprintf_r's |
| 208 | + // `vpush {d8-d9}`) traps into the spinning default and hangs. |
| 209 | + // |
| 210 | + // Provide a strong UndefInstr_Handler (overriding the weak one) that jumps |
| 211 | + // to vPortUndefinedInstruction. The vector reaches this exactly as it |
| 212 | + // reached the spinning default (ARM-mode, UND mode, LR_und set); the |
| 213 | + // `ldr pc, =` interworks to the port handler (Thumb) without disturbing LR, |
| 214 | + // which vPortUndefinedInstruction adjusts itself. FP-using tasks must be |
| 215 | + // created with xTaskCreateFpu so the handler finds their FP context in TLS. |
| 216 | + .global UndefInstr_Handler |
| 217 | + .type UndefInstr_Handler, %function |
| 218 | +UndefInstr_Handler: |
| 219 | + ldr pc, 1f |
| 220 | +1: .word vPortUndefinedInstruction |
| 221 | + .size UndefInstr_Handler, . - UndefInstr_Handler |
| 222 | + |
| 223 | + // ===== IRQ vector -> FreeRTOS interrupt dispatcher ===== |
| 224 | + // NXP's Vector_Table.s routes the IRQ vector to a weak IRQ_Handler |
| 225 | + // (exceptions_stack.s) that saves the interrupted context onto the SYS |
| 226 | + // (task) stack, calls the registered ISR, and returns. It cannot perform a |
| 227 | + // FreeRTOS context switch: xPortSysTickHandler calls vTaskSwitchContext() |
| 228 | + // (updating pxCurrentTCB) but the generic handler never swaps stacks, and |
| 229 | + // the periodic-timer IRQ data-aborts in its `ldmia sp!, {r0-r12}` epilogue. |
| 230 | + // |
| 231 | + // The port ships vPortInterruptDispatcher (port.c) -- the real FreeRTOS IRQ |
| 232 | + // entry that reads ICC_IAR1, dispatches low-priority ISRs, and routes OS |
| 233 | + // interrupts (>= configMAX_SYSCALL_INTERRUPT_PRIORITY) through its osInt |
| 234 | + // path which does the full save / vTaskSwitchContext / restore. Override |
| 235 | + // the weak IRQ_Handler to jump to it. It uses the IRQ/SVC mode stacks that |
| 236 | + // startup.s set up, and looks up ISRs via __VECTOR_RAM (defsym'd to the |
| 237 | + // core-0 RAM vector table that NXP's startup populates). |
| 238 | + .global IRQ_Handler |
| 239 | + .type IRQ_Handler, %function |
| 240 | +IRQ_Handler: |
| 241 | + ldr pc, 2f |
| 242 | +2: .word vPortInterruptDispatcher |
| 243 | + .size IRQ_Handler, . - IRQ_Handler |
0 commit comments