From 533b7c9e7151afeba86c4751129eb037961d987c Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 8 Oct 2022 15:00:42 +0200 Subject: [PATCH] Refactor ACPI::get_rsdt_or_xsdt() Much better now. Also, remove a FIXME in PMM.cpp, since we do map the page bitmap to virtual memory now. --- kernel/src/acpi/RSDT.cpp | 30 ++++++++++++++++-------------- kernel/src/memory/PMM.cpp | 3 +-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/kernel/src/acpi/RSDT.cpp b/kernel/src/acpi/RSDT.cpp index 5a9ee145..52ffaec7 100644 --- a/kernel/src/acpi/RSDT.cpp +++ b/kernel/src/acpi/RSDT.cpp @@ -9,26 +9,28 @@ extern BOOTBOOT bootboot; -ACPI::SDTHeader* ACPI::get_rsdt_or_xsdt() // FIXME: Refactor this ugly code. +ACPI::SDTHeader* ACPI::get_rsdt_or_xsdt() { - static void* cache = nullptr; - if (cache) return (SDTHeader*)cache; + static SDTHeader* cache = nullptr; + if (cache) return cache; + kdbgln("First time accessing the RSDT/XSDT, mapping it into memory"); void* physical = (void*)bootboot.arch.x86_64.acpi_ptr; - uint64_t offset = (uint64_t)physical % PAGE_SIZE; kdbgln("RSDT/XSDT physical address: %lx", (uint64_t)physical); - cache = MemoryManager::get_unaligned_mapping(physical); - uint64_t numPages = 1; - while ((offset + ((SDTHeader*)cache)->Length) > (numPages * PAGE_SIZE)) + + cache = (SDTHeader*)MemoryManager::get_unaligned_mapping(physical); + + uint64_t offset = (uint64_t)physical % PAGE_SIZE; + uint64_t rsdt_pages = (offset + cache->Length) / PAGE_SIZE; + + if (rsdt_pages > 1) { - kwarnln("RSDT/XSDT extends beyond the mapped page, mapping one more page"); - MemoryManager::release_unaligned_mappings(cache, numPages); - numPages++; - cache = MemoryManager::get_unaligned_mappings(cache, numPages); + MemoryManager::release_unaligned_mapping(cache); + cache = (SDTHeader*)MemoryManager::get_unaligned_mappings(cache, rsdt_pages); } - kdbgln("Mapped RSDT/XSDT to virtual address %lx, uses %ld pages", (uint64_t)cache, numPages); - SDTHeader* result = (SDTHeader*)cache; - return result; + + kdbgln("Mapped RSDT/XSDT to virtual address %lx, uses %ld pages", (uint64_t)cache, rsdt_pages); + return cache; } bool ACPI::validate_rsdt_or_xsdt(ACPI::SDTHeader* root_sdt) diff --git a/kernel/src/memory/PMM.cpp b/kernel/src/memory/PMM.cpp index b8ab8ea1..94f35373 100644 --- a/kernel/src/memory/PMM.cpp +++ b/kernel/src/memory/PMM.cpp @@ -48,8 +48,7 @@ void PMM::init() } bitmap_addr = (char*)biggest_chunk; - virtual_bitmap_addr = - bitmap_addr; // FIXME: map this to a virtual address (ideally in the kernel heap between -128M and -64M) + virtual_bitmap_addr = bitmap_addr; ASSERT((total_mem / PAGE_SIZE / 8) < biggest_chunk_size); bitmap_size = total_mem / PAGE_SIZE / 8 + 1; memset(bitmap_addr, 0xFF, bitmap_size);