diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-03-17 14:19:02 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-03-17 14:19:02 +0100 |
| commit | 541670e49812b5b07079cc86367247402ace331a (patch) | |
| tree | 1276c3664c73f8d44c5c028b5c88ac2decaa9b66 /arch/x86_64/include | |
| parent | 796ce76185b00feb86f6b4f738ac6f953c247116 (diff) | |
| download | teachos-541670e49812b5b07079cc86367247402ace331a.tar.xz teachos-541670e49812b5b07079cc86367247402ace331a.zip | |
x86_64/memory: finish HHDM-based mapper
Diffstat (limited to 'arch/x86_64/include')
| -rw-r--r-- | arch/x86_64/include/arch/memory/higher_half_mapper.hpp | 25 | ||||
| -rw-r--r-- | arch/x86_64/include/arch/memory/page_utilities.hpp | 15 |
2 files changed, 36 insertions, 4 deletions
diff --git a/arch/x86_64/include/arch/memory/higher_half_mapper.hpp b/arch/x86_64/include/arch/memory/higher_half_mapper.hpp index 66fb58b..c8df40c 100644 --- a/arch/x86_64/include/arch/memory/higher_half_mapper.hpp +++ b/arch/x86_64/include/arch/memory/higher_half_mapper.hpp @@ -2,16 +2,41 @@ #define TEACHOS_X86_64_HIGHER_HALF_MAPPER_HPP #include "kapi/memory.hpp" + +#include "arch/memory/page_table.hpp" + +#include <cstddef> + namespace arch::memory { + //! A simple page mapper making use of a Higher Half Direct Map (HHDM) to access and modify page tables. struct higher_half_mapper : kapi::memory::page_mapper { + //! Construct a new mapper for a hierarchy rooted in the given PML. + //! + //! @param root The root of the hierarchy to operate on. + explicit higher_half_mapper(page_table * root); + + //! @copydoc kapi::memory::page_mapper::map auto map(kapi::memory::page page, kapi::memory::frame frame, flags flags) -> std::byte * override; + //! @copydoc kapi::memory::page_mapper::unmap auto unmap(kapi::memory::page page) -> void override; + //! @copydoc kapi::memory::page_mapper::try_unmap auto try_unmap(kapi::memory::page page) noexcept -> bool override; + + private: + //! Try to retrieve the the PML1 responsible for mapping this page, creating one if necessary. + //! + //! This function will create a page table hierarchy leading to the target PML1 if it doesn't exist. + //! + //! @param page The page to get the PML1 for. + //! @return The PML1 that manages the given page, nullptr it the system runs out of memory. + auto get_or_create_page_table(kapi::memory::page page) noexcept -> page_table *; + + page_table * m_root; }; } // namespace arch::memory diff --git a/arch/x86_64/include/arch/memory/page_utilities.hpp b/arch/x86_64/include/arch/memory/page_utilities.hpp index 8c25af3..c48e74f 100644 --- a/arch/x86_64/include/arch/memory/page_utilities.hpp +++ b/arch/x86_64/include/arch/memory/page_utilities.hpp @@ -8,15 +8,22 @@ namespace arch::memory { - template<std::size_t Level> - requires(Level > 0uz && Level < 5uz) - constexpr auto pml_index(kapi::memory::page page) noexcept -> std::size_t + constexpr auto inline pml_index(std::size_t index, kapi::memory::page page) noexcept -> std::size_t { - constexpr auto shift_width = (Level - 1) * 9; + constexpr auto bits_per_level = 9; + auto shift_width = (index - 1) * bits_per_level; constexpr auto index_mask = 0x1ffuz; return page.number() >> shift_width & index_mask; } + template<typename ValueType = void> + [[nodiscard]] constexpr auto to_higher_half_pointer(kapi::memory::physical_address address) -> ValueType * + { + using namespace kapi::memory; + auto const higher_half_address = higher_half_direct_map_base + address.raw(); + return static_cast<ValueType *>(higher_half_address); + } + } // namespace arch::memory #endif
\ No newline at end of file |
