:: commit 07ac3de4e6ea332f9ab45c1eb0d1bd07759e33ec

mintsuki <mintsuki@protonmail.com> — 2020-10-25 11:41

parents: b7f4412948

SMP: Fix struct duplication bug

diff --git a/limine.bin b/limine.bin
index 316186ad..35f3c69a 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2/protos/stivale2.c b/stage2/protos/stivale2.c
index 508cf611..6aedbd05 100644
--- a/stage2/protos/stivale2.c
+++ b/stage2/protos/stivale2.c
@@ -320,25 +320,21 @@ void stivale2_load(char *cmdline) {
     {
     struct stivale2_header_tag_smp *smp_hdr_tag = get_tag(&stivale2_hdr, STIVALE2_HEADER_TAG_SMP_ID);
     if (smp_hdr_tag != NULL) {
+        struct stivale2_struct_tag_smp *tag;
         struct smp_information *smp_info;
         size_t cpu_count;
         uint32_t bsp_lapic_id;
-        smp_info = init_smp(&cpu_count, &bsp_lapic_id,
+        smp_info = init_smp(sizeof(struct stivale2_struct_tag_smp), (void **)&tag,
+                            &cpu_count, &bsp_lapic_id,
                             bits == 64, level5pg && level5pg_requested,
                             pagemap, smp_hdr_tag->flags & 1);
 
         if (smp_info != NULL) {
-            struct stivale2_struct_tag_smp *tag =
-                conv_mem_alloc(sizeof(struct stivale2_struct_tag_smp)
-                             + sizeof(struct smp_information) * cpu_count);
             tag->tag.identifier = STIVALE2_STRUCT_TAG_SMP_ID;
             tag->bsp_lapic_id   = bsp_lapic_id;
             tag->cpu_count      = cpu_count;
             tag->flags         |= (smp_hdr_tag->flags & 1) && x2apic_check();
 
-            memcpy((void*)tag + sizeof(struct stivale2_struct_tag_smp),
-                   smp_info, sizeof(struct smp_information) * cpu_count);
-
             append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
         }
     }
diff --git a/stage2/sys/smp.c b/stage2/sys/smp.c
index 9e9dc539..90c48c98 100644
--- a/stage2/sys/smp.c
+++ b/stage2/sys/smp.c
@@ -95,7 +95,9 @@ static bool smp_start_ap(uint32_t lapic_id, struct gdtr *gdtr,
     return false;
 }
 
-struct smp_information *init_smp(size_t   *cpu_count,
+struct smp_information *init_smp(size_t    header_hack_size,
+                                 void    **header_ptr,
+                                 size_t   *cpu_count,
                                  uint32_t *_bsp_lapic_id,
                                  bool      longmode,
                                  bool      lv5,
@@ -172,7 +174,9 @@ struct smp_information *init_smp(size_t   *cpu_count,
         }
     }
 
-    struct smp_information *ret = ext_mem_alloc(max_cpus * sizeof(struct smp_information));
+    *header_ptr = ext_mem_alloc(
+                  header_hack_size + max_cpus * sizeof(struct smp_information));
+    struct smp_information *ret = *header_ptr + header_hack_size;
     *cpu_count = 0;
 
     // Try to start all APs
diff --git a/stage2/sys/smp.h b/stage2/sys/smp.h
index ec4741c7..0e36df14 100644
--- a/stage2/sys/smp.h
+++ b/stage2/sys/smp.h
@@ -14,7 +14,9 @@ struct smp_information {
     uint64_t extra_argument;
 } __attribute__((packed));
 
-struct smp_information *init_smp(size_t   *cpu_count,
+struct smp_information *init_smp(size_t    header_hack_size,
+                                 void    **header_ptr,
+                                 size_t   *cpu_count,
                                  uint32_t *_bsp_lapic_id,
                                  bool      longmode,
                                  bool      lv5,
diff --git a/test/stivale2.c b/test/stivale2.c
index 789d320f..c2b83b97 100644
--- a/test/stivale2.c
+++ b/test/stivale2.c
@@ -3,7 +3,8 @@
 #include <e9print.h>
 #include <stddef.h>
 
-static uint8_t stack[4096] = {0};
+typedef uint8_t stack[4096];
+static stack stacks[10] = {0};
 void stivale2_main(struct stivale2_struct *info);
 
 struct stivale2_header_tag_smp smp_request = {
@@ -27,11 +28,16 @@ struct stivale2_header_tag_framebuffer framebuffer_request = {
 __attribute__((section(".stivale2hdr"), used))
 struct stivale2_header header2 = {
     .entry_point = (uint64_t)stivale2_main,
-    .stack       = (uintptr_t)stack + sizeof(stack),
+    .stack       = (uintptr_t)stacks[0] + sizeof(stack),
     .flags       = 0,
     .tags        = (uint64_t)&framebuffer_request
 };
 
+void ap_entry(struct stivale2_smp_info *s) {
+    e9_printf("AP %u started", s->lapic_id);
+    for (;;);
+}
+
 void stivale2_main(struct stivale2_struct *info) {
     // Print stuff.
     e9_puts("Stivale2 info passed to the kernel:");
@@ -104,12 +110,16 @@ void stivale2_main(struct stivale2_struct *info) {
                 e9_printf("\tBSP LAPIC ID: %d", s->bsp_lapic_id);
                 e9_printf("\tCPU Count:    %d", s->cpu_count);
                 for (size_t i = 0; i < s->cpu_count; i++) {
-                    struct stivale2_smp_info in = s->smp_info[i];
-                    e9_printf("\t\tProcessor ID:   %d", in.processor_id);
-                    e9_printf("\t\tLAPIC ID:       %d", in.lapic_id);
-                    e9_printf("\t\tTarget Stack:   %x", in.target_stack);
-                    e9_printf("\t\tGOTO Address:   %x", in.goto_address);
-                    e9_printf("\t\tExtra Argument: %x", in.extra_argument);
+                    struct stivale2_smp_info *in = &s->smp_info[i];
+                    e9_printf("\t\tProcessor ID:   %d", in->processor_id);
+                    e9_printf("\t\tLAPIC ID:       %d", in->lapic_id);
+                    e9_printf("\t\tTarget Stack:   %x", in->target_stack);
+                    e9_printf("\t\tGOTO Address:   %x", in->goto_address);
+                    e9_printf("\t\tExtra Argument: %x", in->extra_argument);
+                    if (in->lapic_id != s->bsp_lapic_id) {
+                        in->target_stack = (uintptr_t)stacks[in->lapic_id] + sizeof(stack);
+                        in->goto_address = ap_entry;
+                    }
                 }
                 break;
             }
tab: 248 wrap: offon