Skip to content

Commit def6ff1

Browse files
vhscamposkeith-packard
authored andcommitted
Fixes to make AArch32 A-profile work better in Thumb state
- Make Armv7-A memcpy work in Thumb state The implementation of Armv7-A's `memcpy` requires Arm state, therefore callers from Thumb state must perform an interworking call by the use of `BLX`. Before this patch, the alias declaration for `__aeabi_memcpy(4|8)?` used a macro (`ASM_ALIAS`) that, when targeting Thumb, used the directive `.thumb_set`. This directive, in this specific case, wrongly marked the alias as a Thumb entry point. This patch defines a new macro to define an alias unconditionally for the Arm state, and uses this new macro to define the aliases to `memcpy`. - AArch32 A-profile using Thumb state must stay at it on exception entry On exception entry, the architecture may switch ISA depending on the TE bit of the SCTLR register. Before this patch, the startup code was overprescriptive and set the TE bit only if the target is Thumb but not A-profile. This last condition isn't correct: A-profile architectures may run on Thumb state too. This change fixes the preprocessor conditional to test only `__thumb2__`. Moreover, the second read of SCTLR needs to be marked as volatile in inline assembly to prevent the compiler from optimizing it away. Without this mark, the compiler presumes that the second read is redundant because it isn't aware of the write that happens in between. - AArch32 A-profile interrupt vectors in Thumb2 mode should use Thumb2 instructions This code assumes that A-profile can't run in Thumb mode. As a consequence, code that does run in it faults if exceptions are taken. This patch changes the conditional to check only for Thumb mode.
1 parent 41318fc commit def6ff1

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

newlib/libc/machine/arm/arm_asm.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@
531531
#endif
532532
.endm
533533

534+
.macro ASM_ALIAS_ARM_STATE new old
535+
.global \new
536+
.type \new, %function
537+
.set \new, \old
538+
.endm
539+
534540
#endif /* __ASSEMBLER__ */
535541

536542
#endif /* ARM_ASM__H */

newlib/libc/machine/arm/memcpy-armv7a.S

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@
157157
.endm
158158

159159
def_fn memcpy p2align=6
160-
ASM_ALIAS __aeabi_memcpy, memcpy
161-
ASM_ALIAS __aeabi_memcpy4, memcpy
162-
ASM_ALIAS __aeabi_memcpy8, memcpy
160+
ASM_ALIAS_ARM_STATE __aeabi_memcpy, memcpy
161+
ASM_ALIAS_ARM_STATE __aeabi_memcpy4, memcpy
162+
ASM_ALIAS_ARM_STATE __aeabi_memcpy8, memcpy
163163

164164
mov dst, dstin /* Preserve dstin, we need to return it. */
165165
cmp count, #64

newlib/libc/picolib/machine/arm/interrupt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ __weak_vector_table(void)
137137
* Exception vector that lives at the
138138
* start of program text (usually 0x0)
139139
*/
140-
#if __thumb2__ && __ARM_ARCH_PROFILE != 'A'
140+
#ifdef __thumb2__
141141
/* Thumb 2 processors start in thumb mode */
142142
__asm__(".thumb\n"
143143
".syntax unified\n"

picocrt/machine/arm/crt0.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ _cstart(void)
243243
_set_stacks();
244244
#endif
245245

246-
#if __thumb2__ && __ARM_ARCH_PROFILE != 'A'
246+
#ifdef __thumb2__
247247
/* Make exceptions run in Thumb mode */
248248
uint32_t sctlr;
249249
__asm__("mrc p15, 0, %0, c1, c0, 0" : "=r" (sctlr));
@@ -308,7 +308,7 @@ _cstart(void)
308308

309309
/* Enable caches, branch prediction and the MMU. Disable TRE */
310310
uint32_t sctlr;
311-
__asm__("mrc p15, 0, %0, c1, c0, 0" : "=r" (sctlr));
311+
__asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0" : "=r" (sctlr));
312312
sctlr |= SCTLR_ICACHE | SCTLR_BRANCH_PRED | SCTLR_DATA_L2 | SCTLR_MMU;
313313
#ifndef __ARM_FEATURE_UNALIGNED
314314
sctlr |= SCTLR_A;

0 commit comments

Comments
 (0)