2012-12-20 01:54:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libnxflat/libnxflat_addrenv.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 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>
|
|
|
|
|
2012-12-20 05:16:03 +08:00
|
|
|
#include <string.h>
|
2012-12-20 01:54:26 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
|
|
|
#include "libnxflat.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-Processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-11 05:55:36 +08:00
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(x,y) ((x) < (y) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
|
2012-12-20 01:54:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-12-20 06:18:30 +08:00
|
|
|
* Name: nxflat_addrenv_alloc
|
2012-12-20 01:54:26 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2014-08-25 01:54:14 +08:00
|
|
|
* Allocate data memory for the NXFLAT image. If CONFIG_ARCH_ADDRENV=n,
|
2014-09-01 07:34:44 +08:00
|
|
|
* memory will be allocated using kmm_zalloc(). If CONFIG_ARCH_ADDRENV-y,
|
2014-08-25 01:54:14 +08:00
|
|
|
* then memory will be allocated using up_addrenv_create().
|
2012-12-20 01:54:26 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* loadinfo - Load state information
|
|
|
|
* envsize - The size (in bytes) of the address environment needed for the
|
|
|
|
* ELF image.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-12-20 06:18:30 +08:00
|
|
|
int nxflat_addrenv_alloc(FAR struct nxflat_loadinfo_s *loadinfo, size_t envsize)
|
2012-12-20 01:54:26 +08:00
|
|
|
{
|
|
|
|
FAR struct dspace_s *dspace;
|
2014-08-24 20:42:11 +08:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-08-25 01:54:14 +08:00
|
|
|
FAR void *vdata;
|
2014-08-24 23:57:53 +08:00
|
|
|
save_addrenv_t oldenv;
|
2014-09-11 05:55:36 +08:00
|
|
|
size_t heapsize;
|
2012-12-20 01:54:26 +08:00
|
|
|
int ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DEBUGASSERT(!loadinfo->dspace);
|
|
|
|
|
|
|
|
/* Allocate the struct dspace_s container for the D-Space allocation */
|
|
|
|
|
2014-09-01 07:26:36 +08:00
|
|
|
dspace = (FAR struct dspace_s *)kmm_malloc(sizeof(struct dspace_s));
|
2012-12-20 01:54:26 +08:00
|
|
|
if (dspace == 0)
|
|
|
|
{
|
|
|
|
bdbg("ERROR: Failed to allocate DSpace\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2014-08-24 20:42:11 +08:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-09-11 05:55:36 +08:00
|
|
|
/* Determine the heapsize to allocate */
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_STACK_DYNAMIC
|
|
|
|
heapsize = ARCH_HEAP_SIZE;
|
|
|
|
#else
|
|
|
|
heapsize = MIN(loadinfo->stacksize, ARCH_HEAP_SIZE);
|
|
|
|
#endif
|
|
|
|
|
2012-12-20 01:54:26 +08:00
|
|
|
/* Create a D-Space address environment for the new NXFLAT task */
|
|
|
|
|
2014-09-11 05:55:36 +08:00
|
|
|
ret = up_addrenv_create(0, envsize, heapsize, &loadinfo->addrenv);
|
2012-12-20 01:54:26 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
bdbg("ERROR: up_addrenv_create failed: %d\n", ret);
|
|
|
|
goto errout_with_dspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the virtual address associated with the start of the address
|
|
|
|
* environment. This is the base address that we will need to use to
|
|
|
|
* access the D-Space region (but only if the address environment has been
|
|
|
|
* selected.
|
|
|
|
*/
|
|
|
|
|
2014-08-26 03:28:13 +08:00
|
|
|
ret = up_addrenv_vdata(&loadinfo->addrenv, 0, &vdata);
|
2012-12-20 01:54:26 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-08-25 01:54:14 +08:00
|
|
|
bdbg("ERROR: up_addrenv_vdata failed: %d\n", ret);
|
2012-12-20 01:54:26 +08:00
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear all of the allocated D-Space memory. We have to temporarily
|
|
|
|
* selected the D-Space address environment to do this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = up_addrenv_select(loadinfo->addrenv, &oldenv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
bdbg("ERROR: up_addrenv_select failed: %d\n", ret);
|
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
|
2014-08-25 01:54:14 +08:00
|
|
|
memset(vdata, 0, envsize);
|
2012-12-20 01:54:26 +08:00
|
|
|
|
|
|
|
ret = up_addrenv_restore(oldenv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
bdbg("ERROR: up_addrenv_restore failed: %d\n", ret);
|
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success... save the fruits of our labor */
|
|
|
|
|
|
|
|
loadinfo->dspace = dspace;
|
2014-04-14 06:22:22 +08:00
|
|
|
dspace->crefs = 1;
|
2014-08-25 01:54:14 +08:00
|
|
|
dspace->region = (FAR uint8_t *)vdata;
|
2012-12-20 01:54:26 +08:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_addrenv:
|
2014-08-26 03:28:13 +08:00
|
|
|
(void)up_addrenv_destroy(&loadinfo->addrenv);
|
2012-12-20 01:54:26 +08:00
|
|
|
loadinfo->addrenv = 0;
|
|
|
|
|
|
|
|
errout_with_dspace:
|
2014-09-01 07:04:02 +08:00
|
|
|
kmm_free(dspace);
|
2012-12-20 01:54:26 +08:00
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
/* Allocate (and zero) memory to hold the ELF image */
|
|
|
|
|
2014-09-01 06:24:24 +08:00
|
|
|
dspace->region = (FAR uint8_t *)kumm_zalloc(envsize);
|
2012-12-20 01:54:26 +08:00
|
|
|
if (!dspace->region)
|
|
|
|
{
|
2014-09-01 07:04:02 +08:00
|
|
|
kmm_free(dspace);
|
2012-12-20 01:54:26 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadinfo->dspace = dspace;
|
2014-04-14 06:22:22 +08:00
|
|
|
dspace->crefs = 1;
|
2012-12-20 01:54:26 +08:00
|
|
|
return OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_addrenv_free
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the address environment previously created by
|
|
|
|
* nxflat_addrenv_create(). This function is called only under certain
|
2013-08-27 23:40:19 +08:00
|
|
|
* error conditions after the module has been loaded but not yet
|
2012-12-20 01:54:26 +08:00
|
|
|
* started. After the module has been started, the address environment
|
|
|
|
* will automatically be freed when the module exits.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* loadinfo - Load state information
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void nxflat_addrenv_free(FAR struct nxflat_loadinfo_s *loadinfo)
|
|
|
|
{
|
|
|
|
FAR struct dspace_s *dspace;
|
2014-08-24 20:42:11 +08:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-20 01:54:26 +08:00
|
|
|
int ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DEBUGASSERT(loadinfo);
|
|
|
|
dspace = loadinfo->dspace;
|
|
|
|
|
|
|
|
if (dspace)
|
|
|
|
{
|
2014-08-24 20:42:11 +08:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-20 01:54:26 +08:00
|
|
|
/* Destroy the address environment */
|
|
|
|
|
|
|
|
ret = up_addrenv_destroy(loadinfo->addrenv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
bdbg("ERROR: up_addrenv_destroy failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadinfo->addrenv = 0;
|
|
|
|
#else
|
|
|
|
/* Free the allocated D-Space region */
|
|
|
|
|
|
|
|
if (dspace->region)
|
|
|
|
{
|
2014-09-01 06:15:11 +08:00
|
|
|
kumm_free(dspace->region);
|
2012-12-20 01:54:26 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now destroy the D-Space container */
|
|
|
|
|
|
|
|
DEBUGASSERT(dspace->crefs == 1);
|
2014-09-01 07:04:02 +08:00
|
|
|
kmm_free(dspace);
|
2012-12-20 01:54:26 +08:00
|
|
|
loadinfo->dspace = NULL;
|
|
|
|
}
|
|
|
|
}
|