From 60ba364ec3c458b6bcf3d048781c8dd88937cd0e Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 13 Nov 2026 11:40:07 +0100 Subject: kapi/memory: fix page unmap logic Previously, the page mapper was expected to automatically free all frames that had become unused. However, this only works if all mappings refer to plain memory frames. In the case of MMIO frames, the mapper would still try to release the frame via the global frame allocator, which is incorrect, leading to undefined behavior in the worst case. This problem was discovered during work the PCI/PCIe subsystem, since we want to unmap any unused buses during enumeration. --- arch/x86_64/arch/memory/higher_half_mapper.cpp | 36 ++++++++++++++++++-------- arch/x86_64/arch/memory/higher_half_mapper.hpp | 5 ++-- 2 files changed, 28 insertions(+), 13 deletions(-) (limited to 'arch') diff --git a/arch/x86_64/arch/memory/higher_half_mapper.cpp b/arch/x86_64/arch/memory/higher_half_mapper.cpp index 8a069c86..9019e25b 100644 --- a/arch/x86_64/arch/memory/higher_half_mapper.cpp +++ b/arch/x86_64/arch/memory/higher_half_mapper.cpp @@ -8,10 +8,10 @@ #include -#include #include #include #include +#include #include #include @@ -44,15 +44,17 @@ namespace arch::memory return static_cast>(page.start_address()); } - auto higher_half_mapper::unmap(kapi::memory::page page) -> void + auto higher_half_mapper::unmap(kapi::memory::page page) -> kapi::memory::frame { - if (!try_unmap(page)) + auto frame = try_unmap(page); + if (!frame) { kapi::system::panic("[ARCH:MEM] Tried to unmap a page that is not mapped!"); } + return *frame; } - auto higher_half_mapper::try_unmap(kapi::memory::page page) noexcept -> bool + auto higher_half_mapper::try_unmap(kapi::memory::page page) noexcept -> std::optional { auto table_path = std::array, std::size_t>, 4>{}; table_path[0] = std::pair{m_root, pml_index(4, page)}; @@ -64,15 +66,20 @@ namespace arch::memory if (!entry.present()) { - return false; + return std::nullopt; } auto next_table = to_higher_half_pointer(entry.frame()->start_address()); - auto next_index = pml_index(4 - level - 1, page); - table_path[4 - level - 1] = std::pair{next_table, next_index}; + auto next_index = pml_index(level - 1, page); + table_path[4 - (level - 1)] = std::pair{next_table, next_index}; } - std::ranges::for_each(std::views::reverse(table_path), [previous_was_empty = true](auto & step) mutable { + auto release_path = std::views::reverse(table_path); + auto previous_was_empty = true; + auto unmapped_frame = std::optional{}; + + for (auto & step : release_path) + { auto [table, index] = step; auto & entry = (*table)[index]; auto frame = entry.frame(); @@ -81,11 +88,18 @@ namespace arch::memory { entry.clear(); previous_was_empty = table->empty(); - kapi::memory::get_frame_allocator().release(*frame); + if (!unmapped_frame) + { + unmapped_frame = *frame; + } + else + { + kapi::memory::get_frame_allocator().release(*frame); + } } - }); + } - return true; + return unmapped_frame; } auto higher_half_mapper::get_or_create_page_table(kapi::memory::page page) noexcept -> kstd::observer_ptr diff --git a/arch/x86_64/arch/memory/higher_half_mapper.hpp b/arch/x86_64/arch/memory/higher_half_mapper.hpp index 45ff100a..f46e6f3c 100644 --- a/arch/x86_64/arch/memory/higher_half_mapper.hpp +++ b/arch/x86_64/arch/memory/higher_half_mapper.hpp @@ -8,6 +8,7 @@ #include #include +#include namespace arch::memory { @@ -24,10 +25,10 @@ namespace arch::memory auto map(kapi::memory::page page, kapi::memory::frame frame, flags flags) -> kstd::observer_ptr override; //! @copydoc kapi::memory::page_mapper::unmap - auto unmap(kapi::memory::page page) -> void override; + auto unmap(kapi::memory::page page) -> kapi::memory::frame override; //! @copydoc kapi::memory::page_mapper::try_unmap - auto try_unmap(kapi::memory::page page) noexcept -> bool override; + auto try_unmap(kapi::memory::page page) noexcept -> std::optional override; private: //! Try to retrieve the the PML1 responsible for mapping this page, creating one if necessary. -- cgit v1.2.3