From cc2382220f79a4f0e5092bc9fc758c49714bce09 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 28 Sep 2020 10:51:45 +0200 Subject: [PATCH 1/2] cpu/arm7_common: Align thread stacks to 32 bit This didn't change binaries for me. Either the linker script already took care of it through the section names of the stacks, or I just was lucky. If I was just luck, this fixes a bug. If not, it makes the hidden alignment explicit in the C code, so that code review is easier. --- cpu/arm7_common/arm_cpu.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cpu/arm7_common/arm_cpu.c b/cpu/arm7_common/arm_cpu.c index 2214540293..c1b78a2b2e 100644 --- a/cpu/arm7_common/arm_cpu.c +++ b/cpu/arm7_common/arm_cpu.c @@ -25,12 +25,12 @@ #define STACK_MARKER (0x77777777) #define REGISTER_CNT (12) -__attribute__((used, section(".usr_stack"))) uint8_t usr_stack[USR_STACKSIZE]; -__attribute__((used, section(".und_stack"))) uint8_t und_stack[UND_STACKSIZE]; -__attribute__((used, section(".fiq_stack"))) uint8_t fiq_stack[FIQ_STACKSIZE]; -__attribute__((used, section(".irq_stack"))) uint8_t irq_stack[ISR_STACKSIZE]; -__attribute__((used, section(".abt_stack"))) uint8_t abt_stack[ABT_STACKSIZE]; -__attribute__((used, section(".svc_stack"))) uint8_t svc_stack[ISR_STACKSIZE]; +__attribute__((used, section(".usr_stack"), aligned(4))) uint8_t usr_stack[USR_STACKSIZE]; +__attribute__((used, section(".und_stack"), aligned(4))) uint8_t und_stack[UND_STACKSIZE]; +__attribute__((used, section(".fiq_stack"), aligned(4))) uint8_t fiq_stack[FIQ_STACKSIZE]; +__attribute__((used, section(".irq_stack"), aligned(4))) uint8_t irq_stack[ISR_STACKSIZE]; +__attribute__((used, section(".abt_stack"), aligned(4))) uint8_t abt_stack[ABT_STACKSIZE]; +__attribute__((used, section(".svc_stack"), aligned(4))) uint8_t svc_stack[ISR_STACKSIZE]; void thread_yield_higher(void) { From 833afc03e10a60c72450342fd48c8c4a2337c1f5 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 28 Sep 2020 10:58:49 +0200 Subject: [PATCH 2/2] cpu/arm7_common: Silence -Wcast-align - Enforced that ISR_STACKSIZE is indeed a multiple of 4 - With this enforced, every cast that triggers a -Wcast-align warning is now a false positives, so those were silenced by (intermediately) casting to `uintptr_t`. --- cpu/arm7_common/arm_cpu.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cpu/arm7_common/arm_cpu.c b/cpu/arm7_common/arm_cpu.c index c1b78a2b2e..855318d5b6 100644 --- a/cpu/arm7_common/arm_cpu.c +++ b/cpu/arm7_common/arm_cpu.c @@ -32,6 +32,10 @@ __attribute__((used, section(".irq_stack"), aligned(4))) uint8_t irq_stack[ISR_S __attribute__((used, section(".abt_stack"), aligned(4))) uint8_t abt_stack[ABT_STACKSIZE]; __attribute__((used, section(".svc_stack"), aligned(4))) uint8_t svc_stack[ISR_STACKSIZE]; +#if (ISR_STACKSIZE % 4) +#error "ISR_STACKSIZE must be a multiple of 4" +#endif + void thread_yield_higher(void) { if (irq_is_in()) { @@ -129,13 +133,14 @@ void *thread_isr_stack_pointer(void) /* This function returns the number of bytes used on the ISR stack */ int thread_isr_stack_usage(void) { - uint32_t *ptr = (uint32_t*) &irq_stack[0]; + uint32_t *ptr = (uint32_t *)(uintptr_t)&irq_stack[0]; + uint32_t *end = (uint32_t *)(uintptr_t)&irq_stack[ISR_STACKSIZE]; - while(((*ptr) == STACK_CANARY_WORD) && (ptr < (uint32_t*) &irq_stack[ISR_STACKSIZE])) { + while(((*ptr) == STACK_CANARY_WORD) && (ptr < end)) { ++ptr; } - ptrdiff_t num_used_words = (uint32_t*) &irq_stack[ISR_STACKSIZE] - ptr; + ptrdiff_t num_used_words = (uintptr_t)end - (uintptr_t)ptr; return num_used_words; }