libc: set up memory partitions

* Newlib now defines a special z_newlib_partition containing
  all globals relevant to newlib. Most of these are in libc.a
  with a heap tracking variable in newlib's hooks.

* Both C libraries now expose a k_mem_partition containing the
  bounds of the malloc heap arena. Threads that want to use
  libc malloc() will need to add this to their memory domain.

* z_newlib_get_heap_bounds has been removed, in favor of the
  memory partition for the heap arena

* ztest now includes the C library partitions in its memory
  domain.

* The mem_alloc test now runs in user mode to prove that this
  all works for both C libraries.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
Andrew Boie 2019-02-01 12:18:31 -08:00 committed by Anas Nashif
commit 4b4f773484
9 changed files with 90 additions and 50 deletions

View file

@ -14,12 +14,30 @@
#include <misc/errno_private.h>
#include <misc/libc-hooks.h>
#include <syscall_handler.h>
#include <app_memory/app_memdomain.h>
#include <init.h>
#ifdef CONFIG_APP_SHARED_MEM
K_APPMEM_PARTITION_DEFINE(z_newlib_partition);
#define LIBC_BSS K_APP_BMEM(z_newlib_partition)
#define LIBC_DATA K_APP_DMEM(z_newlib_partition)
#if CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE
K_APPMEM_PARTITION_DEFINE(z_malloc_partition);
#define MALLOC_BSS K_APP_BMEM(z_malloc_partition)
#endif /* CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE */
#else
#define LIBC_BSS
#define LIBC_DATA
#define MALLOC_BSS
#endif /* CONFIG_APP_SHARED_MEM */
#define USED_RAM_END_ADDR POINTER_TO_UINT(&_end)
#if CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE
/* Compiler will throw an error if the provided value isn't a power of two */
static unsigned char __kernel __aligned(CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE)
MALLOC_BSS static unsigned char __kernel __aligned(CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE)
heap_base[CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE];
#define MAX_HEAP_SIZE CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE
#else
@ -48,9 +66,25 @@ extern void *_heap_sentry;
#endif
static unsigned char *heap_base = UINT_TO_POINTER(USED_RAM_END_ADDR);
#ifdef CONFIG_APP_SHARED_MEM
struct k_mem_partition z_malloc_partition;
static int malloc_prepare(struct device *unused)
{
ARG_UNUSED(unused);
z_malloc_partition.start = (u32_t)heap_base;
z_malloc_partition.size = MAX_HEAP_SIZE;
z_malloc_partition.attr = K_MEM_PARTITION_P_RW_U_RW;
return 0;
}
SYS_INIT(malloc_prepare, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
#endif
#endif /* CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE */
static unsigned int heap_sz;
LIBC_BSS static unsigned int heap_sz;
static int _stdout_hook_default(int c)
{
@ -207,12 +241,6 @@ void *_sbrk(int count)
}
FUNC_ALIAS(_sbrk, sbrk, void *);
void z_newlib_get_heap_bounds(void **base, size_t *size)
{
*base = heap_base;
*size = MAX_HEAP_SIZE;
}
int *__errno(void)
{
return z_errno();