2008-03-20 00:56:58 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the libpayload project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
2010-03-25 23:15:19 +01:00
|
|
|
* Copyright (C) 2008-2010 coresystems GmbH
|
2008-03-20 00:56:58 +01:00
|
|
|
*
|
|
|
|
* 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. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/*
|
2008-03-21 19:37:23 +01:00
|
|
|
* This is a classically weak malloc() implementation. We have a relatively
|
2008-03-20 20:54:59 +01:00
|
|
|
* small and static heap, so we take the easy route with an O(N) loop
|
|
|
|
* through the tree for every malloc() and free(). Obviously, this doesn't
|
|
|
|
* scale past a few hundred KB (if that).
|
|
|
|
*
|
2008-03-21 19:37:23 +01:00
|
|
|
* We're also susceptible to the usual buffer overrun poisoning, though the
|
2008-03-20 20:54:59 +01:00
|
|
|
* risk is within acceptable ranges for this implementation (don't overrun
|
|
|
|
* your buffers, kids!).
|
|
|
|
*/
|
|
|
|
|
2010-03-25 23:15:19 +01:00
|
|
|
#define IN_MALLOC_C
|
2008-03-20 00:56:58 +01:00
|
|
|
#include <libpayload.h>
|
2014-05-13 22:47:32 +02:00
|
|
|
#include <stdint.h>
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
struct memory_type {
|
|
|
|
void *start;
|
|
|
|
void *end;
|
|
|
|
struct align_region_t* align_regions;
|
2015-06-30 00:47:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
|
2013-09-14 03:21:46 +02:00
|
|
|
int magic_initialized;
|
|
|
|
size_t minimal_free;
|
|
|
|
const char *name;
|
|
|
|
#endif
|
2013-08-28 00:48:32 +02:00
|
|
|
};
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
extern char _heap, _eheap; /* Defined in the ldscript. */
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2013-09-14 03:21:46 +02:00
|
|
|
static struct memory_type default_type =
|
|
|
|
{ (void *)&_heap, (void *)&_eheap, NULL
|
2015-06-30 00:47:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
|
2013-09-14 03:21:46 +02:00
|
|
|
, 0, 0, "HEAP"
|
|
|
|
#endif
|
|
|
|
};
|
2013-08-28 00:48:32 +02:00
|
|
|
static struct memory_type *const heap = &default_type;
|
|
|
|
static struct memory_type *dma = &default_type;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2013-06-14 15:34:59 +02:00
|
|
|
typedef u64 hdrtype_t;
|
2013-06-14 15:26:49 +02:00
|
|
|
#define HDRSIZE (sizeof(hdrtype_t))
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2013-06-14 15:26:49 +02:00
|
|
|
#define SIZE_BITS ((HDRSIZE << 3) - 7)
|
|
|
|
#define MAGIC (((hdrtype_t)0x2a) << (SIZE_BITS + 1))
|
|
|
|
#define FLAG_FREE (((hdrtype_t)0x01) << (SIZE_BITS + 0))
|
|
|
|
#define MAX_SIZE ((((hdrtype_t)0x01) << SIZE_BITS) - 1)
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2012-10-08 15:03:35 +02:00
|
|
|
#define SIZE(_h) ((_h) & MAX_SIZE)
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2012-10-08 15:03:35 +02:00
|
|
|
#define _HEADER(_s, _f) ((hdrtype_t) (MAGIC | (_f) | ((_s) & MAX_SIZE)))
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
#define FREE_BLOCK(_s) _HEADER(_s, FLAG_FREE)
|
2012-10-08 15:03:35 +02:00
|
|
|
#define USED_BLOCK(_s) _HEADER(_s, 0)
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
#define IS_FREE(_h) (((_h) & (MAGIC | FLAG_FREE)) == (MAGIC | FLAG_FREE))
|
|
|
|
#define HAS_MAGIC(_h) (((_h) & MAGIC) == MAGIC)
|
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
static int free_aligned(void* addr, struct memory_type *type);
|
2008-03-20 00:56:58 +01:00
|
|
|
void print_malloc_map(void);
|
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
void init_dma_memory(void *start, u32 size)
|
|
|
|
{
|
libpayload: Make EHCI driver cache-aware
This patch makes the EHCI driver work on ARM platforms which usually do
not support automatic cache snooping. It uses the new DMA memory
mechanism (which needs to be correctly set up in the Coreboot mainboard
code) to allocate all EHCI-internal communication structures in
cache-coherent memory, and cleans/invalidates the externally supplied
transfer buffers in Bulk and Control functions with explicit calls as
necessary.
Old-Change-Id: Ie8a62545d905b7a4fdd2a56b9405774be69779e5
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/167339
(cherry picked from commit 322338934add36a5372ffe7d2a45e61a4fdd4a54)
libpayload: ehci: Cache management is hard, let's go copying...
It turns out that my previous commit to make the EHCI stack cache aware
on ARM devices wasn't quite correct, and the problem is actually much
trickier than I thought. After having some fun with more weird transfer
problems that appear/disappear based on stack alignment, this is my
current worst-case threat model that any cache managing implementation
would need to handle correctly:
Some upper layer calls ehci_bulk() with a transfer buffer on its stack.
Due to stack alignment, it happens to start just at the top of a cache
line, so up to 64 - 4 bytes of ehci_bulk's stack will share that line.
ehci_bulk() calls dcache_clean() and initializes the USB transfer.
Between that point and the call to dcache_invalidate() at the end of
ehci_bulk(), any access to the stack variables in that cache line (even
a speculative prefetch) will refetch the line into the cache. Afterwards
any other access to a random memory location that just happens to get
aliased to the same cache line may evict it again, causing the processor
to write out stale data to the transfer buffer and possibly overwrite
data that has already been received over USB.
In short, any dcache_clean/dcache_invalidate-based implementation that
preserves correctness while allowing any arbitrary (non cache-aligned)
memory location as a transfer buffer is presumed to be impossible.
Instead, this patch causes all transfer data to be copied to/from a
cache-coherent bounce buffer. It will still transfer directly if the
supplied buffer is already cache-coherent, which can be used by callers
to optimize their transfers (and is true by default on x86).
Old-Change-Id: I112908410bdbc8ca028d44f2f5d388c529f8057f
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/169231
Reviewed-by: Stefan Reinauer <reinauer@chromium.org>
(cherry picked from commit 702dc50f1d56fe206442079fa443437f4336daed)
Squashed the initial commit and a follow up fix.
Change-Id: Idf7e5aa855b4f0221f82fa380a76049f273e4c88
Signed-off-by: Isaac Christensen <isaac.christensen@se-eng.com>
Reviewed-on: http://review.coreboot.org/6633
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
2013-08-28 21:29:28 +02:00
|
|
|
if (dma_initialized()) {
|
2013-09-14 03:21:46 +02:00
|
|
|
printf("ERROR: %s called twice!\n", __func__);
|
2013-08-28 00:48:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-14 03:21:46 +02:00
|
|
|
/*
|
2017-06-04 04:00:36 +02:00
|
|
|
* DMA memory might not be zeroed by coreboot on stage loading, so make
|
2013-09-14 03:21:46 +02:00
|
|
|
* sure we clear the magic cookie from last boot.
|
|
|
|
*/
|
|
|
|
*(hdrtype_t *)start = 0;
|
2013-08-28 00:48:32 +02:00
|
|
|
|
|
|
|
dma = malloc(sizeof(*dma));
|
|
|
|
dma->start = start;
|
|
|
|
dma->end = start + size;
|
|
|
|
dma->align_regions = NULL;
|
2013-09-14 03:21:46 +02:00
|
|
|
|
2015-06-30 00:47:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
|
2013-09-14 03:21:46 +02:00
|
|
|
dma->minimal_free = 0;
|
|
|
|
dma->magic_initialized = 0;
|
|
|
|
dma->name = "DMA";
|
|
|
|
|
|
|
|
printf("Initialized cache-coherent DMA memory at [%p:%p]\n", start, start + size);
|
|
|
|
#endif
|
2013-08-28 00:48:32 +02:00
|
|
|
}
|
|
|
|
|
libpayload: Make EHCI driver cache-aware
This patch makes the EHCI driver work on ARM platforms which usually do
not support automatic cache snooping. It uses the new DMA memory
mechanism (which needs to be correctly set up in the Coreboot mainboard
code) to allocate all EHCI-internal communication structures in
cache-coherent memory, and cleans/invalidates the externally supplied
transfer buffers in Bulk and Control functions with explicit calls as
necessary.
Old-Change-Id: Ie8a62545d905b7a4fdd2a56b9405774be69779e5
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/167339
(cherry picked from commit 322338934add36a5372ffe7d2a45e61a4fdd4a54)
libpayload: ehci: Cache management is hard, let's go copying...
It turns out that my previous commit to make the EHCI stack cache aware
on ARM devices wasn't quite correct, and the problem is actually much
trickier than I thought. After having some fun with more weird transfer
problems that appear/disappear based on stack alignment, this is my
current worst-case threat model that any cache managing implementation
would need to handle correctly:
Some upper layer calls ehci_bulk() with a transfer buffer on its stack.
Due to stack alignment, it happens to start just at the top of a cache
line, so up to 64 - 4 bytes of ehci_bulk's stack will share that line.
ehci_bulk() calls dcache_clean() and initializes the USB transfer.
Between that point and the call to dcache_invalidate() at the end of
ehci_bulk(), any access to the stack variables in that cache line (even
a speculative prefetch) will refetch the line into the cache. Afterwards
any other access to a random memory location that just happens to get
aliased to the same cache line may evict it again, causing the processor
to write out stale data to the transfer buffer and possibly overwrite
data that has already been received over USB.
In short, any dcache_clean/dcache_invalidate-based implementation that
preserves correctness while allowing any arbitrary (non cache-aligned)
memory location as a transfer buffer is presumed to be impossible.
Instead, this patch causes all transfer data to be copied to/from a
cache-coherent bounce buffer. It will still transfer directly if the
supplied buffer is already cache-coherent, which can be used by callers
to optimize their transfers (and is true by default on x86).
Old-Change-Id: I112908410bdbc8ca028d44f2f5d388c529f8057f
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/169231
Reviewed-by: Stefan Reinauer <reinauer@chromium.org>
(cherry picked from commit 702dc50f1d56fe206442079fa443437f4336daed)
Squashed the initial commit and a follow up fix.
Change-Id: Idf7e5aa855b4f0221f82fa380a76049f273e4c88
Signed-off-by: Isaac Christensen <isaac.christensen@se-eng.com>
Reviewed-on: http://review.coreboot.org/6633
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
2013-08-28 21:29:28 +02:00
|
|
|
int dma_initialized()
|
|
|
|
{
|
|
|
|
return dma != heap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For boards that don't initialize DMA we assume all locations are coherent */
|
|
|
|
int dma_coherent(void *ptr)
|
|
|
|
{
|
|
|
|
return !dma_initialized() || (dma->start <= ptr && dma->end > ptr);
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
static void *alloc(int len, struct memory_type *type)
|
2008-03-20 00:56:58 +01:00
|
|
|
{
|
|
|
|
hdrtype_t header;
|
2013-08-28 00:48:32 +02:00
|
|
|
hdrtype_t volatile *ptr = (hdrtype_t volatile *)type->start;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
|
|
|
/* Align the size. */
|
2015-01-21 17:37:34 +01:00
|
|
|
len = ALIGN_UP(len, HDRSIZE);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2012-10-08 15:03:35 +02:00
|
|
|
if (!len || len > MAX_SIZE)
|
2008-03-20 20:54:59 +01:00
|
|
|
return (void *)NULL;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Make sure the region is setup correctly. */
|
2013-09-14 03:21:46 +02:00
|
|
|
if (!HAS_MAGIC(*ptr)) {
|
|
|
|
size_t size = (type->end - type->start) - HDRSIZE;
|
|
|
|
*ptr = FREE_BLOCK(size);
|
2015-06-30 00:47:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
|
2013-09-14 03:21:46 +02:00
|
|
|
type->magic_initialized = 1;
|
|
|
|
type->minimal_free = size;
|
|
|
|
#endif
|
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Find some free space. */
|
2008-03-20 00:56:58 +01:00
|
|
|
do {
|
2012-03-02 00:12:11 +01:00
|
|
|
header = *ptr;
|
2008-03-20 00:56:58 +01:00
|
|
|
int size = SIZE(header);
|
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
if (!HAS_MAGIC(header) || size == 0) {
|
2010-03-25 23:15:19 +01:00
|
|
|
printf("memory allocator panic. (%s%s)\n",
|
|
|
|
!HAS_MAGIC(header) ? " no magic " : "",
|
|
|
|
size == 0 ? " size=0 " : "");
|
2008-04-26 01:08:47 +02:00
|
|
|
halt();
|
2008-08-14 16:40:10 +02:00
|
|
|
}
|
2008-04-26 01:08:47 +02:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
if (header & FLAG_FREE) {
|
2009-04-29 21:11:18 +02:00
|
|
|
if (len <= size) {
|
2014-05-13 22:47:32 +02:00
|
|
|
hdrtype_t volatile *nptr = (hdrtype_t volatile *)((uintptr_t)ptr + HDRSIZE + len);
|
2008-08-14 16:40:10 +02:00
|
|
|
int nsize = size - (HDRSIZE + len);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
/* If there is still room in this block,
|
2009-05-21 12:02:52 +02:00
|
|
|
* then mark it as such otherwise account
|
|
|
|
* the whole space for that block.
|
2008-03-20 20:54:59 +01:00
|
|
|
*/
|
2008-04-26 01:08:47 +02:00
|
|
|
|
2009-05-21 12:02:52 +02:00
|
|
|
if (nsize > 0) {
|
|
|
|
/* Mark the block as used. */
|
2012-03-02 00:12:11 +01:00
|
|
|
*ptr = USED_BLOCK(len);
|
2009-05-21 12:02:52 +02:00
|
|
|
|
|
|
|
/* Create a new free block. */
|
2012-03-02 00:12:11 +01:00
|
|
|
*nptr = FREE_BLOCK(nsize);
|
2009-05-21 12:02:52 +02:00
|
|
|
} else {
|
|
|
|
/* Mark the block as used. */
|
2012-03-02 00:12:11 +01:00
|
|
|
*ptr = USED_BLOCK(size);
|
2009-05-21 12:02:52 +02:00
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2014-05-13 22:47:32 +02:00
|
|
|
return (void *)((uintptr_t)ptr + HDRSIZE);
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 22:47:32 +02:00
|
|
|
ptr = (hdrtype_t volatile *)((uintptr_t)ptr + HDRSIZE + size);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
} while (ptr < (hdrtype_t *) type->end);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Nothing available. */
|
|
|
|
return (void *)NULL;
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
static void _consolidate(struct memory_type *type)
|
2008-03-20 00:56:58 +01:00
|
|
|
{
|
2013-08-28 00:48:32 +02:00
|
|
|
void *ptr = type->start;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
while (ptr < type->end) {
|
2008-03-20 00:56:58 +01:00
|
|
|
void *nptr;
|
|
|
|
hdrtype_t hdr = *((hdrtype_t *) ptr);
|
|
|
|
unsigned int size = 0;
|
|
|
|
|
|
|
|
if (!IS_FREE(hdr)) {
|
|
|
|
ptr += HDRSIZE + SIZE(hdr);
|
|
|
|
continue;
|
|
|
|
}
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
size = SIZE(hdr);
|
|
|
|
nptr = ptr + HDRSIZE + SIZE(hdr);
|
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
while (nptr < type->end) {
|
2008-03-20 20:54:59 +01:00
|
|
|
hdrtype_t nhdr = *((hdrtype_t *) nptr);
|
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
if (!(IS_FREE(nhdr)))
|
|
|
|
break;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
size += SIZE(nhdr) + HDRSIZE;
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
*((hdrtype_t *) nptr) = 0;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
nptr += (HDRSIZE + SIZE(nhdr));
|
|
|
|
}
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
*((hdrtype_t *) ptr) = FREE_BLOCK(size);
|
|
|
|
ptr = nptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(void *ptr)
|
|
|
|
{
|
|
|
|
hdrtype_t hdr;
|
2013-08-28 00:48:32 +02:00
|
|
|
struct memory_type *type = heap;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Sanity check. */
|
2013-08-28 00:48:32 +02:00
|
|
|
if (ptr < type->start || ptr >= type->end) {
|
|
|
|
type = dma;
|
|
|
|
if (ptr < type->start || ptr >= type->end)
|
|
|
|
return;
|
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
if (free_aligned(ptr, type)) return;
|
|
|
|
|
|
|
|
ptr -= HDRSIZE;
|
2008-03-20 00:56:58 +01:00
|
|
|
hdr = *((hdrtype_t *) ptr);
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Not our header (we're probably poisoned). */
|
2008-03-20 00:56:58 +01:00
|
|
|
if (!HAS_MAGIC(hdr))
|
|
|
|
return;
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Double free. */
|
2008-03-20 00:56:58 +01:00
|
|
|
if (hdr & FLAG_FREE)
|
|
|
|
return;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
*((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
|
2013-08-28 00:48:32 +02:00
|
|
|
_consolidate(type);
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *malloc(size_t size)
|
|
|
|
{
|
2013-08-28 00:48:32 +02:00
|
|
|
return alloc(size, heap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *dma_malloc(size_t size)
|
|
|
|
{
|
|
|
|
return alloc(size, dma);
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *calloc(size_t nmemb, size_t size)
|
|
|
|
{
|
2008-04-26 01:08:47 +02:00
|
|
|
size_t total = nmemb * size;
|
2013-08-28 00:48:32 +02:00
|
|
|
void *ptr = alloc(total, heap);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
memset(ptr, 0, total);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *realloc(void *ptr, size_t size)
|
|
|
|
{
|
2008-03-20 20:54:59 +01:00
|
|
|
void *ret, *pptr;
|
2008-03-20 00:56:58 +01:00
|
|
|
unsigned int osize;
|
2013-08-28 00:48:32 +02:00
|
|
|
struct memory_type *type = heap;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
if (ptr == NULL)
|
2013-08-28 00:48:32 +02:00
|
|
|
return alloc(size, type);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
pptr = ptr - HDRSIZE;
|
|
|
|
|
|
|
|
if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
|
|
|
|
return NULL;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
if (ptr < type->start || ptr >= type->end)
|
|
|
|
type = dma;
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Get the original size of the block. */
|
2008-03-20 00:56:58 +01:00
|
|
|
osize = SIZE(*((hdrtype_t *) pptr));
|
2008-03-20 20:54:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the memory to update the tables - this won't touch the actual
|
|
|
|
* memory, so we can still use it for the copy after we have
|
|
|
|
* reallocated the new space.
|
|
|
|
*/
|
|
|
|
free(ptr);
|
2013-08-28 00:48:32 +02:00
|
|
|
ret = alloc(size, type);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/*
|
|
|
|
* if ret == NULL, then doh - failure.
|
|
|
|
* if ret == ptr then woo-hoo! no copy needed.
|
|
|
|
*/
|
2008-03-20 00:56:58 +01:00
|
|
|
if (ret == NULL || ret == ptr)
|
|
|
|
return ret;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
|
|
|
/* Copy the memory to the new location. */
|
2008-03-20 00:56:58 +01:00
|
|
|
memcpy(ret, ptr, osize > size ? size : osize);
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
struct align_region_t
|
|
|
|
{
|
2015-01-22 15:59:03 +01:00
|
|
|
/* If alignment is 0 then the region reqpresents a large region which
|
|
|
|
* has no metadata for tracking subelements. */
|
2009-04-29 21:11:18 +02:00
|
|
|
int alignment;
|
|
|
|
/* start in memory, and size in bytes */
|
|
|
|
void* start;
|
|
|
|
int size;
|
|
|
|
/* layout within a region:
|
|
|
|
- num_elements bytes, 0: free, 1: used, 2: used, combines with next
|
|
|
|
- padding to alignment
|
|
|
|
- data section
|
|
|
|
- waste space
|
|
|
|
|
|
|
|
start_data points to the start of the data section
|
|
|
|
*/
|
|
|
|
void* start_data;
|
|
|
|
/* number of free blocks sized "alignment" */
|
|
|
|
int free;
|
|
|
|
struct align_region_t *next;
|
|
|
|
};
|
|
|
|
|
2015-01-22 15:59:03 +01:00
|
|
|
static inline int region_is_large(const struct align_region_t *r)
|
2009-04-29 21:11:18 +02:00
|
|
|
{
|
2015-01-22 15:59:03 +01:00
|
|
|
return r->alignment == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int addr_in_region(const struct align_region_t *r, void *addr)
|
|
|
|
{
|
|
|
|
return ((addr >= r->start_data) && (addr < r->start_data + r->size));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* num_elements == 0 indicates a large aligned region instead of a smaller
|
|
|
|
* region comprised of alignment-sized chunks. */
|
|
|
|
static struct align_region_t *allocate_region(int alignment, int num_elements,
|
|
|
|
size_t size, struct memory_type *type)
|
|
|
|
{
|
|
|
|
struct align_region_t *r;
|
|
|
|
size_t extra_space;
|
|
|
|
|
2015-06-30 00:47:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
|
2015-01-22 15:59:03 +01:00
|
|
|
printf("%s(old align_regions=%p, alignment=%u, num_elements=%u, size=%zu)\n",
|
|
|
|
__func__, type->align_regions, alignment, num_elements, size);
|
2010-03-25 23:15:19 +01:00
|
|
|
#endif
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2015-01-22 15:59:03 +01:00
|
|
|
r = malloc(sizeof(*r));
|
2010-03-25 23:15:19 +01:00
|
|
|
|
2015-01-22 15:59:03 +01:00
|
|
|
if (r == NULL)
|
2009-07-31 16:45:41 +02:00
|
|
|
return NULL;
|
2015-01-22 15:59:03 +01:00
|
|
|
|
2016-04-05 21:36:34 +02:00
|
|
|
memset(r, 0, sizeof(*r));
|
2015-01-22 15:59:03 +01:00
|
|
|
|
|
|
|
if (num_elements != 0) {
|
|
|
|
r->alignment = alignment;
|
|
|
|
r->size = num_elements * alignment;
|
|
|
|
r->free = num_elements;
|
|
|
|
/* Allocate enough memory for alignment requirements and
|
|
|
|
* metadata for each chunk. */
|
|
|
|
extra_space = num_elements;
|
|
|
|
} else {
|
|
|
|
/* Large aligned allocation. Set alignment = 0. */
|
|
|
|
r->alignment = 0;
|
|
|
|
r->size = size;
|
|
|
|
extra_space = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->start = alloc(r->size + alignment + extra_space, type);
|
|
|
|
|
|
|
|
if (r->start == NULL) {
|
|
|
|
free(r);
|
2009-07-31 16:45:41 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 15:59:03 +01:00
|
|
|
|
|
|
|
r->start_data = (void *)ALIGN_UP((uintptr_t)r->start + extra_space,
|
|
|
|
alignment);
|
|
|
|
|
|
|
|
/* Clear any (if requested) metadata. */
|
|
|
|
memset(r->start, 0, extra_space);
|
|
|
|
|
|
|
|
/* Link the region with the rest. */
|
|
|
|
r->next = type->align_regions;
|
|
|
|
type->align_regions = r;
|
|
|
|
|
|
|
|
return r;
|
2009-04-29 21:11:18 +02:00
|
|
|
}
|
|
|
|
|
2015-01-22 15:59:03 +01:00
|
|
|
static void try_free_region(struct align_region_t **prev_link)
|
|
|
|
{
|
|
|
|
struct align_region_t *r = *prev_link;
|
|
|
|
|
|
|
|
/* All large regions are immediately free-able. Non-large regions
|
|
|
|
* need to be checked for the fully freed state. */
|
|
|
|
if (!region_is_large(r)) {
|
|
|
|
if (r->free != r->size / r->alignment)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlink region from link list. */
|
|
|
|
*prev_link = r->next;
|
|
|
|
|
|
|
|
/* Free the data and metadata. */
|
|
|
|
free(r->start);
|
|
|
|
free(r);
|
|
|
|
}
|
2009-04-29 21:11:18 +02:00
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
static int free_aligned(void* addr, struct memory_type *type)
|
2009-04-29 21:11:18 +02:00
|
|
|
{
|
2015-01-22 15:59:03 +01:00
|
|
|
struct align_region_t **prev_link = &type->align_regions;
|
|
|
|
|
|
|
|
while (*prev_link != NULL)
|
2009-04-29 21:11:18 +02:00
|
|
|
{
|
2015-01-22 15:59:03 +01:00
|
|
|
if (!addr_in_region(*prev_link, addr)) {
|
|
|
|
prev_link = &((*prev_link)->next);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (region_is_large(*prev_link)) {
|
|
|
|
try_free_region(prev_link);
|
2009-04-29 21:11:18 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2015-01-22 15:59:03 +01:00
|
|
|
|
|
|
|
int i = (addr-(*prev_link)->start_data)/(*prev_link)->alignment;
|
|
|
|
u8 *meta = (*prev_link)->start;
|
|
|
|
while (meta[i] == 2)
|
|
|
|
{
|
|
|
|
meta[i++] = 0;
|
|
|
|
(*prev_link)->free++;
|
|
|
|
}
|
|
|
|
meta[i] = 0;
|
|
|
|
(*prev_link)->free++;
|
|
|
|
try_free_region(prev_link);
|
|
|
|
return 1;
|
2009-04-29 21:11:18 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
static void *alloc_aligned(size_t align, size_t size, struct memory_type *type)
|
2008-09-02 17:49:32 +02:00
|
|
|
{
|
2015-01-22 15:59:03 +01:00
|
|
|
/* Define a large request to be 1024 bytes for either alignment or
|
|
|
|
* size of allocation. */
|
|
|
|
const size_t large_request = 1024;
|
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
if (size == 0) return 0;
|
2013-08-28 00:48:32 +02:00
|
|
|
if (type->align_regions == 0) {
|
|
|
|
type->align_regions = malloc(sizeof(struct align_region_t));
|
|
|
|
if (type->align_regions == NULL)
|
2009-07-31 13:39:55 +02:00
|
|
|
return NULL;
|
2013-08-28 00:48:32 +02:00
|
|
|
memset(type->align_regions, 0, sizeof(struct align_region_t));
|
2009-04-29 21:11:18 +02:00
|
|
|
}
|
2013-08-28 00:48:32 +02:00
|
|
|
struct align_region_t *reg = type->align_regions;
|
2015-01-22 15:59:03 +01:00
|
|
|
|
|
|
|
if (size >= large_request || align >= large_request) {
|
|
|
|
reg = allocate_region(align, 0, size, type);
|
|
|
|
if (reg == NULL)
|
|
|
|
return NULL;
|
|
|
|
return reg->start_data;
|
|
|
|
}
|
|
|
|
|
2010-04-27 08:56:47 +02:00
|
|
|
look_further:
|
2009-04-29 21:11:18 +02:00
|
|
|
while (reg != 0)
|
|
|
|
{
|
|
|
|
if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align))
|
|
|
|
{
|
2015-06-30 00:47:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
|
2010-03-25 23:15:19 +01:00
|
|
|
printf(" found memalign region. %x free, %x required\n", reg->free, (size + align - 1)/align);
|
|
|
|
#endif
|
2009-04-29 21:11:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
reg = reg->next;
|
|
|
|
}
|
|
|
|
if (reg == 0)
|
|
|
|
{
|
2015-06-30 00:47:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
|
2010-03-25 23:15:19 +01:00
|
|
|
printf(" need to allocate a new memalign region\n");
|
|
|
|
#endif
|
|
|
|
/* get align regions */
|
2015-01-22 15:59:03 +01:00
|
|
|
reg = allocate_region(align, large_request/align, size, type);
|
2015-06-30 00:47:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
|
2013-08-28 00:48:32 +02:00
|
|
|
printf(" ... returned %p\n", reg);
|
2010-03-25 23:15:19 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (reg == 0) {
|
|
|
|
/* Nothing available. */
|
|
|
|
return (void *)NULL;
|
2009-04-29 21:11:18 +02:00
|
|
|
}
|
2010-03-25 23:15:19 +01:00
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
int i, count = 0, target = (size+align-1)/align;
|
|
|
|
for (i = 0; i < (reg->size/align); i++)
|
|
|
|
{
|
|
|
|
if (((u8*)reg->start)[i] == 0)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
if (count == target) {
|
|
|
|
count = i+1-count;
|
|
|
|
for (i=0; i<target-1; i++)
|
|
|
|
{
|
|
|
|
((u8*)reg->start)[count+i]=2;
|
|
|
|
}
|
|
|
|
((u8*)reg->start)[count+target-1]=1;
|
|
|
|
reg->free -= target;
|
|
|
|
return reg->start_data+(align*count);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-11-22 17:37:32 +01:00
|
|
|
/* The free space in this region is fragmented,
|
|
|
|
so we will move on and try the next one: */
|
|
|
|
reg = reg->next;
|
2009-04-29 21:11:18 +02:00
|
|
|
goto look_further; // end condition is once a new region is allocated - it always has enough space
|
2008-09-02 17:49:32 +02:00
|
|
|
}
|
|
|
|
|
2013-08-28 00:48:32 +02:00
|
|
|
void *memalign(size_t align, size_t size)
|
|
|
|
{
|
|
|
|
return alloc_aligned(align, size, heap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *dma_memalign(size_t align, size_t size)
|
|
|
|
{
|
|
|
|
return alloc_aligned(align, size, dma);
|
|
|
|
}
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* This is for debugging purposes. */
|
2015-06-30 00:47:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
|
2008-03-20 00:56:58 +01:00
|
|
|
void print_malloc_map(void)
|
|
|
|
{
|
2013-09-14 03:21:46 +02:00
|
|
|
struct memory_type *type = heap;
|
|
|
|
void *ptr;
|
|
|
|
int free_memory;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2013-09-14 03:21:46 +02:00
|
|
|
again:
|
|
|
|
ptr = type->start;
|
|
|
|
free_memory = 0;
|
|
|
|
|
|
|
|
while (ptr < type->end) {
|
2008-03-20 00:56:58 +01:00
|
|
|
hdrtype_t hdr = *((hdrtype_t *) ptr);
|
|
|
|
|
|
|
|
if (!HAS_MAGIC(hdr)) {
|
2013-09-14 03:21:46 +02:00
|
|
|
if (type->magic_initialized)
|
|
|
|
printf("%s: Poisoned magic - we're toast\n", type->name);
|
2010-03-25 23:15:19 +01:00
|
|
|
else
|
2013-09-14 03:21:46 +02:00
|
|
|
printf("%s: No magic yet - going to initialize\n", type->name);
|
2008-03-20 00:56:58 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* FIXME: Verify the size of the block. */
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2013-09-14 03:21:46 +02:00
|
|
|
printf("%s %x: %s (%x bytes)\n", type->name,
|
|
|
|
(unsigned int)(ptr - type->start),
|
2008-03-20 20:54:59 +01:00
|
|
|
hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2010-03-25 23:15:19 +01:00
|
|
|
if (hdr & FLAG_FREE)
|
|
|
|
free_memory += SIZE(hdr);
|
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
ptr += HDRSIZE + SIZE(hdr);
|
|
|
|
}
|
2010-03-25 23:15:19 +01:00
|
|
|
|
2013-09-14 03:21:46 +02:00
|
|
|
if (free_memory && (type->minimal_free > free_memory))
|
|
|
|
type->minimal_free = free_memory;
|
|
|
|
printf("%s: Maximum memory consumption: %u bytes\n", type->name,
|
|
|
|
(type->end - type->start) - HDRSIZE - type->minimal_free);
|
|
|
|
|
|
|
|
if (type != dma) {
|
|
|
|
type = dma;
|
|
|
|
goto again;
|
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
#endif
|