2013-09-29 04:00:49 +02:00
|
|
|
// Internal dynamic memory allocations.
|
2013-09-15 02:23:54 +02:00
|
|
|
//
|
2013-09-29 04:00:49 +02:00
|
|
|
// Copyright (C) 2009-2013 Kevin O'Connor <kevin@koconnor.net>
|
2013-09-15 02:23:54 +02:00
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU LGPLv3 license.
|
|
|
|
|
|
|
|
#include "biosvar.h" // GET_BDA
|
2013-09-15 03:55:26 +02:00
|
|
|
#include "config.h" // BUILD_BIOS_ADDR
|
2015-09-29 15:40:46 +02:00
|
|
|
#include "e820map.h" // struct e820entry
|
2013-09-15 02:23:54 +02:00
|
|
|
#include "list.h" // hlist_node
|
|
|
|
#include "malloc.h" // _malloc
|
2015-09-29 15:40:46 +02:00
|
|
|
#include "memmap.h" // PAGE_SIZE
|
2013-09-15 03:55:26 +02:00
|
|
|
#include "output.h" // dprintf
|
2013-09-15 02:23:54 +02:00
|
|
|
#include "stacks.h" // wait_preempt
|
2013-09-15 04:27:14 +02:00
|
|
|
#include "std/optionrom.h" // OPTION_ROM_ALIGN
|
2013-09-15 02:23:54 +02:00
|
|
|
#include "string.h" // memset
|
|
|
|
|
|
|
|
// Information on a reserved area.
|
|
|
|
struct allocinfo_s {
|
|
|
|
struct hlist_node node;
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 range_start, range_end, alloc_size;
|
2013-09-15 02:23:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Information on a tracked memory allocation.
|
|
|
|
struct allocdetail_s {
|
|
|
|
struct allocinfo_s detailinfo;
|
|
|
|
struct allocinfo_s datainfo;
|
|
|
|
u32 handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The various memory zones.
|
|
|
|
struct zone_s {
|
|
|
|
struct hlist_head head;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct zone_s ZoneLow VARVERIFY32INIT, ZoneHigh VARVERIFY32INIT;
|
|
|
|
struct zone_s ZoneFSeg VARVERIFY32INIT;
|
|
|
|
struct zone_s ZoneTmpLow VARVERIFY32INIT, ZoneTmpHigh VARVERIFY32INIT;
|
|
|
|
|
|
|
|
static struct zone_s *Zones[] VARVERIFY32INIT = {
|
|
|
|
&ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* low-level memory reservations
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
// Find and reserve space from a given zone
|
2015-09-22 20:45:25 +02:00
|
|
|
static u32
|
2015-09-29 03:45:38 +02:00
|
|
|
alloc_new(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
|
2013-09-15 02:23:54 +02:00
|
|
|
{
|
|
|
|
struct allocinfo_s *info;
|
|
|
|
hlist_for_each_entry(info, &zone->head, node) {
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 alloc_end = info->range_start + info->alloc_size;
|
|
|
|
u32 range_end = info->range_end;
|
|
|
|
u32 new_range_end = ALIGN_DOWN(range_end - size, align);
|
|
|
|
if (new_range_end >= alloc_end && new_range_end <= range_end) {
|
2013-09-15 02:23:54 +02:00
|
|
|
// Found space - now reserve it.
|
2015-09-22 20:45:25 +02:00
|
|
|
fill->range_start = new_range_end;
|
|
|
|
fill->range_end = range_end;
|
|
|
|
fill->alloc_size = size;
|
2013-09-15 02:23:54 +02:00
|
|
|
|
2015-09-22 20:45:25 +02:00
|
|
|
info->range_end = new_range_end;
|
2013-09-15 02:23:54 +02:00
|
|
|
hlist_add_before(&fill->node, &info->node);
|
2015-09-22 20:45:25 +02:00
|
|
|
return new_range_end;
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-22 20:45:25 +02:00
|
|
|
return 0;
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
|
2015-09-29 04:20:27 +02:00
|
|
|
// Reserve space for a 'struct allocdetail_s' and fill
|
|
|
|
static struct allocdetail_s *
|
|
|
|
alloc_new_detail(struct allocdetail_s *temp)
|
|
|
|
{
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 detail_addr = alloc_new(&ZoneTmpHigh, sizeof(struct allocdetail_s)
|
|
|
|
, MALLOC_MIN_ALIGN, &temp->detailinfo);
|
|
|
|
if (!detail_addr) {
|
|
|
|
detail_addr = alloc_new(&ZoneTmpLow, sizeof(struct allocdetail_s)
|
|
|
|
, MALLOC_MIN_ALIGN, &temp->detailinfo);
|
|
|
|
if (!detail_addr) {
|
2015-09-29 04:20:27 +02:00
|
|
|
warn_noalloc();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-09-22 20:45:25 +02:00
|
|
|
struct allocdetail_s *detail = memremap(detail_addr, sizeof(*detail));
|
2015-09-29 04:20:27 +02:00
|
|
|
|
|
|
|
// Fill final 'detail' allocation from data in 'temp'
|
|
|
|
memcpy(detail, temp, sizeof(*detail));
|
|
|
|
hlist_replace(&temp->detailinfo.node, &detail->detailinfo.node);
|
|
|
|
hlist_replace(&temp->datainfo.node, &detail->datainfo.node);
|
|
|
|
return detail;
|
|
|
|
}
|
|
|
|
|
2013-09-15 02:23:54 +02:00
|
|
|
// Add new memory to a zone
|
|
|
|
static void
|
2015-09-22 20:45:25 +02:00
|
|
|
alloc_add(struct zone_s *zone, u32 start, u32 end)
|
2013-09-15 02:23:54 +02:00
|
|
|
{
|
|
|
|
// Find position to add space
|
|
|
|
struct allocinfo_s *info;
|
|
|
|
struct hlist_node **pprev;
|
|
|
|
hlist_for_each_entry_pprev(info, pprev, &zone->head, node) {
|
2015-09-22 20:45:25 +02:00
|
|
|
if (info->range_start < start)
|
2013-09-15 02:23:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add space using temporary allocation info.
|
|
|
|
struct allocdetail_s tempdetail;
|
2015-09-29 04:20:27 +02:00
|
|
|
tempdetail.handle = MALLOC_DEFAULT_HANDLE;
|
2015-09-22 20:45:25 +02:00
|
|
|
tempdetail.datainfo.range_start = start;
|
|
|
|
tempdetail.datainfo.range_end = end;
|
|
|
|
tempdetail.datainfo.alloc_size = 0;
|
2013-09-15 02:23:54 +02:00
|
|
|
hlist_add(&tempdetail.datainfo.node, pprev);
|
|
|
|
|
|
|
|
// Allocate final allocation info.
|
2015-09-29 04:20:27 +02:00
|
|
|
struct allocdetail_s *detail = alloc_new_detail(&tempdetail);
|
|
|
|
if (!detail)
|
|
|
|
hlist_del(&tempdetail.datainfo.node);
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
|
2015-09-29 03:45:38 +02:00
|
|
|
// Release space allocated with alloc_new()
|
|
|
|
static void
|
|
|
|
alloc_free(struct allocinfo_s *info)
|
|
|
|
{
|
|
|
|
struct allocinfo_s *next = container_of_or_null(
|
|
|
|
info->node.next, struct allocinfo_s, node);
|
2015-09-22 20:45:25 +02:00
|
|
|
if (next && next->range_end == info->range_start)
|
|
|
|
next->range_end = info->range_end;
|
2015-09-29 03:45:38 +02:00
|
|
|
hlist_del(&info->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search all zones for an allocation obtained from alloc_new()
|
2013-09-15 02:23:54 +02:00
|
|
|
static struct allocinfo_s *
|
2015-09-22 20:45:25 +02:00
|
|
|
alloc_find(u32 data)
|
2013-09-15 02:23:54 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<ARRAY_SIZE(Zones); i++) {
|
|
|
|
struct allocinfo_s *info;
|
|
|
|
hlist_for_each_entry(info, &Zones[i]->head, node) {
|
2015-09-22 20:45:25 +02:00
|
|
|
if (info->range_start == data)
|
2013-09-15 02:23:54 +02:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-29 03:45:38 +02:00
|
|
|
// Find the lowest memory range added by alloc_add()
|
2013-09-15 02:23:54 +02:00
|
|
|
static struct allocinfo_s *
|
2015-09-29 03:45:38 +02:00
|
|
|
alloc_find_lowest(struct zone_s *zone)
|
2013-09-15 02:23:54 +02:00
|
|
|
{
|
|
|
|
struct allocinfo_s *info, *last = NULL;
|
|
|
|
hlist_for_each_entry(info, &zone->head, node) {
|
|
|
|
last = info;
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* ebda movement
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
// Move ebda
|
|
|
|
static int
|
|
|
|
relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
|
|
|
|
{
|
|
|
|
u32 lowram = GET_BDA(mem_size_kb) * 1024;
|
|
|
|
if (oldebda != lowram)
|
|
|
|
// EBDA isn't at end of ram - give up.
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Do copy
|
|
|
|
memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
|
|
|
|
|
|
|
|
// Update indexes
|
|
|
|
dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
|
|
|
|
SET_BDA(mem_size_kb, newebda / 1024);
|
|
|
|
SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support expanding the ZoneLow dynamically.
|
2015-09-22 20:45:25 +02:00
|
|
|
static u32
|
2013-09-15 02:23:54 +02:00
|
|
|
zonelow_expand(u32 size, u32 align, struct allocinfo_s *fill)
|
|
|
|
{
|
|
|
|
// Make sure to not move ebda while an optionrom is running.
|
|
|
|
if (unlikely(wait_preempt())) {
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 data = alloc_new(&ZoneLow, size, align, fill);
|
2013-09-15 02:23:54 +02:00
|
|
|
if (data)
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2015-09-29 03:45:38 +02:00
|
|
|
struct allocinfo_s *info = alloc_find_lowest(&ZoneLow);
|
2013-09-15 02:23:54 +02:00
|
|
|
if (!info)
|
2015-09-22 20:45:25 +02:00
|
|
|
return 0;
|
|
|
|
u32 oldpos = info->range_end;
|
2013-09-15 02:23:54 +02:00
|
|
|
u32 newpos = ALIGN_DOWN(oldpos - size, align);
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 bottom = info->range_start + info->alloc_size;
|
2013-09-15 02:23:54 +02:00
|
|
|
if (newpos >= bottom && newpos <= oldpos)
|
|
|
|
// Space already present.
|
2015-09-29 03:45:38 +02:00
|
|
|
return alloc_new(&ZoneLow, size, align, fill);
|
2013-09-15 02:23:54 +02:00
|
|
|
u16 ebda_seg = get_ebda_seg();
|
|
|
|
u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
|
|
|
|
u8 ebda_size = GET_EBDA(ebda_seg, size);
|
|
|
|
u32 ebda_end = ebda_pos + ebda_size * 1024;
|
|
|
|
if (ebda_end != bottom)
|
|
|
|
// Something else is after ebda - can't use any existing space.
|
|
|
|
newpos = ALIGN_DOWN(ebda_end - size, align);
|
|
|
|
u32 newbottom = ALIGN_DOWN(newpos, 1024);
|
|
|
|
u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
|
|
|
|
if (newebda < BUILD_EBDA_MINIMUM)
|
|
|
|
// Not enough space.
|
2015-09-22 20:45:25 +02:00
|
|
|
return 0;
|
2013-09-15 02:23:54 +02:00
|
|
|
|
|
|
|
// Move ebda
|
|
|
|
int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
|
|
|
|
if (ret)
|
2015-09-22 20:45:25 +02:00
|
|
|
return 0;
|
2013-09-15 02:23:54 +02:00
|
|
|
|
|
|
|
// Update zone
|
2015-09-22 20:45:25 +02:00
|
|
|
if (ebda_end == bottom)
|
|
|
|
info->range_start = newbottom;
|
|
|
|
else
|
|
|
|
alloc_add(&ZoneLow, newbottom, ebda_end);
|
2013-09-15 02:23:54 +02:00
|
|
|
|
2015-09-29 03:45:38 +02:00
|
|
|
return alloc_new(&ZoneLow, size, align, fill);
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* tracked memory allocations
|
|
|
|
****************************************************************/
|
|
|
|
|
2015-09-22 20:45:25 +02:00
|
|
|
// Allocate physical memory from the given zone and track it as a PMM allocation
|
|
|
|
u32
|
|
|
|
malloc_palloc(struct zone_s *zone, u32 size, u32 align)
|
2013-09-15 02:23:54 +02:00
|
|
|
{
|
|
|
|
ASSERT32FLAT();
|
|
|
|
if (!size)
|
2015-09-22 20:45:25 +02:00
|
|
|
return 0;
|
2013-09-15 02:23:54 +02:00
|
|
|
|
|
|
|
// Find and reserve space for main allocation
|
2015-09-29 04:20:27 +02:00
|
|
|
struct allocdetail_s tempdetail;
|
|
|
|
tempdetail.handle = MALLOC_DEFAULT_HANDLE;
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 data = alloc_new(zone, size, align, &tempdetail.datainfo);
|
2013-09-15 02:23:54 +02:00
|
|
|
if (!CONFIG_MALLOC_UPPERMEMORY && !data && zone == &ZoneLow)
|
2015-09-29 04:20:27 +02:00
|
|
|
data = zonelow_expand(size, align, &tempdetail.datainfo);
|
|
|
|
if (!data)
|
2015-09-22 20:45:25 +02:00
|
|
|
return 0;
|
2015-09-29 04:20:27 +02:00
|
|
|
|
|
|
|
// Find and reserve space for bookkeeping.
|
|
|
|
struct allocdetail_s *detail = alloc_new_detail(&tempdetail);
|
|
|
|
if (!detail) {
|
|
|
|
alloc_free(&tempdetail.datainfo);
|
2015-09-22 20:45:25 +02:00
|
|
|
return 0;
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:45:25 +02:00
|
|
|
dprintf(8, "phys_alloc zone=%p size=%d align=%x ret=%x (detail=%p)\n"
|
2013-12-14 19:14:02 +01:00
|
|
|
, zone, size, align, data, detail);
|
2013-09-15 02:23:54 +02:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:45:25 +02:00
|
|
|
// Allocate virtual memory from the given zone
|
|
|
|
void * __malloc
|
|
|
|
_malloc(struct zone_s *zone, u32 size, u32 align)
|
|
|
|
{
|
|
|
|
return memremap(malloc_palloc(zone, size, align), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free a data block allocated with phys_alloc
|
2013-09-15 02:23:54 +02:00
|
|
|
int
|
2015-09-22 20:45:25 +02:00
|
|
|
malloc_pfree(u32 data)
|
2013-09-15 02:23:54 +02:00
|
|
|
{
|
|
|
|
ASSERT32FLAT();
|
2015-09-29 03:45:38 +02:00
|
|
|
struct allocinfo_s *info = alloc_find(data);
|
2015-09-22 20:45:25 +02:00
|
|
|
if (!info || data == virt_to_phys(info) || !info->alloc_size)
|
2013-09-15 02:23:54 +02:00
|
|
|
return -1;
|
|
|
|
struct allocdetail_s *detail = container_of(
|
|
|
|
info, struct allocdetail_s, datainfo);
|
2015-09-22 20:45:25 +02:00
|
|
|
dprintf(8, "phys_free %x (detail=%p)\n", data, detail);
|
2015-09-29 03:45:38 +02:00
|
|
|
alloc_free(info);
|
|
|
|
alloc_free(&detail->detailinfo);
|
2013-09-15 02:23:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-06 21:37:53 +02:00
|
|
|
void
|
|
|
|
free(void *data)
|
|
|
|
{
|
|
|
|
if (!data)
|
|
|
|
return;
|
2015-09-22 20:45:25 +02:00
|
|
|
int ret = malloc_pfree(virt_to_phys(data));
|
2015-10-06 21:37:53 +02:00
|
|
|
if (ret)
|
|
|
|
warn_internalerror();
|
|
|
|
}
|
|
|
|
|
2013-09-15 02:23:54 +02:00
|
|
|
// Find the amount of free space in a given zone.
|
|
|
|
u32
|
|
|
|
malloc_getspace(struct zone_s *zone)
|
|
|
|
{
|
|
|
|
// XXX - doesn't account for ZoneLow being able to grow.
|
|
|
|
// XXX - results not reliable when CONFIG_THREAD_OPTIONROMS
|
|
|
|
u32 maxspace = 0;
|
|
|
|
struct allocinfo_s *info;
|
|
|
|
hlist_for_each_entry(info, &zone->head, node) {
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 space = info->range_end - info->range_start - info->alloc_size;
|
2013-09-15 02:23:54 +02:00
|
|
|
if (space > maxspace)
|
|
|
|
maxspace = space;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
|
|
|
|
return maxspace;
|
|
|
|
// Account for space needed for PMM tracking.
|
|
|
|
u32 reserve = ALIGN(sizeof(struct allocdetail_s), MALLOC_MIN_ALIGN);
|
|
|
|
if (maxspace <= reserve)
|
|
|
|
return 0;
|
|
|
|
return maxspace - reserve;
|
|
|
|
}
|
|
|
|
|
2013-12-14 19:14:02 +01:00
|
|
|
// Set a handle associated with an allocation.
|
|
|
|
void
|
2015-09-22 20:45:25 +02:00
|
|
|
malloc_sethandle(u32 data, u32 handle)
|
2013-12-14 19:14:02 +01:00
|
|
|
{
|
|
|
|
ASSERT32FLAT();
|
2015-09-29 03:45:38 +02:00
|
|
|
struct allocinfo_s *info = alloc_find(data);
|
2015-09-22 20:45:25 +02:00
|
|
|
if (!info || data == virt_to_phys(info) || !info->alloc_size)
|
2013-12-14 19:14:02 +01:00
|
|
|
return;
|
|
|
|
struct allocdetail_s *detail = container_of(
|
|
|
|
info, struct allocdetail_s, datainfo);
|
|
|
|
detail->handle = handle;
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:45:25 +02:00
|
|
|
// Find the data block allocated with phys_alloc with a given handle.
|
|
|
|
u32
|
2013-12-14 19:14:02 +01:00
|
|
|
malloc_findhandle(u32 handle)
|
2013-09-15 02:23:54 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<ARRAY_SIZE(Zones); i++) {
|
|
|
|
struct allocinfo_s *info;
|
|
|
|
hlist_for_each_entry(info, &Zones[i]->head, node) {
|
2015-09-22 20:45:25 +02:00
|
|
|
if (info->range_start != virt_to_phys(info))
|
2013-09-15 02:23:54 +02:00
|
|
|
continue;
|
|
|
|
struct allocdetail_s *detail = container_of(
|
|
|
|
info, struct allocdetail_s, detailinfo);
|
|
|
|
if (detail->handle == handle)
|
2015-09-22 20:45:25 +02:00
|
|
|
return detail->datainfo.range_start;
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-22 20:45:25 +02:00
|
|
|
return 0;
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* 0xc0000-0xf0000 management
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
static u32 RomEnd = BUILD_ROM_START;
|
|
|
|
static struct allocinfo_s *RomBase;
|
|
|
|
|
|
|
|
#define OPROM_HEADER_RESERVE 16
|
|
|
|
|
|
|
|
// Return the maximum memory position option roms may use.
|
|
|
|
u32
|
|
|
|
rom_get_max(void)
|
|
|
|
{
|
|
|
|
if (CONFIG_MALLOC_UPPERMEMORY)
|
2015-09-22 20:45:25 +02:00
|
|
|
return ALIGN_DOWN(RomBase->range_end - OPROM_HEADER_RESERVE
|
2013-09-15 02:23:54 +02:00
|
|
|
, OPTION_ROM_ALIGN);
|
2015-10-08 17:18:17 +02:00
|
|
|
return SYMBOL(final_readonly_start);
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the end of the last deployed option rom.
|
|
|
|
u32
|
|
|
|
rom_get_last(void)
|
|
|
|
{
|
|
|
|
return RomEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request space for an optionrom in 0xc0000-0xf0000 area.
|
|
|
|
struct rom_header *
|
|
|
|
rom_reserve(u32 size)
|
|
|
|
{
|
|
|
|
u32 newend = ALIGN(RomEnd + size, OPTION_ROM_ALIGN);
|
|
|
|
if (newend > rom_get_max())
|
|
|
|
return NULL;
|
|
|
|
if (CONFIG_MALLOC_UPPERMEMORY) {
|
2015-10-08 17:18:17 +02:00
|
|
|
if (newend < SYMBOL(zonelow_base))
|
|
|
|
newend = SYMBOL(zonelow_base);
|
2015-09-22 20:45:25 +02:00
|
|
|
RomBase->range_start = newend + OPROM_HEADER_RESERVE;
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
return (void*)RomEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm space as in use by an optionrom.
|
|
|
|
int
|
|
|
|
rom_confirm(u32 size)
|
|
|
|
{
|
|
|
|
void *new = rom_reserve(size);
|
|
|
|
if (!new) {
|
|
|
|
warn_noalloc();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
RomEnd = ALIGN(RomEnd + size, OPTION_ROM_ALIGN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Setup
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
malloc_preinit(void)
|
|
|
|
{
|
|
|
|
ASSERT32FLAT();
|
|
|
|
dprintf(3, "malloc preinit\n");
|
|
|
|
|
|
|
|
// Don't declare any memory between 0xa0000 and 0x100000
|
2015-09-29 14:53:38 +02:00
|
|
|
e820_remove(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END);
|
2013-09-15 02:23:54 +02:00
|
|
|
|
|
|
|
// Mark known areas as reserved.
|
2015-09-29 15:40:46 +02:00
|
|
|
e820_add(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
|
2013-09-15 02:23:54 +02:00
|
|
|
|
|
|
|
// Populate temp high ram
|
2022-04-25 09:20:02 +02:00
|
|
|
u32 highram_start = 0;
|
2022-04-25 09:25:31 +02:00
|
|
|
u32 highram_size = 0;
|
2013-09-15 02:23:54 +02:00
|
|
|
int i;
|
|
|
|
for (i=e820_count-1; i>=0; i--) {
|
|
|
|
struct e820entry *en = &e820_list[i];
|
|
|
|
u64 end = en->start + en->size;
|
|
|
|
if (end < 1024*1024)
|
|
|
|
break;
|
|
|
|
if (en->type != E820_RAM || end > 0xffffffff)
|
|
|
|
continue;
|
|
|
|
u32 s = en->start, e = end;
|
2022-04-25 09:20:02 +02:00
|
|
|
if (!highram_start) {
|
2022-04-25 09:25:31 +02:00
|
|
|
u32 new_max = ALIGN_DOWN(e - BUILD_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
|
|
|
|
u32 new_min = ALIGN_DOWN(e - BUILD_MIN_HIGHTABLE, MALLOC_MIN_ALIGN);
|
|
|
|
if (new_max <= e && new_max >= s + BUILD_MAX_HIGHTABLE) {
|
|
|
|
highram_start = e = new_max;
|
|
|
|
highram_size = BUILD_MAX_HIGHTABLE;
|
|
|
|
} else if (new_min <= e && new_min >= s) {
|
|
|
|
highram_start = e = new_min;
|
|
|
|
highram_size = BUILD_MIN_HIGHTABLE;
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-22 20:45:25 +02:00
|
|
|
alloc_add(&ZoneTmpHigh, s, e);
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate regions
|
2015-09-22 20:45:25 +02:00
|
|
|
alloc_add(&ZoneTmpLow, BUILD_STACK_ADDR, BUILD_EBDA_MINIMUM);
|
2022-04-25 09:20:02 +02:00
|
|
|
if (highram_start) {
|
|
|
|
alloc_add(&ZoneHigh, highram_start, highram_start + highram_size);
|
|
|
|
e820_add(highram_start, highram_size, E820_RESERVED);
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-15 19:53:04 +02:00
|
|
|
malloc_csm_preinit(u32 low_pmm, u32 low_pmm_size, u32 hi_pmm, u32 hi_pmm_size)
|
2013-09-15 02:23:54 +02:00
|
|
|
{
|
|
|
|
ASSERT32FLAT();
|
|
|
|
|
2023-06-13 17:01:34 +02:00
|
|
|
if (hi_pmm_size > BUILD_MIN_HIGHTABLE) {
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 hi_pmm_end = hi_pmm + hi_pmm_size;
|
2023-06-13 17:01:34 +02:00
|
|
|
alloc_add(&ZoneTmpHigh, hi_pmm, hi_pmm_end - BUILD_MIN_HIGHTABLE);
|
|
|
|
alloc_add(&ZoneHigh, hi_pmm_end - BUILD_MIN_HIGHTABLE, hi_pmm_end);
|
2013-09-15 02:23:54 +02:00
|
|
|
} else {
|
2015-09-22 20:45:25 +02:00
|
|
|
alloc_add(&ZoneTmpHigh, hi_pmm, hi_pmm + hi_pmm_size);
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
2015-09-22 20:45:25 +02:00
|
|
|
alloc_add(&ZoneTmpLow, low_pmm, low_pmm + low_pmm_size);
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 LegacyRamSize VARFSEG;
|
|
|
|
|
|
|
|
// Calculate the maximum ramsize (less than 4gig) from e820 map.
|
|
|
|
static void
|
|
|
|
calcRamSize(void)
|
|
|
|
{
|
|
|
|
u32 rs = 0;
|
|
|
|
int i;
|
|
|
|
for (i=e820_count-1; i>=0; i--) {
|
|
|
|
struct e820entry *en = &e820_list[i];
|
|
|
|
u64 end = en->start + en->size;
|
|
|
|
u32 type = en->type;
|
|
|
|
if (end <= 0xffffffff && (type == E820_ACPI || type == E820_RAM)) {
|
|
|
|
rs = end;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LegacyRamSize = rs >= 1024*1024 ? rs : 1024*1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update pointers after code relocation.
|
|
|
|
void
|
|
|
|
malloc_init(void)
|
|
|
|
{
|
|
|
|
ASSERT32FLAT();
|
|
|
|
dprintf(3, "malloc init\n");
|
|
|
|
|
|
|
|
if (CONFIG_RELOCATE_INIT) {
|
|
|
|
// Fixup malloc pointers after relocation
|
|
|
|
int i;
|
|
|
|
for (i=0; i<ARRAY_SIZE(Zones); i++) {
|
|
|
|
struct zone_s *zone = Zones[i];
|
|
|
|
if (zone->head.first)
|
|
|
|
zone->head.first->pprev = &zone->head.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize low-memory region
|
2015-10-08 17:18:17 +02:00
|
|
|
memmove(VSYMBOL(final_varlow_start), VSYMBOL(varlow_start)
|
|
|
|
, SYMBOL(varlow_end) - SYMBOL(varlow_start));
|
2013-09-15 02:23:54 +02:00
|
|
|
if (CONFIG_MALLOC_UPPERMEMORY) {
|
2015-10-08 17:18:17 +02:00
|
|
|
alloc_add(&ZoneLow, SYMBOL(zonelow_base) + OPROM_HEADER_RESERVE
|
|
|
|
, SYMBOL(final_varlow_start));
|
2015-09-29 03:45:38 +02:00
|
|
|
RomBase = alloc_find_lowest(&ZoneLow);
|
2013-09-15 02:23:54 +02:00
|
|
|
} else {
|
2015-10-08 17:18:17 +02:00
|
|
|
alloc_add(&ZoneLow, ALIGN_DOWN(SYMBOL(final_varlow_start), 1024)
|
|
|
|
, SYMBOL(final_varlow_start));
|
2013-09-15 02:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add space available in f-segment to ZoneFSeg
|
2015-10-08 17:18:17 +02:00
|
|
|
memset(VSYMBOL(zonefseg_start), 0
|
|
|
|
, SYMBOL(zonefseg_end) - SYMBOL(zonefseg_start));
|
|
|
|
alloc_add(&ZoneFSeg, SYMBOL(zonefseg_start), SYMBOL(zonefseg_end));
|
2013-09-15 02:23:54 +02:00
|
|
|
|
|
|
|
calcRamSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
malloc_prepboot(void)
|
|
|
|
{
|
|
|
|
ASSERT32FLAT();
|
|
|
|
dprintf(3, "malloc finalize\n");
|
|
|
|
|
|
|
|
u32 base = rom_get_max();
|
|
|
|
memset((void*)RomEnd, 0, base-RomEnd);
|
|
|
|
if (CONFIG_MALLOC_UPPERMEMORY) {
|
|
|
|
// Place an optionrom signature around used low mem area.
|
|
|
|
struct rom_header *dummyrom = (void*)base;
|
|
|
|
dummyrom->signature = OPTION_ROM_SIGNATURE;
|
|
|
|
int size = (BUILD_BIOS_ADDR - base) / 512;
|
|
|
|
dummyrom->size = (size > 255) ? 255 : size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reserve more low-mem if needed.
|
|
|
|
u32 endlow = GET_BDA(mem_size_kb)*1024;
|
2015-09-29 15:40:46 +02:00
|
|
|
e820_add(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
|
2013-09-15 02:23:54 +02:00
|
|
|
|
|
|
|
// Clear unused f-seg ram.
|
2015-09-29 03:45:38 +02:00
|
|
|
struct allocinfo_s *info = alloc_find_lowest(&ZoneFSeg);
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 size = info->range_end - info->range_start;
|
|
|
|
memset(memremap(info->range_start, size), 0, size);
|
2013-09-15 02:23:54 +02:00
|
|
|
dprintf(1, "Space available for UMB: %x-%x, %x-%x\n"
|
2015-09-22 20:45:25 +02:00
|
|
|
, RomEnd, base, info->range_start, info->range_end);
|
2013-09-15 02:23:54 +02:00
|
|
|
|
|
|
|
// Give back unused high ram.
|
2015-09-29 03:45:38 +02:00
|
|
|
info = alloc_find_lowest(&ZoneHigh);
|
2013-09-15 02:23:54 +02:00
|
|
|
if (info) {
|
2015-09-22 20:45:25 +02:00
|
|
|
u32 giveback = ALIGN_DOWN(info->range_end-info->range_start, PAGE_SIZE);
|
|
|
|
e820_add(info->range_start, giveback, E820_RAM);
|
2013-09-15 02:23:54 +02:00
|
|
|
dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
|
|
|
|
}
|
|
|
|
|
|
|
|
calcRamSize();
|
|
|
|
}
|