2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/binfmt_execmodule.c
|
|
|
|
*
|
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
|
2009-06-17 22:25:27 +02:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-17 22:25:27 +02: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.
|
2009-06-17 22:25:27 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-15 18:06:59 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
2009-06-17 22:25:27 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2013-01-16 15:14:14 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2020-03-26 17:40:10 +01:00
|
|
|
#include <nuttx/sched.h>
|
2014-09-24 15:05:02 +02:00
|
|
|
#include <nuttx/mm/shm.h>
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2015-11-14 14:29:47 +01:00
|
|
|
#include "binfmt.h"
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2009-06-17 23:15:31 +02:00
|
|
|
#ifndef CONFIG_BINFMT_DISABLE
|
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2020-01-07 16:54:35 +01:00
|
|
|
|
2013-01-27 16:52:58 +01:00
|
|
|
/* If C++ constructors are used, then CONFIG_SCHED_STARTHOOK must also be
|
|
|
|
* selected be the start hook is used to schedule execution of the
|
|
|
|
* constructors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_BINFMT_CONSTRUCTORS) && !defined(CONFIG_SCHED_STARTHOOK)
|
2019-09-20 02:19:18 +02:00
|
|
|
# error "CONFIG_SCHED_STARTHOOK must be defined to use constructors"
|
2013-01-27 16:52:58 +01:00
|
|
|
#endif
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: exec_ctors
|
|
|
|
*
|
|
|
|
* Description:
|
2013-01-27 16:52:58 +01:00
|
|
|
* Execute C++ static constructors. This function is registered as a
|
|
|
|
* start hook and runs on the thread of the newly created task before
|
|
|
|
* the new task's main function is called.
|
2012-10-29 20:32:05 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-08-05 16:09:54 +02:00
|
|
|
* arg - Argument is instance of load state info structure cast to void *.
|
2012-10-29 20:32:05 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2013-01-27 16:52:58 +01:00
|
|
|
static void exec_ctors(FAR void *arg)
|
2012-10-29 20:32:05 +01:00
|
|
|
{
|
2013-01-27 16:52:58 +01:00
|
|
|
FAR const struct binary_s *binp = (FAR const struct binary_s *)arg;
|
2012-10-29 21:43:35 +01:00
|
|
|
binfmt_ctor_t *ctor = binp->ctors;
|
2012-10-29 20:32:05 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Execute each constructor */
|
|
|
|
|
|
|
|
for (i = 0; i < binp->nctors; i++)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("Calling ctor %d at %p\n", i, (FAR void *)ctor);
|
2012-10-29 20:32:05 +01:00
|
|
|
|
|
|
|
(*ctor)();
|
|
|
|
ctor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: exec_module
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Execute a module that has been loaded into memory by load_module().
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-10-02 23:30:55 +02:00
|
|
|
* This is a NuttX internal function so it follows the convention that
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
2009-06-17 22:25:27 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-24 11:12:06 +02:00
|
|
|
int exec_module(FAR const struct binary_s *binp,
|
2022-04-04 12:11:06 +02:00
|
|
|
FAR const char *filename, FAR char * const *argv,
|
|
|
|
FAR char * const *envp)
|
2009-06-17 22:25:27 +02:00
|
|
|
{
|
2013-02-04 22:24:00 +01:00
|
|
|
FAR struct task_tcb_s *tcb;
|
2014-09-11 18:31:12 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2014-09-10 01:32:32 +02:00
|
|
|
save_addrenv_t oldenv;
|
2022-03-17 12:55:50 +01:00
|
|
|
FAR void *vheap;
|
2014-09-10 01:32:32 +02:00
|
|
|
#endif
|
2022-10-19 21:27:21 +02:00
|
|
|
FAR void *stackaddr = NULL;
|
2013-02-04 19:46:28 +01:00
|
|
|
pid_t pid;
|
|
|
|
int ret;
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Sanity checking */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-10-29 20:32:05 +01:00
|
|
|
if (!binp || !binp->entrypt || binp->stacksize <= 0)
|
2009-06-17 22:25:27 +02:00
|
|
|
{
|
2017-10-02 23:30:55 +02:00
|
|
|
return -EINVAL;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-05-24 11:12:06 +02:00
|
|
|
binfo("Executing %s\n", filename);
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
tcb = (FAR struct task_tcb_s *)kmm_zalloc(sizeof(struct task_tcb_s));
|
2009-06-17 22:25:27 +02:00
|
|
|
if (!tcb)
|
|
|
|
{
|
2017-10-02 23:30:55 +02:00
|
|
|
return -ENOMEM;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
ret = binfmt_copyargv(&argv, argv);
|
|
|
|
if (ret < 0)
|
2021-05-24 09:06:29 +02:00
|
|
|
{
|
2022-10-17 17:24:34 +02:00
|
|
|
goto errout_with_tcb;
|
2021-05-24 09:06:29 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
/* Make a copy of the environment here */
|
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
if (envp == NULL)
|
|
|
|
{
|
|
|
|
envp = environ;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = binfmt_copyenv(&envp, envp);
|
|
|
|
if (ret < 0)
|
2022-04-04 12:11:06 +02:00
|
|
|
{
|
2022-10-17 17:24:34 +02:00
|
|
|
goto errout_with_args;
|
2022-04-04 12:11:06 +02:00
|
|
|
}
|
|
|
|
|
2014-09-14 20:40:09 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2014-09-10 23:55:36 +02:00
|
|
|
/* Instantiate the address environment containing the user heap */
|
|
|
|
|
2014-09-10 01:32:32 +02:00
|
|
|
ret = up_addrenv_select(&binp->addrenv, &oldenv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: up_addrenv_select() failed: %d\n", ret);
|
2022-04-04 12:11:06 +02:00
|
|
|
goto errout_with_envp;
|
2014-09-10 01:32:32 +02:00
|
|
|
}
|
2022-02-11 07:45:09 +01:00
|
|
|
|
2022-03-17 12:55:50 +01:00
|
|
|
ret = up_addrenv_vheap(&binp->addrenv, &vheap);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: up_addrenv_vheap() failed: %d\n", ret);
|
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
|
2022-05-02 08:12:46 +02:00
|
|
|
binfo("Initialize the user heap (heapsize=%zu)\n",
|
|
|
|
up_addrenv_heapsize(&binp->addrenv));
|
2022-03-17 12:55:50 +01:00
|
|
|
umm_initialize(vheap, up_addrenv_heapsize(&binp->addrenv));
|
2014-09-10 01:32:32 +02:00
|
|
|
#endif
|
|
|
|
|
2020-06-29 10:26:48 +02:00
|
|
|
/* Note that tcb->flags are not modified. 0=normal task */
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2020-06-29 10:26:48 +02:00
|
|
|
/* tcb->flags |= TCB_FLAG_TTYPE_TASK; */
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Initialize the task */
|
|
|
|
|
2022-10-19 21:27:21 +02:00
|
|
|
#ifndef CONFIG_BUILD_KERNEL
|
|
|
|
stackaddr = binp->stackaddr;
|
|
|
|
#endif
|
|
|
|
|
2021-06-15 19:38:07 +02:00
|
|
|
if (argv && argv[0])
|
|
|
|
{
|
2022-10-19 21:27:21 +02:00
|
|
|
ret = nxtask_init(tcb, argv[0], binp->priority, stackaddr,
|
2022-04-04 12:11:06 +02:00
|
|
|
binp->stacksize, binp->entrypt, &argv[1], envp);
|
2021-06-15 19:38:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-19 21:27:21 +02:00
|
|
|
ret = nxtask_init(tcb, filename, binp->priority, stackaddr,
|
2022-04-04 12:11:06 +02:00
|
|
|
binp->stacksize, binp->entrypt, argv, envp);
|
2021-06-15 19:38:07 +02:00
|
|
|
}
|
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-05-25 23:30:40 +02:00
|
|
|
berr("nxtask_init() failed: %d\n", ret);
|
2014-09-14 22:10:23 +02:00
|
|
|
goto errout_with_addrenv;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
/* The copied argv and envp can now be released */
|
|
|
|
|
2022-04-08 10:10:01 +02:00
|
|
|
binfmt_freeargv(argv);
|
2022-04-04 12:11:06 +02:00
|
|
|
binfmt_freeenv(envp);
|
2022-04-08 10:10:01 +02:00
|
|
|
|
2022-02-24 08:12:51 +01:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_KERNEL_STACK)
|
2014-09-14 20:40:09 +02:00
|
|
|
/* Allocate the kernel stack */
|
|
|
|
|
|
|
|
ret = up_addrenv_kstackalloc(&tcb->cmn);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-26 11:27:21 +01:00
|
|
|
berr("ERROR: up_addrenv_kstackalloc() failed: %d\n", ret);
|
2014-09-14 22:10:23 +02:00
|
|
|
goto errout_with_tcbinit;
|
2014-09-14 20:40:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-04 14:19:40 +02:00
|
|
|
#ifdef CONFIG_MM_SHM
|
2014-09-24 00:04:39 +02:00
|
|
|
/* Initialize the shared memory virtual page allocator */
|
|
|
|
|
2014-09-24 14:55:26 +02:00
|
|
|
ret = shm_group_initialize(tcb->cmn.group);
|
2014-09-24 00:04:39 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: shm_group_initialize() failed: %d\n", ret);
|
2014-09-24 00:04:39 +02:00
|
|
|
goto errout_with_tcbinit;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-26 20:16:05 +02:00
|
|
|
#ifdef CONFIG_PIC
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Add the D-Space address as the PIC base address. By convention, this
|
|
|
|
* must be the first allocated address space.
|
|
|
|
*/
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
tcb->cmn.dspace = binp->alloc[0];
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Re-initialize the task's initial state to account for the new PIC base */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
up_initial_state(&tcb->cmn);
|
2009-06-17 23:15:31 +02:00
|
|
|
#endif
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2014-08-26 20:16:05 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-08-22 20:32:34 +02:00
|
|
|
/* Assign the address environment to the new task group */
|
2012-12-19 18:54:26 +01:00
|
|
|
|
2014-09-24 00:04:39 +02:00
|
|
|
ret = up_addrenv_clone(&binp->addrenv, &tcb->cmn.group->tg_addrenv);
|
2012-12-19 18:54:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: up_addrenv_clone() failed: %d\n", ret);
|
2014-09-14 19:19:34 +02:00
|
|
|
goto errout_with_tcbinit;
|
2012-12-19 18:54:26 +01:00
|
|
|
}
|
2014-08-27 17:37:28 +02:00
|
|
|
|
|
|
|
/* Mark that this group has an address environment */
|
|
|
|
|
|
|
|
tcb->cmn.group->tg_flags |= GROUP_FLAG_ADDRENV;
|
2012-12-19 18:54:26 +01:00
|
|
|
#endif
|
|
|
|
|
2014-08-26 20:16:05 +02:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2013-01-27 16:52:58 +01:00
|
|
|
/* Setup a start hook that will execute all of the C++ static constructors
|
|
|
|
* on the newly created thread. The struct binary_s must persist at least
|
|
|
|
* until the new task has been started.
|
|
|
|
*/
|
2012-10-29 20:32:05 +01:00
|
|
|
|
2017-10-02 22:04:25 +02:00
|
|
|
if (binp->nctors > 0)
|
|
|
|
{
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
nxtask_starthook(tcb, exec_ctors, (FAR void *)binp);
|
2017-10-02 22:04:25 +02:00
|
|
|
}
|
2012-10-29 20:32:05 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-27 16:52:58 +01:00
|
|
|
/* Get the assigned pid before we start the task */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
pid = tcb->cmn.pid;
|
2013-01-27 16:52:58 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/* Then activate the task at the provided priority */
|
|
|
|
|
2020-06-04 14:19:40 +02:00
|
|
|
nxtask_activate((FAR struct tcb_s *)tcb);
|
2012-10-29 20:32:05 +01:00
|
|
|
|
2014-09-11 18:31:12 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2014-09-10 23:55:36 +02:00
|
|
|
/* Restore the address environment of the caller */
|
|
|
|
|
|
|
|
ret = up_addrenv_restore(&oldenv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-26 11:27:21 +01:00
|
|
|
berr("ERROR: up_addrenv_restore() failed: %d\n", ret);
|
2014-09-14 19:19:34 +02:00
|
|
|
goto errout_with_tcbinit;
|
2014-09-10 23:55:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
return (int)pid;
|
|
|
|
|
2020-06-04 14:19:40 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) || defined(CONFIG_MM_SHM)
|
2014-09-14 19:19:34 +02:00
|
|
|
errout_with_tcbinit:
|
2013-02-04 22:24:00 +01:00
|
|
|
tcb->cmn.stack_alloc_ptr = NULL;
|
2020-05-09 16:04:45 +02:00
|
|
|
nxsched_release_tcb(&tcb->cmn, TCB_FLAG_TTYPE_TASK);
|
2017-10-02 23:30:55 +02:00
|
|
|
return ret;
|
2020-06-04 14:19:40 +02:00
|
|
|
#endif
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2014-09-10 16:41:01 +02:00
|
|
|
errout_with_addrenv:
|
2014-09-11 18:31:12 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2020-01-02 17:49:34 +01:00
|
|
|
up_addrenv_restore(&oldenv);
|
2022-04-04 12:11:06 +02:00
|
|
|
errout_with_envp:
|
2014-09-11 18:31:12 +02:00
|
|
|
#endif
|
2022-04-04 12:11:06 +02:00
|
|
|
binfmt_freeenv(envp);
|
2022-04-08 10:10:01 +02:00
|
|
|
errout_with_args:
|
|
|
|
binfmt_freeargv(argv);
|
2021-05-24 09:06:29 +02:00
|
|
|
errout_with_tcb:
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(tcb);
|
2017-10-02 23:30:55 +02:00
|
|
|
return ret;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|
2009-06-17 23:15:31 +02:00
|
|
|
#endif /* CONFIG_BINFMT_DISABLE */
|