Reclaim bootloader page table pages
This commit is contained in:
1 parent
b3fb599b25
commit
71c30fa932
4 files changed
+153
-90
No files matched your search
+20
-82
@@ -1,20 +1,18 @@
|
||||
/**
|
||||
* HIPPO, the HOS In-place Physical Page Organizer.
|
||||
*
|
||||
* HIPPO maintains a list of free physical pages "in place", meaning that the
|
||||
* available page itself is used as the linked list entry so separate memory
|
||||
* is not needed to keep track of the available pages.
|
||||
*/
|
||||
module hulk.hippo;
|
||||
|
||||
import hulk.header;
|
||||
import hulk.bootinfo;
|
||||
import hulk.klog;
|
||||
import hulk.linker_addresses;
|
||||
import hulk.pagetable;
|
||||
|
||||
struct hippo
|
||||
{
|
||||
/**
|
||||
* Linked list node entry for a physical page.
|
||||
*/
|
||||
private struct PhysicalPage
|
||||
private static struct PhysicalPage
|
||||
{
|
||||
PhysicalPage * next;
|
||||
}
|
||||
@@ -27,96 +25,25 @@ struct hippo
|
||||
/**
|
||||
* Number of free physical pages.
|
||||
*/
|
||||
private static __gshared size_t n_free_pages;
|
||||
|
||||
/**
|
||||
* Physical address limit.
|
||||
*/
|
||||
public static __gshared size_t physical_address_limit;
|
||||
|
||||
/**
|
||||
* Initialize HIPPO.
|
||||
*
|
||||
* Ok, what we do here is iterate through all of the memory map regions in
|
||||
* the bootinfo memory_map array, and create a linked list of all free
|
||||
* physical pages that are available. Within the available memory regions,
|
||||
* we have to watch out for the following items:
|
||||
* 1) HULK binary (text + data)
|
||||
* - This includes the HULK header and bootinfo structures
|
||||
* 2) HULK bss
|
||||
* 3) HULK stack
|
||||
* 4) Framebuffer
|
||||
* 5) Page tables
|
||||
*
|
||||
* @param bootinfo HULK boot information structure.
|
||||
*/
|
||||
public static void initialize(HulkHeader * header)
|
||||
{
|
||||
size_t usable_memory;
|
||||
ulong[2][4] reserved = [
|
||||
[header.bootinfo.hulk_phys, LinkerAddresses.hulk_binary_size],
|
||||
[header.bootinfo.bss_phys, LinkerAddresses.hulk_bss_size],
|
||||
[header.bootinfo.stack_phys, header.stack_size],
|
||||
[cast(ulong)header.bootinfo.fb.buffer, header.bootinfo.fb.height * header.bootinfo.fb.stride * 4u],
|
||||
];
|
||||
for (size_t ri = 0u; ri < reserved.length; ri++)
|
||||
{
|
||||
reserved[ri][1] += reserved[ri][0];
|
||||
}
|
||||
for (size_t bii = 0u; bii < header.bootinfo.memory_map_count; bii++)
|
||||
{
|
||||
if ((header.bootinfo.memory_map[bii].type == BootInfo.MemoryRegion.Type.Bootloader) ||
|
||||
(header.bootinfo.memory_map[bii].type == BootInfo.MemoryRegion.Type.Conventional))
|
||||
{
|
||||
ulong phys = header.bootinfo.memory_map[bii].base;
|
||||
ulong phys_end = phys + header.bootinfo.memory_map[bii].size;
|
||||
if (phys_end > physical_address_limit)
|
||||
{
|
||||
physical_address_limit = phys_end;
|
||||
}
|
||||
usable_memory += header.bootinfo.memory_map[bii].size;
|
||||
while (phys < phys_end)
|
||||
{
|
||||
bool is_reserved = false;
|
||||
for (size_t ri = 0u; ri < reserved.length; ri++)
|
||||
{
|
||||
if ((reserved[ri][0] <= phys) && (phys < reserved[ri][1]))
|
||||
{
|
||||
is_reserved = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!is_reserved)
|
||||
{
|
||||
free_page(phys);
|
||||
}
|
||||
phys += PAGE_SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
size_t usable_kb = usable_memory >> 10u;
|
||||
size_t usable_mb = usable_kb >> 10u;
|
||||
size_t frac_mb = ((1000u * (usable_kb & 0x3FFu)) + 512u) >> 10u;
|
||||
klog.writefln("Found %u.%03uMB of usable RAM", usable_mb, frac_mb);
|
||||
}
|
||||
private static __gshared size_t m_n_free_pages;
|
||||
|
||||
/**
|
||||
* Free a physical page.
|
||||
*
|
||||
* @param phys Physical page address.
|
||||
*/
|
||||
private static void free_page(ulong phys)
|
||||
public static void free_page(T)(T phys)
|
||||
{
|
||||
PhysicalPage * pp = cast(PhysicalPage *)phys;
|
||||
pp.next = free_pages;
|
||||
free_pages = pp;
|
||||
n_free_pages++;
|
||||
m_n_free_pages++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a physical page.
|
||||
*
|
||||
* @return Page address, or null if none available.
|
||||
* @return Page address, or null if no pages are available.
|
||||
*/
|
||||
public static void * allocate_page()
|
||||
{
|
||||
@@ -125,7 +52,18 @@ struct hippo
|
||||
{
|
||||
pp = free_pages;
|
||||
free_pages = free_pages.next;
|
||||
m_n_free_pages--;
|
||||
}
|
||||
return cast(void *)pp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of free pages.
|
||||
*
|
||||
* @return The number of free pages.
|
||||
*/
|
||||
public @property size_t n_free_pages() const
|
||||
{
|
||||
return m_n_free_pages;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user