:: commit 5bd25ac5205438867d65501e187c57ae9a7d6acf

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

parents: ebb543f87c

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 05ca393b..c65fe0f9 100644
Binary files a/limine.bin and b/limine.bin differ
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 5e4ca21c..7adcba5a 100644
--- a/stage2/protos/stivale2.c
+++ b/stage2/protos/stivale2.c
@@ -148,19 +148,26 @@ void stivale2_load(char *cmdline, int boot_drive) {
     // 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';
@@ -284,15 +291,17 @@ void stivale2_load(char *cmdline, int boot_drive) {
     // 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