:: commit ee1aee301b3efc2c9440e8e8bd764aa9fd3f9b75

mintsuki <mintsuki@protonmail.com> — 2020-11-14 12:29

parents: 7a03b32167

pmm: Only align memory map when explicitly requested as not to fragment ext_mem allocations too much

diff --git a/limine-pxe.bin b/limine-pxe.bin
index a5b93fc4..0e50a21f 100644
Binary files a/limine-pxe.bin and b/limine-pxe.bin differ
diff --git a/limine.bin b/limine.bin
index 7f400ab7..82ca5687 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2/fs/ext2.c b/stage2/fs/ext2.c
index 6a75ea95..2a079096 100644
--- a/stage2/fs/ext2.c
+++ b/stage2/fs/ext2.c
@@ -209,7 +209,7 @@ static int ext2_get_inode(struct ext2_inode *ret, struct part *part,
 }

 

 static uint32_t *create_alloc_map(struct ext2_file_handle *fd,

-                            struct ext2_inode *inode) {

+                                  struct ext2_inode *inode) {

     if (inode->i_flags & EXT4_EXTENTS_FLAG)

         return NULL;

 

diff --git a/stage2/mm/pmm.c b/stage2/mm/pmm.c
index b747744e..96cf18c0 100644
--- a/stage2/mm/pmm.c
+++ b/stage2/mm/pmm.c
@@ -45,7 +45,7 @@ void print_memmap(struct e820_entry_t *mm, size_t size) {
     }
 }
 
-static int align_entry(uint64_t *base, uint64_t *length) {
+static bool align_entry(uint64_t *base, uint64_t *length) {
     if (*length < PAGE_SIZE)
         return -1;
 
@@ -57,7 +57,7 @@ static int align_entry(uint64_t *base, uint64_t *length) {
     *length =  ALIGN_DOWN(*length, PAGE_SIZE);
 
     if (!length)
-        return -1;
+        return false;
 
     uint64_t top = *base + *length;
 
@@ -66,14 +66,14 @@ static int align_entry(uint64_t *base, uint64_t *length) {
             *length -= MEMMAP_BASE - *base;
             *base    = MEMMAP_BASE;
         } else {
-            return -1;
+            return false;
         }
     }
 
-    return 0;
+    return true;
 }
 
-static void sanitise_entries(void) {
+static void sanitise_entries(bool align_entries) {
     for (size_t i = 0; i < memmap_entries; i++) {
         if (memmap[i].type != MEMMAP_USABLE)
             continue;
@@ -109,7 +109,11 @@ static void sanitise_entries(void) {
             memmap[i].length = top - base;
         }
 
-        if (!memmap[i].length || align_entry(&memmap[i].base, &memmap[i].length)) {
+        bool success = true;
+        if (align_entries)
+            success = align_entry(&memmap[i].base, &memmap[i].length);
+
+        if (!memmap[i].length || !success) {
             // Eradicate from memmap
             for (size_t j = i; j < memmap_entries - 1; j++) {
                 memmap[j] = memmap[j+1];
@@ -154,7 +158,7 @@ static void sanitise_entries(void) {
 }
 
 struct e820_entry_t *get_memmap(size_t *entries) {
-    sanitise_entries();
+    sanitise_entries(true);
 
     *entries = memmap_entries;
 
@@ -170,7 +174,7 @@ void init_memmap(void) {
         memmap[memmap_entries++] = e820_map[i];
     }
 
-    sanitise_entries();
+    sanitise_entries(false);
 }
 
 void *ext_mem_alloc(size_t count) {
@@ -217,7 +221,7 @@ void *ext_mem_alloc_aligned_type(size_t count, size_t alignment, uint32_t type)
         // Zero out allocated space
         memset(ret, 0, count);
 
-        sanitise_entries();
+        sanitise_entries(false);
 
         return ret;
     }
diff --git a/stage2/protos/stivale2.c b/stage2/protos/stivale2.c
index c5a10aed..2b2e7c71 100644
--- a/stage2/protos/stivale2.c
+++ b/stage2/protos/stivale2.c
@@ -293,26 +293,6 @@ void stivale2_load(char *cmdline) {
     if (bits == 64)
         pagemap = stivale_build_pagemap(level5pg && level5pg_requested);
 
-    //////////////////////////////////////////////
-    // Create memmap struct tag
-    //////////////////////////////////////////////
-    {
-    size_t memmap_entries;
-    struct e820_entry_t *memmap = get_memmap(&memmap_entries);
-
-    struct stivale2_struct_tag_memmap *tag =
-        conv_mem_alloc(sizeof(struct stivale2_struct_tag_memmap) +
-                       sizeof(struct e820_entry_t) * memmap_entries);
-
-    tag->tag.identifier = STIVALE2_STRUCT_TAG_MEMMAP_ID;
-    tag->entries = (uint64_t)memmap_entries;
-
-    memcpy((void*)tag + sizeof(struct stivale2_struct_tag_memmap),
-           memmap, sizeof(struct e820_entry_t) * memmap_entries);
-
-    append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
-    }
-
     //////////////////////////////////////////////
     // Create SMP struct tag
     //////////////////////////////////////////////
@@ -349,6 +329,26 @@ void stivale2_load(char *cmdline) {
             break;
     }
 
+    //////////////////////////////////////////////
+    // Create memmap struct tag
+    //////////////////////////////////////////////
+    {
+    size_t memmap_entries;
+    struct e820_entry_t *memmap = get_memmap(&memmap_entries);
+
+    struct stivale2_struct_tag_memmap *tag =
+        conv_mem_alloc(sizeof(struct stivale2_struct_tag_memmap) +
+                       sizeof(struct e820_entry_t) * memmap_entries);
+
+    tag->tag.identifier = STIVALE2_STRUCT_TAG_MEMMAP_ID;
+    tag->entries = (uint64_t)memmap_entries;
+
+    memcpy((void*)tag + sizeof(struct stivale2_struct_tag_memmap),
+           memmap, sizeof(struct e820_entry_t) * memmap_entries);
+
+    append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
+    }
+
     stivale_spinup(bits, level5pg && level5pg_requested, pagemap,
                    entry_point, &stivale2_struct, stivale2_hdr.stack);
 }
tab: 248 wrap: offon