diff options
Diffstat (limited to 'arch')
| -rw-r--r-- | arch/x86_64/arch/memory/higher_half_mapper.cpp | 36 | ||||
| -rw-r--r-- | arch/x86_64/arch/memory/higher_half_mapper.hpp | 5 |
2 files changed, 28 insertions, 13 deletions
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 <kstd/memory.hpp> -#include <algorithm> #include <array> #include <cstddef> #include <memory> +#include <optional> #include <ranges> #include <utility> @@ -44,15 +44,17 @@ namespace arch::memory return static_cast<kstd::observer_ptr<std::byte>>(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<kapi::memory::frame> { auto table_path = std::array<std::pair<kstd::observer_ptr<page_table>, 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<page_table>(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<kapi::memory::frame>{}; + + 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<page_table> 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 <kstd/memory.hpp> #include <cstddef> +#include <optional> 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<std::byte> 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<kapi::memory::frame> override; private: //! Try to retrieve the the PML1 responsible for mapping this page, creating one if necessary. |
