arm: userspace: fix initial user sp location
The initial user stack pointer gets rounded to STACK_ALIGN_SIZE,
which can be much finer granularity than MPU regions. With
certain stack size values passed to k_thread_create(), the stack
pointer can be set past the defined region for the user thread's
stack, causing an immediate MPU fault.
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
diff --git a/arch/arm/core/thread.c b/arch/arm/core/thread.c
index 50969fc..4c70cef 100644
--- a/arch/arm/core/thread.c
+++ b/arch/arm/core/thread.c
@@ -134,6 +134,12 @@
_current->arch.priv_stack_size =
(u32_t)CONFIG_PRIVILEGED_STACK_SIZE;
+ /* FIXME: Need a general API for aligning stacks so thet the initial
+ * user thread stack pointer doesn't overshoot the granularity of MPU
+ * regions, that works for ARM/NXP/QEMU.
+ */
+ _current->stack_info.size &= ~0x1f;
+
_arm_userspace_enter(user_entry, p1, p2, p3,
(u32_t)_current->stack_info.start,
_current->stack_info.size);