Skip to content

Commit 465e04c

Browse files
Andy Grossandrewboie
authored andcommitted
arch: arm: Fixup stack end calculations
This patch fixes calculations for the top of the interrupt and main stacks. Due to power of two alignment requirements for certain MPUs, the guard size must be taken into account due to the guard being counted against the initial stack size. Signed-off-by: Andy Gross <andy.gross@linaro.org>
1 parent f35c031 commit 465e04c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

arch/arm/include/cortex_m/stack.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ extern K_THREAD_STACK_DEFINE(_interrupt_stack, CONFIG_ISR_STACK_SIZE);
4040
*/
4141
static ALWAYS_INLINE void _InterruptStackSetup(void)
4242
{
43+
#ifdef CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT
44+
u32_t msp = (u32_t)(K_THREAD_STACK_BUFFER(_interrupt_stack) +
45+
CONFIG_ISR_STACK_SIZE - MPU_GUARD_ALIGN_AND_SIZE);
46+
#else
4347
u32_t msp = (u32_t)(K_THREAD_STACK_BUFFER(_interrupt_stack) +
4448
CONFIG_ISR_STACK_SIZE);
49+
#endif
4550

4651
_MspSet(msp);
4752
}

arch/arm/include/kernel_arch_func.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ _arch_switch_to_main_thread(struct k_thread *main_thread,
4545
/* get high address of the stack, i.e. its start (stack grows down) */
4646
char *start_of_main_stack;
4747

48+
#ifdef CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT
49+
start_of_main_stack =
50+
K_THREAD_STACK_BUFFER(main_stack) + main_stack_size -
51+
MPU_GUARD_ALIGN_AND_SIZE;
52+
#else
4853
start_of_main_stack =
4954
K_THREAD_STACK_BUFFER(main_stack) + main_stack_size;
55+
#endif
5056
start_of_main_stack = (void *)STACK_ROUND_DOWN(start_of_main_stack);
5157

5258
_current = main_thread;

0 commit comments

Comments
 (0)