From c17cdb39d52859e194fb14fc213c7ec73cb83cd8 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 17 Oct 2023 01:15:08 +0300 Subject: [PATCH] Kernel: Map multiboot2 memory in PageTable initialization It cannot be assumed that multiboot data lies between kernel_end and 2 GiB mark, so I properly allocate virtual address space for it. --- kernel/arch/x86_64/PageTable.cpp | 26 ++++++++++++++++---------- kernel/arch/x86_64/boot.S | 2 -- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/kernel/arch/x86_64/PageTable.cpp b/kernel/arch/x86_64/PageTable.cpp index 2444379748..ef0db19fbb 100644 --- a/kernel/arch/x86_64/PageTable.cpp +++ b/kernel/arch/x86_64/PageTable.cpp @@ -138,16 +138,6 @@ namespace Kernel // Map (0 -> phys_kernel_end) to (KERNEL_OFFSET -> virt_kernel_end) map_range_at(0, KERNEL_OFFSET, (uintptr_t)g_kernel_end - KERNEL_OFFSET, Flags::ReadWrite | Flags::Present); - - // Map multiboot info - vaddr_t multiboot_data_start = (vaddr_t)g_multiboot2_info & PAGE_ADDR_MASK; - vaddr_t multiboot_data_end = (vaddr_t)g_multiboot2_info + g_multiboot2_info->total_size; - map_range_at( - V2P(multiboot_data_start), - multiboot_data_start, - multiboot_data_end - multiboot_data_start, - Flags::ReadWrite | Flags::Present - ); // Map executable kernel memory as executable map_range_at( @@ -164,6 +154,22 @@ namespace Kernel g_userspace_end - g_userspace_start, Flags::Execute | Flags::UserSupervisor | Flags::Present ); + + // Map multiboot memory + paddr_t multiboot2_data_start = (vaddr_t)g_multiboot2_info & PAGE_ADDR_MASK; + paddr_t multiboot2_data_end = (vaddr_t)g_multiboot2_info + g_multiboot2_info->total_size; + + size_t multiboot2_needed_pages = BAN::Math::div_round_up(multiboot2_data_end - multiboot2_data_start, PAGE_SIZE); + vaddr_t multiboot2_vaddr = reserve_free_contiguous_pages(multiboot2_needed_pages, KERNEL_OFFSET); + + map_range_at( + multiboot2_data_start, + multiboot2_vaddr, + multiboot2_needed_pages * PAGE_SIZE, + Flags::ReadWrite | Flags::Present + ); + + g_multiboot2_info = (multiboot2_info_t*)(multiboot2_vaddr + ((vaddr_t)g_multiboot2_info % PAGE_SIZE)); } BAN::ErrorOr PageTable::create_userspace() diff --git a/kernel/arch/x86_64/boot.S b/kernel/arch/x86_64/boot.S index b00995d987..ab4d54bf8c 100644 --- a/kernel/arch/x86_64/boot.S +++ b/kernel/arch/x86_64/boot.S @@ -197,8 +197,6 @@ long_mode: jmp *%rcx higher_half: - addq $KERNEL_OFFSET, g_multiboot2_info - # call global constuctors call _init