2009-06-18 04:25:27 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/binfmt_execmodule.c
|
|
|
|
*
|
2020-03-27 00:40:10 +08: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-18 04:25:27 +08:00
|
|
|
*
|
2020-03-27 00:40:10 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-18 04:25:27 +08:00
|
|
|
*
|
2020-03-27 00:40:10 +08: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-18 04:25:27 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-16 01:06:59 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
2009-06-18 04:25:27 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2013-01-16 22:14:14 +08:00
|
|
|
#include <nuttx/kmalloc.h>
|
2020-03-27 00:40:10 +08:00
|
|
|
#include <nuttx/sched.h>
|
2014-09-24 21:05:02 +08:00
|
|
|
#include <nuttx/mm/shm.h>
|
2012-10-25 04:19:44 +08:00
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
2009-06-18 04:25:27 +08:00
|
|
|
|
2015-11-14 21:29:47 +08:00
|
|
|
#include "binfmt.h"
|
2009-06-18 04:25:27 +08:00
|
|
|
|
2009-06-18 05:15:31 +08:00
|
|
|
#ifndef CONFIG_BINFMT_DISABLE
|
|
|
|
|
2009-06-18 04:25:27 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2020-01-07 23:54:35 +08:00
|
|
|
|
2013-01-27 23:52:58 +08: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 08:19:18 +08:00
|
|
|
# error "CONFIG_SCHED_STARTHOOK must be defined to use constructors"
|
2013-01-27 23:52:58 +08:00
|
|
|
#endif
|
2009-06-18 04:25:27 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-30 03:32:05 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: exec_ctors
|
|
|
|
*
|
|
|
|
* Description:
|
2013-01-27 23:52:58 +08: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-30 03:32:05 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-08-05 22:09:54 +08:00
|
|
|
* arg - Argument is instance of load state info structure cast to void *.
|
2012-10-30 03:32:05 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2013-01-27 23:52:58 +08:00
|
|
|
static void exec_ctors(FAR void *arg)
|
2012-10-30 03:32:05 +08:00
|
|
|
{
|
2013-01-27 23:52:58 +08:00
|
|
|
FAR const struct binary_s *binp = (FAR const struct binary_s *)arg;
|
2012-10-30 04:43:35 +08:00
|
|
|
binfmt_ctor_t *ctor = binp->ctors;
|
2012-10-30 03:32:05 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Execute each constructor */
|
|
|
|
|
|
|
|
for (i = 0; i < binp->nctors; i++)
|
|
|
|
{
|
2016-06-12 01:59:51 +08:00
|
|
|
binfo("Calling ctor %d at %p\n", i, (FAR void *)ctor);
|
2012-10-30 03:32:05 +08:00
|
|
|
|
|
|
|
(*ctor)();
|
|
|
|
ctor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-18 04:25:27 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: exec_module
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Execute a module that has been loaded into memory by load_module().
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-10-03 05:30:55 +08: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-18 04:25:27 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-17 08:30:12 +08:00
|
|
|
int exec_module(FAR const struct binary_s *binp)
|
2009-06-18 04:25:27 +08:00
|
|
|
{
|
2013-02-05 05:24:00 +08:00
|
|
|
FAR struct task_tcb_s *tcb;
|
2014-09-12 00:31:12 +08:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2014-09-10 07:32:32 +08:00
|
|
|
save_addrenv_t oldenv;
|
|
|
|
#endif
|
2013-02-05 02:46:28 +08:00
|
|
|
pid_t pid;
|
|
|
|
int ret;
|
2009-06-18 04:25:27 +08:00
|
|
|
|
|
|
|
/* Sanity checking */
|
|
|
|
|
2016-06-12 04:14:08 +08:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-10-30 03:32:05 +08:00
|
|
|
if (!binp || !binp->entrypt || binp->stacksize <= 0)
|
2009-06-18 04:25:27 +08:00
|
|
|
{
|
2017-10-03 05:30:55 +08:00
|
|
|
return -EINVAL;
|
2009-06-18 04:25:27 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
binfo("Executing %s\n", binp->filename);
|
2009-06-18 04:25:27 +08:00
|
|
|
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
|
2015-10-09 09:20:17 +08:00
|
|
|
tcb = (FAR struct task_tcb_s *)kmm_zalloc(sizeof(struct task_tcb_s));
|
2009-06-18 04:25:27 +08:00
|
|
|
if (!tcb)
|
|
|
|
{
|
2017-10-03 05:30:55 +08:00
|
|
|
return -ENOMEM;
|
2009-06-18 04:25:27 +08:00
|
|
|
}
|
|
|
|
|
2014-09-15 02:40:09 +08:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2014-09-11 05:55:36 +08:00
|
|
|
/* Instantiate the address environment containing the user heap */
|
|
|
|
|
2014-09-10 07:32:32 +08:00
|
|
|
ret = up_addrenv_select(&binp->addrenv, &oldenv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("ERROR: up_addrenv_select() failed: %d\n", ret);
|
2014-09-10 07:32:32 +08:00
|
|
|
goto errout_with_tcb;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-29 16:26:48 +08:00
|
|
|
/* Note that tcb->flags are not modified. 0=normal task */
|
2009-06-18 04:25:27 +08:00
|
|
|
|
2020-06-29 16:26:48 +08:00
|
|
|
/* tcb->flags |= TCB_FLAG_TTYPE_TASK; */
|
2009-06-18 04:25:27 +08:00
|
|
|
|
|
|
|
/* Initialize the task */
|
|
|
|
|
2020-06-29 15:52:16 +08:00
|
|
|
ret = nxtask_init(tcb, binp->filename, binp->priority,
|
2020-06-29 16:26:48 +08:00
|
|
|
NULL, binp->stacksize, binp->entrypt, binp->argv);
|
2009-06-18 04:25:27 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-05-26 05:30:40 +08:00
|
|
|
berr("nxtask_init() failed: %d\n", ret);
|
2014-09-15 04:10:23 +08:00
|
|
|
goto errout_with_addrenv;
|
2009-06-18 04:25:27 +08:00
|
|
|
}
|
|
|
|
|
2014-09-16 00:15:47 +08:00
|
|
|
/* We can free the argument buffer now.
|
|
|
|
* REVISIT: It is good to free up memory as soon as possible, but
|
|
|
|
* unfortunately here 'binp' is 'const'. So to do this properly, we will
|
|
|
|
* have to make some more extensive changes.
|
|
|
|
*/
|
2014-09-15 04:10:23 +08:00
|
|
|
|
2014-09-16 00:15:47 +08:00
|
|
|
binfmt_freeargv((FAR struct binary_s *)binp);
|
2014-09-15 04:10:23 +08:00
|
|
|
|
2014-09-15 02:40:09 +08:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
|
|
|
/* Allocate the kernel stack */
|
|
|
|
|
|
|
|
ret = up_addrenv_kstackalloc(&tcb->cmn);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-26 18:27:21 +08:00
|
|
|
berr("ERROR: up_addrenv_kstackalloc() failed: %d\n", ret);
|
2014-09-15 04:10:23 +08:00
|
|
|
goto errout_with_tcbinit;
|
2014-09-15 02:40:09 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-04 20:19:40 +08:00
|
|
|
#ifdef CONFIG_MM_SHM
|
2014-09-24 06:04:39 +08:00
|
|
|
/* Initialize the shared memory virtual page allocator */
|
|
|
|
|
2014-09-24 20:55:26 +08:00
|
|
|
ret = shm_group_initialize(tcb->cmn.group);
|
2014-09-24 06:04:39 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("ERROR: shm_group_initialize() failed: %d\n", ret);
|
2014-09-24 06:04:39 +08:00
|
|
|
goto errout_with_tcbinit;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-27 02:16:05 +08:00
|
|
|
#ifdef CONFIG_PIC
|
2012-10-30 03:32:05 +08:00
|
|
|
/* Add the D-Space address as the PIC base address. By convention, this
|
|
|
|
* must be the first allocated address space.
|
|
|
|
*/
|
2009-06-18 04:25:27 +08:00
|
|
|
|
2013-02-05 05:24:00 +08:00
|
|
|
tcb->cmn.dspace = binp->alloc[0];
|
2009-06-18 04:25:27 +08:00
|
|
|
|
|
|
|
/* Re-initialize the task's initial state to account for the new PIC base */
|
|
|
|
|
2013-02-05 05:24:00 +08:00
|
|
|
up_initial_state(&tcb->cmn);
|
2009-06-18 05:15:31 +08:00
|
|
|
#endif
|
2009-06-18 04:25:27 +08:00
|
|
|
|
2014-08-27 02:16:05 +08:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-08-23 02:32:34 +08:00
|
|
|
/* Assign the address environment to the new task group */
|
2012-12-20 01:54:26 +08:00
|
|
|
|
2014-09-24 06:04:39 +08:00
|
|
|
ret = up_addrenv_clone(&binp->addrenv, &tcb->cmn.group->tg_addrenv);
|
2012-12-20 01:54:26 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("ERROR: up_addrenv_clone() failed: %d\n", ret);
|
2014-09-15 01:19:34 +08:00
|
|
|
goto errout_with_tcbinit;
|
2012-12-20 01:54:26 +08:00
|
|
|
}
|
2014-08-27 23:37:28 +08:00
|
|
|
|
|
|
|
/* Mark that this group has an address environment */
|
|
|
|
|
|
|
|
tcb->cmn.group->tg_flags |= GROUP_FLAG_ADDRENV;
|
2012-12-20 01:54:26 +08:00
|
|
|
#endif
|
|
|
|
|
2014-08-27 02:16:05 +08:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2013-01-27 23:52:58 +08: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-30 03:32:05 +08:00
|
|
|
|
2017-10-03 04:04:25 +08: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-05 03:42:51 +08:00
|
|
|
nxtask_starthook(tcb, exec_ctors, (FAR void *)binp);
|
2017-10-03 04:04:25 +08:00
|
|
|
}
|
2012-10-30 03:32:05 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-27 23:52:58 +08:00
|
|
|
/* Get the assigned pid before we start the task */
|
|
|
|
|
2013-02-05 05:24:00 +08:00
|
|
|
pid = tcb->cmn.pid;
|
2013-01-27 23:52:58 +08:00
|
|
|
|
2009-06-18 04:25:27 +08:00
|
|
|
/* Then activate the task at the provided priority */
|
|
|
|
|
2020-06-04 20:19:40 +08:00
|
|
|
nxtask_activate((FAR struct tcb_s *)tcb);
|
2012-10-30 03:32:05 +08:00
|
|
|
|
2014-09-12 00:31:12 +08:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2014-09-11 05:55:36 +08:00
|
|
|
/* Restore the address environment of the caller */
|
|
|
|
|
|
|
|
ret = up_addrenv_restore(&oldenv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-26 18:27:21 +08:00
|
|
|
berr("ERROR: up_addrenv_restore() failed: %d\n", ret);
|
2014-09-15 01:19:34 +08:00
|
|
|
goto errout_with_tcbinit;
|
2014-09-11 05:55:36 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-18 04:25:27 +08:00
|
|
|
return (int)pid;
|
|
|
|
|
2020-06-04 20:19:40 +08:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) || defined(CONFIG_MM_SHM)
|
2014-09-15 01:19:34 +08:00
|
|
|
errout_with_tcbinit:
|
2013-02-05 05:24:00 +08:00
|
|
|
tcb->cmn.stack_alloc_ptr = NULL;
|
2020-05-09 22:04:45 +08:00
|
|
|
nxsched_release_tcb(&tcb->cmn, TCB_FLAG_TTYPE_TASK);
|
2017-10-03 05:30:55 +08:00
|
|
|
return ret;
|
2020-06-04 20:19:40 +08:00
|
|
|
#endif
|
2009-06-18 04:25:27 +08:00
|
|
|
|
2014-09-10 22:41:01 +08:00
|
|
|
errout_with_addrenv:
|
2014-09-12 00:31:12 +08:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2020-01-03 00:49:34 +08:00
|
|
|
up_addrenv_restore(&oldenv);
|
2014-09-15 01:19:34 +08:00
|
|
|
|
2009-06-18 04:25:27 +08:00
|
|
|
errout_with_tcb:
|
2014-09-12 00:31:12 +08:00
|
|
|
#endif
|
2014-09-01 07:04:02 +08:00
|
|
|
kmm_free(tcb);
|
2017-10-03 05:30:55 +08:00
|
|
|
return ret;
|
2009-06-18 04:25:27 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 05:15:31 +08:00
|
|
|
#endif /* CONFIG_BINFMT_DISABLE */
|