From bb313355f3606e4ab765d314afae7fcfa7863897 Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Wed, 31 Jul 2024 12:44:45 -0700 Subject: [PATCH] riscv: initialize privileged stack during thread init This adds the bits to initialize the privileged stack when a thread is transitioning to user mode. This prevents information leaking if the stack is reused, and also aids in calculating stack space usage during system calls. Signed-off-by: Daniel Leung --- arch/riscv/core/thread.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/riscv/core/thread.c b/arch/riscv/core/thread.c index aeb33b31f3b..61a10f387c2 100644 --- a/arch/riscv/core/thread.c +++ b/arch/riscv/core/thread.c @@ -144,6 +144,12 @@ FUNC_NORETURN void arch_user_mode_enter(k_thread_entry_t user_entry, K_KERNEL_STACK_RESERVED + CONFIG_PRIVILEGED_STACK_SIZE); +#ifdef CONFIG_INIT_STACKS + /* Initialize the privileged stack */ + (void)memset((void *)_current->arch.priv_stack_start, 0xaa, + Z_STACK_PTR_ALIGN(K_KERNEL_STACK_RESERVED + CONFIG_PRIVILEGED_STACK_SIZE)); +#endif /* CONFIG_INIT_STACKS */ + top_of_user_stack = Z_STACK_PTR_ALIGN( _current->stack_info.start + _current->stack_info.size -