arch: arc: implement stack pointer random

implement the stack pointer random for CONFIG
_STACK_POINTER_RANDOM

Signed-off-by: Wayne Ren <wei.ren@synopsys.com>
This commit is contained in:
Wayne Ren 2019-02-19 16:34:43 +08:00 committed by Andrew Boie
commit 6704b82d5b

View file

@ -75,13 +75,21 @@ void z_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
struct init_stack_frame *pInitCtx; struct init_stack_frame *pInitCtx;
#if CONFIG_USERSPACE #if CONFIG_USERSPACE
size_t stackAdjSize;
size_t offset = 0;
/* adjust stack and stack size */ /* adjust stack and stack size */
#if CONFIG_ARC_MPU_VER == 2 #if CONFIG_ARC_MPU_VER == 2
stackSize = POW2_CEIL(STACK_SIZE_ALIGN(stackSize)); stackAdjSize = POW2_CEIL(STACK_SIZE_ALIGN(stackSize));
#elif CONFIG_ARC_MPU_VER == 3 #elif CONFIG_ARC_MPU_VER == 3
stackSize = ROUND_UP(stackSize, STACK_ALIGN); stackAdjSize = ROUND_UP(stackSize, STACK_ALIGN);
#endif
stackEnd = pStackMem + stackAdjSize;
#if CONFIG_STACK_POINTER_RANDOM
offset = stackAdjSize - stackSize;
#endif #endif
stackEnd = pStackMem + stackSize;
if (options & K_USER) { if (options & K_USER) {
thread->arch.priv_stack_start = thread->arch.priv_stack_start =
@ -92,13 +100,15 @@ void z_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
/* reserve 4 bytes for the start of user sp */ /* reserve 4 bytes for the start of user sp */
stackAdjEnd -= 4; stackAdjEnd -= 4;
(*(u32_t *)stackAdjEnd) = (u32_t)stackEnd; (*(u32_t *)stackAdjEnd) = STACK_ROUND_DOWN(
(u32_t)stackEnd - offset);
#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
/* reserve stack space for the userspace local data struct */ /* reserve stack space for the userspace local data struct */
thread->userspace_local_data = thread->userspace_local_data =
(struct _thread_userspace_local_data *) (struct _thread_userspace_local_data *)
STACK_ROUND_DOWN(stackEnd - sizeof(*thread->userspace_local_data)); STACK_ROUND_DOWN(stackEnd -
sizeof(*thread->userspace_local_data) - offset);
/* update the start of user sp */ /* update the start of user sp */
(*(u32_t *)stackAdjEnd) = (u32_t) thread->userspace_local_data; (*(u32_t *)stackAdjEnd) = (u32_t) thread->userspace_local_data;
#endif #endif
@ -115,7 +125,7 @@ void z_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
* --------------------------------------------- * ---------------------------------------------
*/ */
pStackMem += STACK_GUARD_SIZE; pStackMem += STACK_GUARD_SIZE;
stackSize = stackSize + CONFIG_PRIVILEGED_STACK_SIZE; stackAdjSize = stackAdjSize + CONFIG_PRIVILEGED_STACK_SIZE;
stackEnd += CONFIG_PRIVILEGED_STACK_SIZE + STACK_GUARD_SIZE; stackEnd += CONFIG_PRIVILEGED_STACK_SIZE + STACK_GUARD_SIZE;
thread->arch.priv_stack_start = 0; thread->arch.priv_stack_start = 0;
@ -123,15 +133,15 @@ void z_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
/* reserve stack space for the userspace local data struct */ /* reserve stack space for the userspace local data struct */
stackAdjEnd = (char *)STACK_ROUND_DOWN(stackEnd stackAdjEnd = (char *)STACK_ROUND_DOWN(stackEnd
- sizeof(*thread->userspace_local_data)); - sizeof(*thread->userspace_local_data) - offset);
thread->userspace_local_data = thread->userspace_local_data =
(struct _thread_userspace_local_data *)stackAdjEnd; (struct _thread_userspace_local_data *)stackAdjEnd;
#else #else
stackAdjEnd = (char *)STACK_ROUND_DOWN(stackEnd); stackAdjEnd = (char *)STACK_ROUND_DOWN(stackEnd - offset);
#endif #endif
} }
_new_thread_init(thread, pStackMem, stackSize, priority, options); _new_thread_init(thread, pStackMem, stackAdjSize, priority, options);
/* carve the thread entry struct from the "base" of /* carve the thread entry struct from the "base" of