diff --git a/ChangeLog b/ChangeLog index 509efaf51ea..b79a3add18f 100755 --- a/ChangeLog +++ b/ChangeLog @@ -11237,4 +11237,5 @@ just the ELF module support with name changes (2015-12-10). * configs/samv71-xult/module: Add configuration for testing OS modules (2015-12-12). + * sched/module: Add an implementation of rmmod() (2015-11-12). diff --git a/include/nuttx/module.h b/include/nuttx/module.h index 3e4c028a300..e08c92e5925 100644 --- a/include/nuttx/module.h +++ b/include/nuttx/module.h @@ -76,25 +76,43 @@ * Public Types ****************************************************************************/ +/* This is the type of the function that is called to uninitialize the + * the loaded module. This may mean, for example, un-registering a device + * driver. If the module is successfully initialized, its memory will be + * deallocated. + * + * Input Parameters: + * arg - An opaque argument that was previously returned by the initializer + * function. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on any failure to + * initialize the module. If zero is returned, then the module memory + * will be deallocated. If the module is still in use (for example with + * open driver instances), the uninitialization function should fail with + * -EBUSY + */ + +typedef CODE int (*mod_uninitializer_t)(FAR void *arg); + /* A NuttX module is expected to export a function called module_initialize() * that has the following function prototype. This function should appear as * the entry point in the ELF module file and will be called bythe binfmt * logic after the module has been loaded into kernel memory. * - * As an alternative using GCC, the module may mark a function with the - * "constructor" attribute and the module initializer will be called along - * with any other C++ constructors. The "destructor" attribute may also - * be used to mark an module uninitialization function. - * * Input Parameters: - * None + * uninitializer - The pointer to the uninitialization function. NULL may + * be returned if no uninitialization is needed (i.e, the the module + * memory can be deallocated at any time). + * arg - An argument that will be passed to the uninitialization function. * * Returned Value: * Zero (OK) on success; a negated errno value on any failure to * initialize the module. */ -typedef CODE int (*mod_initializer_t)(void); +typedef CODE int (*mod_initializer_t)(mod_uninitializer_t *uninitializer, + FAR void **arg); /* This describes the file to be loaded. */ @@ -111,7 +129,8 @@ struct module_s * resources used by the loaded module. */ - mod_initializer_t initializer; /* Module initializer function */ + mod_uninitializer_t uninitializer; /* Module initializer function */ + FAR void *arg; /* Uninitializer argument */ FAR void *alloc; /* Allocated kernel memory */ }; @@ -139,6 +158,16 @@ extern "C" int insmod(FAR struct module_s *modp); +/**************************************************************************** + * Name: rmmod + * + * Description: + * Remove a previously installed module from memory. + * + ****************************************************************************/ + +int rmmod(FAR struct module_s *modp); + #undef EXTERN #if defined(__cplusplus) } diff --git a/sched/module/Make.defs b/sched/module/Make.defs index 9a0bee54985..fbe5fffca6c 100644 --- a/sched/module/Make.defs +++ b/sched/module/Make.defs @@ -37,7 +37,7 @@ ifeq ($(CONFIG_MODULE),y) # OS module interfaces -CSRCS += mod_insmod.c +CSRCS += mod_insmod.c mod_rmmod.c # loadable module library diff --git a/sched/module/mod_insmod.c b/sched/module/mod_insmod.c index f06961811fc..e27cfe38486 100644 --- a/sched/module/mod_insmod.c +++ b/sched/module/mod_insmod.c @@ -1,5 +1,5 @@ /**************************************************************************** - * sched/module/module.c + * sched/module/mod_insmod.c * * Copyright (C) 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -46,8 +46,6 @@ #include #include -#include - #include #include @@ -172,8 +170,10 @@ static void mod_dumpinitializer(mod_initializer_t initializer, int insmod(FAR struct module_s *modp) { struct mod_loadinfo_s loadinfo; + mod_initializer_t initializer; int ret; + DEBUGASSERT(modp != NULL && modp->filename != NULL); bvdbg("Loading file: %s\n", modp->filename); /* Initialize the ELF library to load the program binary. */ @@ -207,23 +207,20 @@ int insmod(FAR struct module_s *modp) /* Return the load information */ - modp->initializer = (mod_initializer_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry); modp->alloc = (FAR void *)loadinfo.textalloc; /* Get the module initializer entry point */ - if (modp->initializer) + initializer = (mod_initializer_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry); + mod_dumpinitializer(initializer, &loadinfo); + + /* Call the module initializer */ + + ret = initializer(&modp->uninitializer, &modp->arg); + if (ret < 0) { - mod_dumpinitializer(modp->initializer, &loadinfo); - - /* Call the module initializer */ - - ret = modp->initializer(); - if (ret < 0) - { - bdbg("Failed to initialize the module: %d\n", ret); - goto errout_with_load; - } + bdbg("Failed to initialize the module: %d\n", ret); + goto errout_with_load; } mod_uninitialize(&loadinfo); diff --git a/sched/module/mod_rmmod.c b/sched/module/mod_rmmod.c new file mode 100644 index 00000000000..6918b3fde60 --- /dev/null +++ b/sched/module/mod_rmmod.c @@ -0,0 +1,100 @@ +/**************************************************************************** + * sched/module/module.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include + +#include +#include + +#include "module/module.h" + +#ifdef CONFIG_MODULE + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rmmod + * + * Description: + * Remove a previously installed module from memory. + * + ****************************************************************************/ + +int rmmod(FAR struct module_s *modp) +{ + int ret = OK; + + DEBUGASSERT(modp != NULL); + + /* Is there an uninitializer? */ + + if (modp->uninitializer != NULL) + { + /* Try to uninitializer the module */ + + ret = modp->uninitializer(modp->arg); + + /* Did the module sucessfully uninitialize? */ + + if (ret < 0) + { + sdbg("ERROR: Failed to uninitialize the module: %d\n", ret); + return ret; + } + } + + /* Free the module memory */ + + /* Release memory holding the relocated ELF image */ + + if (modp->alloc != 0) + { + kmm_free((FAR void *)modp->alloc); + } + + return ret; +} + +#endif /* CONFIG_MODULE */