arm: mpu: fix: align stack for mpu stack guard
The mimimum mpu size is 32 bytes, but requires mpu base address to be aligned on 32 bytes to work. Define architecture thread macro when MPU_STACK_GUARD config to allocate stack with 32 more bytes. Signed-off-by: Michel Jaouen <michel.jaouen@st.com>
This commit is contained in:
parent
505c45a7ad
commit
deeaa40e1e
3 changed files with 91 additions and 3 deletions
|
@ -24,7 +24,7 @@ void configure_mpu_stack_guard(struct k_thread *thread)
|
||||||
{
|
{
|
||||||
arm_core_mpu_disable();
|
arm_core_mpu_disable();
|
||||||
arm_core_mpu_configure(THREAD_STACK_GUARD_REGION,
|
arm_core_mpu_configure(THREAD_STACK_GUARD_REGION,
|
||||||
thread->stack_info.start,
|
thread->stack_info.start - STACK_ALIGN,
|
||||||
thread->stack_info.size);
|
thread->stack_info.size);
|
||||||
arm_core_mpu_enable();
|
arm_core_mpu_enable();
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,8 +64,8 @@ void _new_thread(struct k_thread *thread, k_thread_stack_t stack,
|
||||||
|
|
||||||
char *stackEnd = pStackMem + stackSize;
|
char *stackEnd = pStackMem + stackSize;
|
||||||
struct __esf *pInitCtx;
|
struct __esf *pInitCtx;
|
||||||
|
_new_thread_init(thread, K_THREAD_STACK_BUFFER(pStackMem), stackSize,
|
||||||
_new_thread_init(thread, pStackMem, stackSize, priority, options);
|
priority, options);
|
||||||
|
|
||||||
/* carve the thread entry struct from the "base" of the stack */
|
/* carve the thread entry struct from the "base" of the stack */
|
||||||
|
|
||||||
|
|
|
@ -42,9 +42,97 @@ extern "C" {
|
||||||
#include <arch/arm/cortex_m/sys_io.h>
|
#include <arch/arm/cortex_m/sys_io.h>
|
||||||
#include <arch/arm/cortex_m/nmi.h>
|
#include <arch/arm/cortex_m/nmi.h>
|
||||||
#endif
|
#endif
|
||||||
|
#if defined(CONFIG_MPU_STACK_GUARD)
|
||||||
|
|
||||||
|
#define STACK_ALIGN 32
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Declare a toplevel thread stack memory region
|
||||||
|
*
|
||||||
|
* This declares a region of memory suitable for use as a thread's stack.
|
||||||
|
*
|
||||||
|
* This is the generic, historical definition. Align to STACK_ALIGN and put in
|
||||||
|
* 'noinit' section so that it isn't zeroed at boot
|
||||||
|
*
|
||||||
|
* The declared symbol will always be a character array which can be passed to
|
||||||
|
* k_thread_create, but should otherwise not be manipulated.
|
||||||
|
*
|
||||||
|
* It is legal to precede this definition with the 'static' keyword.
|
||||||
|
*
|
||||||
|
* It is NOT legal to take the sizeof(sym) and pass that to the stackSize
|
||||||
|
* parameter of k_thread_create(), it may not be the same as the
|
||||||
|
* 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
|
||||||
|
*
|
||||||
|
* @param sym Thread stack symbol name
|
||||||
|
* @param size Size of the stack memory region
|
||||||
|
*/
|
||||||
|
#define _ARCH_THREAD_STACK_DEFINE(sym, size) \
|
||||||
|
char __noinit __aligned(STACK_ALIGN) sym[size+STACK_ALIGN]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Declare a toplevel array of thread stack memory regions
|
||||||
|
*
|
||||||
|
* Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
|
||||||
|
* definition for additional details and constraints.
|
||||||
|
*
|
||||||
|
* This is the generic, historical definition. Align to STACK_ALIGN and put in
|
||||||
|
* 'noinit' section so that it isn't zeroed at boot
|
||||||
|
*
|
||||||
|
* @param sym Thread stack symbol name
|
||||||
|
* @param nmemb Number of stacks to declare
|
||||||
|
* @param size Size of the stack memory region
|
||||||
|
*/
|
||||||
|
#define _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
|
||||||
|
char __noinit __aligned(STACK_ALIGN) sym[nmemb][size+STACK_ALIGN]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Declare an embedded stack memory region
|
||||||
|
*
|
||||||
|
* Used for stacks embedded within other data structures. Use is highly
|
||||||
|
* discouraged but in some cases necessary. For memory protection scenarios,
|
||||||
|
* it is very important that any RAM preceding this member not be writable
|
||||||
|
* by threads else a stack overflow will lead to silent corruption. In other
|
||||||
|
* words, the containing data structure should live in RAM owned by the kernel.
|
||||||
|
*
|
||||||
|
* @param sym Thread stack symbol name
|
||||||
|
* @param size Size of the stack memory region
|
||||||
|
*/
|
||||||
|
#define _ARCH_THREAD_STACK_MEMBER(sym, size) \
|
||||||
|
char __aligned(STACK_ALIGN) sym[size+STACK_ALIGN]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Return the size in bytes of a stack memory region
|
||||||
|
*
|
||||||
|
* Convenience macro for passing the desired stack size to k_thread_create()
|
||||||
|
* since the underlying implementation may actually create something larger
|
||||||
|
* (for instance a guard area).
|
||||||
|
*
|
||||||
|
* The value returned here is guaranteed to match the 'size' parameter
|
||||||
|
* passed to K_THREAD_STACK_DEFINE and related macros.
|
||||||
|
*
|
||||||
|
* @param sym Stack memory symbol
|
||||||
|
* @return Size of the stack
|
||||||
|
*/
|
||||||
|
#define _ARCH_THREAD_STACK_SIZEOF(sym) (sizeof(sym) - STACK_ALIGN)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get a pointer to the physical stack buffer
|
||||||
|
*
|
||||||
|
* Convenience macro to get at the real underlying stack buffer used by
|
||||||
|
* the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
|
||||||
|
* This is really only intended for diagnostic tools which want to examine
|
||||||
|
* stack memory contents.
|
||||||
|
*
|
||||||
|
* @param sym Declared stack symbol name
|
||||||
|
* @return The buffer itself, a char *
|
||||||
|
*/
|
||||||
|
#define _ARCH_THREAD_STACK_BUFFER(sym) (sym + STACK_ALIGN)
|
||||||
|
|
||||||
|
#else /* CONFIG_MPU_STACK_GUARD */
|
||||||
|
|
||||||
#define STACK_ALIGN 4
|
#define STACK_ALIGN 4
|
||||||
|
|
||||||
|
#endif
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue