From 5bc307e60f1f1031a3c58d24e31c81285b1c49d6 Mon Sep 17 00:00:00 2001 From: Wayne Ren Date: Tue, 9 Apr 2019 00:12:45 +0800 Subject: [PATCH] arch: arc: optimize the macros of thread stack * clean up and optimzie the macros of thread stack Signed-off-by: Wayne Ren --- arch/arc/core/thread.c | 14 ++++----- include/arch/arc/arch.h | 69 +++++++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/arch/arc/core/thread.c b/arch/arc/core/thread.c index 798db41c6e7..1936a87a465 100644 --- a/arch/arc/core/thread.c +++ b/arch/arc/core/thread.c @@ -81,9 +81,9 @@ void z_new_thread(struct k_thread *thread, k_thread_stack_t *stack, /* adjust stack and stack size */ #if CONFIG_ARC_MPU_VER == 2 - stackAdjSize = POW2_CEIL(STACK_SIZE_ALIGN(stackSize)); + stackAdjSize = Z_ARC_MPUV2_SIZE_ALIGN(stackSize); #elif CONFIG_ARC_MPU_VER == 3 - stackAdjSize = ROUND_UP(stackSize, STACK_ALIGN); + stackAdjSize = STACK_SIZE_ALIGN(stackSize); #endif stackEnd = pStackMem + stackAdjSize; @@ -95,8 +95,8 @@ void z_new_thread(struct k_thread *thread, k_thread_stack_t *stack, thread->arch.priv_stack_start = (u32_t)(stackEnd + STACK_GUARD_SIZE); - stackAdjEnd = (char *)STACK_ROUND_DOWN(stackEnd + STACK_GUARD_SIZE + - CONFIG_PRIVILEGED_STACK_SIZE); + stackAdjEnd = (char *)STACK_ROUND_DOWN(stackEnd + + Z_ARCH_THREAD_STACK_RESERVED); /* reserve 4 bytes for the start of user sp */ stackAdjEnd -= 4; @@ -126,7 +126,7 @@ void z_new_thread(struct k_thread *thread, k_thread_stack_t *stack, */ pStackMem += STACK_GUARD_SIZE; stackAdjSize = stackAdjSize + CONFIG_PRIVILEGED_STACK_SIZE; - stackEnd += CONFIG_PRIVILEGED_STACK_SIZE + STACK_GUARD_SIZE; + stackEnd += Z_ARCH_THREAD_STACK_RESERVED; thread->arch.priv_stack_start = 0; @@ -165,7 +165,7 @@ void z_new_thread(struct k_thread *thread, k_thread_stack_t *stack, */ pInitCtx->status32 |= _ARC_V2_STATUS32_US; #else /* For no USERSPACE feature */ - pStackMem += STACK_GUARD_SIZE; + pStackMem += Z_ARCH_THREAD_STACK_RESERVED; stackEnd = pStackMem + stackSize; z_new_thread_init(thread, pStackMem, stackSize, priority, options); @@ -203,7 +203,7 @@ void z_new_thread(struct k_thread *thread, k_thread_stack_t *stack, thread->arch.k_stack_top = (u32_t)(stackEnd + STACK_GUARD_SIZE); thread->arch.k_stack_base = (u32_t) - (stackEnd + STACK_GUARD_SIZE + CONFIG_PRIVILEGED_STACK_SIZE); + (stackEnd + Z_ARCH_THREAD_STACK_RESERVED); } else { thread->arch.k_stack_top = (u32_t)pStackMem; thread->arch.k_stack_base = (u32_t)stackEnd; diff --git a/include/arch/arc/arch.h b/include/arch/arc/arch.h index 5c144758929..e696e93d578 100644 --- a/include/arch/arc/arch.h +++ b/include/arch/arc/arch.h @@ -69,8 +69,7 @@ extern "C" { #endif /* CONFIG_MPU_STACK_GUARD */ -#define STACK_SIZE_ALIGN(x) \ - (((x + STACK_ALIGN - 1) / (STACK_ALIGN)) * (STACK_ALIGN)) +#define STACK_SIZE_ALIGN(x) ROUND_UP(x, STACK_ALIGN) /** * @brief Calculate power of two ceiling for a buffer size input @@ -89,53 +88,61 @@ extern "C" { #endif -/* MPUv2 requires size must be power of 2 and >= 2048 */ -#define Z_ARCH_THREAD_STACK_SIZE(size) POW2_CEIL(STACK_SIZE_ALIGN(size)) - #if defined(CONFIG_USERSPACE) && CONFIG_ARC_MPU_VER == 2 -#define Z_ARCH_THREAD_STACK_ALIGN(size) Z_ARCH_THREAD_STACK_SIZE(size) -#define Z_ARCH_THREAD_STACK_LEN(size) \ - (Z_ARCH_THREAD_STACK_SIZE(size) + Z_ARCH_THREAD_STACK_RESERVED) -/* - * for stack arrary, it has more strict requirement on - * size and address alignment which is decided by MPUv2 +/* MPUv2 requires + * - region size must be power of 2 and >= 2048 + * - region start must be aligned to its size */ -#define Z_ARCH_THREAD_STACK_ARRAY_LEN(size) \ - (Z_ARCH_THREAD_STACK_SIZE(size) + \ - MAX(Z_ARCH_THREAD_STACK_SIZE(size), \ - POW2_CEIL(Z_ARCH_THREAD_STACK_RESERVED))) -#elif defined(CONFIG_MPU_STACK_GUARD) && CONFIG_ARC_MPU_VER == 2 -#define Z_ARCH_THREAD_STACK_ALIGN(size) (STACK_ALIGN) +#define Z_ARC_MPUV2_SIZE_ALIGN(size) POW2_CEIL(STACK_SIZE_ALIGN(size)) +/* + * user stack and guard are protected using MPU regions, so need to adhere to + * MPU start, size alignment + */ +#define Z_ARC_THREAD_STACK_ALIGN(size) Z_ARC_MPUV2_SIZE_ALIGN(size) #define Z_ARCH_THREAD_STACK_LEN(size) \ - (size + Z_ARCH_THREAD_STACK_RESERVED) -#define Z_ARCH_THREAD_STACK_ARRAY_LEN(size) \ - (Z_ARCH_THREAD_STACK_SIZE(size) + \ - Z_ARCH_THREAD_STACK_RESERVED) - -#else /* CONFIG_ARC_MPU_VER */ -/* MPUv3, no-mpu and no USERSPACE & MPU_STACK_GUARD are the same case */ -#define Z_ARCH_THREAD_STACK_ALIGN(size) (STACK_ALIGN) + (Z_ARC_MPUV2_SIZE_ALIGN(size) + Z_ARCH_THREAD_STACK_RESERVED) +/* + * for stack array, each array member should be aligned both in size + * and start + */ +#define Z_ARC_THREAD_STACK_ARRAY_LEN(size) \ + (Z_ARC_MPUV2_SIZE_ALIGN(size) + \ + MAX(Z_ARC_MPUV2_SIZE_ALIGN(size), \ + POW2_CEIL(Z_ARCH_THREAD_STACK_RESERVED))) +#else +/* + * MPUv3, no-mpu and no USERSPACE share the same macro definitions. + * For MPU STACK_GUARD kernel stacks do not need a MPU region to protect, + * only guard needs to be protected and aligned. For MPUv3,MPU_STACK_GUARD + * requires start 32 bytes aligned, also for size which is decided by stack + * array and USERSPACE; For MPUv2, MPU_STACK_GUARD requires + * start 2048 bytes aligned, also for size which is decided by stack array. + * + * When no-mpu and no USERSPACE/MPU_STACK_GUARD, everything is 4 bytes + * aligned + */ +#define Z_ARC_THREAD_STACK_ALIGN(size) (STACK_ALIGN) #define Z_ARCH_THREAD_STACK_LEN(size) \ (STACK_SIZE_ALIGN(size) + Z_ARCH_THREAD_STACK_RESERVED) -#define Z_ARCH_THREAD_STACK_ARRAY_LEN(size) \ +#define Z_ARC_THREAD_STACK_ARRAY_LEN(size) \ Z_ARCH_THREAD_STACK_LEN(size) -#endif +#endif /* CONFIG_USERSPACE && CONFIG_ARC_MPU_VER == 2 */ #define Z_ARCH_THREAD_STACK_DEFINE(sym, size) \ struct _k_thread_stack_element __noinit \ - __aligned(Z_ARCH_THREAD_STACK_ALIGN(size)) \ + __aligned(Z_ARC_THREAD_STACK_ALIGN(size)) \ sym[Z_ARCH_THREAD_STACK_LEN(size)] #define Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \ struct _k_thread_stack_element __noinit \ - __aligned(Z_ARCH_THREAD_STACK_ALIGN(size)) \ - sym[nmemb][Z_ARCH_THREAD_STACK_ARRAY_LEN(size)] + __aligned(Z_ARC_THREAD_STACK_ALIGN(size)) \ + sym[nmemb][Z_ARC_THREAD_STACK_ARRAY_LEN(size)] #define Z_ARCH_THREAD_STACK_MEMBER(sym, size) \ struct _k_thread_stack_element \ - __aligned(Z_ARCH_THREAD_STACK_ALIGN(size)) \ + __aligned(Z_ARC_THREAD_STACK_ALIGN(size)) \ sym[Z_ARCH_THREAD_STACK_LEN(size)] #define Z_ARCH_THREAD_STACK_SIZEOF(sym) \