diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-06-17 20:25:27 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-06-17 20:25:27 +0000 |
commit | d8ee4cfaf8afacfe8ff455900ed391af4f642888 (patch) | |
tree | d477c2d0bde57c71a78722de873a78b035357c75 /nuttx/binfmt | |
parent | 7a34357c62dd8c1910f444d37f80ed18425b7fad (diff) | |
download | px4-nuttx-d8ee4cfaf8afacfe8ff455900ed391af4f642888.tar.gz px4-nuttx-d8ee4cfaf8afacfe8ff455900ed391af4f642888.tar.bz2 px4-nuttx-d8ee4cfaf8afacfe8ff455900ed391af4f642888.zip |
Add exec_module
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1895 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/binfmt')
-rw-r--r-- | nuttx/binfmt/Makefile | 5 | ||||
-rw-r--r-- | nuttx/binfmt/binfmt_dumpmodule.c | 5 | ||||
-rw-r--r-- | nuttx/binfmt/binfmt_execmodule.c | 184 | ||||
-rw-r--r-- | nuttx/binfmt/binfmt_loadmodule.c | 24 | ||||
-rw-r--r-- | nuttx/binfmt/binfmt_register.c | 13 | ||||
-rw-r--r-- | nuttx/binfmt/binfmt_unloadmodule.c | 13 | ||||
-rw-r--r-- | nuttx/binfmt/binfmt_unregister.c | 14 | ||||
-rw-r--r-- | nuttx/binfmt/nxflat.c | 12 |
8 files changed, 251 insertions, 19 deletions
diff --git a/nuttx/binfmt/Makefile b/nuttx/binfmt/Makefile index 4b3905e06..015981d4e 100644 --- a/nuttx/binfmt/Makefile +++ b/nuttx/binfmt/Makefile @@ -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 diff --git a/nuttx/binfmt/binfmt_dumpmodule.c b/nuttx/binfmt/binfmt_dumpmodule.c index 5b7bd1fe9..190889d64 100644 --- a/nuttx/binfmt/binfmt_dumpmodule.c +++ b/nuttx/binfmt/binfmt_dumpmodule.c @@ -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) diff --git a/nuttx/binfmt/binfmt_execmodule.c b/nuttx/binfmt/binfmt_execmodule.c new file mode 100644 index 000000000..21c1bf6ac --- /dev/null +++ b/nuttx/binfmt/binfmt_execmodule.c @@ -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; +} + + diff --git a/nuttx/binfmt/binfmt_loadmodule.c b/nuttx/binfmt/binfmt_loadmodule.c index 37910ae7c..b73b6223e 100644 --- a/nuttx/binfmt/binfmt_loadmodule.c +++ b/nuttx/binfmt/binfmt_loadmodule.c @@ -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; } diff --git a/nuttx/binfmt/binfmt_register.c b/nuttx/binfmt/binfmt_register.c index 309c081e6..2323062ec 100644 --- a/nuttx/binfmt/binfmt_register.c +++ b/nuttx/binfmt/binfmt_register.c @@ -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) { diff --git a/nuttx/binfmt/binfmt_unloadmodule.c b/nuttx/binfmt/binfmt_unloadmodule.c index f5d7ec92c..ff745171a 100644 --- a/nuttx/binfmt/binfmt_unloadmodule.c +++ b/nuttx/binfmt/binfmt_unloadmodule.c @@ -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) { diff --git a/nuttx/binfmt/binfmt_unregister.c b/nuttx/binfmt/binfmt_unregister.c index 1eedffcfc..62c156a61 100644 --- a/nuttx/binfmt/binfmt_unregister.c +++ b/nuttx/binfmt/binfmt_unregister.c @@ -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; } diff --git a/nuttx/binfmt/nxflat.c b/nuttx/binfmt/nxflat.c index 711be9353..94b15a32c 100644 --- a/nuttx/binfmt/nxflat.c +++ b/nuttx/binfmt/nxflat.c @@ -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) |