addrenv/kstack: Allocate the kernel stack before initializing tcb

This is preparation to use kernel stack for everything when the user
process enters the kernel. Now the user stack is in use when the user
process runs a system call, which might not be the safest option.
This commit is contained in:
Ville Juven 2023-05-31 14:39:09 +03:00 committed by archer
parent 7732791cd6
commit a636edcbe4
4 changed files with 37 additions and 23 deletions

View File

@ -54,6 +54,9 @@ void up_initial_state(struct tcb_s *tcb)
{
struct xcptcontext *xcp = &tcb->xcp;
uint32_t cpsr;
#ifdef CONFIG_ARCH_KERNEL_STACK
uint32_t *kstack = xcp->kstack;
#endif
/* Initialize the initial exception register context structure */
@ -80,6 +83,10 @@ void up_initial_state(struct tcb_s *tcb)
return;
}
#ifdef CONFIG_ARCH_KERNEL_STACK
xcp->kstack = kstack;
#endif
/* Initialize the context registers to stack top */
xcp->regs = (void *)((uint32_t)tcb->stack_base_ptr +

View File

@ -56,6 +56,9 @@ void up_initial_state(struct tcb_s *tcb)
{
struct xcptcontext *xcp = &tcb->xcp;
uintptr_t regval;
#ifdef CONFIG_ARCH_KERNEL_STACK
uintptr_t *kstack = xcp->kstack;
#endif
/* Initialize the initial exception register context structure */
@ -85,6 +88,10 @@ void up_initial_state(struct tcb_s *tcb)
return;
}
#ifdef CONFIG_ARCH_KERNEL_STACK
xcp->kstack = kstack;
#endif
xcp->regs = (uintptr_t *)(
(uintptr_t)tcb->stack_base_ptr + tcb->adj_stack_size - XCPTCONTEXT_SIZE);

View File

@ -186,6 +186,17 @@ int exec_module(FAR struct binary_s *binp,
umm_initialize(vheap, up_addrenv_heapsize(addrenv));
#endif
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_KERNEL_STACK)
/* Allocate the kernel stack */
ret = up_addrenv_kstackalloc(&tcb->cmn);
if (ret < 0)
{
berr("ERROR: up_addrenv_kstackalloc() failed: %d\n", ret);
goto errout_with_addrenv;
}
#endif
/* Note that tcb->flags are not modified. 0=normal task */
/* tcb->flags |= TCB_FLAG_TTYPE_TASK; */
@ -229,17 +240,6 @@ int exec_module(FAR struct binary_s *binp,
}
}
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_KERNEL_STACK)
/* Allocate the kernel stack */
ret = up_addrenv_kstackalloc(&tcb->cmn);
if (ret < 0)
{
berr("ERROR: up_addrenv_kstackalloc() failed: %d\n", ret);
goto errout_with_tcbinit;
}
#endif
#ifdef CONFIG_PIC
/* Add the D-Space address as the PIC base address. By convention, this
* must be the first allocated address space.

View File

@ -253,6 +253,18 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
goto errout_with_tcb;
}
#if defined(CONFIG_ARCH_ADDRENV) && \
defined(CONFIG_BUILD_KERNEL) && defined(CONFIG_ARCH_KERNEL_STACK)
/* Allocate the kernel stack */
ret = up_addrenv_kstackalloc(&ptcb->cmn);
if (ret < 0)
{
errcode = ENOMEM;
goto errout_with_tcb;
}
#endif
/* Initialize thread local storage */
ret = tls_init_info(&ptcb->cmn);
@ -369,18 +381,6 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
goto errout_with_tcb;
}
#if defined(CONFIG_ARCH_ADDRENV) && \
defined(CONFIG_BUILD_KERNEL) && defined(CONFIG_ARCH_KERNEL_STACK)
/* Allocate the kernel stack */
ret = up_addrenv_kstackalloc(&ptcb->cmn);
if (ret < 0)
{
errcode = ENOMEM;
goto errout_with_tcb;
}
#endif
#ifdef CONFIG_SMP
/* pthread_setup_scheduler() will set the affinity mask by inheriting the
* setting from the parent task. We need to override this setting