2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/binfmt_execmodule.c
|
|
|
|
*
|
2014-08-22 20:32:34 +02:00
|
|
|
* Copyright (C) 2009, 2013-2014 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>
|
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"
|
2009-06-17 22:25:27 +02:00
|
|
|
#include "binfmt_internal.h"
|
|
|
|
|
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 Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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:
|
|
|
|
* loadinfo - Load state information
|
|
|
|
*
|
|
|
|
* 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++)
|
|
|
|
{
|
|
|
|
bvdbg("Calling ctor %d at %p\n", i, (FAR void *)ctor);
|
|
|
|
|
|
|
|
(*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:
|
|
|
|
* This is an end-user function, so it follows the normal convention:
|
2013-01-27 16:52:58 +01:00
|
|
|
* Returns the PID of the exec'ed module. On failure, it returns
|
2009-06-17 22:25:27 +02:00
|
|
|
* -1 (ERROR) and sets errno appropriately.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
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;
|
2009-06-17 22:25:27 +02:00
|
|
|
#ifndef CONFIG_CUSTOM_STACK
|
2009-12-15 18:06:59 +01:00
|
|
|
FAR uint32_t *stack;
|
2009-06-17 22:25:27 +02:00
|
|
|
#endif
|
2013-02-04 19:46:28 +01:00
|
|
|
pid_t pid;
|
|
|
|
int err;
|
|
|
|
int ret;
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Sanity checking */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
2012-10-29 20:32:05 +01:00
|
|
|
if (!binp || !binp->entrypt || binp->stacksize <= 0)
|
2009-06-17 22:25:27 +02:00
|
|
|
{
|
|
|
|
err = EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
bdbg("Executing %s\n", binp->filename);
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
tcb = (FAR struct task_tcb_s*)kzalloc(sizeof(struct task_tcb_s));
|
2009-06-17 22:25:27 +02:00
|
|
|
if (!tcb)
|
|
|
|
{
|
|
|
|
err = ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2013-03-20 19:22:21 +01:00
|
|
|
/* Allocate the stack for the new task (always from the user heap) */
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_CUSTOM_STACK
|
2013-03-09 22:12:20 +01:00
|
|
|
stack = (FAR uint32_t*)kumalloc(binp->stacksize);
|
2009-06-17 22:25:27 +02:00
|
|
|
if (!tcb)
|
|
|
|
{
|
|
|
|
err = ENOMEM;
|
|
|
|
goto errout_with_tcb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
#else
|
|
|
|
/* 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->entrypt, binp->argv);
|
2009-06-17 22:25:27 +02:00
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
err = errno;
|
2009-12-20 14:34:49 +01:00
|
|
|
bdbg("task_init() failed: %d\n", err);
|
2009-06-17 22:25:27 +02:00
|
|
|
goto errout_with_stack;
|
|
|
|
}
|
|
|
|
|
2013-01-16 15:14:14 +01:00
|
|
|
/* Note that tcb->flags are not modified. 0=normal task */
|
|
|
|
/* tcb->flags |= TCB_FLAG_TTYPE_TASK; */
|
|
|
|
|
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
|
|
|
|
2009-06-17 23:15:31 +02:00
|
|
|
#ifdef CONFIG_PIC
|
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-22 20:32:34 +02:00
|
|
|
/* Assign the address environment to the new task group */
|
2012-12-19 18:54:26 +01:00
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-08-24 19:54:14 +02:00
|
|
|
ret = up_addrenv_assign(&binp->addrenv, tcb->cmn.group);
|
2012-12-19 18:54:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
err = -ret;
|
2014-08-24 19:54:14 +02:00
|
|
|
bdbg("ERROR: up_addrenv_assign() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
goto errout_with_stack;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2013-01-27 16:52:58 +01:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
err = errno;
|
2009-12-20 14:34:49 +01:00
|
|
|
bdbg("task_activate() failed: %d\n", err);
|
2009-06-17 22:25:27 +02:00
|
|
|
goto errout_with_stack;
|
|
|
|
}
|
2012-10-29 20:32:05 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
return (int)pid;
|
|
|
|
|
|
|
|
errout_with_stack:
|
|
|
|
#ifndef CONFIG_CUSTOM_STACK
|
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);
|
2013-03-09 22:12:20 +01:00
|
|
|
kufree(stack);
|
2009-06-17 22:25:27 +02:00
|
|
|
#else
|
2013-03-23 20:06:40 +01:00
|
|
|
sched_releasetcb(&tcb->cmn, TCB_FLAG_TTYPE_TASK);
|
2009-06-17 22:25:27 +02:00
|
|
|
#endif
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
errout_with_tcb:
|
2013-01-16 15:14:14 +01:00
|
|
|
kfree(tcb);
|
2009-06-17 22:25:27 +02:00
|
|
|
errout:
|
|
|
|
errno = err;
|
2009-12-20 14:34:49 +01:00
|
|
|
bdbg("returning errno: %d\n", err);
|
2009-06-17 22:25:27 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2009-06-17 23:15:31 +02:00
|
|
|
#endif /* CONFIG_BINFMT_DISABLE */
|
2009-06-17 22:25:27 +02:00
|
|
|
|