Add an implementation of rmmod

This commit is contained in:
Gregory Nutt 2015-12-12 10:51:54 -06:00
parent 658a2a6275
commit 040835de00
5 changed files with 151 additions and 24 deletions

View File

@ -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).

View File

@ -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)
}

View File

@ -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

View File

@ -1,5 +1,5 @@
/****************************************************************************
* sched/module/module.c
* sched/module/mod_insmod.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
@ -46,8 +46,6 @@
#include <debug.h>
#include <errno.h>
#include <arpa/inet.h>
#include <nuttx/arch.h>
#include <nuttx/module.h>
@ -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);

100
sched/module/mod_rmmod.c Normal file
View File

@ -0,0 +1,100 @@
/****************************************************************************
* sched/module/module.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <stdint.h>
#include <string.h>
#include <nuttx/kmalloc.h>
#include <nuttx/module.h>
#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 */