:: commit 13b7bc44806cedc336998167b6efe550be714fb0

Mintsuki <mintsuki@protonmail.com> — 2025-06-20 05:12

parents: c159516850

smp: For x86, ensure that the value of IA32_APIC_BASE is the same for APs as for BSP

diff --git a/common/sys/smp.c b/common/sys/smp.c
index b5b5562d..048b31a6 100644
--- a/common/sys/smp.c
+++ b/common/sys/smp.c
@@ -33,6 +33,7 @@ struct trampoline_passed_info {
     uint32_t smp_tpl_info_struct;
     struct gdtr smp_tpl_gdt;
     uint64_t smp_tpl_hhdm;
+    uint64_t smp_tpl_bsp_apic_addr_msr;
     uint64_t smp_tpl_mtrr_restore;
     uint64_t smp_tpl_temp_stack;
 } __attribute__((packed));
@@ -63,12 +64,12 @@ static bool smp_start_ap(uint32_t lapic_id, struct gdtr *gdtr,
     passed_info->smp_tpl_info_struct = (uint32_t)(uintptr_t)info_struct;
     passed_info->smp_tpl_booted_flag = 0;
     passed_info->smp_tpl_pagemap     = pagemap;
-    passed_info->smp_tpl_target_mode = ((uint32_t)x2apic << 2)
-                                     | ((uint32_t)(paging_mode == PAGING_MODE_X86_64_5LVL) << 1)
+    passed_info->smp_tpl_target_mode = ((uint32_t)(paging_mode == PAGING_MODE_X86_64_5LVL) << 1)
                                      | ((uint32_t)nx << 3)
                                      | ((uint32_t)wp << 4);
     passed_info->smp_tpl_gdt = *gdtr;
     passed_info->smp_tpl_hhdm = hhdm;
+    passed_info->smp_tpl_bsp_apic_addr_msr = rdmsr(0x1b);
     passed_info->smp_tpl_mtrr_restore = (uint64_t)(uintptr_t)mtrr_restore;
     passed_info->smp_tpl_temp_stack = (uint64_t)(uintptr_t)temp_stack;
 
diff --git a/common/sys/smp_trampoline.asm_x86 b/common/sys/smp_trampoline.asm_x86
index 29d01591..1a8a8473 100644
--- a/common/sys/smp_trampoline.asm_x86
+++ b/common/sys/smp_trampoline.asm_x86
@@ -52,13 +52,11 @@ smp_trampoline_start:
   .no_pat:
     mov ebx, esi
 
-    test dword [ebx + (passed_info.target_mode - smp_trampoline_start)], (1 << 2)
-    jz .nox2apic
-
     mov ecx, 0x1b
-    rdmsr
-    bts eax, 10
+    mov eax, [ebx + (passed_info.bsp_apic_addr_msr_lo - smp_trampoline_start)]
+    mov edx, [ebx + (passed_info.bsp_apic_addr_msr_hi - smp_trampoline_start)]
     bts eax, 11
+    btr eax, 8
     wrmsr
 
   .nox2apic:
@@ -216,6 +214,10 @@ passed_info:
         dq 0
     .hhdm:
         dq 0
+    .bsp_apic_addr_msr_lo:
+        dd 0
+    .bsp_apic_addr_msr_hi:
+        dd 0
     .mtrr_restore:
         dq 0
     .temp_stack:
tab: 248 wrap: offon