:: commit 21056a32110421eb98d3ed789590413c46e884ee

mintsuki <mintsuki@protonmail.com> — 2024-11-25 11:58

parents: 244d3a129d

sys/smp: Ensure machines with pre-enabled x2APIC are properly supported

diff --git a/common/sys/smp.c b/common/sys/smp.c
index d9b52c9e..73acc3a0 100644
--- a/common/sys/smp.c
+++ b/common/sys/smp.c
@@ -121,18 +121,28 @@ struct limine_smp_info *init_smp(size_t   *cpu_count,
 
     struct gdtr gdtr = gdt;
 
-    uint8_t bsp_lapic_id = lapic_read(LAPIC_REG_ID) >> 24;
-    *_bsp_lapic_id = bsp_lapic_id;
+    uint8_t bsp_lapic_id;
+    uint32_t bsp_x2apic_id;
 
-    x2apic = x2apic && x2apic_enable();
+    // If x2APIC already enabled by BIOS, then xAPIC is not available
+    if (rdmsr(0x1b) & (1 << 10)) {
+        if (!x2apic) {
+            panic(false, "smp: Kernel does not support x2APIC, but machine requires it");
+        }
+    }
 
-    uint32_t bsp_x2apic_id = bsp_lapic_id;
+    x2apic = x2apic && x2apic_enable();
 
     if (x2apic) {
         bsp_x2apic_id = x2apic_read(LAPIC_REG_ID);
-        *_bsp_lapic_id = bsp_x2apic_id;
+        bsp_lapic_id = bsp_x2apic_id;
+    } else {
+        bsp_lapic_id = lapic_read(LAPIC_REG_ID) >> 24;
+        bsp_x2apic_id = bsp_lapic_id;
     }
 
+    *_bsp_lapic_id = bsp_x2apic_id;
+
     *cpu_count = 0;
 
     // Count the MAX of startable APs and allocate accordingly
tab: 248 wrap: offon