From da611ab07061833ba6de844a356b1061731847b0 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 8 Feb 2026 11:52:33 +0000 Subject: [PATCH] x86_64: find, record, and reserve the memory location of the bsp --- arch/x86_64/init.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/arch/x86_64/init.c b/arch/x86_64/init.c index 9429416..2a74599 100644 --- a/arch/x86_64/init.c +++ b/arch/x86_64/init.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -30,7 +31,7 @@ static void bootstrap_cpu_init(void) ml_cpu_block_use(&g_bootstrap_cpu); } -static void early_vm_init(void) +static void early_vm_init(uintptr_t reserve_end) { uintptr_t alloc_start = VM_KERNEL_VOFFSET; /* boot code mapped 2 GiB of memory from @@ -42,10 +43,10 @@ static void early_vm_init(void) alloc_start, alloc_end); - memblock_reserve(0x00, (uintptr_t)__pend); + memblock_reserve(0x00, reserve_end); printk("memblock: reserved bios+kernel at [0x%016llx-0x%016llx]", 0, - (uintptr_t)__pend); + reserve_end); } void early_console_init(void) @@ -70,6 +71,23 @@ static void use_uniprocessor_topology(void) cpu_set_online(0); } +static void find_bsp(multiboot_info_t *mb, struct boot_module *out) +{ + memset(out, 0x0, sizeof *out); + + printk("modules=%u: %llx", mb->mods_count, mb->mods_addr); + + multiboot_module_t *mods = PTR32(mb->mods_addr); + size_t nr_mods = mb->mods_count; + + if (nr_mods < 1) { + return; + } + + out->mod_base = mods[0].mod_start; + out->mod_size = mods[0].mod_end - mods[0].mod_start; +} + int ml_init(uintptr_t arg) { multiboot_info_t *mb = (multiboot_info_t *)arg; @@ -83,7 +101,16 @@ int ml_init(uintptr_t arg) print_kernel_banner(); - early_vm_init(); + struct boot_module bsp; + find_bsp(mb, &bsp); + bsp_set_location(&bsp); + + uintptr_t reserve_end = (uintptr_t)__pend; + if (bsp.mod_base + bsp.mod_size > reserve_end) { + reserve_end = bsp.mod_base + bsp.mod_size; + } + + early_vm_init(reserve_end); e820_scan(PTR32(mb->mmap_addr), mb->mmap_length);