9
0
Fork 0

Add exec_module

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@1895 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2009-06-17 20:25:27 +00:00
parent adee7d3281
commit fe5083b49a
14 changed files with 490 additions and 82 deletions

View File

@ -784,5 +784,11 @@
CONFIG_FDCLONE_DISABLE, CONFIG_FDCLONE_STDIO, CONFIG_SDCLONE_DISABLE.
* Use of C++ reserved word 'private' in C header files causes problems
for C++ that include them.
* Added 'binfmt' support to allow execution of programs in a file system,
binding to NuttX symbols. A custom format call NXFLAT is used; this
derives from http://xflat.sourceforge.net. At present is supports on
XIP execution from ROMFS file systems. Initial check-in is untested
and probably breaks many builds.

View File

@ -8,7 +8,7 @@
<tr align="center" bgcolor="#e4e4e4">
<td>
<h1><big><font color="#3c34ec"><i>NuttX RTOS</i></font></big></h1>
<p>Last Updated: June 16, 2009</p>
<p>Last Updated: June 17, 2009</p>
</td>
</tr>
</table>
@ -1476,6 +1476,11 @@ nuttx-0.4.9 2009-xx-xx Gregory Nutt &lt;spudmonkey@racsa.co.cr&gt;
via task_create().
* Use of C++ reserved word 'private' in C header files causes problems
for C++ that include them.
* Added 'binfmt' support to allow execution of programs in a file system,
binding to NuttX symbols. A custom format call NXFLAT is used; this
derives from http://xflat.sourceforge.net. At present is supports on
XIP execution from ROMFS file systems. Initial check-in is untested
and probably breaks many builds.
pascal-0.1.3 2009-xx-xx Gregory Nutt &lt;spudmonkey@racsa.co.cr&gt;

View File

@ -35,6 +35,8 @@
-include $(TOPDIR)/Make.defs
CFLAGS += -I$(TOPDIR)/sched
ifeq ($(CONFIG_NXFLAT),y)
include libnxflat/Make.defs
LIBNXFLAT_CSRCS += nxflat.c
@ -42,7 +44,8 @@ endif
BINFMT_ASRCS =
BINFMT_CSRCS = binfmt_globals.c binfmt_register.c binfmt_unregister.c \
binfmt_loadmodule.c binfmt_unloadmodule.c binfmt_dumpmodule.c
binfmt_loadmodule.c binfmt_unloadmodule.c binfmt_execmodule.c \
binfmt_dumpmodule.c
SUBDIRS = libnxflat

View File

@ -76,6 +76,11 @@
* Description:
* Load a module into memory and prep it for execution.
*
* Returned Value:
* 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.
*
***********************************************************************/
int dump_module(FAR const struct binary_s *bin)

View File

@ -0,0 +1,184 @@
/****************************************************************************
* binfmt/binfmt_execmodule.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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>
#include <sys/types.h>
#include <stdlib.h>
#include <sched.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/binfmt.h>
#include "os_internal.h"
#include "binfmt_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* 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:
* Returns the PID of the exec'ed module. On failure, it.returns
* -1 (ERROR) and sets errno appropriately.
*
****************************************************************************/
int exec_module(FAR const struct binary_s *bin, int priority)
{
FAR _TCB *tcb;
#ifndef CONFIG_CUSTOM_STACK
FAR uint32 *stack;
#endif
pid_t pid;
int err;
int ret;
/* Sanity checking */
#ifdef CONFIG_DEBUG
if (!bin || !bin->ispace || !bin->entrypt || bin->stacksize <= 0)
{
err = EINVAL;
goto errout;
}
#endif
bdbg("Executing %s\n", bin->filename);
/* Allocate a TCB for the new task. */
tcb = (FAR _TCB*)zalloc(sizeof(_TCB));
if (!tcb)
{
err = ENOMEM;
goto errout;
}
/* Allocate the stack for the new task */
#ifndef CONFIG_CUSTOM_STACK
stack = (FAR uint32*)malloc(bin->stacksize);
if (!tcb)
{
err = ENOMEM;
goto errout_with_tcb;
}
/* Initialize the task */
ret = task_init(tcb, bin->filename, priority, stack, bin->stacksize, bin->entrypt, bin->argv);
#else
/* Initialize the task */
ret = task_init(tcb, bin->filename, priority, stack, bin->entrypt, bin->argv);
#endif
if (ret < 0)
{
err = errno;
dbg("task_init() failed: %d\n", err);
goto errout_with_stack;
}
/* Add the DSpace address as the PIC base address */
tcb->picbase = bin->dspace;
/* Re-initialize the task's initial state to account for the new PIC base */
up_initial_state(tcb);
/* Get the assigned pid before we start the task */
pid = tcb->pid;
/* Then activate the task at the provided priority */
ret = task_activate(tcb);
if (ret < 0)
{
err = errno;
dbg("task_activate() failed: %d\n", err);
goto errout_with_stack;
}
return (int)pid;
errout_with_stack:
#ifndef CONFIG_CUSTOM_STACK
tcb->stack_alloc_ptr = NULL;
sched_releasetcb(tcb);
free(stack);
#else
sched_releasetcb(tcb);
#endif
goto errout;
errout_with_tcb:
free(tcb);
errout:
errno = err;
dbg("returning errno: %d\n", err);
return ERROR;
}

