2012-12-19 18:54:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libnxflat/libnxflat_addrenv.c
|
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-12-19 18:54:26 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-12-19 18:54:26 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-12-19 18:54:26 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2012-12-19 22:16:03 +01:00
|
|
|
#include <string.h>
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2012-12-19 18:54:26 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2023-01-27 12:45:03 +01:00
|
|
|
#include <nuttx/addrenv.h>
|
2012-12-19 18:54:26 +01:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
|
|
|
#include "libnxflat.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-04 13:51:09 +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
|
2023-01-27 12:45:03 +01:00
|
|
|
FAR struct arch_addrenv_s *addrenv = &loadinfo->addrenv.addrenv;
|
2014-08-24 19:54:14 +02:00
|
|
|
FAR void *vdata;
|
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 */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
dspace = kmm_malloc(sizeof(struct dspace_s));
|
2012-12-19 18:54:26 +01:00
|
|
|
if (dspace == 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: Failed to allocate DSpace\n");
|
2012-12-19 18:54:26 +01:00
|
|
|
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 */
|
|
|
|
|
2023-01-27 12:45:03 +01:00
|
|
|
ret = up_addrenv_create(0, envsize, heapsize, addrenv);
|
2012-12-19 18:54:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: up_addrenv_create failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
goto errout_with_dspace;
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:45:03 +01:00
|
|
|
/* Take a reference to the address environment, so it won't get freed */
|
|
|
|
|
|
|
|
addrenv_take(&loadinfo->addrenv);
|
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2023-01-27 12:45:03 +01:00
|
|
|
ret = up_addrenv_vdata(addrenv, 0, &vdata);
|
2012-12-19 18:54:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("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.
|
|
|
|
*/
|
|
|
|
|
2023-01-27 12:45:03 +01:00
|
|
|
ret = addrenv_select(&loadinfo->addrenv);
|
2012-12-19 18:54:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-01-27 12:45:03 +01:00
|
|
|
berr("ERROR: addrenv_select failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
|
2014-08-24 19:54:14 +02:00
|
|
|
memset(vdata, 0, envsize);
|
2012-12-19 18:54:26 +01:00
|
|
|
|
2023-01-27 12:45:03 +01:00
|
|
|
ret = addrenv_restore();
|
2012-12-19 18:54:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-01-27 12:45:03 +01:00
|
|
|
berr("ERROR: addrenv_restore failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
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:
|
2020-01-02 17:49:34 +01:00
|
|
|
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 */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
dspace->region = 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)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: up_addrenv_destroy failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|