2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/binfmt_execmodule.c
|
|
|
|
*
|
2017-10-02 23:30:55 +02:00
|
|
|
* Copyright (C) 2009, 2013-2014, 2017 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2009-06-17 22:25:27 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2014-09-24 15:29:09 +02:00
|
|
|
#include <nuttx/mm/mm.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
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
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
|
|
|
|
****************************************************************************/
|
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)
|
|
|
|
# errror "CONFIG_SCHED_STARTHOOK must be defined to use constructors"
|
|
|
|
#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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-17 01:30:12 +01:00
|
|
|
int exec_module(FAR const struct binary_s *binp)
|
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;
|
|
|
|
#endif
|
2009-12-15 18:06:59 +01:00
|
|
|
FAR uint32_t *stack;
|
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
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("Executing %s\n", binp->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
|
|
|
}
|
|
|
|
|
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);
|
2014-09-10 01:32:32 +02:00
|
|
|
goto errout_with_tcb;
|
|
|
|
}
|
2014-09-10 23:55:36 +02:00
|
|
|
|
|
|
|
/* Initialize the user heap */
|
|
|
|
|
|
|
|
umm_initialize((FAR void *)CONFIG_ARCH_HEAP_VBASE,
|
|
|
|
up_addrenv_heapsize(&binp->addrenv));
|
2014-09-10 01:32:32 +02:00
|
|
|
#endif
|
|
|
|
|
2014-09-10 23:55:36 +02:00
|
|
|
/* Allocate the stack for the new task.
|
|
|
|
*
|
|
|
|
* REVISIT: This allocation is currently always from the user heap. That
|
|
|
|
* will need to change if/when we want to support dynamic stack allocation.
|
|
|
|
*/
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
stack = (FAR uint32_t *)kumm_malloc(binp->stacksize);
|
2014-09-10 16:41:01 +02:00
|
|
|
if (!stack)
|
2009-06-17 22:25:27 +02:00
|
|
|
{
|
2017-10-02 23:30:55 +02:00
|
|
|
ret = -ENOMEM;
|
2014-09-14 22:10:23 +02:00
|
|
|
goto errout_with_addrenv;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the task */
|
|
|
|
|
2013-02-08 01:17:54 +01:00
|
|
|
ret = task_init((FAR struct tcb_s *)tcb, binp->filename, binp->priority,
|
|
|
|
stack, binp->stacksize, binp->entrypt, binp->argv);
|
2009-06-17 22:25:27 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-02 23:30:55 +02:00
|
|
|
ret = -get_errno();
|
|
|
|
berr("task_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
|
|
|
}
|
|
|
|
|
2014-09-15 18:15:47 +02: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-14 22:10:23 +02:00
|
|
|
|
2014-09-15 18:15:47 +02:00
|
|
|
binfmt_freeargv((FAR struct binary_s *)binp);
|
2014-09-14 22:10:23 +02:00
|
|
|
|
2013-01-16 15:14:14 +01:00
|
|
|
/* Note that tcb->flags are not modified. 0=normal task */
|
|
|
|
/* tcb->flags |= TCB_FLAG_TTYPE_TASK; */
|
|
|
|
|
2014-09-14 20:40:09 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
|
|
|
/* Allocate the kernel stack */
|
|
|
|
|
|
|
|
ret = up_addrenv_kstackalloc(&tcb->cmn);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: up_addrenv_select() 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
|
|
|
|
|
2014-09-24 00:04:39 +02:00
|
|
|
#if defined(CONFIG_BUILD_KERNEL) && defined(CONFIG_MM_SHM)
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
task_starthook(tcb, exec_ctors, (FAR void *)binp);
|
|
|
|
}
|
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 */
|
|
|
|
|
2013-02-08 01:17:54 +01:00
|
|
|
ret = task_activate((FAR struct tcb_s *)tcb);
|
2009-06-17 22:25:27 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-02 23:30:55 +02:00
|
|
|
ret = -get_errno();
|
|
|
|
berr("task_activate() failed: %d\n", ret);
|
2014-09-14 19:19:34 +02:00
|
|
|
goto errout_with_tcbinit;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
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)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: up_addrenv_select() 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;
|
|
|
|
|
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;
|
2013-03-23 20:06:40 +01:00
|
|
|
sched_releasetcb(&tcb->cmn, TCB_FLAG_TTYPE_TASK);
|
2014-09-01 00:15:11 +02:00
|
|
|
kumm_free(stack);
|
2017-10-02 23:30:55 +02:00
|
|
|
return ret;
|
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)
|
2014-09-10 16:41:01 +02:00
|
|
|
(void)up_addrenv_restore(&oldenv);
|
2014-09-14 19:19:34 +02:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
errout_with_tcb:
|
2014-09-11 18:31:12 +02:00
|
|
|
#endif
|
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 */
|