mirror of
https://github.com/zephyrproject-rtos/zephyr
synced 2025-08-08 21:55:21 +00:00
Historically, stacks were just character buffers and could be treated as such if the user wanted to look inside the stack data, and also declared as an array of the desired stack size. This is no longer the case. Certain architectures will create a memory region much larger to account for MPU/MMU guard pages. Unfortunately, the kernel interfaces treat both the declared stack, and the valid stack buffer within it as the same char * data type, even though these absolutely cannot be used interchangeably. We introduce an opaque k_thread_stack_t which gets instantiated by K_THREAD_STACK_DECLARE(), this is no longer treated by the compiler as a character pointer, even though it really is. To access the real stack buffer within, the result of K_THREAD_STACK_BUFFER() can be used, which will return a char * type. This should catch a bunch of programming mistakes at build time: - Declaring a character array outside of K_THREAD_STACK_DECLARE() and passing it to K_THREAD_CREATE - Directly examining the stack created by K_THREAD_STACK_DECLARE() which is not actually the memory desired and may trigger a CPU exception Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
135 lines
3.9 KiB
C
135 lines
3.9 KiB
C
/*
|
|
* Copyright (c) 2016 Cadence Design Systems, Inc.
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#ifdef CONFIG_INIT_STACKS
|
|
#include <string.h>
|
|
#endif /* CONFIG_INIT_STACKS */
|
|
#ifdef CONFIG_DEBUG
|
|
#include <misc/printk.h>
|
|
#endif
|
|
#include <kernel_structs.h>
|
|
#include <wait_q.h>
|
|
#include <xtensa_config.h>
|
|
|
|
extern void _xt_user_exit(void);
|
|
|
|
/*
|
|
* @brief Initialize a new thread
|
|
*
|
|
* Any coprocessor context data is put at the lower address of the stack. An
|
|
* initial context, to be "restored" by __return_from_coop(), is put at
|
|
* the other end of the stack, and thus reusable by the stack when not
|
|
* needed anymore.
|
|
*
|
|
* The initial context is a basic stack frame that contains arguments for
|
|
* _thread_entry() return address, that points at _thread_entry()
|
|
* and status register.
|
|
*
|
|
* <options> is currently unused.
|
|
*
|
|
* @param thread pointer to k_thread memory
|
|
* @param pStackmem the pointer to aligned stack memory
|
|
* @param stackSize the stack size in bytes
|
|
* @param pEntry thread entry point routine
|
|
* @param p1 first param to entry point
|
|
* @param p2 second param to entry point
|
|
* @param p3 third param to entry point
|
|
* @param priority thread priority
|
|
* @param options is unused (saved for future expansion)
|
|
*
|
|
* @return N/A
|
|
*/
|
|
|
|
void _new_thread(struct k_thread *thread, k_thread_stack_t stack,
|
|
size_t stackSize,
|
|
void (*pEntry)(void *, void *, void *),
|
|
void *p1, void *p2, void *p3,
|
|
int priority, unsigned int options)
|
|
{
|
|
char *pStack = K_THREAD_STACK_BUFFER(stack);
|
|
|
|
/* Align stack end to maximum alignment requirement. */
|
|
char *stackEnd = (char *)ROUND_DOWN(pStack + stackSize, 16);
|
|
#if XCHAL_CP_NUM > 0
|
|
u32_t *cpSA;
|
|
char *cpStack;
|
|
#endif
|
|
|
|
_new_thread_init(thread, pStack, stackSize, priority, options);
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
printk("\nstackPtr = %p, stackSize = %d\n", pStack, stackSize);
|
|
printk("stackEnd = %p\n", stackEnd);
|
|
#endif
|
|
#if XCHAL_CP_NUM > 0
|
|
/* Ensure CP state descriptor is correctly initialized */
|
|
cpStack = thread->arch.preempCoprocReg.cpStack; /* short hand alias */
|
|
memset(cpStack, 0, XT_CP_ASA); /* Set to zero to avoid bad surprises */
|
|
/* Coprocessor's stack is allocated just after the k_thread */
|
|
cpSA = (u32_t *)(thread->arch.preempCoprocReg.cpStack + XT_CP_ASA);
|
|
/* Coprocessor's save area alignment is at leat 16 bytes */
|
|
*cpSA = ROUND_UP(cpSA + 1,
|
|
(XCHAL_TOTAL_SA_ALIGN < 16 ? 16 : XCHAL_TOTAL_SA_ALIGN));
|
|
#ifdef CONFIG_DEBUG
|
|
printk("cpStack = %p\n", thread->arch.preempCoprocReg.cpStack);
|
|
printk("cpAsa = %p\n",
|
|
*(void **)(thread->arch.preempCoprocReg.cpStack + XT_CP_ASA));
|
|
#endif
|
|
#endif
|
|
/* Thread's first frame alignment is granted as both operands are
|
|
* aligned
|
|
*/
|
|
XtExcFrame *pInitCtx =
|
|
(XtExcFrame *)(stackEnd - (XT_XTRA_SIZE - XT_CP_SIZE));
|
|
#ifdef CONFIG_DEBUG
|
|
printk("pInitCtx = %p\n", pInitCtx);
|
|
#endif
|
|
/* Explicitly initialize certain saved registers */
|
|
|
|
/* task entrypoint */
|
|
pInitCtx->pc = (u32_t)_thread_entry;
|
|
|
|
/* physical top of stack frame */
|
|
pInitCtx->a1 = (u32_t)pInitCtx + XT_STK_FRMSZ;
|
|
|
|
/* user exception exit dispatcher */
|
|
pInitCtx->exit = (u32_t)_xt_user_exit;
|
|
|
|
/* Set initial PS to int level 0, EXCM disabled, user mode.
|
|
* Also set entry point argument arg.
|
|
*/
|
|
#ifdef __XTENSA_CALL0_ABI__
|
|
pInitCtx->a2 = (u32_t)pEntry;
|
|
pInitCtx->a3 = (u32_t)p1;
|
|
pInitCtx->a4 = (u32_t)p2;
|
|
pInitCtx->a5 = (u32_t)p3;
|
|
pInitCtx->ps = PS_UM | PS_EXCM;
|
|
#else
|
|
/* For windowed ABI set also WOE and CALLINC
|
|
* (pretend task is 'call4')
|
|
*/
|
|
pInitCtx->a6 = (u32_t)pEntry;
|
|
pInitCtx->a7 = (u32_t)p1;
|
|
pInitCtx->a8 = (u32_t)p2;
|
|
pInitCtx->a9 = (u32_t)p3;
|
|
pInitCtx->ps = PS_UM | PS_EXCM | PS_WOE | PS_CALLINC(1);
|
|
#endif
|
|
thread->callee_saved.topOfStack = pInitCtx;
|
|
thread->arch.flags = 0;
|
|
#ifdef CONFIG_THREAD_MONITOR
|
|
/*
|
|
* In debug mode thread->entry give direct access to the thread entry
|
|
* and the corresponding parameters.
|
|
*/
|
|
thread->entry = (struct __thread_entry *)(pInitCtx);
|
|
#endif
|
|
/* initial values in all other registers/k_thread entries are
|
|
* irrelevant
|
|
*/
|
|
|
|
thread_monitor_init(thread);
|
|
}
|
|
|