From a5ecd71163fc9ee37b9a87981ef81a4b3beea19b Mon Sep 17 00:00:00 2001 From: Ioannis Glaropoulos Date: Tue, 11 Feb 2020 16:57:32 +0100 Subject: [PATCH] arch: arm: cortex-m: fix PSPLIM configuring in context-switch When configuring the built-in stack guard, via setting the PSPLIM register, during thread context-switch, we shall only set PSPLIM to "guard" the thread's privileged stack area when the thread is actually using it (PSP is on this stack). Signed-off-by: Ioannis Glaropoulos --- arch/arm/core/aarch32/thread.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/arch/arm/core/aarch32/thread.c b/arch/arm/core/aarch32/thread.c index 6c8547ef60d..7563b9b893b 100644 --- a/arch/arm/core/aarch32/thread.c +++ b/arch/arm/core/aarch32/thread.c @@ -208,9 +208,17 @@ void configure_builtin_stack_guard(struct k_thread *thread) __set_PSPLIM(0); return; } - u32_t guard_start = thread->arch.priv_stack_start ? - (u32_t)thread->arch.priv_stack_start : - (u32_t)thread->stack_obj; + /* Only configure PSPLIM to guard the privileged stack area, if + * the thread is currently using it, otherwise guard the default + * thread stack. Note that the conditional check relies on the + * thread privileged stack being allocated in higher memory area + * than the default thread stack (ensured by design). + */ + u32_t guard_start = + ((thread->arch.priv_stack_start) && + (__get_PSP() >= thread->arch.priv_stack_start)) ? + (u32_t)thread->arch.priv_stack_start : + (u32_t)thread->stack_obj; __ASSERT(thread->stack_info.start == ((u32_t)thread->stack_obj), "stack_info.start does not point to the start of the"