:: commit e721c3c814736b9052d050b58830f970ceb09c99

mintsuki <mintsuki@protonmail.com> — 2020-09-20 10:03

parents: f59a3d67d6

Reorganise pmm code

diff --git a/limine.bin b/limine.bin
index 2e4c8676..bc696fbc 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2/drivers/disk.c b/stage2/drivers/disk.c
index 296dd476..a81bd41b 100644
--- a/stage2/drivers/disk.c
+++ b/stage2/drivers/disk.c
@@ -6,6 +6,7 @@
 #include <lib/blib.h>
 #include <lib/part.h>
 #include <lib/print.h>
+#include <mm/pmm.h>
 
 #define SECTOR_SIZE 512
 #define BLOCK_SIZE_IN_SECTORS 16
@@ -29,7 +30,7 @@ static int cache_block(int drive, uint64_t block) {
         return 0;
 
     if (!cache)
-        cache = balloc_aligned(BLOCK_SIZE, 16);
+        cache = conv_mem_alloc_aligned(BLOCK_SIZE, 16);
 
     dap.segment = rm_seg(cache);
     dap.offset  = rm_off(cache);
diff --git a/stage2/drivers/vbe.c b/stage2/drivers/vbe.c
index d3da1003..0884990c 100644
--- a/stage2/drivers/vbe.c
+++ b/stage2/drivers/vbe.c
@@ -2,11 +2,11 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <drivers/vbe.h>
-#include <lib/memmap.h>
 #include <lib/libc.h>
 #include <lib/blib.h>
 #include <lib/real.h>
 #include <lib/print.h>
+#include <mm/pmm.h>
 
 #define VGA_FONT_WIDTH  8
 #define VGA_FONT_HEIGHT 16
@@ -22,7 +22,7 @@ static void vga_font_retrieve(void) {
     r.ebx = 0x0600;
     rm_int(0x10, &r, &r);
 
-    vga_font = ext_mem_balloc(VGA_FONT_MAX, MEMMAP_BOOTLOADER_RECLAIMABLE);
+    vga_font = ext_mem_alloc(VGA_FONT_MAX);
 
     memcpy(vga_font, (void *)rm_desegment(r.es, r.ebp), VGA_FONT_MAX);
 }
@@ -222,7 +222,7 @@ void vbe_tty_init(int *_rows, int *_cols) {
     vga_font_retrieve();
     *_cols = cols = vbe_width / VGA_FONT_WIDTH;
     *_rows = rows = vbe_height / VGA_FONT_HEIGHT;
-    grid = ext_mem_balloc(rows * cols * sizeof(struct vbe_char), MEMMAP_BOOTLOADER_RECLAIMABLE);
+    grid = ext_mem_alloc(rows * cols * sizeof(struct vbe_char));
     vbe_clear(true);
 }
 
diff --git a/stage2/fs/echfs.c b/stage2/fs/echfs.c
index b9f1af9e..258be885 100644
--- a/stage2/fs/echfs.c
+++ b/stage2/fs/echfs.c
@@ -5,6 +5,7 @@
 #include <lib/print.h>
 #include <drivers/disk.h>
 #include <stdbool.h>
+#include <mm/pmm.h>
 
 struct echfs_identity_table {
     uint8_t  jmp[4];
@@ -123,7 +124,7 @@ found:;
     // Load the allocation map.
     uint64_t file_block_count = DIV_ROUNDUP(ret->dir_entry.size, ret->block_size);
 
-    ret->alloc_map = balloc(file_block_count * sizeof(uint64_t));
+    ret->alloc_map = conv_mem_alloc(file_block_count * sizeof(uint64_t));
 
     ret->alloc_map[0] = ret->dir_entry.payload;
     for (uint64_t i = 1; i < file_block_count; i++) {
diff --git a/stage2/fs/ext2.c b/stage2/fs/ext2.c
index a087ee33..4bba946f 100644
--- a/stage2/fs/ext2.c
+++ b/stage2/fs/ext2.c
@@ -5,6 +5,7 @@
 #include <drivers/disk.h>

 #include <lib/libc.h>

 #include <lib/blib.h>

+#include <mm/pmm.h>

 

 /* EXT2 Filesystem States */

 #define EXT2_FS_UNRECOVERABLE_ERRORS 3

@@ -125,7 +126,7 @@ static int ext2_parse_dirent(struct ext2_dir_entry *dir, struct ext2_file_handle
     if (*path == '/')

         path++;

 

-    struct ext2_inode *current_inode = balloc(sizeof(struct ext2_inode));

+    struct ext2_inode *current_inode = conv_mem_alloc(sizeof(struct ext2_inode));

     *current_inode = fd->root_inode;

 

     bool escape = false;

@@ -148,7 +149,7 @@ next:
                    fd->drive, &fd->part);

 

         // name read

-        char *name = balloc(dir->name_len + 1);

+        char *name = conv_mem_alloc(dir->name_len + 1);

 

         memset(name, 0, dir->name_len + 1);

         inode_read(name, i + sizeof(struct ext2_dir_entry), dir->name_len,

@@ -156,11 +157,11 @@ next:
 

         int r = strcmp(token, name);

 

-        brewind(dir->name_len);

+        conv_mem_rewind();

 

         if (!r) {

             if (escape) {

-                brewind(sizeof(struct ext2_inode));

+                conv_mem_rewind();

                 return 0;

             } else {

                 // update the current inode

@@ -172,7 +173,7 @@ next:
         i += dir->rec_len;

     }

 

-    brewind(sizeof(struct ext2_inode));

+    conv_mem_rewind();

     return -1;

 }

 

diff --git a/stage2/fs/file.c b/stage2/fs/file.c
index f2b2d2c1..a125cf50 100644
--- a/stage2/fs/file.c
+++ b/stage2/fs/file.c
@@ -5,10 +5,11 @@
 #include <fs/ext2.h>
 #include <fs/fat32.h>
 #include <lib/blib.h>
+#include <mm/pmm.h>
 
 int fopen(struct file_handle *ret, int disk, int partition, const char *filename) {
     if (echfs_check_signature(disk, partition)) {
-        struct echfs_file_handle *fd = balloc(sizeof(struct echfs_file_handle));
+        struct echfs_file_handle *fd = conv_mem_alloc(sizeof(struct echfs_file_handle));
 
         int r = echfs_open(fd, disk, partition, filename);
         if (r)
@@ -24,7 +25,7 @@ int fopen(struct file_handle *ret, int disk, int partition, const char *filename
     }
 
     if (ext2_check_signature(disk, partition)) {
-        struct ext2_file_handle *fd = balloc(sizeof(struct ext2_file_handle));
+        struct ext2_file_handle *fd = conv_mem_alloc(sizeof(struct ext2_file_handle));
 
         int r = ext2_open(fd, disk, partition, filename);
         if (r)
@@ -40,7 +41,7 @@ int fopen(struct file_handle *ret, int disk, int partition, const char *filename
     }
 
     if (fat32_check_signature(disk, partition)) {
-        struct fat32_file_handle *fd = balloc(sizeof(struct fat32_file_handle));
+        struct fat32_file_handle *fd = conv_mem_alloc(sizeof(struct fat32_file_handle));
 
         int r = fat32_open(fd, disk, partition, filename);
 
diff --git a/stage2/lib/blib.c b/stage2/lib/blib.c
index 4247ec5e..e7be9956 100644
--- a/stage2/lib/blib.c
+++ b/stage2/lib/blib.c
@@ -46,42 +46,6 @@ __attribute__((noreturn)) void panic(const char *fmt, ...) {
     }
 }
 
-extern symbol bss_end;
-static size_t bump_allocator_base = (size_t)bss_end;
-static size_t bump_allocator_limit = 0;
-
-void brewind(size_t count) {
-    bump_allocator_base -= count;
-}
-
-void *balloc(size_t count) {
-    return balloc_aligned(count, 4);
-}
-
-void *balloc_aligned(size_t count, size_t alignment) {
-    if (!bump_allocator_limit) {
-        // The balloc limit is the beginning of the GDT
-        struct {
-            uint16_t limit;
-            uint32_t ptr;
-        } __attribute__((packed)) gdtr;
-        asm volatile ("sgdt %0" :: "m"(gdtr));
-        bump_allocator_limit = gdtr.ptr;
-    }
-
-    size_t new_base = ALIGN_UP(bump_allocator_base, alignment);
-    void *ret = (void *)new_base;
-    new_base += count;
-    if (new_base >= bump_allocator_limit)
-        panic("Memory allocation failed");
-    bump_allocator_base = new_base;
-
-    // Zero out allocated space
-    memset(ret, 0, count);
-
-    return ret;
-}
-
 uint64_t strtoui(const char *s) {
     uint64_t n = 0;
     while (*s)
diff --git a/stage2/lib/blib.h b/stage2/lib/blib.h
index 0510de67..67d34891 100644
--- a/stage2/lib/blib.h
+++ b/stage2/lib/blib.h
@@ -13,10 +13,6 @@ __attribute__((noreturn)) void panic(const char *fmt, ...);
 
 int pit_sleep_and_quit_on_keypress(uint32_t pit_ticks);
 
-void brewind(size_t count);
-void *balloc(size_t count);
-void *balloc_aligned(size_t count, size_t alignment);
-
 #define GETCHAR_CURSOR_LEFT  (-10)
 #define GETCHAR_CURSOR_RIGHT (-11)
 #define GETCHAR_CURSOR_UP    (-12)
diff --git a/stage2/lib/config.c b/stage2/lib/config.c
index 5486d058..4fcca19d 100644
--- a/stage2/lib/config.c
+++ b/stage2/lib/config.c
@@ -2,6 +2,7 @@
 #include <lib/config.h>
 #include <lib/libc.h>
 #include <lib/blib.h>
+#include <mm/pmm.h>
 #include <fs/file.h>
 
 #define SEPARATOR '\n'
@@ -18,7 +19,7 @@ int init_config(int drive, int part) {
     }
 
     size_t config_size = f.size + 1;
-    config_addr = balloc(config_size);
+    config_addr = conv_mem_alloc(config_size);
 
     fread(&f, config_addr, 0, f.size);
 
diff --git a/stage2/lib/elf.c b/stage2/lib/elf.c
index 336fef15..2d65bad4 100644
--- a/stage2/lib/elf.c
+++ b/stage2/lib/elf.c
@@ -4,7 +4,7 @@
 #include <lib/libc.h>
 #include <lib/elf.h>
 #include <lib/print.h>
-#include <lib/memmap.h>
+#include <mm/pmm.h>
 #include <fs/file.h>
 
 #define PT_LOAD     0x00000001
diff --git a/stage2/lib/print.c b/stage2/lib/print.c
index 6531654a..ecd77056 100644
--- a/stage2/lib/print.c
+++ b/stage2/lib/print.c
@@ -111,8 +111,9 @@ void print(const char *fmt, ...) {
     va_end(args);
 }
 
+static char print_buf[PRINT_BUF_MAX];
+
 void vprint(const char *fmt, va_list args) {
-    char  *print_buf = balloc(PRINT_BUF_MAX);
     size_t print_buf_i = 0;
 
     for (;;) {
@@ -173,6 +174,4 @@ out:
     for (size_t i = 0; i < print_buf_i; i++)
         port_out_b(0xe9, print_buf[i]);
 #endif
-
-    brewind(PRINT_BUF_MAX);
 }
diff --git a/stage2/lib/rand.c b/stage2/lib/rand.c
index 923b7ecd..6bc7f569 100644
--- a/stage2/lib/rand.c
+++ b/stage2/lib/rand.c
@@ -4,6 +4,7 @@
 #include <lib/blib.h>
 #include <lib/print.h>
 #include <lib/rand.h>
+#include <mm/pmm.h>
 
 // TODO: Find where this mersenne twister implementation is inspired from
 //       and properly credit the original author(s).
@@ -64,7 +65,7 @@ static void init_rand(void) {
         seed *= (seed ^ rdrand(uint32_t));
     }
 
-    status = balloc(n);
+    status = conv_mem_alloc(n);
 
     srand(seed);
 
diff --git a/stage2/main.c b/stage2/main.c
index 47e54ee4..2a8a8f14 100644
--- a/stage2/main.c
+++ b/stage2/main.c
@@ -6,10 +6,10 @@
 #include <lib/part.h>
 #include <lib/config.h>
 #include <sys/e820.h>
-#include <lib/memmap.h>
 #include <lib/print.h>
 #include <fs/file.h>
 #include <lib/elf.h>
+#include <mm/pmm.h>
 #include <protos/stivale.h>
 #include <protos/stivale2.h>
 #include <protos/linux.h>
diff --git a/stage2/menu.c b/stage2/menu.c
index 7eaec97b..390867ed 100644
--- a/stage2/menu.c
+++ b/stage2/menu.c
@@ -8,6 +8,7 @@
 #include <lib/libc.h>
 #include <lib/config.h>
 #include <lib/term.h>
+#include <mm/pmm.h>
 
 static char *cmdline;
 #define CMDLINE_MAX 1024
@@ -15,7 +16,7 @@ static char *cmdline;
 static char config_entry_name[1024];
 
 char *menu(void) {
-    cmdline = balloc(CMDLINE_MAX);
+    cmdline = conv_mem_alloc(CMDLINE_MAX);
 
     char buf[16];
 
diff --git a/stage2/lib/memmap.c b/stage2/mm/pmm.c
similarity index 78%
rename from stage2/lib/memmap.c
rename to stage2/mm/pmm.c
index 15fffa0b..652bc7d0 100644
--- a/stage2/lib/memmap.c
+++ b/stage2/mm/pmm.c
@@ -1,9 +1,10 @@
 #include <stddef.h>
 #include <stdint.h>
 #include <stdbool.h>
-#include <lib/memmap.h>
+#include <mm/pmm.h>
 #include <sys/e820.h>
 #include <lib/blib.h>
+#include <lib/libc.h>
 #include <lib/print.h>
 
 #define PAGE_SIZE   4096
@@ -157,13 +158,20 @@ void init_memmap(void) {
     sanitise_entries();
 }
 
-void *ext_mem_balloc(size_t count, uint32_t type) {
-    return ext_mem_balloc_aligned(count, 4, type);
+void *ext_mem_alloc(size_t count) {
+    return ext_mem_alloc_type(count, MEMMAP_BOOTLOADER_RECLAIMABLE);
 }
 
-// TODO: this basically only works for the 1st extended memory entry in the
-//       memmap and allocates until the first hole following it. Fix that.
-void *ext_mem_balloc_aligned(size_t count, size_t alignment, uint32_t type) {
+void *ext_mem_alloc_aligned(size_t count, size_t alignment) {
+    return ext_mem_alloc_aligned_type(count, alignment, MEMMAP_BOOTLOADER_RECLAIMABLE);
+}
+
+void *ext_mem_alloc_type(size_t count, uint32_t type) {
+    return ext_mem_alloc_aligned_type(count, 4, type);
+}
+
+// Allocate memory top down, hopefully without bumping into kernel or modules
+void *ext_mem_alloc_aligned_type(size_t count, size_t alignment, uint32_t type) {
     for (int i = memmap_entries - 1; i >= 0; i--) {
         if (memmap[i].type != 1)
             continue;
@@ -189,7 +197,12 @@ void *ext_mem_balloc_aligned(size_t count, size_t alignment, uint32_t type) {
         int64_t aligned_length = entry_top - alloc_base;
         memmap_alloc_range((uint64_t)alloc_base, (uint64_t)aligned_length, type);
 
-        return (void *)(size_t)alloc_base;
+        void *ret = (void *)(size_t)alloc_base;
+
+        // Zero out allocated space
+        memset(ret, 0, count);
+
+        return ret;
     }
 
     panic("High memory allocator: Out of memory");
@@ -246,3 +259,45 @@ void memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type) {
 
     panic("Out of memory");
 }
+
+extern symbol bss_end;
+static size_t bump_allocator_base = (size_t)bss_end;
+static size_t bump_allocator_limit = 0;
+
+void conv_mem_rewind(void) {
+    size_t *old_base = (size_t *)(bump_allocator_base - sizeof(size_t));
+    bump_allocator_base = *old_base;
+}
+
+void *conv_mem_alloc(size_t count) {
+    return conv_mem_alloc_aligned(count, 4);
+}
+
+void *conv_mem_alloc_aligned(size_t count, size_t alignment) {
+    if (!bump_allocator_limit) {
+        // The balloc limit is the beginning of the GDT
+        struct {
+            uint16_t limit;
+            uint32_t ptr;
+        } __attribute__((packed)) gdtr;
+        asm volatile ("sgdt %0" :: "m"(gdtr));
+        bump_allocator_limit = gdtr.ptr;
+    }
+
+    size_t new_base = ALIGN_UP(bump_allocator_base, alignment);
+    void *ret = (void *)new_base;
+
+    size_t *old_base = (size_t *)(new_base + count);
+    new_base += count + sizeof(size_t);
+
+    if (new_base >= bump_allocator_limit)
+        panic("Memory allocation failed");
+
+    *old_base = bump_allocator_base;
+    bump_allocator_base = new_base;
+
+    // Zero out allocated space
+    memset(ret, 0, count);
+
+    return ret;
+}
diff --git a/stage2/lib/memmap.h b/stage2/mm/pmm.h
similarity index 57%
rename from stage2/lib/memmap.h
rename to stage2/mm/pmm.h
index 48fee95b..27e6ba48 100644
--- a/stage2/lib/memmap.h
+++ b/stage2/mm/pmm.h
@@ -1,5 +1,5 @@
-#ifndef __LIB__MEMMAP_H__
-#define __LIB__MEMMAP_H__
+#ifndef __MM__PMM_H__
+#define __MM__PMM_H__
 
 #include <stdint.h>
 #include <sys/e820.h>
@@ -13,10 +13,17 @@
 #define MEMMAP_KERNEL_AND_MODULES     0x1001
 
 void init_memmap(void);
-void *ext_mem_balloc(size_t count, uint32_t type);
-void *ext_mem_balloc_aligned(size_t count, size_t alignment, uint32_t type);
-void memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type);
 struct e820_entry_t *get_memmap(size_t *entries);
 void print_memmap(struct e820_entry_t *mm, size_t size);
+void memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type);
+
+void *ext_mem_alloc(size_t count);
+void *ext_mem_alloc_type(size_t count, uint32_t type);
+void *ext_mem_alloc_aligned(size_t count, size_t alignment);
+void *ext_mem_alloc_aligned_type(size_t count, size_t alignment, uint32_t type);
+
+void conv_mem_rewind(void);
+void *conv_mem_alloc(size_t count);
+void *conv_mem_alloc_aligned(size_t count, size_t alignment);
 
 #endif
diff --git a/stage2/mm/vmm64.c b/stage2/mm/vmm64.c
index 644a59b4..97503ebe 100644
--- a/stage2/mm/vmm64.c
+++ b/stage2/mm/vmm64.c
@@ -1,6 +1,7 @@
 #include <stdint.h>
 #include <stddef.h>
 #include <mm/vmm64.h>
+#include <mm/pmm.h>
 #include <lib/blib.h>
 
 #define PT_SIZE ((uint64_t)0x1000)
@@ -15,7 +16,7 @@ static pt_entry_t *get_next_level(pt_entry_t *current_level, size_t entry) {
         ret = (pt_entry_t *)(current_level[entry] & ~((pt_entry_t)0xfff));
     } else {
         // Allocate a table for the next level
-        ret = balloc_aligned(PT_SIZE, PT_SIZE);
+        ret = ext_mem_alloc_aligned(PT_SIZE, PT_SIZE);
         // Present + writable + user (0b111)
         current_level[entry] = (pt_entry_t)ret | 0b111;
     }
@@ -26,7 +27,7 @@ static pt_entry_t *get_next_level(pt_entry_t *current_level, size_t entry) {
 pagemap_t new_pagemap(int lv) {
     pagemap_t pagemap;
     pagemap.levels    = lv;
-    pagemap.top_level = balloc_aligned(PT_SIZE, PT_SIZE);
+    pagemap.top_level = ext_mem_alloc_aligned(PT_SIZE, PT_SIZE);
     return pagemap;
 }
 
diff --git a/stage2/protos/linux.c b/stage2/protos/linux.c
index 0c3d9483..2d9da1d1 100644
--- a/stage2/protos/linux.c
+++ b/stage2/protos/linux.c
@@ -7,7 +7,7 @@
 #include <lib/term.h>
 #include <lib/config.h>
 #include <lib/print.h>
-#include <lib/memmap.h>
+#include <mm/pmm.h>
 
 #define KERNEL_LOAD_ADDR ((size_t)0x100000)
 #define INITRD_LOAD_ADDR ((size_t)0x1000000)
@@ -31,12 +31,12 @@ void linux_load(char *cmdline, int boot_drive) {
         }
     }
 
-    char *kernel_path = balloc(128);
+    char *kernel_path = conv_mem_alloc(128);
     if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
         panic("KERNEL_PATH not specified");
     }
 
-    struct file_handle *fd = balloc(sizeof(struct file_handle));
+    struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
     if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
         panic("Could not open kernel file");
     }
@@ -63,7 +63,7 @@ void linux_load(char *cmdline, int boot_drive) {
 
     print("linux: Real Mode code size: %x\n", real_mode_code_size);
 
-    void *real_mode_code = balloc_aligned(real_mode_code_size, 0x1000);
+    void *real_mode_code = conv_mem_alloc_aligned(real_mode_code_size, 0x1000);
 
     fread(fd, real_mode_code, 0, real_mode_code_size);
 
diff --git a/stage2/protos/stivale.c b/stage2/protos/stivale.c
index 2a74e41a..f6abde9f 100644
--- a/stage2/protos/stivale.c
+++ b/stage2/protos/stivale.c
@@ -5,7 +5,6 @@
 #include <lib/elf.h>
 #include <lib/blib.h>
 #include <lib/acpi.h>
-#include <lib/memmap.h>
 #include <lib/config.h>
 #include <lib/time.h>
 #include <lib/print.h>
@@ -16,6 +15,7 @@
 #include <sys/pic.h>
 #include <fs/file.h>
 #include <mm/vmm64.h>
+#include <mm/pmm.h>
 #include <stivale/stivale.h>
 
 #define KASLR_SLIDE_BITMASK 0x03FFFF000u
@@ -43,12 +43,12 @@ void stivale_load(char *cmdline, int boot_drive) {
         }
     }
 
-    char *kernel_path = balloc(128);
+    char *kernel_path = conv_mem_alloc(128);
     if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
         panic("KERNEL_PATH not specified");
     }
 
-    struct file_handle *fd = balloc(sizeof(struct file_handle));
+    struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
     if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
         panic("Could not open kernel file");
     }
@@ -139,7 +139,7 @@ void stivale_load(char *cmdline, int boot_drive) {
 
         stivale_struct.module_count++;
 
-        struct stivale_module *m = balloc(sizeof(struct stivale_module));
+        struct stivale_module *m = conv_mem_alloc(sizeof(struct stivale_module));
 
         if (!config_get_value(m->string, i, 128, "MODULE_STRING")) {
             m->string[0] = '\0';
diff --git a/stage2/protos/stivale.h b/stage2/protos/stivale.h
index d390fe91..cba6fd31 100644
--- a/stage2/protos/stivale.h
+++ b/stage2/protos/stivale.h
@@ -3,7 +3,6 @@
 
 #include <stdbool.h>
 #include <stdint.h>
-#include <lib/memmap.h>
 #include <sys/e820.h>
 #include <mm/vmm64.h>
 
diff --git a/stage2/protos/stivale2.c b/stage2/protos/stivale2.c
index 791dacb7..d2fc6729 100644
--- a/stage2/protos/stivale2.c
+++ b/stage2/protos/stivale2.c
@@ -7,7 +7,6 @@
 #include <lib/elf.h>
 #include <lib/blib.h>
 #include <lib/acpi.h>
-#include <lib/memmap.h>
 #include <lib/config.h>
 #include <lib/time.h>
 #include <lib/print.h>
@@ -19,6 +18,7 @@
 #include <lib/term.h>
 #include <sys/pic.h>
 #include <fs/file.h>
+#include <mm/pmm.h>
 #include <stivale/stivale2.h>
 
 #define KASLR_SLIDE_BITMASK 0x03FFFF000u
@@ -66,12 +66,12 @@ void stivale2_load(char *cmdline, int boot_drive) {
         }
     }
 
-    char *kernel_path = balloc(128);
+    char *kernel_path = conv_mem_alloc(128);
     if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
         panic("KERNEL_PATH not specified");
     }
 
-    struct file_handle *fd = balloc(sizeof(struct file_handle));
+    struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
     if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
         panic("Could not open kernel file");
     }
@@ -160,7 +160,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
     // Create firmware struct tag
     //////////////////////////////////////////////
     {
-    struct stivale2_struct_tag_firmware *tag = balloc(sizeof(struct stivale2_struct_tag_firmware));
+    struct stivale2_struct_tag_firmware *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_firmware));
     tag->tag.identifier = STIVALE2_STRUCT_TAG_FIRMWARE_ID;
 
     tag->flags = 1 << 0;   // bit 0 = BIOS boot
@@ -172,7 +172,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
     // Create modules struct tag
     //////////////////////////////////////////////
     {
-    struct stivale2_struct_tag_modules *tag = balloc(sizeof(struct stivale2_struct_tag_modules));
+    struct stivale2_struct_tag_modules *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_modules));
     tag->tag.identifier = STIVALE2_STRUCT_TAG_MODULES_ID;
 
     tag->module_count = 0;
@@ -184,7 +184,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
 
         tag->module_count++;
 
-        struct stivale2_module *m = balloc_aligned(sizeof(struct stivale2_module), 1);
+        struct stivale2_module *m = conv_mem_alloc_aligned(sizeof(struct stivale2_module), 1);
 
         if (!config_get_value(m->string, i, 128, "MODULE_STRING")) {
             m->string[0] = '\0';
@@ -230,7 +230,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
     // Create RSDP struct tag
     //////////////////////////////////////////////
     {
-    struct stivale2_struct_tag_rsdp *tag = balloc(sizeof(struct stivale2_struct_tag_rsdp));
+    struct stivale2_struct_tag_rsdp *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_rsdp));
     tag->tag.identifier = STIVALE2_STRUCT_TAG_RSDP_ID;
 
     tag->rsdp = (uint64_t)(size_t)acpi_get_rsdp();
@@ -242,7 +242,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
     // Create cmdline struct tag
     //////////////////////////////////////////////
     {
-    struct stivale2_struct_tag_cmdline *tag = balloc(sizeof(struct stivale2_struct_tag_cmdline));
+    struct stivale2_struct_tag_cmdline *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_cmdline));
     tag->tag.identifier = STIVALE2_STRUCT_TAG_CMDLINE_ID;
 
     tag->cmdline = (uint64_t)(size_t)cmdline;
@@ -254,7 +254,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
     // Create epoch struct tag
     //////////////////////////////////////////////
     {
-    struct stivale2_struct_tag_epoch *tag = balloc(sizeof(struct stivale2_struct_tag_epoch));
+    struct stivale2_struct_tag_epoch *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_epoch));
     tag->tag.identifier = STIVALE2_STRUCT_TAG_EPOCH_ID;
 
     tag->epoch = time();
@@ -272,7 +272,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
     term_deinit();
 
     if (hdrtag != NULL) {
-        struct stivale2_struct_tag_framebuffer *tag = balloc(sizeof(struct stivale2_struct_tag_framebuffer));
+        struct stivale2_struct_tag_framebuffer *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_framebuffer));
         tag->tag.identifier = STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID;
 
         tag->framebuffer_width  = hdrtag->framebuffer_width;
@@ -298,14 +298,14 @@ void stivale2_load(char *cmdline, int boot_drive) {
     // Create memmap struct tag
     //////////////////////////////////////////////
     {
-    struct stivale2_struct_tag_memmap *tag = balloc(sizeof(struct stivale2_struct_tag_memmap));
+    struct stivale2_struct_tag_memmap *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_memmap));
     tag->tag.identifier = STIVALE2_STRUCT_TAG_MEMMAP_ID;
 
     memmap = get_memmap(&memmap_entries);
 
     tag->entries = (uint64_t)memmap_entries;
 
-    void *tag_memmap = balloc_aligned(sizeof(struct e820_entry_t) * memmap_entries, 1);
+    void *tag_memmap = conv_mem_alloc_aligned(sizeof(struct e820_entry_t) * memmap_entries, 1);
     memcpy(tag_memmap, memmap, sizeof(struct e820_entry_t) * memmap_entries);
 
     append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
@@ -325,7 +325,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
     {
     struct stivale2_header_tag_smp *smp_hdr_tag = get_tag(&stivale2_hdr, STIVALE2_HEADER_TAG_SMP_ID);
     if (smp_hdr_tag != NULL) {
-        struct stivale2_struct_tag_smp *tag = balloc(sizeof(struct stivale2_struct_tag_smp));
+        struct stivale2_struct_tag_smp *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_smp));
         tag->tag.identifier = STIVALE2_STRUCT_TAG_SMP_ID;
 
         init_smp((size_t*)&tag->cpu_count, bits == 64, level5pg && level5pg_requested,
diff --git a/stage2/sys/e820.c b/stage2/sys/e820.c
index b757b83e..5507149c 100644
--- a/stage2/sys/e820.c
+++ b/stage2/sys/e820.c
@@ -4,7 +4,7 @@
 #include <lib/real.h>
 #include <lib/blib.h>
 #include <lib/print.h>
-#include <lib/memmap.h>
+#include <mm/pmm.h>
 
 struct e820_entry_t *e820_map;
 size_t e820_entries;
@@ -12,7 +12,7 @@ size_t e820_entries;
 void init_e820(void) {
     struct rm_regs r = {0};
 
-    e820_map = balloc(sizeof(struct e820_entry_t));
+    e820_map = conv_mem_alloc(sizeof(struct e820_entry_t));
     for (size_t i = 0; ; i++) {
         struct e820_entry_t entry;
 
@@ -34,7 +34,7 @@ void init_e820(void) {
             break;
         }
 
-        balloc(sizeof(struct e820_entry_t));
+        conv_mem_alloc(sizeof(struct e820_entry_t));
     }
 
     print("E820 memory map:\n");
diff --git a/stage2/sys/smp.c b/stage2/sys/smp.c
index 7db89388..de60c559 100644
--- a/stage2/sys/smp.c
+++ b/stage2/sys/smp.c
@@ -8,6 +8,7 @@
 #include <sys/smp.h>
 #include <sys/lapic.h>
 #include <mm/vmm64.h>
+#include <mm/pmm.h>
 
 struct madt {
     struct sdt;
@@ -88,7 +89,7 @@ struct smp_information *init_smp(size_t   *cpu_count,
     struct gdtr gdtr;
     asm volatile ("sgdt %0" :: "m"(gdtr));
 
-    struct smp_information *ret = balloc_aligned(0, 1);
+    struct smp_information *ret = conv_mem_alloc_aligned(0, 1);
     *cpu_count = 0;
 
     // Parse the MADT entries
@@ -104,7 +105,7 @@ struct smp_information *init_smp(size_t   *cpu_count,
                 struct madt_lapic *lapic = (void *)madt_ptr;
 
                 struct smp_information *info_struct =
-                        balloc_aligned(sizeof(struct smp_information), 1);
+                        conv_mem_alloc_aligned(sizeof(struct smp_information), 1);
 
                 info_struct->acpi_processor_uid = lapic->acpi_processor_uid;
                 info_struct->lapic_id           = lapic->lapic_id;
@@ -125,7 +126,7 @@ struct smp_information *init_smp(size_t   *cpu_count,
                 if (!smp_start_ap(lapic->lapic_id, &gdtr, info_struct,
                                   longmode, lv5, (uint32_t)pagemap.top_level)) {
                     print("smp: FAILED to bring-up AP\n");
-                    brewind(sizeof(struct smp_information));
+                    conv_mem_rewind();
                     continue;
                 }
 
tab: 248 wrap: offon