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. --- kapi/kapi/memory.hpp | 3 ++- kapi/kapi/memory/page_mapper.hpp | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'kapi') diff --git a/kapi/kapi/memory.hpp b/kapi/kapi/memory.hpp index 4b14b553..2ae8b91b 100644 --- a/kapi/kapi/memory.hpp +++ b/kapi/kapi/memory.hpp @@ -82,7 +82,8 @@ namespace kapi::memory //! @warning This function will panic if no page mapper has been registered, or the page is not mapped. //! //! @param page The page to unmap - auto unmap(page page) -> void; + //! @return The frame the page was previously mapped to. + [[nodiscard]] auto unmap(page page) -> frame; //! Initialize the Memory-mapped I/O region system. //! diff --git a/kapi/kapi/memory/page_mapper.hpp b/kapi/kapi/memory/page_mapper.hpp index 5ac0210f..4a4827b6 100644 --- a/kapi/kapi/memory/page_mapper.hpp +++ b/kapi/kapi/memory/page_mapper.hpp @@ -11,6 +11,7 @@ #include #include +#include #include namespace kapi::memory @@ -54,13 +55,14 @@ namespace kapi::memory //! terminate execution via a kernel panic. //! //! @param page The page to unmap. - virtual auto unmap(page page) -> void = 0; + //! @return The frame the page was previously mapped to. + virtual auto unmap(page page) -> frame = 0; //! Try to unmap the given page. //! //! @param page The page to unmap - //! @return true iff. the page was successfully unmapped, false otherwise. - virtual auto try_unmap(page page) noexcept -> bool = 0; + //! @return The frame that was previously mapped to the page, if any. + virtual auto try_unmap(page page) noexcept -> std::optional = 0; //! @qualifier kernel-defined //! Map a page into a given frame, applyint the given flags. -- cgit v1.2.3