diff options
| author | Fabian Imhof <fabian.imhof@ost.ch> | 2024-10-20 07:46:51 +0000 |
|---|---|---|
| committer | Fabian Imhof <fabian.imhof@ost.ch> | 2024-10-20 07:46:51 +0000 |
| commit | f8928c877c48b5beba9bb42876f70213aba68f88 (patch) | |
| tree | a72240b5765fccb98e05cdb0248023d5f5464823 /arch/x86_64/include | |
| parent | e5206b3bf1883fd9601a37f5cce392d8080b8791 (diff) | |
| download | teachos-f8928c877c48b5beba9bb42876f70213aba68f88.tar.xz teachos-f8928c877c48b5beba9bb42876f70213aba68f88.zip | |
translate
Diffstat (limited to 'arch/x86_64/include')
| -rw-r--r-- | arch/x86_64/include/arch/memory/paging/virtual_page.hpp | 63 |
1 files changed, 62 insertions, 1 deletions
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 a2e5316..b2adca3 100644 --- a/arch/x86_64/include/arch/memory/paging/virtual_page.hpp +++ b/arch/x86_64/include/arch/memory/paging/virtual_page.hpp @@ -1,13 +1,74 @@ #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 "arch/memory/allocator/physical_frame.hpp" +#include "arch/memory/paging/page_table.hpp" + #include <compare> #include <cstdint> +#include <optional> 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 |
