:: commit f59a3d67d66d022d357f273dac543a3a3a1df646

mintsuki <mintsuki@protonmail.com> — 2020-09-20 08:28

parents: 6c0ba4104f

Change how smp works a little

diff --git a/STIVALE2.md b/STIVALE2.md
index 745abb46..c44d2c30 100644
--- a/STIVALE2.md
+++ b/STIVALE2.md
@@ -378,10 +378,9 @@ This tag reports to the kernel info about the firmware.
 struct stivale2_struct_tag_smp {
     uint64_t identifier;        // Identifier: 0x34d1d96339647025
     uint64_t next;
-    uint64_t cpu_count;         // Total number of logical CPUs (excluding BSP)
+    uint64_t cpu_count;         // Total number of logical CPUs (including BSP)
     struct stivale2_smp_info smp_info[];  // Array of smp_info structs, one per
-                                          // additional logical processor, note
-                                          // that the BSP does not have one.
+                                          // logical processor, including BSP.
 } __attribute__((packed));
 ```
 
@@ -393,6 +392,8 @@ struct stivale2_smp_info {
                                  // once the goto_address field is loaded.
                                  // This MUST point to a valid stack of at least
                                  // 256 bytes in size, and 16-byte aligned.
+                                 // target_stack is an unused field for the
+                                 // struct describing the BSP (lapic_id == 0)
     uint64_t goto_address;       // This address is polled by the started APs
                                  // until the kernel on another CPU performs an
                                  // atomic write to this field.
@@ -405,12 +406,18 @@ struct stivale2_smp_info {
                                  // then, goto_address is called (a bogus return
                                  // address is pushed onto the stack) and execution
                                  // is handed off.
-                                 // All general purpose registers are cleared
-                                 // except ESP/RSP, and RDI in 64-bit mode.
+                                 // The CPU state will be the same as described
+                                 // in kernel entry machine state, with the exception
+                                 // of ESP/RSP and RDI/stack arg being set up as
+                                 // above.
+                                 // goto_address is an unused field for the
+                                 // struct describing the BSP (lapic_id == 0)
     uint64_t extra_argument;     // This field is here for the kernel to use
                                  // for whatever it wants. Writes here should
                                  // be performed before writing to goto_address
                                  // so that the receiving processor can safely
                                  // retrieve the data.
+                                 // extra_argument is an unused field for the
+                                 // struct describing the BSP (lapic_id == 0)
 } __attribute__((packed));
 ```
diff --git a/limine.bin b/limine.bin
index 24888aea..2e4c8676 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2/sys/smp.c b/stage2/sys/smp.c
index 36cd15ba..7db89388 100644
--- a/stage2/sys/smp.c
+++ b/stage2/sys/smp.c
@@ -103,9 +103,17 @@ struct smp_information *init_smp(size_t   *cpu_count,
 
                 struct madt_lapic *lapic = (void *)madt_ptr;
 
+                struct smp_information *info_struct =
+                        balloc_aligned(sizeof(struct smp_information), 1);
+
+                info_struct->acpi_processor_uid = lapic->acpi_processor_uid;
+                info_struct->lapic_id           = lapic->lapic_id;
+
                 // Do not try to restart the BSP
-                if (lapic->lapic_id == 0)
+                if (lapic->lapic_id == 0) {
+                    (*cpu_count)++;
                     continue;
+                }
 
                 // Check if we can actually try to start the AP
                 if (!((lapic->flags & 1) ^ ((lapic->flags >> 1) & 1)))
@@ -113,12 +121,6 @@ struct smp_information *init_smp(size_t   *cpu_count,
 
                 print("smp: Found candidate AP for bring-up. LAPIC ID: %u\n", lapic->lapic_id);
 
-                struct smp_information *info_struct =
-                        balloc_aligned(sizeof(struct smp_information), 1);
-
-                info_struct->acpi_processor_uid = lapic->acpi_processor_uid;
-                info_struct->lapic_id           = lapic->lapic_id;
-
                 // Try to start the AP
                 if (!smp_start_ap(lapic->lapic_id, &gdtr, info_struct,
                                   longmode, lv5, (uint32_t)pagemap.top_level)) {
@@ -130,7 +132,7 @@ struct smp_information *init_smp(size_t   *cpu_count,
                 print("smp: Successfully brought up AP\n");
 
                 (*cpu_count)++;
-                break;
+                continue;
             }
         }
     }
tab: 248 wrap: offon