From 7b09c9beedb3de8ce97085806bfe30900485819b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 1 Jan 2023 22:02:48 -0500 Subject: [PATCH] Add A1 memory allocator --- src/hulk/hurl/a1.d | 51 ++++++++++++++++++++++++++++++++++++++ src/hulk/{ => hurl}/hurl.d | 5 +++- src/hulk/hurl/package.d | 3 +++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/hulk/hurl/a1.d rename src/hulk/{ => hurl}/hurl.d (98%) create mode 100644 src/hulk/hurl/package.d diff --git a/src/hulk/hurl/a1.d b/src/hulk/hurl/a1.d new file mode 100644 index 0000000..bb9861c --- /dev/null +++ b/src/hulk/hurl/a1.d @@ -0,0 +1,51 @@ +/** + * A1 memory allocator. + */ +module hulk.hurl.a1; + +import hulk.hurl; +import hulk.hippo; + +/** + * The A1 memory allocator is a one-shot memory allocator for kernel memory + * that is allocated but never freed. + */ +struct A1 +{ + /** + * Number of bytes allocated in the A1 region so far. + */ + private static __gshared size_t allocated; + + /** + * Allocate memory. + * + * @param size + * Size of memory to allocate. + * + * @return Address of allocated memory. This address will always be aligned + * to a multiple of 16 bytes. + */ + public static void * allocate(size_t size) + { + /* Round size up to a multiple of 16. */ + size = (size + 0xFu) & ~0xFu; + + ulong address = Hurl.A1_BASE + allocated; + + ulong mapped_limit = (address + 0xFFFu) & ~0xFFFu; + + allocated += size; + + ulong desired_limit = (Hurl.A1_BASE + allocated + 0xFFFu) & ~0xFFFu; + + while (desired_limit > mapped_limit) + { + void * page = Hippo.allocate_page(); + Hurl.map(Hurl.A1_BASE + mapped_limit, page, PT_WRITABLE); + mapped_limit += PAGE_SIZE; + } + + return cast(void *)address; + } +} diff --git a/src/hulk/hurl.d b/src/hulk/hurl/hurl.d similarity index 98% rename from src/hulk/hurl.d rename to src/hulk/hurl/hurl.d index 426ef58..b1a1839 100644 --- a/src/hulk/hurl.d +++ b/src/hulk/hurl/hurl.d @@ -3,7 +3,7 @@ * * HURL provides virtual memory management for HULK. */ -module hulk.hurl; +module hulk.hurl.hurl; public import hulk.pagetable; import hulk.cpu; @@ -25,6 +25,9 @@ struct Hurl /** HULK framebuffer address. */ enum ulong HULK_FRAMEBUFFER = 0xFFFF_8001_0000_0000u; + /** A1 allocator range base address. */ + enum ulong A1_BASE = 0xFFFF_8002_0000_0000u; + /** * Pointer to the base page table. */ diff --git a/src/hulk/hurl/package.d b/src/hulk/hurl/package.d new file mode 100644 index 0000000..92311a9 --- /dev/null +++ b/src/hulk/hurl/package.d @@ -0,0 +1,3 @@ +module hulk.hurl; + +public import hulk.hurl.hurl;