2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_init.c
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2007-03-02 22:27:47 +01:00
|
|
|
* Included Files
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
#include <nuttx/config.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2007-03-02 22:27:47 +01:00
|
|
|
#include <sys/types.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <stdint.h>
|
2007-03-02 22:27:47 +01:00
|
|
|
#include <sched.h>
|
2020-05-27 17:32:14 +02:00
|
|
|
#include <queue.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2013-01-25 18:23:38 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2007-03-11 18:37:47 +01:00
|
|
|
#include <nuttx/arch.h>
|
2020-03-26 17:40:10 +01:00
|
|
|
#include <nuttx/sched.h>
|
2021-04-12 17:44:08 +02:00
|
|
|
#include <nuttx/tls.h>
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2022-04-04 12:11:06 +02:00
|
|
|
#include "environ/environ.h"
|
2014-08-08 22:06:42 +02:00
|
|
|
#include "group/group.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "task/task.h"
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2007-03-02 22:27:47 +01:00
|
|
|
* Public Functions
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2020-05-25 23:30:40 +02:00
|
|
|
* Name: nxtask_init
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2009-06-17 22:25:27 +02:00
|
|
|
* This function initializes a Task Control Block (TCB) in preparation for
|
|
|
|
* starting a new thread. It performs a subset of the functionality of
|
|
|
|
* task_create()
|
2007-03-11 23:19:01 +01:00
|
|
|
*
|
2009-06-17 22:25:27 +02:00
|
|
|
* Unlike task_create():
|
2020-03-08 13:51:34 +01:00
|
|
|
* 1. Allocate the TCB. The pre-allocated TCB is passed in argv.
|
|
|
|
* 2. Allocate the stack. The pre-allocated stack is passed in argv.
|
2020-05-25 23:30:40 +02:00
|
|
|
* 3. Activate the task. This must be done by calling nxtask_activate().
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2020-05-27 19:33:20 +02:00
|
|
|
* Certain fields of the pre-allocated TCB may be set to change the
|
|
|
|
* nature of the created task. For example:
|
|
|
|
*
|
2020-05-28 15:39:46 +02:00
|
|
|
* - Task type may be set in the TCB flags to create kernel thread
|
2020-05-27 19:33:20 +02:00
|
|
|
*
|
2007-03-02 22:27:47 +01:00
|
|
|
* Input Parameters:
|
2021-06-15 07:28:40 +02:00
|
|
|
* tcb - Address of the new task's TCB
|
|
|
|
* name - Name of the new task (not used)
|
|
|
|
* priority - Priority of the new task
|
|
|
|
* stack - Start of the pre-allocated stack
|
|
|
|
* stack_size - Size (in bytes) of the stack allocated
|
|
|
|
* entry - Application start point of the new task
|
|
|
|
* argv - A pointer to an array of input parameters. The array
|
|
|
|
* should be terminated with a NULL argv[] value. If no
|
|
|
|
* parameters are required, argv may be NULL.
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-05-03 13:56:49 +02:00
|
|
|
* OK on success; negative error value on failure appropriately. (See
|
2020-05-16 18:05:46 +02:00
|
|
|
* nxtask_setup_scheduler() for possible failure conditions). On failure,
|
|
|
|
* the caller is responsible for freeing the stack memory and for calling
|
2020-05-09 17:05:29 +02:00
|
|
|
* nxsched_release_tcb() to free the TCB (which could be in most any
|
|
|
|
* state).
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2021-06-15 07:28:40 +02:00
|
|
|
int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority,
|
2020-06-29 05:23:01 +02:00
|
|
|
FAR void *stack, uint32_t stack_size,
|
2022-04-04 12:11:06 +02:00
|
|
|
main_t entry, FAR char * const argv[],
|
|
|
|
FAR char * const envp[])
|
2007-03-02 22:27:47 +01:00
|
|
|
{
|
2020-06-29 09:52:16 +02:00
|
|
|
uint8_t ttype = tcb->cmn.flags & TCB_FLAG_TTYPE_MASK;
|
2021-06-29 12:35:42 +02:00
|
|
|
FAR struct tls_info_s *info;
|
2009-12-13 19:01:46 +01:00
|
|
|
int ret;
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2021-03-24 18:34:47 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2013-02-04 22:24:00 +01:00
|
|
|
/* Only tasks and kernel threads can be initialized in this way */
|
|
|
|
|
2020-06-29 04:44:33 +02:00
|
|
|
DEBUGASSERT(tcb && ttype != TCB_FLAG_TTYPE_PTHREAD);
|
2013-02-04 22:24:00 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Create a new task group */
|
|
|
|
|
2020-06-29 09:52:16 +02:00
|
|
|
ret = group_allocate(tcb, tcb->cmn.flags);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-06-29 04:44:33 +02:00
|
|
|
return ret;
|
2013-01-25 18:23:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
/* Duplicate the parent tasks environment */
|
|
|
|
|
|
|
|
ret = env_dup(tcb->cmn.group, envp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_group;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Associate file descriptors with the new task */
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2022-01-31 16:03:02 +01:00
|
|
|
ret = group_setuptaskfiles(tcb);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < 0)
|
2009-06-17 22:25:27 +02:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
goto errout_with_group;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 04:44:33 +02:00
|
|
|
if (stack)
|
|
|
|
{
|
|
|
|
/* Use pre-allocated stack */
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2020-06-29 09:52:16 +02:00
|
|
|
ret = up_use_stack(&tcb->cmn, stack, stack_size);
|
2020-06-29 04:44:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Allocate the stack for the TCB */
|
|
|
|
|
2021-04-12 17:44:08 +02:00
|
|
|
ret = up_create_stack(&tcb->cmn,
|
2021-12-03 14:35:39 +01:00
|
|
|
up_tls_size() + stack_size,
|
2021-04-12 17:44:08 +02:00
|
|
|
ttype);
|
2020-06-29 04:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < OK)
|
|
|
|
{
|
|
|
|
goto errout_with_group;
|
|
|
|
}
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2021-04-12 17:44:08 +02:00
|
|
|
/* Initialize thread local storage */
|
2021-04-03 14:33:16 +02:00
|
|
|
|
2021-12-03 14:35:39 +01:00
|
|
|
info = up_stack_frame(&tcb->cmn, up_tls_size());
|
2021-04-12 17:44:08 +02:00
|
|
|
if (info == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_group;
|
|
|
|
}
|
2021-03-24 18:34:47 +01:00
|
|
|
|
2021-04-12 17:44:08 +02:00
|
|
|
DEBUGASSERT(info == tcb->cmn.stack_alloc_ptr);
|
2021-03-24 18:34:47 +01:00
|
|
|
|
2021-06-29 12:35:42 +02:00
|
|
|
info->tl_task = tcb->cmn.group->tg_info;
|
|
|
|
|
2021-12-03 14:35:39 +01:00
|
|
|
up_tls_initialize(info);
|
|
|
|
|
2020-10-22 09:45:59 +02:00
|
|
|
/* Initialize the task control block */
|
|
|
|
|
|
|
|
ret = nxtask_setup_scheduler(tcb, priority, nxtask_start,
|
|
|
|
entry, ttype);
|
|
|
|
if (ret < OK)
|
|
|
|
{
|
|
|
|
goto errout_with_group;
|
|
|
|
}
|
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Setup to pass parameters to the new task */
|
|
|
|
|
2021-06-15 07:28:40 +02:00
|
|
|
nxtask_setup_arguments(tcb, name, argv);
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
/* Now we have enough in place that we can join the group */
|
|
|
|
|
2022-03-01 19:24:05 +01:00
|
|
|
group_initialize(tcb);
|
|
|
|
return ret;
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
errout_with_group:
|
2020-06-29 09:52:16 +02:00
|
|
|
if (!stack && tcb->cmn.stack_alloc_ptr)
|
2020-06-29 04:44:33 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_BUILD_KERNEL
|
|
|
|
/* If the exiting thread is not a kernel thread, then it has an
|
|
|
|
* address environment. Don't bother to release the stack memory
|
|
|
|
* in this case... There is no point since the memory lies in the
|
|
|
|
* user memory region that will be destroyed anyway (and the
|
|
|
|
* address environment has probably already been destroyed at
|
|
|
|
* this point.. so we would crash if we even tried it). But if
|
|
|
|
* this is a privileged group, when we still have to release the
|
|
|
|
* memory using the kernel allocator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ttype == TCB_FLAG_TTYPE_KERNEL)
|
|
|
|
#endif
|
|
|
|
{
|
2020-06-29 09:52:16 +02:00
|
|
|
up_release_stack(&tcb->cmn, ttype);
|
2020-06-29 04:44:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 09:52:16 +02:00
|
|
|
group_leave(&tcb->cmn);
|
2020-05-03 13:56:49 +02:00
|
|
|
return ret;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
2020-05-27 17:32:14 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxtask_uninit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Undo all operations on a TCB performed by task_init() and release the
|
|
|
|
* TCB by calling kmm_free(). This is intended primarily to support
|
|
|
|
* error recovery operations after a successful call to task_init() such
|
|
|
|
* was when a subsequent call to task_activate fails.
|
|
|
|
*
|
|
|
|
* Caution: Freeing of the TCB itself might be an unexpected side-effect.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tcb - Address of the TCB initialized by task_init()
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; negative error value on failure appropriately.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-06-29 09:52:16 +02:00
|
|
|
void nxtask_uninit(FAR struct task_tcb_s *tcb)
|
2020-05-27 17:32:14 +02:00
|
|
|
{
|
|
|
|
/* The TCB was added to the inactive task list by
|
|
|
|
* nxtask_setup_scheduler().
|
|
|
|
*/
|
|
|
|
|
|
|
|
dq_rem((FAR dq_entry_t *)tcb, (FAR dq_queue_t *)&g_inactivetasks);
|
|
|
|
|
|
|
|
/* Release all resources associated with the TCB... Including the TCB
|
|
|
|
* itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsched_release_tcb((FAR struct tcb_s *)tcb,
|
2020-06-29 09:52:16 +02:00
|
|
|
tcb->cmn.flags & TCB_FLAG_TTYPE_MASK);
|
2020-05-27 17:32:14 +02:00
|
|
|
}
|