From e5206b3bf1883fd9601a37f5cce392d8080b8791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 07:43:00 +0000 Subject: Add get level index method to virtual page --- arch/x86_64/include/arch/memory/paging/page_entry.hpp | 2 +- arch/x86_64/include/arch/memory/paging/page_table.hpp | 10 +--------- arch/x86_64/include/arch/memory/paging/virtual_page.hpp | 11 ++++++++++- arch/x86_64/src/memory/paging/page_table.cpp | 8 ++------ arch/x86_64/src/memory/paging/virtual_page.cpp | 5 +++++ 5 files changed, 19 insertions(+), 17 deletions(-) (limited to 'arch/x86_64') diff --git a/arch/x86_64/include/arch/memory/paging/page_entry.hpp b/arch/x86_64/include/arch/memory/paging/page_entry.hpp index a40e764..016e054 100644 --- a/arch/x86_64/include/arch/memory/paging/page_entry.hpp +++ b/arch/x86_64/include/arch/memory/paging/page_entry.hpp @@ -87,4 +87,4 @@ namespace teachos::arch::memory::paging }; } // namespace teachos::arch::memory::paging -#endif // TEACHOS_ARCH_X86_64_MEMORY_PAGING_PAGE_ENTRY_HPP \ No newline at end of file +#endif // TEACHOS_ARCH_X86_64_MEMORY_PAGING_PAGE_ENTRY_HPP diff --git a/arch/x86_64/include/arch/memory/paging/page_table.hpp b/arch/x86_64/include/arch/memory/paging/page_table.hpp index 73b75ad..bbee477 100644 --- a/arch/x86_64/include/arch/memory/paging/page_table.hpp +++ b/arch/x86_64/include/arch/memory/paging/page_table.hpp @@ -73,14 +73,6 @@ namespace teachos::arch::memory::paging auto operator[](std::size_t index) const -> entry const &; private: - /** - * @brief Constructor. Used internally to create new page tables. - * - * @param new_level New level of the page table. - * @param new_table New table data contained in the page table. - */ - page_table(level new_level, table_content * new_table); - /** * @brief Calculates the address of the next page table level for the given table index. The next page table address * is only valid if the corresponding entry is present and not a huge page. Meaning we use an index into a @@ -97,4 +89,4 @@ namespace teachos::arch::memory::paging }; } // namespace teachos::arch::memory::paging -#endif // TEACHOS_ARCH_X86_64_MEMORY_PAGING_PAGE_TABLE_HPP \ No newline at end of file +#endif // TEACHOS_ARCH_X86_64_MEMORY_PAGING_PAGE_TABLE_HPP diff --git a/arch/x86_64/include/arch/memory/paging/virtual_page.hpp b/arch/x86_64/include/arch/memory/paging/virtual_page.hpp index 7871e4b..a2e5316 100644 --- a/arch/x86_64/include/arch/memory/paging/virtual_page.hpp +++ b/arch/x86_64/include/arch/memory/paging/virtual_page.hpp @@ -1,6 +1,7 @@ #ifndef TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP #define TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP +#include "page_table.hpp" #include #include @@ -33,6 +34,14 @@ namespace teachos::arch::memory::paging */ auto start_address() const -> uint64_t; + /** + * @brief Calculates the index into the page table with the given level, which leads to this virtual page. + * + * @param level Level of the page table we want to calculate the index for. + * @return Index into the page table with the given level. + */ + auto get_level_index(page_table::level level) const -> uint64_t; + /** * @brief Defaulted equals operator. */ @@ -47,4 +56,4 @@ namespace teachos::arch::memory::paging }; } // namespace teachos::arch::memory::paging -#endif // TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP \ No newline at end of file +#endif // TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 8345161..9857294 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -26,12 +26,8 @@ namespace teachos::arch::memory::paging exception_handling::assert(current_level != LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); auto address = next_table_address(table_index); - - if (address.has_value()) - { - current_table = reinterpret_cast(address.value()); - current_level = static_cast(current_level - 1U); - } + current_table = reinterpret_cast(address.value()); + current_level = static_cast(current_level - 1U); } auto page_table::operator[](std::size_t index) -> entry & diff --git a/arch/x86_64/src/memory/paging/virtual_page.cpp b/arch/x86_64/src/memory/paging/virtual_page.cpp index 3fb6caf..dcdec7f 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -19,4 +19,9 @@ namespace teachos::arch::memory::paging } auto virtual_page::start_address() const -> uint64_t { return page_number * allocator::PAGE_FRAME_SIZE; } + + auto virtual_page::get_level_index(page_table::level level) const -> uint64_t + { + return (start_address() >> (level * 9U)) & 0x1FF; + } } // namespace teachos::arch::memory::paging -- cgit v1.2.3