:: commit 2b34d88d19ec92f2ec9abb57f72a838729d62f73

mintsuki <mintsuki@protonmail.com> — 2020-10-20 06:51

parents: 74c7c0d776

Remove instances in which conv_mem_alloc() was implicitly used as a realloc() as that subtly introduces bugs

diff --git a/limine.bin b/limine.bin
index 8869a106..55f23cc1 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2/lib/part.c b/stage2/lib/part.c
index a3d764e2..da7dccdf 100644
--- a/stage2/lib/part.c
+++ b/stage2/lib/part.c
@@ -144,8 +144,6 @@ static struct part *part_index = NULL;
 static size_t part_index_i = 0;
 
 void part_create_index(void) {
-    part_index = conv_mem_alloc(0);
-
     for (uint8_t drive = 0x80; drive < 0x8f; drive++) {
         struct rm_regs r = {0};
         struct bios_drive_params drive_params;
@@ -167,6 +165,8 @@ void part_create_index(void) {
               drive_params.lba_count, drive_params.bytes_per_sect);
 
         size_t part_count = 0;
+
+load_up:
         for (int part = 0; ; part++) {
             struct part p;
             int ret = get_part(&p, drive, part);
@@ -176,21 +176,16 @@ void part_create_index(void) {
             if (ret == NO_PARTITION)
                 continue;
 
-            part_count++;
+            if (part_index)
+                part_index[part_index_i++] = p;
+            else
+                part_count++;
         }
 
-        part_index = conv_mem_alloc(sizeof(struct part) * part_count);
-
-        for (int part = 0; ; part++) {
-            struct part p;
-            int ret = get_part(&p, drive, part);
-
-            if (ret == END_OF_TABLE)
-                break;
-            if (ret == NO_PARTITION)
-                continue;
+        if (part_index)
+            return;
 
-            part_index[part_index_i++] = p;
-        }
+        part_index = conv_mem_alloc(sizeof(struct part) * part_count);
+        goto load_up;
     }
 }
diff --git a/stage2/lib/rand.c b/stage2/lib/rand.c
index 06608125..abfa2597 100644
--- a/stage2/lib/rand.c
+++ b/stage2/lib/rand.c
@@ -66,7 +66,7 @@ static void init_rand(void) {
         seed *= (seed ^ rdrand(uint32_t));
     }
 
-    status = conv_mem_alloc(n);
+    status = ext_mem_alloc(n);
 
     srand(seed);
 
diff --git a/stage2/protos/stivale2.c b/stage2/protos/stivale2.c
index b952de42..60bb6d0a 100644
--- a/stage2/protos/stivale2.c
+++ b/stage2/protos/stivale2.c
@@ -156,19 +156,26 @@ void stivale2_load(char *cmdline) {
     // Create modules struct tag
     //////////////////////////////////////////////
     {
-    struct stivale2_struct_tag_modules *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_modules));
-    tag->tag.identifier = STIVALE2_STRUCT_TAG_MODULES_ID;
+    size_t module_count;
+    for (module_count = 0; ; module_count++) {
+        char module_file[64];
+        if (!config_get_value(module_file, module_count, 64, "MODULE_PATH"))
+            break;
+    }
+
+    struct stivale2_struct_tag_modules *tag =
+        conv_mem_alloc(sizeof(struct stivale2_struct_tag_modules)
+                     + sizeof(struct stivale2_module) * module_count);
 
-    tag->module_count = 0;
+    tag->tag.identifier = STIVALE2_STRUCT_TAG_MODULES_ID;
+    tag->module_count   = module_count;
 
     for (int i = 0; ; i++) {
         char module_file[64];
         if (!config_get_value(module_file, i, 64, "MODULE_PATH"))
             break;
 
-        tag->module_count++;
-
-        struct stivale2_module *m = conv_mem_alloc_aligned(sizeof(struct stivale2_module), 1);
+        struct stivale2_module *m = &tag->modules[i];
 
         if (!config_get_value(m->string, i, 128, "MODULE_STRING")) {
             m->string[0] = '\0';
@@ -292,15 +299,17 @@ void stivale2_load(char *cmdline) {
     // Create memmap struct tag
     //////////////////////////////////////////////
     {
-    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);
 
+    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;
 
-    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);
+    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);
     }
diff --git a/stage2/sys/e820.c b/stage2/sys/e820.c
index 71533b28..c275be26 100644
--- a/stage2/sys/e820.c
+++ b/stage2/sys/e820.c
@@ -6,13 +6,14 @@
 #include <lib/print.h>
 #include <mm/pmm.h>
 
-struct e820_entry_t *e820_map;
+struct e820_entry_t *e820_map = NULL;
 size_t e820_entries;
 
 void init_e820(void) {
     struct rm_regs r = {0};
 
-    e820_map = conv_mem_alloc(sizeof(struct e820_entry_t));
+load_up:
+    // Figure out the number of entries
     for (size_t i = 0; ; i++) {
         struct e820_entry_t entry;
 
@@ -22,18 +23,23 @@ void init_e820(void) {
         r.edi = (uint32_t)&entry;
         rm_int(0x15, &r, &r);
 
-        e820_map[i] = entry;
-
         if (r.eflags & EFLAGS_CF) {
             e820_entries = i;
             break;
         }
 
+        if (e820_map)
+            e820_map[i] = entry;
+
         if (!r.ebx) {
             e820_entries = ++i;
             break;
         }
-
-        conv_mem_alloc(sizeof(struct e820_entry_t));
     }
+
+    if (e820_map)
+        return;
+
+    e820_map = conv_mem_alloc(sizeof(struct e820_entry_t) * e820_entries);
+    goto load_up;
 }
tab: 248 wrap: offon