:: commit fb7e9fdb3cea872cfeb3e704fe758715272fb5c3

mintsuki <mintsuki@protonmail.com> — 2021-04-20 11:52

parents: 6554ff8c38

pmm: Remove dead code and misc bug fixes

diff --git a/stage23/mm/pmm.h b/stage23/mm/pmm.h
index 871cd095..a5ccda61 100644
--- a/stage23/mm/pmm.h
+++ b/stage23/mm/pmm.h
@@ -6,7 +6,6 @@
 #include <stdbool.h>
 #include <sys/e820.h>
 
-#define MEMMAP_REMOVE_RANGE           0
 #define MEMMAP_USABLE                 1
 #define MEMMAP_RESERVED               2
 #define MEMMAP_ACPI_RECLAIMABLE       3
diff --git a/stage23/mm/pmm.s2.c b/stage23/mm/pmm.s2.c
index 36b78a7e..98c5d934 100644
--- a/stage23/mm/pmm.s2.c
+++ b/stage23/mm/pmm.s2.c
@@ -180,23 +180,6 @@ static void sanitise_entries(bool align_entries) {
             i--;
         }
     }
-
-    // Align bootloader-reclaimable entries
-    if (align_entries) {
-        for (size_t i = 0; i < memmap_entries; i++) {
-            if (memmap[i].type != MEMMAP_BOOTLOADER_RECLAIMABLE)
-                continue;
-
-            if (!align_entry(&memmap[i].base, &memmap[i].length)) {
-                // Eradicate from memmap
-                for (size_t j = i; j < memmap_entries - 1; j++) {
-                    memmap[j] = memmap[j+1];
-                }
-                memmap_entries--;
-                i--;
-            }
-        }
-    }
 }
 
 struct e820_entry_t *get_memmap(size_t *entries) {
@@ -403,24 +386,7 @@ bool memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type, bool free
         uint64_t entry_top  = memmap[i].base + memmap[i].length;
         uint32_t entry_type = memmap[i].type;
 
-        if (type == MEMMAP_REMOVE_RANGE &&
-            base == entry_base && top == entry_top) {
-
-            if (simulation)
-                return true;
-
-            // Eradicate from memmap
-            for (size_t j = i; j < memmap_entries - 1; j++) {
-                memmap[j] = memmap[j+1];
-            }
-            memmap_entries--;
-
-            return true;
-        }
-
-        if (base >= entry_base && base <  entry_top &&
-            top  >= entry_base && top  <= entry_top) {
-
+        if (base >= entry_base && base < entry_top && top <= entry_top) {
             if (simulation)
                 return true;
 
@@ -428,42 +394,38 @@ bool memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type, bool free
 
             memmap[i].length -= entry_top - base;
 
-            if (type != MEMMAP_REMOVE_RANGE) {
-                if (memmap[i].length == 0) {
-                    target = &memmap[i];
-                } else {
-                    if (memmap_entries >= MEMMAP_MAX_ENTRIES)
-                        panic("Memory map exhausted.");
-
-                    target = &memmap[memmap_entries++];
-                }
+            if (memmap[i].length == 0) {
+                target = &memmap[i];
+            } else {
+                if (memmap_entries >= MEMMAP_MAX_ENTRIES)
+                    panic("Memory map exhausted.");
 
-                target->type   = type;
-                target->base   = base;
-                target->length = length;
+                target = &memmap[memmap_entries++];
             }
 
+            target->type   = type;
+            target->base   = base;
+            target->length = length;
+
             if (top < entry_top) {
-                if (memmap[i].length == 0) {
-                    target = &memmap[i];
-                } else {
-                    if (memmap_entries >= MEMMAP_MAX_ENTRIES)
-                        panic("Memory map exhausted.");
+                if (memmap_entries >= MEMMAP_MAX_ENTRIES)
+                    panic("Memory map exhausted.");
 
-                    target = &memmap[memmap_entries++];
-                }
+                target = &memmap[memmap_entries++];
 
                 target->type   = entry_type;
                 target->base   = top;
                 target->length = entry_top - top;
             }
 
+            sanitise_entries(false);
+
             return true;
         }
     }
 
     if (!new_entry && do_panic)
-        panic("Out of memory");
+        panic("Memory allocation failure.");
 
     if (new_entry) {
         if (memmap_entries >= MEMMAP_MAX_ENTRIES)
@@ -474,6 +436,10 @@ bool memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type, bool free
         target->type = type;
         target->base = base;
         target->length = length;
+
+        sanitise_entries(false);
+
+        return true;
     }
 
     return false;
tab: 248 wrap: offon