diff options
| author | Fabian Imhof <fabian.imhof@ost.ch> | 2024-10-20 08:11:19 +0000 |
|---|---|---|
| committer | Fabian Imhof <fabian.imhof@ost.ch> | 2024-10-20 08:11:19 +0000 |
| commit | 882ccdcc0e3c19fbcc595c6a371ef79587f63648 (patch) | |
| tree | 2520cfb16eebc6f58206fe96633183946533da87 /arch/x86_64/include | |
| parent | f8928c877c48b5beba9bb42876f70213aba68f88 (diff) | |
| download | teachos-882ccdcc0e3c19fbcc595c6a371ef79587f63648.tar.xz teachos-882ccdcc0e3c19fbcc595c6a371ef79587f63648.zip | |
implement translate_page
Diffstat (limited to 'arch/x86_64/include')
| -rw-r--r-- | arch/x86_64/include/arch/memory/paging/page_table.hpp | 2 | ||||
| -rw-r--r-- | arch/x86_64/include/arch/memory/paging/virtual_page.hpp | 121 |
2 files changed, 63 insertions, 60 deletions
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 bbee477..aa8bd5e 100644 --- a/arch/x86_64/include/arch/memory/paging/page_table.hpp +++ b/arch/x86_64/include/arch/memory/paging/page_table.hpp @@ -54,7 +54,7 @@ namespace teachos::arch::memory::paging * * @param table_index Index of this page table in the page table one level higher. */ - auto next_table(std::size_t table_index) -> void; + auto next_table(std::size_t table_index) -> bool; /** * @brief Index operator overload to access specific mutable entry directy. 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 b2adca3..5a025fa 100644 --- a/arch/x86_64/include/arch/memory/paging/virtual_page.hpp +++ b/arch/x86_64/include/arch/memory/paging/virtual_page.hpp @@ -2,8 +2,8 @@ #define TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP #include "arch/memory/allocator/physical_frame.hpp" -#include "arch/memory/paging/page_table.hpp" +#include "page_table.hpp" #include <compare> #include <cstdint> #include <optional> @@ -11,64 +11,6 @@ namespace teachos::arch::memory::paging { /** - * @brief Translate virtual into phyical address - * - * @param virtual_address Address to translate into physical - * @return Physical address - */ - auto static translate_address(std::size_t virtual_address) -> std::optional<std::size_t> - { - std::size_t offset = virtual_address % allocator::PAGE_FRAME_SIZE; - virtual_page page = virtual_page::containing_address(virtual_address); - allocator::physical_frame frame = paging::translate_page(page); - - if (frame.value()) - { - return frame->number * allocator::PAGE_FRAME_SIZE + offset; - } - - return std::nullopt; - } - - /** - * @brief Translate page into physical_frame - * - * @param page Page to translate into physical_frame - * @return Physical frame corresponding to the provided page_table - */ - auto static translate_page(virtual_page page) -> std::optional<allocator::physical_frame> - { - auto level3 = - reinterpret_cast<page_table>(page_table::LEVEL4)->next_table(page.get_level_index(page_table::LEVEL4)); - - auto huge_page = []() { - // TODO - }; - - if (level3.value()) - { - auto level2 = level3->next_table(page.get_level_index(page_table::LEVEL3)); - - if (level2.value()) - { - auto level1 = level2->next_table(page.get_level_index(page_table::LEVEL2)); - - if (level1.value()) - { - auto frame = level1[page.get_level_index(page_table::LEVEL1)].calculate_pointed_to_frame(); - - if (frame.value()) - { - return frame; - } - } - } - } - - return huge_page(); - } - - /** * @brief Virtual page entry contained in P1 page tables */ struct virtual_page @@ -115,6 +57,67 @@ namespace teachos::arch::memory::paging std::size_t page_number; ///< Index number of the current virtual page, used to distinguish it from other pages. }; + + /** + * @brief Translate page into physical_frame + * + * @param page Page to translate into physical_frame + * @return Physical frame corresponding to the provided page_table + */ + auto translate_page(virtual_page page) -> std::optional<allocator::physical_frame> + { + page_table page_table{}; + bool is_valid = page_table.next_table(page.get_level_index(page_table::LEVEL4)); + + auto huge_page = []() -> std::optional<allocator::physical_frame> { + // TODO + return std::nullopt; + }; + + if (is_valid) + { + is_valid = page_table.next_table(page.get_level_index(page_table::LEVEL3)); + + if (is_valid) + { + is_valid = page_table.next_table(page.get_level_index(page_table::LEVEL2)); + + if (is_valid) + { + auto level1_index = page.get_level_index(page_table::LEVEL1); + auto frame = page_table[level1_index].calculate_pointed_to_frame(); + + if (frame.has_value()) + { + return frame; + } + } + } + } + + return huge_page(); + } + + /** + * @brief Translate virtual into phyical address + * + * @param virtual_address Address to translate into physical + * @return Physical address + */ + auto translate_address(std::size_t virtual_address) -> std::optional<std::size_t> + { + std::size_t offset = virtual_address % allocator::PAGE_FRAME_SIZE; + virtual_page page = virtual_page::containing_address(virtual_address); + std::optional<allocator::physical_frame> frame = translate_page(page); + + if (frame.has_value()) + { + return frame.value().frame_number * allocator::PAGE_FRAME_SIZE + offset; + } + + return std::nullopt; + } + } // namespace teachos::arch::memory::paging #endif // TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP |
