:: commit 04f30a48cb388a2aa7e18956b50532a787f74c88

mintsuki <mintsuki@protonmail.com> — 2021-10-29 23:53

parents: f730eb0b0f

pmm: Sanitise out 0-length usable entries

diff --git a/stage23/mm/pmm.s2.c b/stage23/mm/pmm.s2.c
index f023c7f2..3f567bf3 100644
--- a/stage23/mm/pmm.s2.c
+++ b/stage23/mm/pmm.s2.c
@@ -167,6 +167,21 @@ static void sanitise_entries(struct e820_entry_t *m, size_t *_count, bool align_
         }
     }
 
+    // Remove 0 length usable entries
+    for (size_t i = 0; i < count; i++) {
+        if (m[i].type != MEMMAP_USABLE)
+            continue;
+
+        if (m[i].length == 0) {
+            // Eradicate from memmap
+            for (size_t j = i + 1; j < count; j++) {
+                m[j - 1] = m[j];
+            }
+            count--;
+            i--;
+        }
+    }
+
     // Sort the entries
     for (size_t p = 0; p < count - 1; p++) {
         uint64_t min = m[p].base;
@@ -503,8 +518,9 @@ void pmm_release_uefi_mem(void) {
 
         status = gBS->FreePages(memmap[i].base, memmap[i].length / 4096);
 
-        if (status)
+        if (status) {
             panic("pmm: FreePages failure (%x)", status);
+        }
     }
 
     allocations_disallowed = true;
@@ -622,7 +638,6 @@ void *ext_mem_alloc_type_aligned(size_t count, uint32_t type, size_t alignment)
     panic("High memory allocator: Out of memory");
 }
 
-
 /// Compute and returns the amount of upper and lower memory till
 /// the first hole.
 struct meminfo mmap_get_info(size_t mmap_count, struct e820_entry_t *mmap) {
tab: 248 wrap: offon