Skip to content

Commit 0e7def1

Browse files
dcpleungcarlescufi
authored andcommitted
xtensa: selectively init interrupt stack at boot
During arch_kernel_init(), the interrupt stack is being initialized. However, if the current in-use stack is the interrupt stack, it would wipe all the data up to that point in stack, and might result in crash. So skip initializing the interrupt stack if the current stack pointer is within the boundary of interrupt stack. Signed-off-by: Daniel Leung <[email protected]>
1 parent 6252fcf commit 0e7def1

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

arch/xtensa/include/kernel_arch_func.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,22 @@ static ALWAYS_INLINE void arch_kernel_init(void)
5353
XTENSA_WSR(ZSR_CPU_STR, cpu0);
5454

5555
#ifdef CONFIG_INIT_STACKS
56-
memset(Z_KERNEL_STACK_BUFFER(z_interrupt_stacks[0]), 0xAA,
57-
K_KERNEL_STACK_SIZEOF(z_interrupt_stacks[0]));
56+
char *stack_start = Z_KERNEL_STACK_BUFFER(z_interrupt_stacks[0]);
57+
size_t stack_sz = K_KERNEL_STACK_SIZEOF(z_interrupt_stacks[0]);
58+
char *stack_end = stack_start + stack_sz;
59+
60+
uint32_t sp;
61+
62+
__asm__ volatile("mov %0, sp" : "=a"(sp));
63+
64+
/* Only clear the interrupt stack if the current stack pointer
65+
* is not within the interrupt stack. Or else we would be
66+
* wiping the in-use stack.
67+
*/
68+
if (((uintptr_t)sp < (uintptr_t)stack_start) ||
69+
((uintptr_t)sp >= (uintptr_t)stack_end)) {
70+
memset(stack_start, 0xAA, stack_sz);
71+
}
5872
#endif
5973

6074
#ifdef CONFIG_XTENSA_MMU

0 commit comments

Comments
 (0)