diff options
| author | Matteo Gmür <matteo.gmuer1@ost.ch> | 2024-10-21 09:31:58 +0000 |
|---|---|---|
| committer | Matteo Gmür <matteo.gmuer1@ost.ch> | 2024-10-21 09:31:58 +0000 |
| commit | f171efed99684bf03c315405efda34e36d7db82c (patch) | |
| tree | 9935c48f6030f4a438ebd9fd1bcf58d4d4c8a233 /arch/x86_64/include | |
| parent | 49ae81912f3a440f1958e86296d468ec669f71a2 (diff) | |
| download | teachos-f171efed99684bf03c315405efda34e36d7db82c.tar.xz teachos-f171efed99684bf03c315405efda34e36d7db82c.zip | |
Ensure only one instance of global page table can exist
Diffstat (limited to 'arch/x86_64/include')
3 files changed, 38 insertions, 63 deletions
diff --git a/arch/x86_64/include/arch/memory/paging/active_page_table.hpp b/arch/x86_64/include/arch/memory/paging/active_page_table.hpp deleted file mode 100644 index 3933d5a..0000000 --- a/arch/x86_64/include/arch/memory/paging/active_page_table.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP -#define TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP - -#include "arch/memory/paging/page_table.hpp" - -namespace teachos::arch::memory::paging -{ - - struct active_page_table - { - /** - * @brief Ensures only one instance of active_page_table exists. - * - * @param level4_page_table A pointer to the level 4 page table. - * @return The only instance of active_page_table. - */ - auto create(page_table * level4_page_table) -> active_page_table *; - - private: - /** - * @brief Construct a new active page table object. - * - * @param level4_page_table A pointer to the level 4 page table. - */ - active_page_table(page_table * level4_page_table); - - bool instantiated = false; ///< Indicates wether an instance already exists. - page_table * level4_page_table; ///< The active level4 page table. - }; -} // namespace teachos::arch::memory::paging - -#endif // TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP 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 ae3502e..ebd044a 100644 --- a/arch/x86_64/include/arch/memory/paging/page_mapper.hpp +++ b/arch/x86_64/include/arch/memory/paging/page_mapper.hpp @@ -10,6 +10,15 @@ namespace teachos::arch::memory::paging { /** + * @brief Creates a single instance of the level 4 page table table and returns it or alternatively returns the + * previously created instance. The instance is owned by this method and is static, meaning it lives on for the + * complete lifetime of the program. + * + * @return Single unique instance of the level 4 page table. + */ + auto create_or_get() -> page_table *; + + /** * @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. * @@ -49,27 +58,35 @@ namespace teachos::arch::memory::paging auto map_page_to_frame(T & allocator, virtual_page page, allocator::physical_frame frame, std::bitset<64U> flags) -> void { - page_table page_table{}; - bool table_exists = false; + page_table * current_page_table = create_or_get(); for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--) { - std::size_t level_index = page.get_level_index(level); - table_exists = page_table.next_table(level_index); - - if (!table_exists) + auto level_index = page.get_level_index(level); + auto next_page_table = current_page_table->next_table(level_index); + // If the next table method failed then it means that the page level of the frame we want allocate has not yet + // been created itself. So we have to do that before we are able to allocate the wanted frame. This has to be done + // for every level, meaning we potenitally create a level 4, level 3 and level 2 page entry, each pointing to a + // page table one level below. + if (!next_page_table) { auto allocated_frame = allocator.allocate_frame(); - exception_handling::assert(!allocated_frame.has_value(), "[Page mapper]: Unable to allocate frame"); - page_table[level_index].set_entry(allocated_frame.value(), entry::PRESENT | entry::WRITABLE); - page_table.zero_entries(); + exception_handling::assert(!allocated_frame.has_value(), "[Page mapper] Unable to allocate frame"); + current_page_table->operator[](level_index) + .set_entry(allocated_frame.value(), entry::PRESENT | entry::WRITABLE); + // There should now be an entry at the previously not existent index, therefore we can simply access it again. + next_page_table = current_page_table->next_table(page.get_level_index(level)); + exception_handling::assert(!next_page_table.has_value(), + "[Page mapper] Unable to create new entry into page table"); + next_page_table.value()->zero_entries(); } + current_page_table = next_page_table.value(); } - auto level1_entry = page_table[page.get_level_index(page_table::LEVEL1)]; + auto level1_entry = current_page_table->operator[](page.get_level_index(page_table::LEVEL1)); arch::exception_handling::assert(!level1_entry.contains_flags(entry::HUGE_PAGE), - "[Page Mapper]: Unable to map huge pages"); - arch::exception_handling::assert(!level1_entry.is_unused(), "[Page Mapper]: Page table entry is already used"); + "[Page Mapper] Unable to map huge pages"); + arch::exception_handling::assert(!level1_entry.is_unused(), "[Page Mapper] Page table entry is already used"); level1_entry.set_entry(frame, flags | std::bitset<64U>{entry::PRESENT}); } } // namespace teachos::arch::memory::paging 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 0fe667c..3439127 100644 --- a/arch/x86_64/include/arch/memory/paging/page_table.hpp +++ b/arch/x86_64/include/arch/memory/paging/page_table.hpp @@ -11,16 +11,6 @@ namespace teachos::arch::memory::paging } /** - * @brief Actual data that is contained in every page table, this is the structure we cast a specific address too, - * because it consists of x amount os entries, which is a simple address. - */ - struct table_content - { - entry entries[PAGE_TABLE_ENTRY_COUNT]; ///< Entries containing addresses to page tables of a level below or actual - ///< virtual addresses for the level 1 page table. - }; - - /** * @brief A Page table containing 512 entries. */ struct page_table @@ -38,9 +28,10 @@ namespace teachos::arch::memory::paging }; /** - * @brief Constructor. Automatically starts on the fixed address of the Level 4 page table. + * @brief Deleted constructor. Object can only be created by casting from the fixed Level 4 + * page table address `reinterpret_cast<page_table *>(0xfffffffffffff000)`. */ - page_table(); + page_table() = delete; /** * @brief Set every entry of the page to unused. @@ -48,13 +39,13 @@ namespace teachos::arch::memory::paging auto zero_entries() -> void; /** - * @brief Turn this page table into the next page table level from the given page table index. Meaning we - * use an index into a Level 4 page table to get the according Level 3 page table. When using this on an a level 1 - * page table it will cause an assertion. + * @brief Returns the next page table level from the given page table index. Meaning we + * use an index into a Level 4 page table to get the according Level 3 page table. This method should not be called + * on a Level 1 page table or it will return invalid addresses and cause hard to debug issues. * * @param table_index Index of this page table in the page table one level higher. */ - auto next_table(std::size_t table_index) -> bool; + auto next_table(std::size_t table_index) -> std::optional<page_table *>; /** * @brief Index operator overload to access specific mutable entry directy. @@ -83,9 +74,8 @@ namespace teachos::arch::memory::paging */ auto next_table_address(std::size_t table_index) -> std::optional<std::size_t>; - level current_level; ///< Current level of the page table, used to ensure next_table() is never called with a level - ///< 1 page table - table_content * current_table; ///< Current table we are accessing and indexing. + entry entries[PAGE_TABLE_ENTRY_COUNT]; ///< Entries containing addresses to page tables of a level below or actual + ///< virtual addresses for the level 1 page table. }; auto operator--(page_table::level & level, int) -> page_table::level; |
