diff --git a/arch/arm/src/armv7-a/arm_initialstate.c b/arch/arm/src/armv7-a/arm_initialstate.c index 0125f2bf8d..c9ea5ec8ca 100644 --- a/arch/arm/src/armv7-a/arm_initialstate.c +++ b/arch/arm/src/armv7-a/arm_initialstate.c @@ -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 + diff --git a/arch/risc-v/src/common/riscv_initialstate.c b/arch/risc-v/src/common/riscv_initialstate.c index 802c2103d7..2041d3cceb 100644 --- a/arch/risc-v/src/common/riscv_initialstate.c +++ b/arch/risc-v/src/common/riscv_initialstate.c @@ -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); diff --git a/binfmt/binfmt_execmodule.c b/binfmt/binfmt_execmodule.c index 6e6b26649d..9b169114c9 100644 --- a/binfmt/binfmt_execmodule.c +++ b/binfmt/binfmt_execmodule.c @@ -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. diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c index 81072c255e..a158af4518 100644 --- a/sched/pthread/pthread_create.c +++ b/sched/pthread/pthread_create.c @@ -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