2012-12-19 18:54:26 +01: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-19 22:16:03 +01:00
|
|
|
#include <string.h>
|
2012-12-19 18:54:26 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
|
|
|
#include "libnxflat.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 16:29:03 +02:00
|
|
|
* Pre-processor Definitions
|
2012-12-19 18:54:26 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-10 23:55:36 +02:00
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(x,y) ((x) < (y) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-12-19 23:18:30 +01:00
|
|
|
* Name: nxflat_addrenv_alloc
|
2012-12-19 18:54:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2014-08-24 19:54:14 +02:00
|
|
|
* Allocate data memory for the NXFLAT image. If CONFIG_ARCH_ADDRENV=n,
|
2014-09-01 01:34:44 +02:00
|
|
|
* memory will be allocated using kmm_zalloc(). If CONFIG_ARCH_ADDRENV-y,
|
2014-08-24 19:54:14 +02:00
|
|
|
* then memory will be allocated using up_addrenv_create().
|
2012-12-19 18:54:26 +01: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-19 23:18:30 +01:00
|
|
|
int nxflat_addrenv_alloc(FAR struct nxflat_loadinfo_s *loadinfo, size_t envsize)
|
2012-12-19 18:54:26 +01:00
|
|
|
{
|
|
|
|
FAR struct dspace_s *dspace;
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-08-24 19:54:14 +02:00
|
|
|
FAR void *vdata;
|
2014-08-24 17:57:53 +02:00
|
|
|
save_addrenv_t oldenv;
|
2014-09-10 23:55:36 +02:00
|
|
|
size_t heapsize;
|
2012-12-19 18:54:26 +01:00
|
|
|
int ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DEBUGASSERT(!loadinfo->dspace);
|
|
|
|
|
|
|
|
/* Allocate the struct dspace_s container for the D-Space allocation */
|
|
|
|
|
2014-09-01 01:26:36 +02:00
|
|
|
dspace = (FAR struct dspace_s *)kmm_malloc(sizeof(struct dspace_s));
|
2012-12-19 18:54:26 +01:00
|
|
|
if (dspace == 0)
|
|
|
|
{
|
|
|
|
bdbg("ERROR: Failed to allocate DSpace\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-10-14 18:21:18 +02:00
|
|
|
/* Determine the heapsize to allocate. If there is no dynamic stack then
|
|
|
|
* heapsize must at least as big as the fixed stack size since the stack
|
|
|
|
* will be allocated from the heap in that case.
|
|
|
|
*/
|
2014-09-10 23:55:36 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_STACK_DYNAMIC
|
|
|
|
heapsize = ARCH_HEAP_SIZE;
|
|
|
|
#else
|
|
|
|
heapsize = MIN(loadinfo->stacksize, ARCH_HEAP_SIZE);
|
|
|
|
#endif
|
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* Create a D-Space address environment for the new NXFLAT task */
|
|
|
|
|
2014-09-10 23:55:36 +02:00
|
|
|
ret = up_addrenv_create(0, envsize, heapsize, &loadinfo->addrenv);
|
2012-12-19 18:54:26 +01: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-25 21:28:13 +02:00
|
|
|
ret = up_addrenv_vdata(&loadinfo->addrenv, 0, &vdata);
|
2012-12-19 18:54:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-08-24 19:54:14 +02:00
|
|
|
bdbg("ERROR: up_addrenv_vdata failed: %d\n", ret);
|
2012-12-19 18:54:26 +01: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-24 19:54:14 +02:00
|
|
|
memset(vdata, 0, envsize);
|
2012-12-19 18:54:26 +01: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 00:22:22 +02:00
|
|
|
dspace->crefs = 1;
|
2014-08-24 19:54:14 +02:00
|
|
|
dspace->region = (FAR uint8_t *)vdata;
|
2012-12-19 18:54:26 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_addrenv:
|
2014-08-25 21:28:13 +02:00
|
|
|
(void)up_addrenv_destroy(&loadinfo->addrenv);
|
2012-12-19 18:54:26 +01:00
|
|
|
loadinfo->addrenv = 0;
|
|
|
|
|
|
|
|
errout_with_dspace:
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dspace);
|
2012-12-19 18:54:26 +01:00
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
/* Allocate (and zero) memory to hold the ELF image */
|
|
|
|
|
2014-09-01 00:24:24 +02:00
|
|
|
dspace->region = (FAR uint8_t *)kumm_zalloc(envsize);
|
2012-12-19 18:54:26 +01:00
|
|
|
if (!dspace->region)
|
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dspace);
|
2012-12-19 18:54:26 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadinfo->dspace = dspace;
|
2014-04-14 00:22:22 +02:00
|
|
|
dspace->crefs = 1;
|
2012-12-19 18:54:26 +01: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 17:40:19 +02:00
|
|
|
* error conditions after the module has been loaded but not yet
|
2012-12-19 18:54:26 +01: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 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
int ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DEBUGASSERT(loadinfo);
|
|
|
|
dspace = loadinfo->dspace;
|
|
|
|
|
|
|
|
if (dspace)
|
|
|
|
{
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01: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 00:15:11 +02:00
|
|
|
kumm_free(dspace->region);
|
2012-12-19 18:54:26 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now destroy the D-Space container */
|
|
|
|
|
|
|
|
DEBUGASSERT(dspace->crefs == 1);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dspace);
|
2012-12-19 18:54:26 +01:00
|
|
|
loadinfo->dspace = NULL;
|
|
|
|
}
|
|
|
|
}
|