diff options
| -rw-r--r-- | arch/x86_64/include/arch/memory/allocator/physical_frame.hpp | 2 | ||||
| -rw-r--r-- | arch/x86_64/include/arch/memory/paging/page_mapper.hpp | 2 | ||||
| -rw-r--r-- | arch/x86_64/include/arch/memory/paging/page_table.hpp | 22 | ||||
| -rw-r--r-- | arch/x86_64/include/arch/memory/paging/virtual_page.hpp | 2 | ||||
| -rw-r--r-- | arch/x86_64/src/kernel/main.cpp | 27 | ||||
| -rw-r--r-- | arch/x86_64/src/memory/paging/page_mapper.cpp | 14 | ||||
| -rw-r--r-- | arch/x86_64/src/memory/paging/page_table.cpp | 16 |
7 files changed, 61 insertions, 24 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 e013e0d..e33c77a 100644 --- a/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp +++ b/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp @@ -27,7 +27,7 @@ namespace teachos::arch::memory::allocator * @param physical_address Physical address we want to get the corresponding physical frame for. * @return Frame the given address is contained in. */ - static auto containing_address(std::size_t physical_address) -> physical_frame; + auto static containing_address(std::size_t physical_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 0f226e2..a8cca4c 100644 --- a/arch/x86_64/include/arch/memory/paging/page_mapper.hpp +++ b/arch/x86_64/include/arch/memory/paging/page_mapper.hpp @@ -145,7 +145,9 @@ namespace teachos::arch::memory::paging exception_handling::assert(level1_frame.has_value(), "[Page Mapper] Attempted to unmap page, which has not been mapped previously"); level1_entry.set_unused(); + // TODO: Flush the translation lookaside buffer to clear the entry from cache as well. (mov cr3, cr3) // TODO: Deallocate and unmap level 1, 2, 3 page table entry if this was the last page in them. + // TODO: Fix deallocate because it is not yet implemented. allocator.deallocate_frame(level1_frame.value()); } } // 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 91ce81c..3a31dc5 100644 --- a/arch/x86_64/include/arch/memory/paging/page_table.hpp +++ b/arch/x86_64/include/arch/memory/paging/page_table.hpp @@ -5,10 +5,7 @@ namespace teachos::arch::memory::paging { - namespace - { - constexpr std::size_t PAGE_TABLE_ENTRY_COUNT = 512U; ///< Default entry count of a page table in x86_84 is 512. - } + constexpr std::size_t PAGE_TABLE_ENTRY_COUNT = 512U; ///< Default entry count of a page table in x86_84 is 512. /** * @brief Forward delcaration of the page_table, because it should only be accessible over the handle, the actual @@ -66,13 +63,26 @@ namespace teachos::arch::memory::paging */ auto operator[](std::size_t index) const -> entry; + /** + * @brief Decrements the page table handle level enum by one, is defined so we can use it as a replacement for an + * int index in a range based for loop. Will halt execution if called with page_table_handle::LEVEL1, because there + * is no level below. Has to be defined as either a friend function or inline header method, because we define an + * operator of another type. In this instance friend function was choosen, because the struct itself also requires + * the operator, but declaring before the struct is not possible, because the enum is in the struct. This is + * inpossible because the struct requires the operator declared before itself to work, and the operator requires the + * struct declared before itself to work. Furthermore this allows the defintion of the method to be done in the cpp, + * avoiding includes in the header file. + * + * @param value Value we want to decrement on + * @return level New level value decrement by one, meaning the level is also decrement by one Level4 --> Level3, ... + */ + friend auto operator--(level value, int) -> level; + private: page_table * handle; ///< Handle to underlying page table, can never be null (invariant ensured by constructor) level handle_level; ///< Level page table is currently on, depends on how often next_level was ///< called successfully. }; - - auto operator--(page_table_handle::level & level, int) -> page_table_handle::level; } // namespace teachos::arch::memory::paging #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 ca11b3c..79801ee 100644 --- a/arch/x86_64/include/arch/memory/paging/virtual_page.hpp +++ b/arch/x86_64/include/arch/memory/paging/virtual_page.hpp @@ -28,7 +28,7 @@ namespace teachos::arch::memory::paging * @param virtual_address Virtual address we want to get the corresponding virtual page for. * @return Frame the given address is contained in. */ - static auto containing_address(std::size_t virtual_address) -> virtual_page; + auto static containing_address(std::size_t virtual_address) -> virtual_page; /** * @brief Evaluates the start address of the virtual page. diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 8e5aa05..3e25d2d 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -10,6 +10,7 @@ namespace teachos::arch::kernel { + auto main() -> void { video::vga::text::clear(); @@ -19,6 +20,25 @@ namespace teachos::arch::kernel auto memory_information = memory::multiboot::read_multiboot2(); memory::allocator::area_frame_allocator allocator(memory_information); + size_t address = 42 * memory::paging::PAGE_TABLE_ENTRY_COUNT * memory::paging::PAGE_TABLE_ENTRY_COUNT * + memory::allocator::PAGE_FRAME_SIZE; // 42th P3 entry + auto page = memory::paging::virtual_page::containing_address(address); + auto frame = allocator.allocate_frame(); + exception_handling::assert(frame.has_value(), "[Main] Out of memory exception"); + auto optional_frame = memory::paging::translate_page(page); + memory::paging::map_page_to_frame(allocator, page, frame.value(), 0U); + optional_frame = memory::paging::translate_page(page); + video::vga::text::newline(); + video::vga::text::write("Mapped physical frame: ", video::vga::text::common_attributes::green_on_black); + video::vga::text::write_number(optional_frame.value().frame_number, + video::vga::text::common_attributes::green_on_black); + + memory::paging::unmap_page(allocator, page); + video::vga::text::write("Unapped physical page: ", video::vga::text::common_attributes::green_on_black); + optional_frame = memory::paging::translate_page(page); + exception_handling::assert(!optional_frame.has_value(), "[Main] Ummapping failed"); + video::vga::text::write_number(page.page_number, video::vga::text::common_attributes::green_on_black); + auto last_allocated = allocator.allocate_frame(); auto allocated = last_allocated; do @@ -26,12 +46,9 @@ namespace teachos::arch::kernel last_allocated = allocated; allocated = allocator.allocate_frame(); } while (allocated); - video::vga::text::write("Allocated Frames", video::vga::text::common_attributes::green_on_black); + video::vga::text::newline(); + video::vga::text::write("Allocated Frames: ", video::vga::text::common_attributes::green_on_black); video::vga::text::write_number(last_allocated.value().frame_number, video::vga::text::common_attributes::green_on_black); - memory::paging::map_page_to_frame(allocator, memory::paging::virtual_page{0U}, last_allocated.value(), 0U); - memory::paging::map_next_free_page_to_frame(allocator, memory::paging::virtual_page{0U}, 0U); - memory::paging::identity_map(allocator, last_allocated.value(), 0U); - memory::paging::unmap_page(allocator, memory::paging::virtual_page{0U}); } } // namespace teachos::arch::kernel diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index b8f6b89..75b17ff 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -14,15 +14,23 @@ namespace teachos::arch::memory::paging return page_table_handle{active_page, page_table_handle::LEVEL4}; } + auto subtract_level(page_table_handle::level level) -> page_table_handle::level + { + exception_handling::assert(level != page_table_handle::LEVEL1, + "[Page table] Attemptd to decrement enum to value outside of range"); + auto value = static_cast<std::underlying_type<page_table_handle::level>::type>(level); + return static_cast<page_table_handle::level>(--value); + } + auto translate_page(virtual_page page) -> std::optional<allocator::physical_frame> { auto current_handle = create_or_get(); - for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; level--) + for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; level = subtract_level(level)) { auto next_handle = current_handle.next_table(page.get_level_index(level)); - // If the next table method failed then it is highly likely that it was a huge page and we therefore have to parse - // the table differently. Therefore, we attempt to parse it using the method required by huge pages. + // If the next table method failed then it is highly likely that it was a huge page and we therefore have to + // parse the table differently. Therefore, we attempt to parse it using the method required by huge pages. if (!next_handle) { return translate_huge_page(page); diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 852bbd5..9c9ca76 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -47,14 +47,6 @@ namespace teachos::arch::memory::paging ///< virtual addresses for the level 1 page table. }; - auto operator--(page_table_handle::level level, int) -> page_table_handle::level - { - exception_handling::assert(level != page_table_handle::LEVEL1, - "[Page table] Attemptd to decrement enum to value outside of range"); - auto value = static_cast<std::underlying_type<page_table_handle::level>::type>(level); - return static_cast<page_table_handle::level>(--value); - } - auto page_table::zero_entries() -> void { constexpr size_t entry_amount = sizeof(entries) / sizeof(entries[0]); @@ -117,4 +109,12 @@ namespace teachos::arch::memory::paging } auto page_table_handle::operator[](std::size_t index) const -> entry { return handle->operator[](index); } + + auto operator--(page_table_handle::level value, int) -> page_table_handle::level + { + exception_handling::assert(value != page_table_handle::LEVEL1, + "[Page table] Attemptd to decrement enum to value outside of range"); + auto new_value = static_cast<std::underlying_type<page_table_handle::level>::type>(value); + return static_cast<page_table_handle::level>(--new_value); + } } // namespace teachos::arch::memory::paging |