View File

@ -64,17 +64,22 @@
* Private Functions
****************************************************************************/
/***********************************************************************
/****************************************************************************
* Public Functions
***********************************************************************/
****************************************************************************/
/***********************************************************************
/****************************************************************************
* Name: load_module
*
* Description:
* Load a module into memory and prep it for execution.
*
***********************************************************************/
* Returned Value:
* This is an end-user function, so it follows the normal convention:
* Returns 0 (OK) on success. On failure, it returns -1 (ERROR) with
* errno set appropriately.
*
****************************************************************************/
int load_module(const char *filename, FAR struct binary_s *bin)
{
@ -120,8 +125,15 @@ int load_module(const char *filename, FAR struct binary_s *bin)
sched_unlock();
}
if (ret < 0) bdbg("Returning %d\n", ret);
return ret;
/* This is an end-user function. Return failures via errno */
if (ret < 0)
{
bdbg("Returning errno %d\n", -ret);
errno = -ret;
return ERROR;
}
return OK;
}

View File

@ -65,17 +65,22 @@
* Private Functions
****************************************************************************/
/***********************************************************************
/****************************************************************************
* Public Functions
***********************************************************************/
****************************************************************************/
/***********************************************************************
/****************************************************************************
* Name: register_binfmt
*
* Description:
* Register a loader for a binary format
*
***********************************************************************/
* Returned Value:
* 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.
*
****************************************************************************/
int register_binfmt(FAR struct binfmt_s *binfmt)
{

View File

@ -66,18 +66,23 @@
* Private Functions
****************************************************************************/
/***********************************************************************
/****************************************************************************
* Public Functions
***********************************************************************/
****************************************************************************/
/***********************************************************************
/****************************************************************************
* Name: unload_module
*
* Description:
* Unload a (non-executing) module from memory. If the module has
* been started (via exec_module), calling this will be fatal.
*
***********************************************************************/
* Returned Value:
* 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.
*
****************************************************************************/
int unload_module(FAR const struct binary_s *bin)
{

View File

@ -65,17 +65,22 @@
* Private Functions
****************************************************************************/
/***********************************************************************
/****************************************************************************
* Public Functions
***********************************************************************/
****************************************************************************/
/***********************************************************************
/****************************************************************************
* Name: unregister_binfmt
*
* Description:
* Register a loader for a binary format
*
***********************************************************************/
* Returned Value:
* 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.
*
****************************************************************************/
int unregister_binfmt(FAR struct binfmt_s *binfmt)
{
@ -124,6 +129,7 @@ int unregister_binfmt(FAR struct binfmt_s *binfmt)
sched_unlock();
}
return ret;
}

View File

@ -197,6 +197,11 @@ static int nxflat_loadbinary(struct binary_s *binp)
* use this binary format, this function must be called during system
* format in order to register the NXFLAT binary format.
*
* Returned Value:
* 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.
*
***********************************************************************/
int nxflat_initialize(void)
@ -216,6 +221,13 @@ int nxflat_initialize(void)
/****************************************************************************
* Name: nxflat_uninitialize
*
* Description:
* Unregister the NXFLAT binary loader
*
* Returned Value:
* None
*
****************************************************************************/
void nxflat_uninitialize(void)

View File

@ -90,25 +90,81 @@ extern "C" {
#define EXTERN extern
#endif
/* Register a binary format handler */
/****************************************************************************
* Name: register_binfmt
*
* Description:
* Register a loader for a binary format
*
* Returned Value:
* 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.
*
****************************************************************************/
EXTERN int register_binfmt(FAR struct binfmt_s *binfmt);
/* Unregister a binary format handler */
/****************************************************************************
* Name: unregister_binfmt
*
* Description:
* Register a loader for a binary format
*
* Returned Value:
* 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.
*
****************************************************************************/
EXTERN int unregister_binfmt(FAR struct binfmt_s *binfmt);
/* Load a module into memory */
/****************************************************************************
* Name: load_module
*
* Description:
* Load a module into memory and prep it for execution.
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
* Returns 0 (OK) on success. On failure, it returns -1 (ERROR) with
* errno set appropriately.
*
****************************************************************************/
EXTERN int load_module(const char *filename, FAR struct binary_s *bin);
/* Unload a (non-running) module from memory */
/****************************************************************************
* Name: unload_module
*
* Description:
* Unload a (non-executing) module from memory. If the module has
* been started (via exec_module), calling this will be fatal.
*
* Returned Value:
* 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.
*
****************************************************************************/
EXTERN int unload_module(FAR const struct binary_s *bin);
/* Execute a module that has been loaded into memory */
/****************************************************************************
* 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:
* Returns the PID of the exec'ed module. On failure, it.returns
* -1 (ERROR) and sets errno appropriately.
*
****************************************************************************/
EXTERN int exec_module(FAR const struct binary_s *bin);
EXTERN int exec_module(FAR const struct binary_s *bin, int priority);
#undef EXTERN
#if defined(__cplusplus)

View File

@ -100,43 +100,134 @@ extern "C" {
#define EXTERN extern
#endif
/* Given the header from a possible NXFLAT executable, verify that it
* is an NXFLAT executable.
*/
/****************************************************************************
* These are APIs exported by libnxflat (and may be used outside of NuttX):
****************************************************************************/
/***********************************************************************
* Name:
*
* Description:
* Given the header from a possible NXFLAT executable, verify that it
* is an NXFLAT executable.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
***********************************************************************/
EXTERN int nxflat_verifyheader(const struct nxflat_hdr_s *header);
/* This function is called to configure the library to process an NXFLAT
* program binary.
*/
/***********************************************************************
* Name:
*
* Description:
* This function is called to configure the library to process an NXFLAT
* program binary.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
***********************************************************************/
EXTERN int nxflat_init(const char *filename,
struct nxflat_hdr_s *header,
EXTERN int nxflat_init(const char *filename, struct nxflat_hdr_s *header,
struct nxflat_loadinfo_s *loadinfo);
/* Releases any resources committed by nxflat_init(). This essentially
* undoes the actions of nxflat_init.
*/
/***********************************************************************
* Name:
*
* Description:
* Releases any resources committed by nxflat_init(). This essentially
* undoes the actions of nxflat_init.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
***********************************************************************/
EXTERN int nxflat_uninit(struct nxflat_loadinfo_s *loadinfo);
/* Loads the binary specified by nxflat_init into memory,
* Completes all relocations, and clears BSS.
*/
/***********************************************************************
* Name:
*
* Description:
* Loads the binary specified by nxflat_init into memory,
* Completes all relocations, and clears BSS.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
***********************************************************************/
EXTERN int nxflat_load(struct nxflat_loadinfo_s *loadinfo);
/* Read 'readsize' bytes from the object file at 'offset' */
/***********************************************************************
* Name:
*
* Description:
* Read 'readsize' bytes from the object file at 'offset'
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
***********************************************************************/
EXTERN int nxflat_read(struct nxflat_loadinfo_s *loadinfo, char *buffer,
int readsize, int offset);
/* This function unloads the object from memory. This essentially
* undoes the actions of nxflat_load.
*/
/***********************************************************************
* Name:
*
* Description:
* This function unloads the object from memory. This essentially
* undoes the actions of nxflat_load.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
***********************************************************************/
EXTERN int nxflat_unload(struct nxflat_loadinfo_s *loadinfo);
/****************************************************************************
* These are APIs used internally only by NuttX:
****************************************************************************/
/***********************************************************************
* Name: nxflat_initialize
*
* Description:
* NXFLAT support is built unconditionally. However, it order to
* use this binary format, this function must be called during system
* format in order to register the NXFLAT binary format.
*
* Returned Value:
* 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.
*
***********************************************************************/
EXTERN int nxflat_initialize(void);
/****************************************************************************
* Name: nxflat_uninitialize
*
* Description:
* Unregister the NXFLAT binary loader
*
* Returned Value:
* None
*
****************************************************************************/
EXTERN void nxflat_uninitialize(void);
#undef EXTERN
#if defined(__cplusplus)
}

View File

@ -1,7 +1,7 @@
/****************************************************************************
* task_create.c
*
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without

View File

@ -1,7 +1,7 @@
/************************************************************
/****************************************************************************
* task_init.c
*
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
@ -14,7 +14,7 @@
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
* 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.
*
@ -31,56 +31,60 @@
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
************************************************************/
****************************************************************************/
/************************************************************
/****************************************************************************
* Included Files
************************************************************/
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sched.h>
#include <nuttx/arch.h>
#include "os_internal.h"
#include "env_internal.h"
/************************************************************
/****************************************************************************
* Definitions
************************************************************/
****************************************************************************/
/************************************************************
/****************************************************************************
* Private Type Declarations
************************************************************/
****************************************************************************/
/************************************************************
/****************************************************************************
* Global Variables
************************************************************/
****************************************************************************/
/************************************************************
/****************************************************************************
* Private Variables
************************************************************/
****************************************************************************/
/************************************************************
/****************************************************************************
* Private Function Prototypes
************************************************************/
****************************************************************************/
/************************************************************
/****************************************************************************
* Private Functions
************************************************************/
****************************************************************************/
/************************************************************
/****************************************************************************
* Public Functions
************************************************************/
****************************************************************************/
/************************************************************
/****************************************************************************
* Name: task_init
*
* Description:
* 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()
* 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()
*
* Unlike task_create(), task_init() does not activate the
* task. This must be done by calling task_activate().
* Unlike task_create():
* 1. Allocate the TCB. The pre-allocated TCB is passed in the arguments.
* 2. Allocate the stack. The pre-allocated stack is passed in the arguments.
* 3. Activate the task. This must be done by calling task_activate().
*
* Input Parameters:
* tcb - Address of the new task's TCB
@ -89,19 +93,19 @@
* stack - Start of the pre-allocated stack
* stack_size - Size (in bytes) of the stack allocated
* entry - Application start point of the new task
* arg - A pointer to an array of input parameters.
* Up to CONFIG_MAX_TASK_ARG parameters may
* be provided. If fewer than CONFIG_MAX_TASK_ARG
* parameters are passed, the list should be
* terminated with a NULL argv[] value.
* If no parameters are required, argv may be
* NULL.
* arg - A pointer to an array of input parameters. Up to
* CONFIG_MAX_TASK_ARG parameters may be provided. If fewer
* than CONFIG_MAX_TASK_ARG parameters are passed, the list
* should be terminated with a NULL argv[] value. If no
* parameters are required, argv may be NULL.
*
* Return Value:
* OK on success; ERROR on failure. (See task_schedsetup()
* for possible failure conditions).
* OK on success; ERROR on failure. (See task_schedsetup() for possible
* failure conditions). On failure, the caller is responsible for freeing
* the stack memory and for calling sched_releasetcb() to free the TCB
* (which could be in most any state).
*
************************************************************/
****************************************************************************/
#ifndef CONFIG_CUSTOM_STACK
STATUS task_init(FAR _TCB *tcb, const char *name, int priority,
@ -114,6 +118,19 @@ STATUS task_init(FAR _TCB *tcb, const char *name, int priority,
{
STATUS ret;
/* Associate file descriptors with the new task */
#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0
if (sched_setuptaskfiles(tcb) != OK)
{
return ERROR;
}
#endif
/* Clone the parent's task environment */
(void)env_dup(tcb);
/* Configure the user provided stack region */
#ifndef CONFIG_CUSTOM_STACK
@ -130,4 +147,5 @@ STATUS task_init(FAR _TCB *tcb, const char *name, int priority,
(void)task_argsetup(tcb, name, argv);
}
return ret;
}
}