diff options
| author | Matteo Gmür <matteo.gmuer1@ost.ch> | 2024-10-27 14:55:52 +0000 |
|---|---|---|
| committer | Matteo Gmür <matteo.gmuer1@ost.ch> | 2024-10-27 14:55:52 +0000 |
| commit | acbd91c9898808a928af0b1bdd9d5058e8a91f62 (patch) | |
| tree | f082572bd210424a9f59a29a64d7037e402bd355 | |
| parent | 38e87d52891429d56d20a54ce205d1e421068f36 (diff) | |
| download | teachos-acbd91c9898808a928af0b1bdd9d5058e8a91f62.tar.xz teachos-acbd91c9898808a928af0b1bdd9d5058e8a91f62.zip | |
Add typedef for virtual / physical addresses
6 files changed, 36 insertions, 32 deletions
diff --git a/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp b/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp index e33c77a..87c14ac 100644 --- a/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp +++ b/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp @@ -6,6 +6,8 @@ namespace teachos::arch::memory::allocator { + typedef std::size_t physical_address; + constexpr std::size_t PAGE_FRAME_SIZE = 4096U; ///< Default page size of x86_84 is always 4KiB. /** @@ -24,10 +26,10 @@ namespace teachos::arch::memory::allocator /** * @brief Returns the physical frame the given address is contained in. * - * @param physical_address Physical address we want to get the corresponding physical frame for. + * @param address Physical address we want to get the corresponding physical frame for. * @return Frame the given address is contained in. */ - auto static containing_address(std::size_t physical_address) -> physical_frame; + auto static containing_address(physical_address address) -> physical_frame; /** * @brief Evaluates the start address of the physical frame. diff --git a/arch/x86_64/include/arch/memory/paging/page_mapper.hpp b/arch/x86_64/include/arch/memory/paging/page_mapper.hpp index 7a8e2c9..5b74bd2 100644 --- a/arch/x86_64/include/arch/memory/paging/page_mapper.hpp +++ b/arch/x86_64/include/arch/memory/paging/page_mapper.hpp @@ -30,8 +30,16 @@ namespace teachos::arch::memory::paging auto create_or_get() -> page_table_handle; /** - * @brief Translates page into physical frame, will first attempt to parse normally using default page size and if it - * failed attempt to parse using huge pages. + * @brief Translates virtual address into corresponding physical address. Calls translate_page under the hood. + * + * @param address Virtual address we want to translate into physical one. + * @return Physical address corresponding to the provided virtual address. + */ + auto translate_address(virtual_address address) -> std::optional<allocator::physical_address>; + + /** + * @brief Translates page into physical frame, will first attempt to parse normally using default page size and if + * it failed attempt to parse using huge pages. * * @param page Page to translate into physical frame. * @return Physical frame corresponding to the provided virtual page. @@ -47,14 +55,6 @@ namespace teachos::arch::memory::paging auto translate_huge_page(virtual_page page) -> std::optional<allocator::physical_frame>; /** - * @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>; - - /** * @brief Maps a virtual page to a physical frame in the page table with the specified flags. * * @note Allocates and maps an entry in every page level if it does not exists yet down to level 1. If the level 1 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 f01073d..b9e2195 100644 --- a/arch/x86_64/include/arch/memory/paging/virtual_page.hpp +++ b/arch/x86_64/include/arch/memory/paging/virtual_page.hpp @@ -10,6 +10,8 @@ namespace teachos::arch::memory::paging { + typedef std::size_t virtual_address; + /** * @brief Virtual page entry contained in P1 page tables */ @@ -25,10 +27,10 @@ namespace teachos::arch::memory::paging /** * @brief Returns the virtual page the given address is contained in. * - * @param virtual_address Virtual address we want to get the corresponding virtual page for. + * @param address Virtual address we want to get the corresponding virtual page for. * @return Frame the given address is contained in. */ - auto static containing_address(std::size_t virtual_address) -> virtual_page; + auto static containing_address(virtual_address address) -> virtual_page; /** * @brief Evaluates the start address of the virtual page. diff --git a/arch/x86_64/src/memory/allocator/physical_frame.cpp b/arch/x86_64/src/memory/allocator/physical_frame.cpp index 03bd965..b05254b 100644 --- a/arch/x86_64/src/memory/allocator/physical_frame.cpp +++ b/arch/x86_64/src/memory/allocator/physical_frame.cpp @@ -8,7 +8,7 @@ namespace teachos::arch::memory::allocator // Nothing to do } - auto physical_frame::containing_address(std::size_t address) -> physical_frame + auto physical_frame::containing_address(physical_address address) -> physical_frame { return physical_frame{address / PAGE_FRAME_SIZE}; } diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index bc0c0d9..00c27b0 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -19,6 +19,20 @@ namespace teachos::arch::memory::paging return active_handle; } + auto translate_address(virtual_address address) -> std::optional<allocator::physical_address> + { + auto offset = address % allocator::PAGE_FRAME_SIZE; + auto page = virtual_page::containing_address(address); + auto frame = translate_page(page); + + if (frame.has_value()) + { + return frame.value().frame_number * allocator::PAGE_FRAME_SIZE + offset; + } + + return std::nullopt; + } + auto translate_page(virtual_page page) -> std::optional<allocator::physical_frame> { auto current_handle = create_or_get(); @@ -77,18 +91,4 @@ namespace teachos::arch::memory::paging } return std::nullopt; } - - 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 diff --git a/arch/x86_64/src/memory/paging/virtual_page.cpp b/arch/x86_64/src/memory/paging/virtual_page.cpp index db0d96c..d39bb7f 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -10,11 +10,11 @@ namespace teachos::arch::memory::paging // Nothing to do } - auto virtual_page::containing_address(std::size_t virtual_address) -> virtual_page + auto virtual_page::containing_address(virtual_address address) -> virtual_page { - exception_handling::assert(virtual_address < 0x0000800000000000 || virtual_address >= 0xffff800000000000, + exception_handling::assert(address < 0x0000800000000000 || address >= 0xffff800000000000, "[Virtual Page] Attempted to create virtual page from invalid address"); - return virtual_page{virtual_address / allocator::PAGE_FRAME_SIZE}; + return virtual_page{address / allocator::PAGE_FRAME_SIZE}; } auto virtual_page::start_address() const -> size_t { return page_number * allocator::PAGE_FRAME_SIZE; } |
