From 675f38d6733fb19b4ffc7e9fbddb93acdd1d1e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 19 Oct 2024 14:40:25 +0000 Subject: Seperate allocation and paging code into multiple files as well --- arch/x86_64/src/memory/paging/page_entry.cpp | 42 ++++++++++++++++++ arch/x86_64/src/memory/paging/page_table.cpp | 65 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 arch/x86_64/src/memory/paging/page_entry.cpp create mode 100644 arch/x86_64/src/memory/paging/page_table.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp new file mode 100644 index 0000000..43c0b71 --- /dev/null +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -0,0 +1,42 @@ +#include "arch/memory/paging/page_entry.hpp" + +#include "arch/exception_handling/assert.hpp" + +namespace teachos::arch::memory::paging +{ + auto entry::is_unused() const -> bool { return flags == 0U; } + + auto entry::set_unused() -> void { flags = 0U; } + + auto entry::calculate_pointed_to_frame() const -> std::optional + { + if (contains_flags(PRESENT)) + { + auto physical_address = calculate_physical_address(); + return allocator::physical_frame::containing_address(physical_address); + } + return std::nullopt; + } + + auto entry::calculate_physical_address() const -> std::size_t + { + constexpr std::size_t start_bit = 12U; + constexpr std::size_t end_bit = 52U; + size_t value = 0U; + + for (auto i = start_bit; i < end_bit; i++) + { + value |= (flags[i] ? (1 << (i - start_bit)) : 0); + } + return value; + } + + auto entry::contains_flags(std::bitset<64U> other) const -> bool { return (flags & other) == other; } + + auto entry::set_address(allocator::physical_frame frame) -> void + { + arch::exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, + "Start address is not aligned with Page"); + flags = std::bitset<64U>(frame.start_address()) | flags; + } +} // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp new file mode 100644 index 0000000..a1cbc72 --- /dev/null +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -0,0 +1,65 @@ +#include "arch/memory/paging/page_table.hpp" + +#include "arch/exception_handling/assert.hpp" + +namespace teachos::arch::memory::paging +{ + page_table::page_table() + : current_level(LEVEL4) + , current_table(reinterpret_cast(0xfffffffffffff000)) + { + // Nothing to do + } + + auto page_table::zero_entries() -> void + { + constexpr size_t entry_amount = sizeof(current_table->entries) / sizeof(current_table->entries[0]); + for (size_t i = 0; i < entry_amount; ++i) + { + auto entry = this->operator[](i); + entry.set_unused(); + } + } + + auto page_table::next_table(std::size_t table_index) -> void + { + arch::exception_handling::assert(current_level != LEVEL1, + "[Page Table] Attempted to call next_table on level 1 page table"); + auto address = next_table_address(table_index); + + if (address.has_value()) + { + current_table = reinterpret_cast(*address); + current_level = static_cast(current_level - 1U); + } + } + + auto page_table::operator[](std::size_t index) -> entry & + { + // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which + // could be incredibly hard to debug later. + arch::exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] index out of bounds"); + return current_table->entries[index]; + } + + auto page_table::operator[](std::size_t index) const -> entry const & + { + // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which + // could be incredibly hard to debug later. + arch::exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] index out of bounds"); + return current_table->entries[index]; + } + + auto page_table::next_table_address(std::size_t table_index) const -> std::optional + { + auto entry = this->operator[](table_index); + + if (entry.contains_flags(entry::PRESENT) && !entry.contains_flags(entry::HUGE_PAGE)) + { + std::size_t const table_address = reinterpret_cast(this); + return (table_address << 9) | (table_index << 12); + } + // TODO: Implement behaviour for huge pages currently not done + return std::nullopt; + } +} // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 7ebfe9e09efa84044d1470132b7f55ebf53a7f89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 06:32:02 +0000 Subject: Fix next_table_address --- arch/x86_64/src/memory/paging/page_table.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index a1cbc72..786ff69 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -29,7 +29,7 @@ namespace teachos::arch::memory::paging if (address.has_value()) { - current_table = reinterpret_cast(*address); + current_table = reinterpret_cast(address.value()); current_level = static_cast(current_level - 1U); } } @@ -42,21 +42,13 @@ namespace teachos::arch::memory::paging return current_table->entries[index]; } - auto page_table::operator[](std::size_t index) const -> entry const & - { - // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which - // could be incredibly hard to debug later. - arch::exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] index out of bounds"); - return current_table->entries[index]; - } - - auto page_table::next_table_address(std::size_t table_index) const -> std::optional + auto page_table::next_table_address(std::size_t table_index) -> std::optional { auto entry = this->operator[](table_index); if (entry.contains_flags(entry::PRESENT) && !entry.contains_flags(entry::HUGE_PAGE)) { - std::size_t const table_address = reinterpret_cast(this); + std::size_t const table_address = reinterpret_cast(current_table); return (table_address << 9) | (table_index << 12); } // TODO: Implement behaviour for huge pages currently not done -- cgit v1.2.3 From d728052d62470799f73f6d9a2b8baa2b0b357383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 07:01:53 +0000 Subject: Add helper methods to phyisca frame --- arch/x86_64/src/memory/paging/page_entry.cpp | 4 ++-- arch/x86_64/src/memory/paging/page_table.cpp | 6 +++--- arch/x86_64/src/memory/paging/virtual_page.cpp | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 arch/x86_64/src/memory/paging/virtual_page.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 43c0b71..692f8ae 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -35,8 +35,8 @@ namespace teachos::arch::memory::paging auto entry::set_address(allocator::physical_frame frame) -> void { - arch::exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, - "Start address is not aligned with Page"); + exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, + "Start address is not aligned with Page"); flags = std::bitset<64U>(frame.start_address()) | flags; } } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 786ff69..8345161 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -23,8 +23,8 @@ namespace teachos::arch::memory::paging auto page_table::next_table(std::size_t table_index) -> void { - arch::exception_handling::assert(current_level != LEVEL1, - "[Page Table] Attempted to call next_table on level 1 page table"); + exception_handling::assert(current_level != LEVEL1, + "[Page Table] Attempted to call next_table on level 1 page table"); auto address = next_table_address(table_index); if (address.has_value()) @@ -38,7 +38,7 @@ namespace teachos::arch::memory::paging { // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which // could be incredibly hard to debug later. - arch::exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] index out of bounds"); + exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] index out of bounds"); return current_table->entries[index]; } diff --git a/arch/x86_64/src/memory/paging/virtual_page.cpp b/arch/x86_64/src/memory/paging/virtual_page.cpp new file mode 100644 index 0000000..3fb6caf --- /dev/null +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -0,0 +1,22 @@ +#include "arch/memory/paging/virtual_page.hpp" + +#include "arch/exception_handling/assert.hpp" +#include "arch/memory/allocator/physical_frame.hpp" + +namespace teachos::arch::memory::paging +{ + virtual_page::virtual_page(std::size_t page_number) + : page_number(page_number) + { + // Nothing to do + } + + auto virtual_page::containing_address(std::size_t virtual_address) -> virtual_page + { + exception_handling::assert(virtual_address < 0x0000800000000000 || virtual_address >= 0xffff800000000000, + "[Virtual Page] Attempted to create virtual page from invalid address"); + return virtual_page{virtual_address / allocator::PAGE_FRAME_SIZE}; + } + + auto virtual_page::start_address() const -> uint64_t { return page_number * allocator::PAGE_FRAME_SIZE; } +} // namespace teachos::arch::memory::paging -- cgit v1.2.3 From e5206b3bf1883fd9601a37f5cce392d8080b8791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 07:43:00 +0000 Subject: Add get level index method to virtual page --- arch/x86_64/src/memory/paging/page_table.cpp | 8 ++------ arch/x86_64/src/memory/paging/virtual_page.cpp | 5 +++++ 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 8345161..9857294 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -26,12 +26,8 @@ namespace teachos::arch::memory::paging exception_handling::assert(current_level != LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); auto address = next_table_address(table_index); - - if (address.has_value()) - { - current_table = reinterpret_cast(address.value()); - current_level = static_cast(current_level - 1U); - } + current_table = reinterpret_cast(address.value()); + current_level = static_cast(current_level - 1U); } auto page_table::operator[](std::size_t index) -> entry & diff --git a/arch/x86_64/src/memory/paging/virtual_page.cpp b/arch/x86_64/src/memory/paging/virtual_page.cpp index 3fb6caf..dcdec7f 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -19,4 +19,9 @@ namespace teachos::arch::memory::paging } auto virtual_page::start_address() const -> uint64_t { return page_number * allocator::PAGE_FRAME_SIZE; } + + auto virtual_page::get_level_index(page_table::level level) const -> uint64_t + { + return (start_address() >> (level * 9U)) & 0x1FF; + } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From f8928c877c48b5beba9bb42876f70213aba68f88 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 07:46:51 +0000 Subject: translate --- arch/x86_64/src/memory/paging/page_table.cpp | 2 +- arch/x86_64/src/memory/paging/virtual_page.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 9857294..3fb7a3f 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -45,7 +45,7 @@ namespace teachos::arch::memory::paging if (entry.contains_flags(entry::PRESENT) && !entry.contains_flags(entry::HUGE_PAGE)) { std::size_t const table_address = reinterpret_cast(current_table); - return (table_address << 9) | (table_index << 12); + return ((table_address << 9) | (table_index << 12)); } // TODO: Implement behaviour for huge pages currently not done return std::nullopt; diff --git a/arch/x86_64/src/memory/paging/virtual_page.cpp b/arch/x86_64/src/memory/paging/virtual_page.cpp index dcdec7f..9b803d2 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -1,7 +1,6 @@ #include "arch/memory/paging/virtual_page.hpp" #include "arch/exception_handling/assert.hpp" -#include "arch/memory/allocator/physical_frame.hpp" namespace teachos::arch::memory::paging { -- cgit v1.2.3 From 882ccdcc0e3c19fbcc595c6a371ef79587f63648 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 08:11:19 +0000 Subject: implement translate_page --- arch/x86_64/src/memory/paging/page_table.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 3fb7a3f..3c4f6d8 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -21,7 +21,7 @@ namespace teachos::arch::memory::paging } } - auto page_table::next_table(std::size_t table_index) -> void + auto page_table::next_table(std::size_t table_index) -> bool { exception_handling::assert(current_level != LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); -- cgit v1.2.3 From 89ad7a0b60ce74c70fc3865e0967c601a6050674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 08:13:31 +0000 Subject: Make next table return boolean --- arch/x86_64/src/memory/paging/page_table.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 3c4f6d8..ab7331a 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -26,8 +26,13 @@ namespace teachos::arch::memory::paging exception_handling::assert(current_level != LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); auto address = next_table_address(table_index); - current_table = reinterpret_cast(address.value()); - current_level = static_cast(current_level - 1U); + bool const success = address.has_value(); + if (success) + { + current_table = reinterpret_cast(address.value()); + current_level = static_cast(current_level - 1U); + } + return success; } auto page_table::operator[](std::size_t index) -> entry & @@ -47,7 +52,6 @@ namespace teachos::arch::memory::paging std::size_t const table_address = reinterpret_cast(current_table); return ((table_address << 9) | (table_index << 12)); } - // TODO: Implement behaviour for huge pages currently not done return std::nullopt; } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From d934ea6875932a7139eba77bb5248fca8be7fd01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 08:37:49 +0000 Subject: =?UTF-8?q?Implement=20translate=20page=20in=20a=20big=20brain=20w?= =?UTF-8?q?ay=20=F0=9F=A4=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arch/x86_64/src/memory/paging/page_table.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index ab7331a..02ababe 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -54,4 +54,12 @@ namespace teachos::arch::memory::paging } return std::nullopt; } + + auto operator--(page_table::level & level, int) -> page_table::level + { + exception_handling::assert(level != page_table::LEVEL1, + "[Page table] Attemptd to decrement enum to value outside of range"); + auto value = static_cast::type>(level); + return static_cast(--value); + } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 1d157101eb550f99dac9ec943053f85bbf6c7644 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 09:03:30 +0000 Subject: create page_mapper --- arch/x86_64/src/memory/paging/page_mapper.cpp | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 arch/x86_64/src/memory/paging/page_mapper.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp new file mode 100644 index 0000000..5372561 --- /dev/null +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -0,0 +1,57 @@ +#include "arch/exception_handling/assert.hpp" +#include "arch/memory/allocator/area_frame_allocator.hpp" +#include "arch/memory/paging/virtual_page.hpp" + +namespace teachos::arch::memory::paging +{ + auto translate_page(virtual_page page) -> std::optional + { + page_table page_table{}; + bool is_valid = false; + auto huge_page = []() -> std::optional { return std::nullopt; }; + + for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--) + { + is_valid = page_table.next_table(page.get_level_index(level)); + if (!is_valid) + { + break; + } + } + + if (is_valid) + { + auto level1_index = page.get_level_index(page_table::LEVEL1); + auto frame = page_table[level1_index].calculate_pointed_to_frame(); + return frame; + } + + return huge_page(); + } + + auto translate_address(std::size_t virtual_address) -> std::optional + { + std::size_t offset = virtual_address % allocator::PAGE_FRAME_SIZE; + virtual_page page = virtual_page::containing_address(virtual_address); + std::optional frame = translate_page(page); + + if (frame.has_value()) + { + return frame.value().frame_number * allocator::PAGE_FRAME_SIZE + offset; + } + + return std::nullopt; + } + + auto map_page_to_frame(memory::allocator::area_frame_allocator & allocator, const virtual_page & page, + const memory::allocator::physical_frame & frame, entry::bitset flags) -> void + { + auto p4 = reinterpret_cast(P4); // Assuming P4 is defined somewhere + auto p3 = p4->next_table_create(page.p4_index(), allocator); + auto p2 = p3->next_table_create(page.p3_index(), allocator); + auto p1 = p2->next_table_create(page.p2_index(), allocator); + + assert(p1[page.p1_index()].is_unused()); + p1[page.p1_index()].set(frame, flags | PRESENT); + } +} // namespace teachos::arch::memory::paging \ No newline at end of file -- cgit v1.2.3 From 04bd6a059b9eb105024044d74122b0cb76cebf14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 09:34:03 +0000 Subject: Add translate huge page method --- arch/x86_64/src/memory/paging/page_mapper.cpp | 44 ++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 5372561..e68bc68 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -8,7 +8,8 @@ namespace teachos::arch::memory::paging { page_table page_table{}; bool is_valid = false; - auto huge_page = []() -> std::optional { return std::nullopt; }; + + auto huge_page = [&]() -> std::optional {}; for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--) { @@ -22,11 +23,46 @@ namespace teachos::arch::memory::paging if (is_valid) { auto level1_index = page.get_level_index(page_table::LEVEL1); - auto frame = page_table[level1_index].calculate_pointed_to_frame(); - return frame; + auto level1_frame = page_table[level1_index].calculate_pointed_to_frame(); + return level1_frame; } - return huge_page(); + return translate_huge_page(page); + } + + auto translate_huge_page(virtual_page page) -> std::optional + { + page_table page_table{}; + bool is_valid = page_table.next_table(page.get_level_index(page_table::LEVEL3)); + + if (is_valid) + { + auto level3_entry = page_table[page.get_level_index(page_table::LEVEL3)]; + auto level3_optional_frame = level3_entry.calculate_pointed_to_frame(); + if (level3_optional_frame.has_value() && level3_entry.contains_flags(entry::HUGE_PAGE)) + { + auto level3_frame = level3_optional_frame.value(); + exception_handling::assert(level3_frame.frame_number % (PAGE_TABLE_ENTRY_COUNT * PAGE_TABLE_ENTRY_COUNT) == 0U, + "[Page Mapper] Physical address must be 1 GiB aligned"); + return allocator::physical_frame{level3_frame.frame_number + + page.get_level_index(page_table::LEVEL2) * PAGE_TABLE_ENTRY_COUNT + + page.get_level_index(page_table::LEVEL1)}; + } + } + is_valid = page_table.next_table(page.get_level_index(page_table::LEVEL3)); + if (is_valid) + { + auto level2_entry = page_table[page.get_level_index(page_table::LEVEL2)]; + auto level2_optional_frame = level2_entry.calculate_pointed_to_frame(); + if (level2_optional_frame.has_value() && level2_entry.contains_flags(entry::HUGE_PAGE)) + { + auto level2_frame = level2_optional_frame.value(); + exception_handling::assert(level2_frame.frame_number % PAGE_TABLE_ENTRY_COUNT == 0U, + "[Page Mapper] Physical address must be 2 MiB aligned"); + return allocator::physical_frame{level2_frame.frame_number + page.get_level_index(page_table::LEVEL1)}; + } + } + return std::nullopt; } auto translate_address(std::size_t virtual_address) -> std::optional -- cgit v1.2.3 From ecf89844100248db3ad08d5c76091de563488b95 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 09:37:08 +0000 Subject: implement map_page_to_frame --- arch/x86_64/src/memory/paging/page_entry.cpp | 4 ++-- arch/x86_64/src/memory/paging/page_mapper.cpp | 32 ++++++++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 692f8ae..0dbbae1 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -33,10 +33,10 @@ namespace teachos::arch::memory::paging auto entry::contains_flags(std::bitset<64U> other) const -> bool { return (flags & other) == other; } - auto entry::set_address(allocator::physical_frame frame) -> void + auto entry::set_entry(allocator::physical_frame frame, std::bitset<64U> additional_flags) -> void { exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, "Start address is not aligned with Page"); - flags = std::bitset<64U>(frame.start_address()) | flags; + flags = std::bitset<64U>(frame.start_address()) | additional_flags; } } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index e68bc68..ec1d5d5 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -1,6 +1,6 @@ +#include "arch/memory/paging/page_mapper.hpp" + #include "arch/exception_handling/assert.hpp" -#include "arch/memory/allocator/area_frame_allocator.hpp" -#include "arch/memory/paging/virtual_page.hpp" namespace teachos::arch::memory::paging { @@ -9,8 +9,6 @@ namespace teachos::arch::memory::paging page_table page_table{}; bool is_valid = false; - auto huge_page = [&]() -> std::optional {}; - for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--) { is_valid = page_table.next_table(page.get_level_index(level)); @@ -82,12 +80,26 @@ namespace teachos::arch::memory::paging auto map_page_to_frame(memory::allocator::area_frame_allocator & allocator, const virtual_page & page, const memory::allocator::physical_frame & frame, entry::bitset flags) -> void { - auto p4 = reinterpret_cast(P4); // Assuming P4 is defined somewhere - auto p3 = p4->next_table_create(page.p4_index(), allocator); - auto p2 = p3->next_table_create(page.p3_index(), allocator); - auto p1 = p2->next_table_create(page.p2_index(), allocator); + page_table page_table{}; + bool is_valid = false; + + for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--) + { + std::size_t level_index = page.get_level_index(level); + is_valid = page_table.next_table(level_index); + page_table[level_index].set_entry(frame, entry::PRESENT | entry::WRITABLE); + page_table.zero_entries(); + + if (!is_valid) + { + break; + } + } - assert(p1[page.p1_index()].is_unused()); - p1[page.p1_index()].set(frame, flags | PRESENT); + entry page_table_entry = page_table[page.get_level_index(page_table::LEVEL1)]; + arch::exception_handling::assert(!page_table_entry.contains_flags(entry::HUGE_PAGE), + "[Page Mapper]: Unable to map huge pages"); + arch::exception_handling::assert(!page_table_entry.is_unused(), "[Page Mapper]: Page table entry is already used"); + page_table_entry.set_entry(frame, flags | entry::PRESENT); } } // namespace teachos::arch::memory::paging \ No newline at end of file -- cgit v1.2.3 From f47f1468c8c281ee9603b7669f5e685e5f36fa3f Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 11:23:02 +0000 Subject: finalize map_page_to_frame --- arch/x86_64/src/memory/paging/page_mapper.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index ec1d5d5..6b01548 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -77,8 +77,8 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto map_page_to_frame(memory::allocator::area_frame_allocator & allocator, const virtual_page & page, - const memory::allocator::physical_frame & frame, entry::bitset flags) -> void + auto map_page_to_frame(memory::allocator::area_frame_allocator & allocator, virtual_page const & page, + memory::allocator::physical_frame const & frame, entry::bitset flags) -> void { page_table page_table{}; bool is_valid = false; @@ -87,7 +87,10 @@ namespace teachos::arch::memory::paging { std::size_t level_index = page.get_level_index(level); is_valid = page_table.next_table(level_index); - page_table[level_index].set_entry(frame, entry::PRESENT | entry::WRITABLE); + + std::optional 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(); if (!is_valid) -- cgit v1.2.3 From f4a14572a661b1f2bac283615d5b070408b382f7 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 11:24:58 +0000 Subject: fix syntax --- arch/x86_64/src/memory/paging/page_mapper.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 6b01548..eaafafa 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -77,8 +77,8 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto map_page_to_frame(memory::allocator::area_frame_allocator & allocator, virtual_page const & page, - memory::allocator::physical_frame const & frame, entry::bitset flags) -> void + auto map_page_to_frame(allocator::area_frame_allocator & allocator, virtual_page const & page, + allocator::physical_frame const & frame, entry::bitset flags) -> void { page_table page_table{}; bool is_valid = false; @@ -88,7 +88,7 @@ namespace teachos::arch::memory::paging std::size_t level_index = page.get_level_index(level); is_valid = page_table.next_table(level_index); - std::optional allocated_frame = allocator.allocate_frame(); + std::optional 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(); -- cgit v1.2.3 From 0d42fed17834a21d29032dd2d8e56e11596056bc Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 11:29:53 +0000 Subject: fix logic --- arch/x86_64/src/memory/paging/page_mapper.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index eaafafa..eb1e18c 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -88,15 +88,15 @@ namespace teachos::arch::memory::paging std::size_t level_index = page.get_level_index(level); is_valid = page_table.next_table(level_index); - std::optional 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(); - if (!is_valid) { break; } + + 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(); } entry page_table_entry = page_table[page.get_level_index(page_table::LEVEL1)]; -- cgit v1.2.3 From da2341ec12128d3b4983a67d39aeaf76b1781fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 12:02:20 +0000 Subject: Add printf like behaviour to assert --- arch/x86_64/src/memory/paging/page_entry.cpp | 2 +- arch/x86_64/src/memory/paging/page_mapper.cpp | 24 +++++++++++------------- arch/x86_64/src/memory/paging/page_table.cpp | 2 +- 3 files changed, 13 insertions(+), 15 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 0dbbae1..30a8961 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -36,7 +36,7 @@ namespace teachos::arch::memory::paging auto entry::set_entry(allocator::physical_frame frame, std::bitset<64U> additional_flags) -> void { exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, - "Start address is not aligned with Page"); + "[Paging Entry] Start address is not aligned with page"); flags = std::bitset<64U>(frame.start_address()) | additional_flags; } } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index eb1e18c..8c64f22 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -81,28 +81,26 @@ namespace teachos::arch::memory::paging allocator::physical_frame const & frame, entry::bitset flags) -> void { page_table page_table{}; - bool is_valid = false; + bool table_exists = false; for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--) { std::size_t level_index = page.get_level_index(level); - is_valid = page_table.next_table(level_index); + table_exists = page_table.next_table(level_index); - if (!is_valid) + if (!table_exists) { - break; + 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(); } - - 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(); } - entry page_table_entry = page_table[page.get_level_index(page_table::LEVEL1)]; - arch::exception_handling::assert(!page_table_entry.contains_flags(entry::HUGE_PAGE), + auto level1_entry = page_table[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(!page_table_entry.is_unused(), "[Page Mapper]: Page table entry is already used"); - page_table_entry.set_entry(frame, flags | entry::PRESENT); + arch::exception_handling::assert(!level1_entry.is_unused(), "[Page Mapper]: Page table entry is already used"); + level1_entry.set_entry(frame, flags | entry::PRESENT); } } // namespace teachos::arch::memory::paging \ No newline at end of file diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 02ababe..ea2e9c2 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -39,7 +39,7 @@ namespace teachos::arch::memory::paging { // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which // could be incredibly hard to debug later. - exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] index out of bounds"); + exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] Index out of bounds"); return current_table->entries[index]; } -- cgit v1.2.3 From aa0634589cb7d51f57e3e555bb15ebca99bc162e Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 12:17:58 +0000 Subject: document page mapping and entry --- arch/x86_64/src/memory/paging/page_entry.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 30a8961..8a7442e 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -33,10 +33,10 @@ namespace teachos::arch::memory::paging auto entry::contains_flags(std::bitset<64U> other) const -> bool { return (flags & other) == other; } - auto entry::set_entry(allocator::physical_frame frame, std::bitset<64U> additional_flags) -> void + auto entry::set_entry(allocator::physical_frame frame, std::bitset<64U> flags) -> void { exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, "[Paging Entry] Start address is not aligned with page"); - flags = std::bitset<64U>(frame.start_address()) | additional_flags; + flags = std::bitset<64U>(frame.start_address()) | flags; } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 8e9a924f9fba18de9a5e37b1baf1d97e9b008251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 12:21:57 +0000 Subject: Fix useless copy by reference --- arch/x86_64/src/memory/paging/page_mapper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 8c64f22..1912174 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -77,8 +77,8 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto map_page_to_frame(allocator::area_frame_allocator & allocator, virtual_page const & page, - allocator::physical_frame const & frame, entry::bitset flags) -> void + auto map_page_to_frame(allocator::area_frame_allocator & allocator, virtual_page page, + allocator::physical_frame frame, entry::bitset flags) -> void { page_table page_table{}; bool table_exists = false; -- cgit v1.2.3 From dd5dab7fd34c6745a16ac572ecd099e4bc26c1fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 12:35:39 +0000 Subject: Fix set_entry flags usage --- arch/x86_64/src/memory/paging/page_entry.cpp | 2 +- arch/x86_64/src/memory/paging/page_mapper.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 8a7442e..a172ca8 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -37,6 +37,6 @@ namespace teachos::arch::memory::paging { exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, "[Paging Entry] Start address is not aligned with page"); - flags = std::bitset<64U>(frame.start_address()) | flags; + flags = std::bitset<64U>{frame.start_address()} | flags; } } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 1912174..0874230 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -78,7 +78,7 @@ namespace teachos::arch::memory::paging } auto map_page_to_frame(allocator::area_frame_allocator & allocator, virtual_page page, - allocator::physical_frame frame, entry::bitset flags) -> void + allocator::physical_frame frame, std::bitset<64U> flags) -> void { page_table page_table{}; bool table_exists = false; @@ -101,6 +101,6 @@ namespace teachos::arch::memory::paging 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"); - level1_entry.set_entry(frame, flags | entry::PRESENT); + level1_entry.set_entry(frame, flags | std::bitset<64U>{entry::PRESENT}); } -} // namespace teachos::arch::memory::paging \ No newline at end of file +} // namespace teachos::arch::memory::paging -- cgit v1.2.3 From bb8b3f9c0734220702e3e930e7acb4906aa13db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 12:48:51 +0000 Subject: Make map_page_to_frame use concept --- arch/x86_64/src/memory/paging/page_mapper.cpp | 29 --------------------------- 1 file changed, 29 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 0874230..5b72a63 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -1,7 +1,5 @@ #include "arch/memory/paging/page_mapper.hpp" -#include "arch/exception_handling/assert.hpp" - namespace teachos::arch::memory::paging { auto translate_page(virtual_page page) -> std::optional @@ -76,31 +74,4 @@ namespace teachos::arch::memory::paging return std::nullopt; } - - auto map_page_to_frame(allocator::area_frame_allocator & allocator, virtual_page page, - allocator::physical_frame frame, std::bitset<64U> flags) -> void - { - page_table page_table{}; - bool table_exists = false; - - 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 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(); - } - } - - auto level1_entry = page_table[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"); - level1_entry.set_entry(frame, flags | std::bitset<64U>{entry::PRESENT}); - } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 1b03bcecac1276b486e17daf0384de7fa203d974 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 12:57:21 +0000 Subject: create active_page_table --- .../x86_64/src/memory/paging/active_page_table.cpp | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 arch/x86_64/src/memory/paging/active_page_table.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp new file mode 100644 index 0000000..eb85c34 --- /dev/null +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -0,0 +1,31 @@ +#include "arch/memory/paging/active_page_table.hpp" + +namespace teachos::arch::memory::paging +{ + struct active_page_table + { + auto create(page_table * level4_page_table) -> active_page_table * + { + if (instantiated) + { + return this; + } + + instantiated = true; + return &active_page_table(level4_page_table); + } + + active_page_table(const active_page_table &) = delete; + active_page_table & operator=(const active_page_table &) = delete; + + private: + active_page_table(page_table * level4_page_table) + : level4_page_table(level4_page_table) + { + // Nothing to do + } + + bool instantiated = false; + page_table * level4_page_table; + }; +} // namespace teachos::arch::memory::paging \ No newline at end of file -- cgit v1.2.3 From 49ae81912f3a440f1958e86296d468ec669f71a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 13:17:24 +0000 Subject: Fix active page table cpp --- .../x86_64/src/memory/paging/active_page_table.cpp | 34 ++++++++-------------- 1 file changed, 12 insertions(+), 22 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp index eb85c34..ec89e0e 100644 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -2,30 +2,20 @@ namespace teachos::arch::memory::paging { - struct active_page_table + auto active_page_table::create(page_table * level4_page_table) -> active_page_table * { - auto create(page_table * level4_page_table) -> active_page_table * + if (instantiated) { - if (instantiated) - { - return this; - } - - instantiated = true; - return &active_page_table(level4_page_table); + return this; } - active_page_table(const active_page_table &) = delete; - active_page_table & operator=(const active_page_table &) = delete; + instantiated = true; + return &active_page_table(level4_page_table); + } - private: - active_page_table(page_table * level4_page_table) - : level4_page_table(level4_page_table) - { - // Nothing to do - } - - bool instantiated = false; - page_table * level4_page_table; - }; -} // namespace teachos::arch::memory::paging \ No newline at end of file + active_page_table::active_page_table(page_table * level4_page_table) + : level4_page_table(level4_page_table) + { + // Nothing to do + } +} // namespace teachos::arch::memory::paging -- cgit v1.2.3 From f171efed99684bf03c315405efda34e36d7db82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 21 Oct 2024 09:31:58 +0000 Subject: Ensure only one instance of global page table can exist --- .../x86_64/src/memory/paging/active_page_table.cpp | 21 ----- arch/x86_64/src/memory/paging/page_mapper.cpp | 91 +++++++++++++--------- arch/x86_64/src/memory/paging/page_table.cpp | 28 +++---- 3 files changed, 65 insertions(+), 75 deletions(-) delete mode 100644 arch/x86_64/src/memory/paging/active_page_table.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp deleted file mode 100644 index ec89e0e..0000000 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "arch/memory/paging/active_page_table.hpp" - -namespace teachos::arch::memory::paging -{ - auto active_page_table::create(page_table * level4_page_table) -> active_page_table * - { - if (instantiated) - { - return this; - } - - instantiated = true; - return &active_page_table(level4_page_table); - } - - active_page_table::active_page_table(page_table * level4_page_table) - : level4_page_table(level4_page_table) - { - // Nothing to do - } -} // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 5b72a63..cedda54 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -2,60 +2,79 @@ namespace teachos::arch::memory::paging { + namespace + { + constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0xfffffffffffff000; + } + + auto create_or_get() -> page_table * + { + static bool instantiated = false; + static page_table * active_page = nullptr; + + if (instantiated) + { + return active_page; + } + + instantiated = true; + active_page = reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS); + return active_page; + } + auto translate_page(virtual_page page) -> std::optional { - page_table page_table{}; - bool is_valid = false; + page_table * current_page_table = create_or_get(); for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--) { - is_valid = page_table.next_table(page.get_level_index(level)); - if (!is_valid) + auto next_page_table = current_page_table->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 (!next_page_table) { - break; + return translate_huge_page(page); } + current_page_table = next_page_table.value(); } - if (is_valid) - { - auto level1_index = page.get_level_index(page_table::LEVEL1); - auto level1_frame = page_table[level1_index].calculate_pointed_to_frame(); - return level1_frame; - } - - return translate_huge_page(page); + auto level1_index = page.get_level_index(page_table::LEVEL1); + auto level1_frame = current_page_table->operator[](level1_index).calculate_pointed_to_frame(); + return level1_frame; } auto translate_huge_page(virtual_page page) -> std::optional { - page_table page_table{}; - bool is_valid = page_table.next_table(page.get_level_index(page_table::LEVEL3)); + page_table * current_page_table = create_or_get(); + auto level3_page_table = current_page_table->next_table(page.get_level_index(page_table::LEVEL4)); - if (is_valid) + if (!level3_page_table) { - auto level3_entry = page_table[page.get_level_index(page_table::LEVEL3)]; - auto level3_optional_frame = level3_entry.calculate_pointed_to_frame(); - if (level3_optional_frame.has_value() && level3_entry.contains_flags(entry::HUGE_PAGE)) - { - auto level3_frame = level3_optional_frame.value(); - exception_handling::assert(level3_frame.frame_number % (PAGE_TABLE_ENTRY_COUNT * PAGE_TABLE_ENTRY_COUNT) == 0U, - "[Page Mapper] Physical address must be 1 GiB aligned"); - return allocator::physical_frame{level3_frame.frame_number + - page.get_level_index(page_table::LEVEL2) * PAGE_TABLE_ENTRY_COUNT + - page.get_level_index(page_table::LEVEL1)}; - } + return std::nullopt; } - is_valid = page_table.next_table(page.get_level_index(page_table::LEVEL3)); - if (is_valid) + + auto level3_entry = level3_page_table.value()->operator[](page.get_level_index(page_table::LEVEL3)); + auto level3_frame = level3_entry.calculate_pointed_to_frame(); + if (level3_frame && level3_entry.contains_flags(entry::HUGE_PAGE)) + { + exception_handling::assert( + level3_frame.value().frame_number % (PAGE_TABLE_ENTRY_COUNT * PAGE_TABLE_ENTRY_COUNT) == 0U, + "[Page Mapper] Physical address must be 1 GiB aligned"); + return allocator::physical_frame{level3_frame.value().frame_number + + page.get_level_index(page_table::LEVEL2) * PAGE_TABLE_ENTRY_COUNT + + page.get_level_index(page_table::LEVEL1)}; + } + + auto level2_page_table = level3_page_table.value()->next_table(page.get_level_index(page_table::LEVEL3)); + if (level2_page_table) { - auto level2_entry = page_table[page.get_level_index(page_table::LEVEL2)]; - auto level2_optional_frame = level2_entry.calculate_pointed_to_frame(); - if (level2_optional_frame.has_value() && level2_entry.contains_flags(entry::HUGE_PAGE)) + auto level2_entry = level2_page_table.value()->operator[](page.get_level_index(page_table::LEVEL2)); + auto level2_frame = level2_entry.calculate_pointed_to_frame(); + if (level2_frame && level2_entry.contains_flags(entry::HUGE_PAGE)) { - auto level2_frame = level2_optional_frame.value(); - exception_handling::assert(level2_frame.frame_number % PAGE_TABLE_ENTRY_COUNT == 0U, + exception_handling::assert(level2_frame.value().frame_number % PAGE_TABLE_ENTRY_COUNT == 0U, "[Page Mapper] Physical address must be 2 MiB aligned"); - return allocator::physical_frame{level2_frame.frame_number + page.get_level_index(page_table::LEVEL1)}; + return allocator::physical_frame{level2_frame.value().frame_number + page.get_level_index(page_table::LEVEL1)}; } } return std::nullopt; @@ -67,7 +86,7 @@ namespace teachos::arch::memory::paging virtual_page page = virtual_page::containing_address(virtual_address); std::optional frame = translate_page(page); - if (frame.has_value()) + if (frame) { return frame.value().frame_number * allocator::PAGE_FRAME_SIZE + offset; } diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index ea2e9c2..5daf8bb 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -4,16 +4,9 @@ namespace teachos::arch::memory::paging { - page_table::page_table() - : current_level(LEVEL4) - , current_table(reinterpret_cast(0xfffffffffffff000)) - { - // Nothing to do - } - auto page_table::zero_entries() -> void { - constexpr size_t entry_amount = sizeof(current_table->entries) / sizeof(current_table->entries[0]); + constexpr size_t entry_amount = sizeof(entries) / sizeof(entries[0]); for (size_t i = 0; i < entry_amount; ++i) { auto entry = this->operator[](i); @@ -21,18 +14,17 @@ namespace teachos::arch::memory::paging } } - auto page_table::next_table(std::size_t table_index) -> bool + auto page_table::next_table(std::size_t table_index) -> std::optional { - exception_handling::assert(current_level != LEVEL1, - "[Page Table] Attempted to call next_table on level 1 page table"); + // TODO: Find another way to ensure the current page table is not LEVEL1 + // exception_handling::assert(current_level != LEVEL1, "[Page Table] Attempted to call next_table on level 1 page + // table"); auto address = next_table_address(table_index); - bool const success = address.has_value(); - if (success) + if (address) { - current_table = reinterpret_cast(address.value()); - current_level = static_cast(current_level - 1U); + return reinterpret_cast(address.value()); } - return success; + return std::nullopt; } auto page_table::operator[](std::size_t index) -> entry & @@ -40,7 +32,7 @@ namespace teachos::arch::memory::paging // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which // could be incredibly hard to debug later. exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] Index out of bounds"); - return current_table->entries[index]; + return entries[index]; } auto page_table::next_table_address(std::size_t table_index) -> std::optional @@ -49,7 +41,7 @@ namespace teachos::arch::memory::paging if (entry.contains_flags(entry::PRESENT) && !entry.contains_flags(entry::HUGE_PAGE)) { - std::size_t const table_address = reinterpret_cast(current_table); + std::size_t const table_address = reinterpret_cast(this); return ((table_address << 9) | (table_index << 12)); } return std::nullopt; -- cgit v1.2.3 From 72cb015567cb65527e9105e653c001be3c04eab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 21 Oct 2024 12:03:21 +0000 Subject: Use handle struct to ensure next_table is not called on page table level 1 --- arch/x86_64/src/memory/paging/page_mapper.cpp | 37 +++++++++++---------------- arch/x86_64/src/memory/paging/page_table.cpp | 34 +++++++++++++++++++----- 2 files changed, 42 insertions(+), 29 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index cedda54..01c39ca 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -7,53 +7,46 @@ namespace teachos::arch::memory::paging constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0xfffffffffffff000; } - auto create_or_get() -> page_table * + auto create_or_get() -> page_table_handle { - static bool instantiated = false; - static page_table * active_page = nullptr; + static page_table_handle active_page{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), + page_table::LEVEL4}; - if (instantiated) - { - return active_page; - } - - instantiated = true; - active_page = reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS); return active_page; } auto translate_page(virtual_page page) -> std::optional { - page_table * current_page_table = create_or_get(); + auto current_handle = create_or_get(); for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--) { - auto next_page_table = current_page_table->next_table(page.get_level_index(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 (!next_page_table) + if (!next_handle) { return translate_huge_page(page); } - current_page_table = next_page_table.value(); + current_handle = next_handle.value(); } auto level1_index = page.get_level_index(page_table::LEVEL1); - auto level1_frame = current_page_table->operator[](level1_index).calculate_pointed_to_frame(); + auto level1_frame = current_handle[level1_index].calculate_pointed_to_frame(); return level1_frame; } auto translate_huge_page(virtual_page page) -> std::optional { - page_table * current_page_table = create_or_get(); - auto level3_page_table = current_page_table->next_table(page.get_level_index(page_table::LEVEL4)); + auto current_handle = create_or_get(); + auto level3_handle = current_handle.next_table(page.get_level_index(page_table::LEVEL4)); - if (!level3_page_table) + if (!level3_handle) { return std::nullopt; } - auto level3_entry = level3_page_table.value()->operator[](page.get_level_index(page_table::LEVEL3)); + auto level3_entry = level3_handle.value()[page.get_level_index(page_table::LEVEL3)]; auto level3_frame = level3_entry.calculate_pointed_to_frame(); if (level3_frame && level3_entry.contains_flags(entry::HUGE_PAGE)) { @@ -65,10 +58,10 @@ namespace teachos::arch::memory::paging page.get_level_index(page_table::LEVEL1)}; } - auto level2_page_table = level3_page_table.value()->next_table(page.get_level_index(page_table::LEVEL3)); - if (level2_page_table) + auto level2_handle = level3_handle.value().next_table(page.get_level_index(page_table::LEVEL3)); + if (level2_handle) { - auto level2_entry = level2_page_table.value()->operator[](page.get_level_index(page_table::LEVEL2)); + auto level2_entry = level2_handle.value()[page.get_level_index(page_table::LEVEL2)]; auto level2_frame = level2_entry.calculate_pointed_to_frame(); if (level2_frame && level2_entry.contains_flags(entry::HUGE_PAGE)) { diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 5daf8bb..907b64f 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -14,11 +14,8 @@ namespace teachos::arch::memory::paging } } - auto page_table::next_table(std::size_t table_index) -> std::optional + auto page_table::next_table(std::size_t table_index) const -> std::optional { - // TODO: Find another way to ensure the current page table is not LEVEL1 - // exception_handling::assert(current_level != LEVEL1, "[Page Table] Attempted to call next_table on level 1 page - // table"); auto address = next_table_address(table_index); if (address) { @@ -27,7 +24,7 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto page_table::operator[](std::size_t index) -> entry & + auto page_table::operator[](std::size_t index) const -> entry { // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which // could be incredibly hard to debug later. @@ -35,7 +32,7 @@ namespace teachos::arch::memory::paging return entries[index]; } - auto page_table::next_table_address(std::size_t table_index) -> std::optional + auto page_table::next_table_address(std::size_t table_index) const -> std::optional { auto entry = this->operator[](table_index); @@ -47,11 +44,34 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto operator--(page_table::level & level, int) -> page_table::level + auto operator--(page_table::level level, int) -> page_table::level { exception_handling::assert(level != page_table::LEVEL1, "[Page table] Attemptd to decrement enum to value outside of range"); auto value = static_cast::type>(level); return static_cast(--value); } + + page_table_handle::page_table_handle(page_table * handle, page_table::level handle_level) + : handle(handle) + , handle_level(handle_level) + { + exception_handling::assert(handle, "[Page table] Attemptd to pass nullptr as handle to page table handle method"); + } + + auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } + + auto page_table_handle::next_table(std::size_t table_index) const -> std::optional + { + exception_handling::assert(handle_level != page_table::LEVEL1, + "[Page Table] Attempted to call next_table on level 1 page table"); + auto next_table = handle->next_table(table_index); + if (next_table) + { + return page_table_handle{next_table.value(), handle_level--}; + } + return std::nullopt; + } + + auto page_table_handle::operator[](std::size_t index) const -> entry { return handle->operator[](index); } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 4b6e66f686a0341613a303b45e16e5bc744c32dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 21 Oct 2024 12:37:36 +0000 Subject: Fix weird linker error --- arch/x86_64/src/memory/paging/page_mapper.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 01c39ca..28e248b 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -9,10 +9,9 @@ namespace teachos::arch::memory::paging auto create_or_get() -> page_table_handle { - static page_table_handle active_page{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), - page_table::LEVEL4}; - - return active_page; + static page_table * const active_page{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS)}; + // We can not save page_table_handle as a static variable directly, if we do the program will fail to link. + return page_table_handle{active_page, page_table::LEVEL4}; } auto translate_page(virtual_page page) -> std::optional -- cgit v1.2.3 From 552477a12a1163f0f80801b4055dcb5ab3f79e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 21 Oct 2024 12:53:23 +0000 Subject: Use forward declaration to hide actual page_table --- arch/x86_64/src/memory/paging/page_mapper.cpp | 21 +++++---- arch/x86_64/src/memory/paging/page_table.cpp | 63 ++++++++++++++++++++++---- arch/x86_64/src/memory/paging/virtual_page.cpp | 2 +- 3 files changed, 65 insertions(+), 21 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 28e248b..b8f6b89 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -11,14 +11,14 @@ namespace teachos::arch::memory::paging { static page_table * const active_page{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS)}; // We can not save page_table_handle as a static variable directly, if we do the program will fail to link. - return page_table_handle{active_page, page_table::LEVEL4}; + return page_table_handle{active_page, page_table_handle::LEVEL4}; } auto translate_page(virtual_page page) -> std::optional { auto current_handle = create_or_get(); - for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--) + for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; 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 @@ -30,7 +30,7 @@ namespace teachos::arch::memory::paging current_handle = next_handle.value(); } - auto level1_index = page.get_level_index(page_table::LEVEL1); + auto level1_index = page.get_level_index(page_table_handle::LEVEL1); auto level1_frame = current_handle[level1_index].calculate_pointed_to_frame(); return level1_frame; } @@ -38,14 +38,14 @@ namespace teachos::arch::memory::paging auto translate_huge_page(virtual_page page) -> std::optional { auto current_handle = create_or_get(); - auto level3_handle = current_handle.next_table(page.get_level_index(page_table::LEVEL4)); + auto level3_handle = current_handle.next_table(page.get_level_index(page_table_handle::LEVEL4)); if (!level3_handle) { return std::nullopt; } - auto level3_entry = level3_handle.value()[page.get_level_index(page_table::LEVEL3)]; + auto level3_entry = level3_handle.value()[page.get_level_index(page_table_handle::LEVEL3)]; auto level3_frame = level3_entry.calculate_pointed_to_frame(); if (level3_frame && level3_entry.contains_flags(entry::HUGE_PAGE)) { @@ -53,20 +53,21 @@ namespace teachos::arch::memory::paging level3_frame.value().frame_number % (PAGE_TABLE_ENTRY_COUNT * PAGE_TABLE_ENTRY_COUNT) == 0U, "[Page Mapper] Physical address must be 1 GiB aligned"); return allocator::physical_frame{level3_frame.value().frame_number + - page.get_level_index(page_table::LEVEL2) * PAGE_TABLE_ENTRY_COUNT + - page.get_level_index(page_table::LEVEL1)}; + page.get_level_index(page_table_handle::LEVEL2) * PAGE_TABLE_ENTRY_COUNT + + page.get_level_index(page_table_handle::LEVEL1)}; } - auto level2_handle = level3_handle.value().next_table(page.get_level_index(page_table::LEVEL3)); + auto level2_handle = level3_handle.value().next_table(page.get_level_index(page_table_handle::LEVEL3)); if (level2_handle) { - auto level2_entry = level2_handle.value()[page.get_level_index(page_table::LEVEL2)]; + auto level2_entry = level2_handle.value()[page.get_level_index(page_table_handle::LEVEL2)]; auto level2_frame = level2_entry.calculate_pointed_to_frame(); if (level2_frame && level2_entry.contains_flags(entry::HUGE_PAGE)) { exception_handling::assert(level2_frame.value().frame_number % PAGE_TABLE_ENTRY_COUNT == 0U, "[Page Mapper] Physical address must be 2 MiB aligned"); - return allocator::physical_frame{level2_frame.value().frame_number + page.get_level_index(page_table::LEVEL1)}; + return allocator::physical_frame{level2_frame.value().frame_number + + page.get_level_index(page_table_handle::LEVEL1)}; } } return std::nullopt; diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 907b64f..852bbd5 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -4,6 +4,57 @@ namespace teachos::arch::memory::paging { + /** + * @brief A Page table containing 512 entries. + */ + struct page_table + { + /** + * @brief Set every entry of the page to unused. + */ + auto zero_entries() -> void; + + /** + * @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, because there is no furthere page table and mangeling up and returning the physical + * address would cause hard to debug issues. + * + * @param table_index Index of this page table in the page table one level lower. + */ + auto next_table(std::size_t table_index) const -> std::optional; + + /** + * @brief Index operator overload to access specific entries directy. + * + * @param index Index of the entry we want to access and read or write too. + * @return Entry at the given table index. + */ + auto operator[](std::size_t index) const -> entry; + + private: + /** + * @brief Calculates the address of the next page table level for the given table index. The next page table address + * is only valid if the corresponding entry is present and not a huge page. Meaning we use an index into a + * Level 4 page table to get the according Level 3 page table address. + * + * @param table_index Index of this page table in the page table one level higher. + * @return An optional of the address of the next page table or null. + */ + auto next_table_address(std::size_t table_index) const -> std::optional; + + 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_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::type>(level); + return static_cast(--value); + } + auto page_table::zero_entries() -> void { constexpr size_t entry_amount = sizeof(entries) / sizeof(entries[0]); @@ -44,15 +95,7 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto operator--(page_table::level level, int) -> page_table::level - { - exception_handling::assert(level != page_table::LEVEL1, - "[Page table] Attemptd to decrement enum to value outside of range"); - auto value = static_cast::type>(level); - return static_cast(--value); - } - - page_table_handle::page_table_handle(page_table * handle, page_table::level handle_level) + page_table_handle::page_table_handle(page_table * handle, page_table_handle::level handle_level) : handle(handle) , handle_level(handle_level) { @@ -63,7 +106,7 @@ namespace teachos::arch::memory::paging auto page_table_handle::next_table(std::size_t table_index) const -> std::optional { - exception_handling::assert(handle_level != page_table::LEVEL1, + exception_handling::assert(handle_level != page_table_handle::LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); auto next_table = handle->next_table(table_index); if (next_table) diff --git a/arch/x86_64/src/memory/paging/virtual_page.cpp b/arch/x86_64/src/memory/paging/virtual_page.cpp index 9b803d2..fd2085b 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -19,7 +19,7 @@ namespace teachos::arch::memory::paging auto virtual_page::start_address() const -> uint64_t { return page_number * allocator::PAGE_FRAME_SIZE; } - auto virtual_page::get_level_index(page_table::level level) const -> uint64_t + auto virtual_page::get_level_index(page_table_handle::level level) const -> uint64_t { return (start_address() >> (level * 9U)) & 0x1FF; } -- cgit v1.2.3 From 8502cc9b19a9e934e3a4c0b3190b690ee0407a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 21 Oct 2024 14:48:03 +0000 Subject: =?UTF-8?q?Fix=20linker=20error=20using=20friend=20method=20?= =?UTF-8?q?=F0=9F=91=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arch/x86_64/src/memory/paging/page_mapper.cpp | 14 +++++++++++--- arch/x86_64/src/memory/paging/page_table.cpp | 16 ++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) (limited to 'arch/x86_64/src/memory/paging') 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::type>(level); + return static_cast(--value); + } + auto translate_page(virtual_page page) -> std::optional { 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::type>(level); - return static_cast(--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::type>(value); + return static_cast(--new_value); + } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 7d1d7c5f63791506049cc188740cc4956fc8d14c Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Tue, 22 Oct 2024 05:37:28 +0000 Subject: update comments --- arch/x86_64/src/memory/paging/page_table.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 9c9ca76..939f3b1 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -15,10 +15,11 @@ namespace teachos::arch::memory::paging auto zero_entries() -> void; /** - * @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, because there is no furthere page table and mangeling up and returning the physical - * address would cause hard to debug issues. + * @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, because there is no furthere page table and mangeling up and returning the + * physical address would cause hard to debug issues. * * @param table_index Index of this page table in the page table one level lower. */ @@ -34,7 +35,9 @@ namespace teachos::arch::memory::paging private: /** - * @brief Calculates the address of the next page table level for the given table index. The next page table address + * @brief Calculates the address of the next page table level for the given table index. + * + * The next page table address * is only valid if the corresponding entry is present and not a huge page. Meaning we use an index into a * Level 4 page table to get the according Level 3 page table address. * @@ -43,8 +46,8 @@ namespace teachos::arch::memory::paging */ auto next_table_address(std::size_t table_index) const -> std::optional; - 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. + 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 page_table::zero_entries() -> void @@ -91,7 +94,7 @@ namespace teachos::arch::memory::paging : handle(handle) , handle_level(handle_level) { - exception_handling::assert(handle, "[Page table] Attemptd to pass nullptr as handle to page table handle method"); + exception_handling::assert(handle, "[Page table] Attempted to pass nullptr as handle to page table handle method"); } auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } @@ -113,7 +116,7 @@ namespace teachos::arch::memory::paging 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"); + "[Page table] Attempted to decrement enum to value outside of range"); auto new_value = static_cast::type>(value); return static_cast(--new_value); } -- cgit v1.2.3 From ba054441f93a30e2042a71d632a6a5fb04007d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 22 Oct 2024 06:16:51 +0000 Subject: Adjust all briefs --- arch/x86_64/src/memory/paging/page_table.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 939f3b1..555d38a 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -15,11 +15,12 @@ namespace teachos::arch::memory::paging auto zero_entries() -> void; /** - * @brief Returns the next page table level from the given page table index. + * @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. * - * 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, because there is no furthere page table and mangeling up and returning the - * physical address would cause hard to debug issues. + * @note This method + * should not be called on a Level 1 page table, because there is no furthere page table and mangeling up and + * returning the physical address would cause hard to debug issues. * * @param table_index Index of this page table in the page table one level lower. */ @@ -37,9 +38,8 @@ namespace teachos::arch::memory::paging /** * @brief Calculates the address of the next page table level for the given table index. * - * The next page table address - * is only valid if the corresponding entry is present and not a huge page. Meaning we use an index into a - * Level 4 page table to get the according Level 3 page table address. + * @note The next page table address is only valid if the corresponding entry is present and not a huge page. + * Meaning we use an index into a Level 4 page table to get the according Level 3 page table address. * * @param table_index Index of this page table in the page table one level higher. * @return An optional of the address of the next page table or null. -- cgit v1.2.3 From 5a2892f65abe13c32bb07de697826f374c1d2b1d Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Tue, 22 Oct 2024 07:40:35 +0000 Subject: change table level 4 address --- arch/x86_64/src/memory/paging/page_mapper.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 75b17ff..1371868 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -4,8 +4,9 @@ namespace teachos::arch::memory::paging { namespace { - constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0xfffffffffffff000; - } + // TODO: Set the address to a sensible location, this is merely a placeholder + constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0x27AC40; + } // namespace auto create_or_get() -> page_table_handle { @@ -14,19 +15,11 @@ 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::type>(level); - return static_cast(--value); - } - auto translate_page(virtual_page page) -> std::optional { auto current_handle = create_or_get(); - for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; level = subtract_level(level)) + for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; 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 -- cgit v1.2.3 From 7d4139b6fa0214604f467a782951cdc0f90e903f Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Tue, 22 Oct 2024 08:00:57 +0000 Subject: fix page level -- operator --- arch/x86_64/src/memory/paging/page_table.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 555d38a..b229ff7 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -99,25 +99,30 @@ namespace teachos::arch::memory::paging auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } - auto page_table_handle::next_table(std::size_t table_index) const -> std::optional + auto page_table_handle::next_table(std::size_t table_index) -> std::optional { exception_handling::assert(handle_level != page_table_handle::LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); auto next_table = handle->next_table(table_index); if (next_table) { - return page_table_handle{next_table.value(), handle_level--}; + handle_level--; + return page_table_handle{next_table.value(), handle_level}; } return std::nullopt; } 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 + auto operator--(page_table_handle::level & value, int) -> page_table_handle::level { exception_handling::assert(value != page_table_handle::LEVEL1, "[Page table] Attempted to decrement enum to value outside of range"); + + page_table_handle::level original_value = value; auto new_value = static_cast::type>(value); - return static_cast(--new_value); + value = static_cast(--new_value); + + return original_value; } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 9a1f3e66b6c860fdca689241e78f85bdbb5b4da5 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Tue, 22 Oct 2024 08:01:40 +0000 Subject: add previous state to comment --- arch/x86_64/src/memory/paging/page_mapper.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 1371868..03f9f10 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -5,6 +5,7 @@ namespace teachos::arch::memory::paging namespace { // TODO: Set the address to a sensible location, this is merely a placeholder + // constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0xfffffffffffff000; constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0x27AC40; } // namespace -- cgit v1.2.3 From d43f5cb4c13b39b89951434e73078859d121cd4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 22 Oct 2024 08:13:13 +0000 Subject: Implement precrement instead --- arch/x86_64/src/memory/paging/page_mapper.cpp | 2 +- arch/x86_64/src/memory/paging/page_table.cpp | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 03f9f10..e9c6766 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -20,7 +20,7 @@ namespace teachos::arch::memory::paging { 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) { 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 diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index b229ff7..c735a62 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -106,23 +106,19 @@ namespace teachos::arch::memory::paging auto next_table = handle->next_table(table_index); if (next_table) { - handle_level--; - return page_table_handle{next_table.value(), handle_level}; + return page_table_handle{next_table.value(), --handle_level}; } return std::nullopt; } 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 + auto operator--(page_table_handle::level & value) -> page_table_handle::level & { exception_handling::assert(value != page_table_handle::LEVEL1, "[Page table] Attempted to decrement enum to value outside of range"); - - page_table_handle::level original_value = value; auto new_value = static_cast::type>(value); value = static_cast(--new_value); - - return original_value; + return value; } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From c29d8c3b65f63bfd54031412d9c2975ef7571460 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Tue, 22 Oct 2024 08:58:48 +0000 Subject: use actual page_table address --- arch/x86_64/src/memory/paging/page_entry.cpp | 4 ++-- arch/x86_64/src/memory/paging/page_mapper.cpp | 9 ++++----- arch/x86_64/src/memory/paging/page_table.cpp | 14 +++++++------- 3 files changed, 13 insertions(+), 14 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index a172ca8..5d597ab 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -33,10 +33,10 @@ namespace teachos::arch::memory::paging auto entry::contains_flags(std::bitset<64U> other) const -> bool { return (flags & other) == other; } - auto entry::set_entry(allocator::physical_frame frame, std::bitset<64U> flags) -> void + auto entry::set_entry(allocator::physical_frame frame, std::bitset<64U> additional_flags) -> void { exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, "[Paging Entry] Start address is not aligned with page"); - flags = std::bitset<64U>{frame.start_address()} | flags; + flags = std::bitset<64U>{frame.start_address()} | additional_flags; } } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 03f9f10..1fe72e3 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -1,19 +1,18 @@ #include "arch/memory/paging/page_mapper.hpp" +#include "arch/boot/pointers.hpp" + namespace teachos::arch::memory::paging { namespace { - // TODO: Set the address to a sensible location, this is merely a placeholder - // constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0xfffffffffffff000; - constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0x27AC40; + page_table * PAGE_TABLE_LEVEL_4_ADDRESS = boot::page_map_level_4; } // namespace auto create_or_get() -> page_table_handle { - static page_table * const active_page{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS)}; // We can not save page_table_handle as a static variable directly, if we do the program will fail to link. - return page_table_handle{active_page, page_table_handle::LEVEL4}; + return page_table_handle{PAGE_TABLE_LEVEL_4_ADDRESS, page_table_handle::LEVEL4}; } auto translate_page(virtual_page page) -> std::optional diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index b229ff7..8971209 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -24,7 +24,7 @@ namespace teachos::arch::memory::paging * * @param table_index Index of this page table in the page table one level lower. */ - auto next_table(std::size_t table_index) const -> std::optional; + auto next_table(std::size_t table_index) -> std::optional; /** * @brief Index operator overload to access specific entries directy. @@ -32,7 +32,7 @@ namespace teachos::arch::memory::paging * @param index Index of the entry we want to access and read or write too. * @return Entry at the given table index. */ - auto operator[](std::size_t index) const -> entry; + auto operator[](std::size_t index) -> entry &; private: /** @@ -44,7 +44,7 @@ namespace teachos::arch::memory::paging * @param table_index Index of this page table in the page table one level higher. * @return An optional of the address of the next page table or null. */ - auto next_table_address(std::size_t table_index) const -> std::optional; + auto next_table_address(std::size_t table_index) -> std::optional; 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. @@ -60,7 +60,7 @@ namespace teachos::arch::memory::paging } } - auto page_table::next_table(std::size_t table_index) const -> std::optional + auto page_table::next_table(std::size_t table_index) -> std::optional { auto address = next_table_address(table_index); if (address) @@ -70,7 +70,7 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto page_table::operator[](std::size_t index) const -> entry + auto page_table::operator[](std::size_t index) -> entry & { // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which // could be incredibly hard to debug later. @@ -78,7 +78,7 @@ namespace teachos::arch::memory::paging return entries[index]; } - auto page_table::next_table_address(std::size_t table_index) const -> std::optional + auto page_table::next_table_address(std::size_t table_index) -> std::optional { auto entry = this->operator[](table_index); @@ -112,7 +112,7 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto page_table_handle::operator[](std::size_t index) const -> entry { return handle->operator[](index); } + auto page_table_handle::operator[](std::size_t index) -> entry & { return handle->operator[](index); } auto operator--(page_table_handle::level & value, int) -> page_table_handle::level { -- cgit v1.2.3 From d42ef85dbc3786fb45b8b4625b3f07446f51390d Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Tue, 22 Oct 2024 09:19:57 +0000 Subject: add todos --- arch/x86_64/src/memory/paging/page_table.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 33d0b75..5faad24 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -46,8 +46,8 @@ namespace teachos::arch::memory::paging */ auto next_table_address(std::size_t table_index) -> std::optional; - 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. + 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 page_table::zero_entries() -> void @@ -55,6 +55,7 @@ namespace teachos::arch::memory::paging constexpr size_t entry_amount = sizeof(entries) / sizeof(entries[0]); for (size_t i = 0; i < entry_amount; ++i) { + // TODO: Crashes when trying to access entry! auto entry = this->operator[](i); entry.set_unused(); } -- cgit v1.2.3 From ef22c9394534014dbda3e2a070df3d02a9fd041e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 22 Oct 2024 09:22:04 +0000 Subject: Remove default initalization of member variables --- arch/x86_64/src/memory/paging/page_table.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 5faad24..33d0b75 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -46,8 +46,8 @@ namespace teachos::arch::memory::paging */ auto next_table_address(std::size_t table_index) -> std::optional; - 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. + 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 page_table::zero_entries() -> void @@ -55,7 +55,6 @@ namespace teachos::arch::memory::paging constexpr size_t entry_amount = sizeof(entries) / sizeof(entries[0]); for (size_t i = 0; i < entry_amount; ++i) { - // TODO: Crashes when trying to access entry! auto entry = this->operator[](i); entry.set_unused(); } -- cgit v1.2.3 From 61db7fa6097a9e3784f8fb18a8d3de47d7d7c007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 23 Oct 2024 13:28:07 +0000 Subject: Extract next_table_or_create into page_table --- arch/x86_64/src/memory/paging/page_table.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 33d0b75..40662e3 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -1,7 +1,5 @@ #include "arch/memory/paging/page_table.hpp" -#include "arch/exception_handling/assert.hpp" - namespace teachos::arch::memory::paging { /** -- cgit v1.2.3 From c373109b4912e857273c30b3ebd2d25296da85fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 23 Oct 2024 13:40:02 +0000 Subject: Ensure page table is static --- arch/x86_64/src/memory/paging/page_mapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index b89b637..995f2c3 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -6,7 +6,7 @@ namespace teachos::arch::memory::paging { namespace { - page_table * PAGE_TABLE_LEVEL_4_ADDRESS = boot::page_map_level_4; + static page_table * PAGE_TABLE_LEVEL_4_ADDRESS = boot::page_map_level_4; } // namespace auto create_or_get() -> page_table_handle -- cgit v1.2.3 From 355993d16fcdb2d020d6f1ff02adfa191f8f4d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 23 Oct 2024 13:42:32 +0000 Subject: Initalize page table on first call --- arch/x86_64/src/memory/paging/page_mapper.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 995f2c3..39b5e64 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -4,15 +4,11 @@ namespace teachos::arch::memory::paging { - namespace - { - static page_table * PAGE_TABLE_LEVEL_4_ADDRESS = boot::page_map_level_4; - } // namespace - auto create_or_get() -> page_table_handle { + static page_table * table = boot::page_map_level_4; // We can not save page_table_handle as a static variable directly, if we do the program will fail to link. - return page_table_handle{PAGE_TABLE_LEVEL_4_ADDRESS, page_table_handle::LEVEL4}; + return page_table_handle{table, page_table_handle::LEVEL4}; } auto translate_page(virtual_page page) -> std::optional -- cgit v1.2.3 From cf52909d11fba4e81e77f264abe1f7055399793a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 23 Oct 2024 13:55:23 +0000 Subject: Revert "Initalize page table on first call" This reverts commit 355993d16fcdb2d020d6f1ff02adfa191f8f4d48. --- arch/x86_64/src/memory/paging/page_mapper.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 39b5e64..995f2c3 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -4,11 +4,15 @@ namespace teachos::arch::memory::paging { + namespace + { + static page_table * PAGE_TABLE_LEVEL_4_ADDRESS = boot::page_map_level_4; + } // namespace + auto create_or_get() -> page_table_handle { - static page_table * table = boot::page_map_level_4; // We can not save page_table_handle as a static variable directly, if we do the program will fail to link. - return page_table_handle{table, page_table_handle::LEVEL4}; + return page_table_handle{PAGE_TABLE_LEVEL_4_ADDRESS, page_table_handle::LEVEL4}; } auto translate_page(virtual_page page) -> std::optional -- cgit v1.2.3 From c7bc900ef9293045265f503696277b0405119c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 23 Oct 2024 14:29:08 +0000 Subject: Use virtual level 4 address again --- arch/x86_64/src/memory/paging/page_mapper.cpp | 6 ++++-- arch/x86_64/src/memory/paging/page_table.cpp | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 995f2c3..687bffd 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -6,13 +6,15 @@ namespace teachos::arch::memory::paging { namespace { - static page_table * PAGE_TABLE_LEVEL_4_ADDRESS = boot::page_map_level_4; + constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0xfffffffffffff000; } // namespace auto create_or_get() -> page_table_handle { + // Perhaps we need to access boot::page_map_level_4; so we do not crash when returning entry? + static page_table * const active_page = reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS); // We can not save page_table_handle as a static variable directly, if we do the program will fail to link. - return page_table_handle{PAGE_TABLE_LEVEL_4_ADDRESS, page_table_handle::LEVEL4}; + return page_table_handle{active_page, page_table_handle::LEVEL4}; } auto translate_page(virtual_page page) -> std::optional diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 40662e3..bfd91da 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -51,6 +51,7 @@ namespace teachos::arch::memory::paging auto page_table::zero_entries() -> void { constexpr size_t entry_amount = sizeof(entries) / sizeof(entries[0]); + static_assert(entry_amount == PAGE_TABLE_ENTRY_COUNT); for (size_t i = 0; i < entry_amount; ++i) { auto entry = this->operator[](i); @@ -73,7 +74,8 @@ namespace teachos::arch::memory::paging // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which // could be incredibly hard to debug later. exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] Index out of bounds"); - return entries[index]; + auto & entry = entries[index]; + return entry; } auto page_table::next_table_address(std::size_t table_index) -> std::optional -- cgit v1.2.3 From 97fe123c2d5bd0c49e8d360a989fcee8971c61ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 23 Oct 2024 14:32:27 +0000 Subject: Add todo comment on why code crashes --- arch/x86_64/src/memory/paging/page_table.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index bfd91da..21611e1 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -74,6 +74,7 @@ namespace teachos::arch::memory::paging // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which // could be incredibly hard to debug later. exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] Index out of bounds"); + // TODO: This section seems to return completly invalid entry, debugger doesn't even show a value? Revert once fixed auto & entry = entries[index]; return entry; } -- cgit v1.2.3 From 4c97a4e1e2055ce63a0d16d1e5be880792a5e7eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 23 Oct 2024 15:08:10 +0000 Subject: Replace uint64_t with size_t --- arch/x86_64/src/memory/paging/virtual_page.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/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 fd2085b..91f63b4 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -17,9 +17,9 @@ namespace teachos::arch::memory::paging return virtual_page{virtual_address / allocator::PAGE_FRAME_SIZE}; } - auto virtual_page::start_address() const -> uint64_t { return page_number * allocator::PAGE_FRAME_SIZE; } + auto virtual_page::start_address() const -> size_t { return page_number * allocator::PAGE_FRAME_SIZE; } - auto virtual_page::get_level_index(page_table_handle::level level) const -> uint64_t + auto virtual_page::get_level_index(page_table_handle::level level) const -> size_t { return (start_address() >> (level * 9U)) & 0x1FF; } -- cgit v1.2.3 From c2c7503fec31c07645fdc087dbf1ef487e90912e Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sat, 26 Oct 2024 07:03:01 +0000 Subject: fix mmap type, adjust linker, use std::array --- arch/x86_64/src/memory/paging/page_table.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 21611e1..1d7b8a0 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -1,5 +1,7 @@ #include "arch/memory/paging/page_table.hpp" +#include + namespace teachos::arch::memory::paging { /** @@ -44,8 +46,9 @@ namespace teachos::arch::memory::paging */ auto next_table_address(std::size_t table_index) -> std::optional; - 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. + std::array + entries{}; ///< Entries containing addresses to page tables of a level below or + ///< actual virtual addresses for the level 1 page table. }; auto page_table::zero_entries() -> void -- cgit v1.2.3 From e44a16e45a309aa96375a2215d0d2405cf467dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 26 Oct 2024 07:37:50 +0000 Subject: Use construct at to zero memory before using it. --- arch/x86_64/src/memory/paging/page_mapper.cpp | 11 ++--------- arch/x86_64/src/memory/paging/page_table.cpp | 15 +++++++++++---- 2 files changed, 13 insertions(+), 13 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 687bffd..cd77366 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -4,17 +4,10 @@ namespace teachos::arch::memory::paging { - namespace - { - constexpr size_t PAGE_TABLE_LEVEL_4_ADDRESS = 0xfffffffffffff000; - } // namespace - auto create_or_get() -> page_table_handle { - // Perhaps we need to access boot::page_map_level_4; so we do not crash when returning entry? - static page_table * const active_page = reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS); - // We can not save page_table_handle as a static variable directly, if we do the program will fail to link. - return page_table_handle{active_page, page_table_handle::LEVEL4}; + // TODO: As soon as linker error is fixed in toolchain make handle static and return that. + return page_table_handle{arch::boot::page_map_level_4, page_table_handle::LEVEL4}; } auto translate_page(virtual_page page) -> std::optional diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 1d7b8a0..2f97a80 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -1,6 +1,7 @@ #include "arch/memory/paging/page_table.hpp" #include +#include namespace teachos::arch::memory::paging { @@ -9,6 +10,11 @@ namespace teachos::arch::memory::paging */ struct page_table { + /** + * @brief Defaulted constructor. + */ + page_table() = default; + /** * @brief Set every entry of the page to unused. */ @@ -46,9 +52,9 @@ namespace teachos::arch::memory::paging */ auto next_table_address(std::size_t table_index) -> std::optional; - std::array - entries{}; ///< Entries containing addresses to page tables of a level below or - ///< actual virtual addresses for the level 1 page table. + std::array entries = + {}; ///< Entries containing addresses to page tables of a level below or + ///< actual virtual addresses for the level 1 page table. }; auto page_table::zero_entries() -> void @@ -67,7 +73,8 @@ namespace teachos::arch::memory::paging auto address = next_table_address(table_index); if (address) { - return reinterpret_cast(address.value()); + // TODO: Probably erases the data even if the page table already existed previously and had values. + return std::construct_at(reinterpret_cast(address.value())); } return std::nullopt; } -- cgit v1.2.3 From bca36b0c10fcae447c90e211e83987fea28eecdc Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sat, 26 Oct 2024 08:47:26 +0000 Subject: wip --- arch/x86_64/src/memory/paging/page_mapper.cpp | 12 +++++++++++- arch/x86_64/src/memory/paging/page_table.cpp | 15 +++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index cd77366..dff9ae4 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -4,10 +4,20 @@ namespace teachos::arch::memory::paging { + auto create_or_get() -> page_table_handle { + static auto initialized = false; // TODO: As soon as linker error is fixed in toolchain make handle static and return that. - return page_table_handle{arch::boot::page_map_level_4, page_table_handle::LEVEL4}; + page_table_handle active_handle{boot::page_map_level_4, page_table_handle::LEVEL4}; + + if (!initialized) + { + active_handle.initialize_page_tables(); + initialized = true; + } + + return active_handle; } auto translate_page(virtual_page page) -> std::optional diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 2f97a80..b5315a0 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -10,11 +10,6 @@ namespace teachos::arch::memory::paging */ struct page_table { - /** - * @brief Defaulted constructor. - */ - page_table() = default; - /** * @brief Set every entry of the page to unused. */ @@ -105,7 +100,15 @@ namespace teachos::arch::memory::paging : handle(handle) , handle_level(handle_level) { - exception_handling::assert(handle, "[Page table] Attempted to pass nullptr as handle to page table handle method"); + exception_handling::assert(handle, "[Page Table] Attempted to pass nullptr as handle to page table handle method"); + } + + auto page_table_handle::initialize_page_table() -> void + { + exception_handling::assert(handle_level == page_table_handle::LEVEL4, + "[Page Table] Attempted to initialize a page table of level 3 or lower"); + + auto level_3_page_table = boot::page_map_level_3; } auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } -- cgit v1.2.3 From e56964da2dd54e598d3a7fca20fc0071f32026a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 26 Oct 2024 09:20:53 +0000 Subject: Adjust to toolchain updates --- arch/x86_64/src/memory/paging/page_mapper.cpp | 3 +-- arch/x86_64/src/memory/paging/page_table.cpp | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index dff9ae4..75d6759 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -8,8 +8,7 @@ namespace teachos::arch::memory::paging auto create_or_get() -> page_table_handle { static auto initialized = false; - // TODO: As soon as linker error is fixed in toolchain make handle static and return that. - page_table_handle active_handle{boot::page_map_level_4, page_table_handle::LEVEL4}; + static page_table_handle active_handle{boot::page_map_level_4, page_table_handle::LEVEL4}; if (!initialized) { diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index b5315a0..808fbbc 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -103,12 +103,11 @@ namespace teachos::arch::memory::paging exception_handling::assert(handle, "[Page Table] Attempted to pass nullptr as handle to page table handle method"); } - auto page_table_handle::initialize_page_table() -> void + auto page_table_handle::initialize_page_tables() -> void { exception_handling::assert(handle_level == page_table_handle::LEVEL4, "[Page Table] Attempted to initialize a page table of level 3 or lower"); - - auto level_3_page_table = boot::page_map_level_3; + // std::construct_at(reinterpret_cast(_end_of_image)); } auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } -- cgit v1.2.3 From 47ec911e3b3624d875da5b9cc0102e3d17a6d54a Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sat, 26 Oct 2024 10:09:46 +0000 Subject: use linker variable --- arch/x86_64/src/memory/paging/page_table.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 808fbbc..059ada2 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -3,6 +3,8 @@ #include #include +extern char _end_of_image; + namespace teachos::arch::memory::paging { /** @@ -107,7 +109,7 @@ namespace teachos::arch::memory::paging { exception_handling::assert(handle_level == page_table_handle::LEVEL4, "[Page Table] Attempted to initialize a page table of level 3 or lower"); - // std::construct_at(reinterpret_cast(_end_of_image)); + auto level3_page_table = std::construct_at(reinterpret_cast(_end_of_image)); } auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } -- cgit v1.2.3 From ffb6507d565585bb2a817ff01317b7643bd6d981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 26 Oct 2024 11:23:58 +0000 Subject: Create for loops to construct base level 3 page table --- arch/x86_64/src/memory/paging/page_table.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 059ada2..935dd8c 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -70,8 +70,7 @@ namespace teachos::arch::memory::paging auto address = next_table_address(table_index); if (address) { - // TODO: Probably erases the data even if the page table already existed previously and had values. - return std::construct_at(reinterpret_cast(address.value())); + return reinterpret_cast(address.value()); } return std::nullopt; } @@ -109,7 +108,17 @@ namespace teachos::arch::memory::paging { exception_handling::assert(handle_level == page_table_handle::LEVEL4, "[Page Table] Attempted to initialize a page table of level 3 or lower"); - auto level3_page_table = std::construct_at(reinterpret_cast(_end_of_image)); + auto level3_page_table = std::construct_at(reinterpret_cast(&_end_of_image)); + for (size_t n = 1; n <= PAGE_TABLE_ENTRY_COUNT; ++n) + { + size_t offset = sizeof(page_table) * n; + std::construct_at(reinterpret_cast(&_end_of_image + offset)); + for (size_t m = 0; m < PAGE_TABLE_ENTRY_COUNT; ++m) + { + size_t offset = sizeof(page_table) * (n + m); + std::construct_at(reinterpret_cast(&_end_of_image + offset)); + } + } } auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } -- cgit v1.2.3 From bc993ccf7c5a80a8f4b1cd05437701efb81beec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 26 Oct 2024 11:24:13 +0000 Subject: Fix typo --- arch/x86_64/src/memory/paging/page_table.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 935dd8c..38d5025 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -113,7 +113,7 @@ namespace teachos::arch::memory::paging { size_t offset = sizeof(page_table) * n; std::construct_at(reinterpret_cast(&_end_of_image + offset)); - for (size_t m = 0; m < PAGE_TABLE_ENTRY_COUNT; ++m) + for (size_t m = 1; m <= PAGE_TABLE_ENTRY_COUNT; ++m) { size_t offset = sizeof(page_table) * (n + m); std::construct_at(reinterpret_cast(&_end_of_image + offset)); -- cgit v1.2.3 From 454dcb6f2033ee80a43f99d23323b02e8fd0a115 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sat, 26 Oct 2024 11:30:01 +0000 Subject: add doc --- arch/x86_64/src/memory/paging/page_table.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 059ada2..14ead9e 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -3,6 +3,10 @@ #include #include +/* + * This is a linker variable reference. This referenc cannot reside inside a namespace, because in + * that case the compiler would try to find arch::memory::paging::_end_of_image inside the ELF file. + */ extern char _end_of_image; namespace teachos::arch::memory::paging -- cgit v1.2.3 From f4ab51df7f9ed783dbcfbecffc0a9d919c501135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 26 Oct 2024 11:44:12 +0000 Subject: Write P3 table into P4 table at index 0 --- arch/x86_64/src/memory/paging/page_entry.cpp | 6 ++++++ arch/x86_64/src/memory/paging/page_table.cpp | 2 ++ 2 files changed, 8 insertions(+) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 5d597ab..09d048b 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -4,6 +4,12 @@ namespace teachos::arch::memory::paging { + entry::entry(uint64_t flags) + : flags(flags) + { + // Nothing to do. + } + auto entry::is_unused() const -> bool { return flags == 0U; } auto entry::set_unused() -> void { flags = 0U; } diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index c0f199a..4747e0b 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -123,6 +123,8 @@ namespace teachos::arch::memory::paging std::construct_at(reinterpret_cast(&_end_of_image + offset)); } } + size_t const flags = reinterpret_cast(level3_page_table); + this->operator[](0U) = entry{flags}; } auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } -- cgit v1.2.3 From 6eec9c34a83fc5c0d93bc485c2ee0252824c2201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 26 Oct 2024 11:57:11 +0000 Subject: Fix missing default constructor. --- arch/x86_64/src/memory/paging/page_table.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 4747e0b..c9b4a37 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -85,8 +85,7 @@ namespace teachos::arch::memory::paging // could be incredibly hard to debug later. exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] Index out of bounds"); // TODO: This section seems to return completly invalid entry, debugger doesn't even show a value? Revert once fixed - auto & entry = entries[index]; - return entry; + return entries[index]; } auto page_table::next_table_address(std::size_t table_index) -> std::optional -- cgit v1.2.3 From d86af47f0b62cb3ae302fb292747944620542db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 26 Oct 2024 12:19:16 +0000 Subject: Attempt to fix initalize_page_tables, crashes on n = 1, m = 507 --- arch/x86_64/src/memory/paging/page_table.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index c9b4a37..93cc0de 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -118,7 +118,7 @@ namespace teachos::arch::memory::paging std::construct_at(reinterpret_cast(&_end_of_image + offset)); for (size_t m = 1; m <= PAGE_TABLE_ENTRY_COUNT; ++m) { - size_t offset = sizeof(page_table) * (n + m); + size_t offset = sizeof(page_table) * ((PAGE_TABLE_ENTRY_COUNT * n) + m); std::construct_at(reinterpret_cast(&_end_of_image + offset)); } } -- cgit v1.2.3 From ca17ed52ea768f1e1c837207f7d27afa6ed99cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 26 Oct 2024 13:43:51 +0000 Subject: Update boot.s comments and comment initalize page tables out --- arch/x86_64/src/memory/paging/page_table.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 93cc0de..868cf86 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -109,6 +109,8 @@ namespace teachos::arch::memory::paging auto page_table_handle::initialize_page_tables() -> void { + // TODO: This should be done differently and only once the other methods are working see Rempaing the Kernel + /* exception_handling::assert(handle_level == page_table_handle::LEVEL4, "[Page Table] Attempted to initialize a page table of level 3 or lower"); auto level3_page_table = std::construct_at(reinterpret_cast(&_end_of_image)); @@ -124,6 +126,7 @@ namespace teachos::arch::memory::paging } size_t const flags = reinterpret_cast(level3_page_table); this->operator[](0U) = entry{flags}; + */ } auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } -- cgit v1.2.3 From 1518177efac38961a36db0bc40152d00c38e6281 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 27 Oct 2024 13:27:33 +0000 Subject: add correct optional handling --- arch/x86_64/src/memory/paging/page_mapper.cpp | 12 ++++++------ arch/x86_64/src/memory/paging/page_table.cpp | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 75d6759..bc0c0d9 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -28,7 +28,7 @@ namespace teachos::arch::memory::paging 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 (!next_handle) + if (!next_handle.has_value()) { return translate_huge_page(page); } @@ -45,14 +45,14 @@ namespace teachos::arch::memory::paging auto current_handle = create_or_get(); auto level3_handle = current_handle.next_table(page.get_level_index(page_table_handle::LEVEL4)); - if (!level3_handle) + if (!level3_handle.has_value()) { return std::nullopt; } auto level3_entry = level3_handle.value()[page.get_level_index(page_table_handle::LEVEL3)]; auto level3_frame = level3_entry.calculate_pointed_to_frame(); - if (level3_frame && level3_entry.contains_flags(entry::HUGE_PAGE)) + if (level3_frame.has_value() && level3_entry.contains_flags(entry::HUGE_PAGE)) { exception_handling::assert( level3_frame.value().frame_number % (PAGE_TABLE_ENTRY_COUNT * PAGE_TABLE_ENTRY_COUNT) == 0U, @@ -63,11 +63,11 @@ namespace teachos::arch::memory::paging } auto level2_handle = level3_handle.value().next_table(page.get_level_index(page_table_handle::LEVEL3)); - if (level2_handle) + if (level2_handle.has_value()) { auto level2_entry = level2_handle.value()[page.get_level_index(page_table_handle::LEVEL2)]; auto level2_frame = level2_entry.calculate_pointed_to_frame(); - if (level2_frame && level2_entry.contains_flags(entry::HUGE_PAGE)) + if (level2_frame.has_value() && level2_entry.contains_flags(entry::HUGE_PAGE)) { exception_handling::assert(level2_frame.value().frame_number % PAGE_TABLE_ENTRY_COUNT == 0U, "[Page Mapper] Physical address must be 2 MiB aligned"); @@ -84,7 +84,7 @@ namespace teachos::arch::memory::paging virtual_page page = virtual_page::containing_address(virtual_address); std::optional frame = translate_page(page); - if (frame) + if (frame.has_value()) { return frame.value().frame_number * allocator::PAGE_FRAME_SIZE + offset; } diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 868cf86..f1df31d 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -72,7 +72,7 @@ namespace teachos::arch::memory::paging auto page_table::next_table(std::size_t table_index) -> std::optional { auto address = next_table_address(table_index); - if (address) + if (address.has_value()) { return reinterpret_cast(address.value()); } @@ -136,7 +136,7 @@ namespace teachos::arch::memory::paging exception_handling::assert(handle_level != page_table_handle::LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); auto next_table = handle->next_table(table_index); - if (next_table) + if (next_table.has_value()) { return page_table_handle{next_table.value(), --handle_level}; } -- cgit v1.2.3 From d0f47ef0cd8cb2f5079808a261dd724b3eb1a3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 27 Oct 2024 14:12:35 +0000 Subject: Fix typos --- arch/x86_64/src/memory/paging/page_entry.cpp | 2 +- arch/x86_64/src/memory/paging/virtual_page.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 09d048b..8b4aa36 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -19,7 +19,7 @@ namespace teachos::arch::memory::paging if (contains_flags(PRESENT)) { auto physical_address = calculate_physical_address(); - return allocator::physical_frame::containing_address(physical_address); + return allocator::physical_frame::containing_address(physical_address & 0x000ffffffffff000); } return std::nullopt; } diff --git a/arch/x86_64/src/memory/paging/virtual_page.cpp b/arch/x86_64/src/memory/paging/virtual_page.cpp index 91f63b4..db0d96c 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -21,6 +21,6 @@ namespace teachos::arch::memory::paging auto virtual_page::get_level_index(page_table_handle::level level) const -> size_t { - return (start_address() >> (level * 9U)) & 0x1FF; + return (page_number >> (level * 9U)) & 0x1FF; } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 38e87d52891429d56d20a54ce205d1e421068f36 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 27 Oct 2024 14:16:35 +0000 Subject: update gas --- arch/x86_64/src/memory/paging/page_entry.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 8b4aa36..f3b5be1 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -18,25 +18,12 @@ namespace teachos::arch::memory::paging { if (contains_flags(PRESENT)) { - auto physical_address = calculate_physical_address(); - return allocator::physical_frame::containing_address(physical_address & 0x000ffffffffff000); + constexpr std::size_t mask = 0x000fffff'fffff000; + return allocator::physical_frame::containing_address(flags.to_ullong() & mask); } return std::nullopt; } - auto entry::calculate_physical_address() const -> std::size_t - { - constexpr std::size_t start_bit = 12U; - constexpr std::size_t end_bit = 52U; - size_t value = 0U; - - for (auto i = start_bit; i < end_bit; i++) - { - value |= (flags[i] ? (1 << (i - start_bit)) : 0); - } - return value; - } - auto entry::contains_flags(std::bitset<64U> other) const -> bool { return (flags & other) == other; } auto entry::set_entry(allocator::physical_frame frame, std::bitset<64U> additional_flags) -> void -- cgit v1.2.3 From acbd91c9898808a928af0b1bdd9d5058e8a91f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 27 Oct 2024 14:55:52 +0000 Subject: Add typedef for virtual / physical addresses --- arch/x86_64/src/memory/paging/page_mapper.cpp | 28 +++++++++++++------------- arch/x86_64/src/memory/paging/virtual_page.cpp | 6 +++--- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'arch/x86_64/src/memory/paging') 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 + { + 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 { 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 offset = virtual_address % allocator::PAGE_FRAME_SIZE; - virtual_page page = virtual_page::containing_address(virtual_address); - std::optional 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; } -- cgit v1.2.3 From 58d01f6aae80a66faa5163602c399c28dcf30cb6 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 27 Oct 2024 15:25:57 +0000 Subject: revert p4 address update --- arch/x86_64/src/memory/paging/page_entry.cpp | 10 +++++++--- arch/x86_64/src/memory/paging/page_mapper.cpp | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index f3b5be1..c3e9614 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -4,6 +4,11 @@ namespace teachos::arch::memory::paging { + namespace + { + constexpr std::size_t PHYSICAL_ADDRESS_MASK = 0x000fffff'fffff000; + } + entry::entry(uint64_t flags) : flags(flags) { @@ -18,8 +23,7 @@ namespace teachos::arch::memory::paging { if (contains_flags(PRESENT)) { - constexpr std::size_t mask = 0x000fffff'fffff000; - return allocator::physical_frame::containing_address(flags.to_ullong() & mask); + return allocator::physical_frame::containing_address(flags.to_ullong() & PHYSICAL_ADDRESS_MASK); } return std::nullopt; } @@ -28,7 +32,7 @@ namespace teachos::arch::memory::paging auto entry::set_entry(allocator::physical_frame frame, std::bitset<64U> additional_flags) -> void { - exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, + exception_handling::assert((frame.start_address() & ~PHYSICAL_ADDRESS_MASK) == 0, "[Paging Entry] Start address is not aligned with page"); flags = std::bitset<64U>{frame.start_address()} | additional_flags; } diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 00c27b0..fb9ea39 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -8,7 +8,8 @@ namespace teachos::arch::memory::paging auto create_or_get() -> page_table_handle { static auto initialized = false; - static page_table_handle active_handle{boot::page_map_level_4, page_table_handle::LEVEL4}; + static page_table_handle active_handle{reinterpret_cast(0xffffffff'fffff000), + page_table_handle::LEVEL4}; if (!initialized) { -- cgit v1.2.3 From 58680f7d84ca5771be31c0037e8caf5791d79b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 28 Oct 2024 12:29:39 +0000 Subject: Fix invalid assertion --- arch/x86_64/src/memory/paging/page_table.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index f1df31d..e919a1e 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -84,7 +84,6 @@ namespace teachos::arch::memory::paging // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which // could be incredibly hard to debug later. exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] Index out of bounds"); - // TODO: This section seems to return completly invalid entry, debugger doesn't even show a value? Revert once fixed return entries[index]; } -- cgit v1.2.3 From efcb913196ccf0386a557e8c1053c430e5896179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 28 Oct 2024 12:45:46 +0000 Subject: Convert bitset to uint64_t instead of uint64_t to bitset --- arch/x86_64/src/memory/paging/page_entry.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index c3e9614..dbf23e2 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -23,7 +23,7 @@ namespace teachos::arch::memory::paging { if (contains_flags(PRESENT)) { - return allocator::physical_frame::containing_address(flags.to_ullong() & PHYSICAL_ADDRESS_MASK); + return allocator::physical_frame::containing_address(flags.to_ulong() & PHYSICAL_ADDRESS_MASK); } return std::nullopt; } @@ -34,6 +34,6 @@ namespace teachos::arch::memory::paging { exception_handling::assert((frame.start_address() & ~PHYSICAL_ADDRESS_MASK) == 0, "[Paging Entry] Start address is not aligned with page"); - flags = std::bitset<64U>{frame.start_address()} | additional_flags; + flags = frame.start_address() | additional_flags.to_ulong(); } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From e5925df93411429340d2887594004aaa690d2ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 28 Oct 2024 13:32:09 +0000 Subject: Adjust constant and make all possible variables const --- arch/x86_64/src/memory/paging/page_entry.cpp | 2 +- arch/x86_64/src/memory/paging/page_mapper.cpp | 26 +++++++++++++++----------- arch/x86_64/src/memory/paging/page_table.cpp | 22 +++++++++++----------- arch/x86_64/src/memory/paging/virtual_page.cpp | 2 +- 4 files changed, 28 insertions(+), 24 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index dbf23e2..8923fea 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -6,7 +6,7 @@ namespace teachos::arch::memory::paging { namespace { - constexpr std::size_t PHYSICAL_ADDRESS_MASK = 0x000fffff'fffff000; + std::size_t constexpr PHYSICAL_ADDRESS_MASK = 0x000fffff'fffff000; } entry::entry(uint64_t flags) diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index fb9ea39..3baf6ae 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -4,11 +4,15 @@ namespace teachos::arch::memory::paging { + namespace + { + std::size_t constexpr PAGE_TABLE_LEVEL_4_ADDRESS = 0xffffffff'fffff000; + } // namespace auto create_or_get() -> page_table_handle { static auto initialized = false; - static page_table_handle active_handle{reinterpret_cast(0xffffffff'fffff000), + static page_table_handle active_handle{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), page_table_handle::LEVEL4}; if (!initialized) @@ -22,9 +26,9 @@ namespace teachos::arch::memory::paging auto translate_address(virtual_address address) -> std::optional { - auto offset = address % allocator::PAGE_FRAME_SIZE; - auto page = virtual_page::containing_address(address); - auto frame = translate_page(page); + auto const offset = address % allocator::PAGE_FRAME_SIZE; + auto const page = virtual_page::containing_address(address); + auto const frame = translate_page(page); if (frame.has_value()) { @@ -40,7 +44,7 @@ namespace teachos::arch::memory::paging for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; --level) { - auto next_handle = current_handle.next_table(page.get_level_index(level)); + auto const 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 (!next_handle.has_value()) @@ -50,8 +54,8 @@ namespace teachos::arch::memory::paging current_handle = next_handle.value(); } - auto level1_index = page.get_level_index(page_table_handle::LEVEL1); - auto level1_frame = current_handle[level1_index].calculate_pointed_to_frame(); + auto const level1_index = page.get_level_index(page_table_handle::LEVEL1); + auto const level1_frame = current_handle[level1_index].calculate_pointed_to_frame(); return level1_frame; } @@ -65,8 +69,8 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto level3_entry = level3_handle.value()[page.get_level_index(page_table_handle::LEVEL3)]; - auto level3_frame = level3_entry.calculate_pointed_to_frame(); + auto const level3_entry = level3_handle.value()[page.get_level_index(page_table_handle::LEVEL3)]; + auto const level3_frame = level3_entry.calculate_pointed_to_frame(); if (level3_frame.has_value() && level3_entry.contains_flags(entry::HUGE_PAGE)) { exception_handling::assert( @@ -80,8 +84,8 @@ namespace teachos::arch::memory::paging auto level2_handle = level3_handle.value().next_table(page.get_level_index(page_table_handle::LEVEL3)); if (level2_handle.has_value()) { - auto level2_entry = level2_handle.value()[page.get_level_index(page_table_handle::LEVEL2)]; - auto level2_frame = level2_entry.calculate_pointed_to_frame(); + auto const level2_entry = level2_handle.value()[page.get_level_index(page_table_handle::LEVEL2)]; + auto const level2_frame = level2_entry.calculate_pointed_to_frame(); if (level2_frame.has_value() && level2_entry.contains_flags(entry::HUGE_PAGE)) { exception_handling::assert(level2_frame.value().frame_number % PAGE_TABLE_ENTRY_COUNT == 0U, diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index e919a1e..a01c431 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -60,9 +60,9 @@ namespace teachos::arch::memory::paging auto page_table::zero_entries() -> void { - constexpr size_t entry_amount = sizeof(entries) / sizeof(entries[0]); + std::size_t constexpr entry_amount = sizeof(entries) / sizeof(entries[0]); static_assert(entry_amount == PAGE_TABLE_ENTRY_COUNT); - for (size_t i = 0; i < entry_amount; ++i) + for (std::size_t i = 0; i < entry_amount; ++i) { auto entry = this->operator[](i); entry.set_unused(); @@ -71,7 +71,7 @@ namespace teachos::arch::memory::paging auto page_table::next_table(std::size_t table_index) -> std::optional { - auto address = next_table_address(table_index); + auto const address = next_table_address(table_index); if (address.has_value()) { return reinterpret_cast(address.value()); @@ -89,11 +89,11 @@ namespace teachos::arch::memory::paging auto page_table::next_table_address(std::size_t table_index) -> std::optional { - auto entry = this->operator[](table_index); + auto const entry = this->operator[](table_index); if (entry.contains_flags(entry::PRESENT) && !entry.contains_flags(entry::HUGE_PAGE)) { - std::size_t const table_address = reinterpret_cast(this); + auto const table_address = reinterpret_cast(this); return ((table_address << 9) | (table_index << 12)); } return std::nullopt; @@ -113,17 +113,17 @@ namespace teachos::arch::memory::paging exception_handling::assert(handle_level == page_table_handle::LEVEL4, "[Page Table] Attempted to initialize a page table of level 3 or lower"); auto level3_page_table = std::construct_at(reinterpret_cast(&_end_of_image)); - for (size_t n = 1; n <= PAGE_TABLE_ENTRY_COUNT; ++n) + for (std::size_t n = 1; n <= PAGE_TABLE_ENTRY_COUNT; ++n) { - size_t offset = sizeof(page_table) * n; + std::size_t offset = sizeof(page_table) * n; std::construct_at(reinterpret_cast(&_end_of_image + offset)); - for (size_t m = 1; m <= PAGE_TABLE_ENTRY_COUNT; ++m) + for (std::size_t m = 1; m <= PAGE_TABLE_ENTRY_COUNT; ++m) { - size_t offset = sizeof(page_table) * ((PAGE_TABLE_ENTRY_COUNT * n) + m); + std::size_t offset = sizeof(page_table) * ((PAGE_TABLE_ENTRY_COUNT * n) + m); std::construct_at(reinterpret_cast(&_end_of_image + offset)); } } - size_t const flags = reinterpret_cast(level3_page_table); + std::size_t const flags = reinterpret_cast(level3_page_table); this->operator[](0U) = entry{flags}; */ } @@ -134,7 +134,7 @@ namespace teachos::arch::memory::paging { exception_handling::assert(handle_level != page_table_handle::LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); - auto next_table = handle->next_table(table_index); + auto const next_table = handle->next_table(table_index); if (next_table.has_value()) { return page_table_handle{next_table.value(), --handle_level}; diff --git a/arch/x86_64/src/memory/paging/virtual_page.cpp b/arch/x86_64/src/memory/paging/virtual_page.cpp index d39bb7f..4221335 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -12,7 +12,7 @@ namespace teachos::arch::memory::paging auto virtual_page::containing_address(virtual_address address) -> virtual_page { - exception_handling::assert(address < 0x0000800000000000 || address >= 0xffff800000000000, + exception_handling::assert(address < 0x00008000'00000000 || address >= 0xffff8000'00000000, "[Virtual Page] Attempted to create virtual page from invalid address"); return virtual_page{address / allocator::PAGE_FRAME_SIZE}; } -- cgit v1.2.3 From aa981cad951c4aa2a5e2f7a7f8f1b7b9a0ff4bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 28 Oct 2024 14:41:31 +0000 Subject: Fix lost updates, because of writing into copies instead of references --- arch/x86_64/src/memory/paging/page_entry.cpp | 3 ++- arch/x86_64/src/memory/paging/page_mapper.cpp | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 8923fea..23c700f 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -23,7 +23,8 @@ namespace teachos::arch::memory::paging { if (contains_flags(PRESENT)) { - return allocator::physical_frame::containing_address(flags.to_ulong() & PHYSICAL_ADDRESS_MASK); + auto const address = flags.to_ulong() & PHYSICAL_ADDRESS_MASK; + return allocator::physical_frame::containing_address(address); } return std::nullopt; } diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 3baf6ae..0a1fc65 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -55,8 +55,8 @@ namespace teachos::arch::memory::paging } auto const level1_index = page.get_level_index(page_table_handle::LEVEL1); - auto const level1_frame = current_handle[level1_index].calculate_pointed_to_frame(); - return level1_frame; + auto const level1_entry = current_handle[level1_index]; + return level1_entry.calculate_pointed_to_frame(); } auto translate_huge_page(virtual_page page) -> std::optional -- cgit v1.2.3 From bd3ae51093b504bd035cb698c637ef6f82994ba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 28 Oct 2024 14:48:32 +0000 Subject: Remove not required includes --- arch/x86_64/src/memory/paging/page_mapper.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 0a1fc65..be4b259 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -1,7 +1,5 @@ #include "arch/memory/paging/page_mapper.hpp" -#include "arch/boot/pointers.hpp" - namespace teachos::arch::memory::paging { namespace -- cgit v1.2.3 From f9f047f519d0100c40b914d3ce777ac2f8430b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 28 Oct 2024 15:18:31 +0000 Subject: Add is empty check method for page table --- arch/x86_64/src/memory/paging/page_table.cpp | 55 ++++++++++++---------------- 1 file changed, 23 insertions(+), 32 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index a01c431..64a4fd9 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -1,5 +1,6 @@ #include "arch/memory/paging/page_table.hpp" +#include #include #include @@ -16,31 +17,16 @@ namespace teachos::arch::memory::paging */ struct page_table { - /** - * @brief Set every entry of the page to unused. - */ auto zero_entries() -> void; - /** - * @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. - * - * @note This method - * should not be called on a Level 1 page table, because there is no furthere page table and mangeling up and - * returning the physical address would cause hard to debug issues. - * - * @param table_index Index of this page table in the page table one level lower. - */ - auto next_table(std::size_t table_index) -> std::optional; + auto is_empty() const -> bool; + + auto next_table(std::size_t table_index) const -> std::optional; - /** - * @brief Index operator overload to access specific entries directy. - * - * @param index Index of the entry we want to access and read or write too. - * @return Entry at the given table index. - */ auto operator[](std::size_t index) -> entry &; + auto operator[](std::size_t index) const -> entry const &; + private: /** * @brief Calculates the address of the next page table level for the given table index. @@ -51,7 +37,7 @@ namespace teachos::arch::memory::paging * @param table_index Index of this page table in the page table one level higher. * @return An optional of the address of the next page table or null. */ - auto next_table_address(std::size_t table_index) -> std::optional; + auto next_table_address(std::size_t table_index) const -> std::optional; std::array entries = {}; ///< Entries containing addresses to page tables of a level below or @@ -60,16 +46,15 @@ namespace teachos::arch::memory::paging auto page_table::zero_entries() -> void { - std::size_t constexpr entry_amount = sizeof(entries) / sizeof(entries[0]); - static_assert(entry_amount == PAGE_TABLE_ENTRY_COUNT); - for (std::size_t i = 0; i < entry_amount; ++i) - { - auto entry = this->operator[](i); - entry.set_unused(); - } + std::ranges::for_each(entries, [](entry & entry) { entry.set_unused(); }); + } + + auto page_table::is_empty() const -> bool + { + return std::all_of(entries.begin(), entries.end(), [](entry const & entry) { return entry.is_unused(); }); } - auto page_table::next_table(std::size_t table_index) -> std::optional + auto page_table::next_table(std::size_t table_index) const -> std::optional { auto const address = next_table_address(table_index); if (address.has_value()) @@ -81,13 +66,17 @@ namespace teachos::arch::memory::paging auto page_table::operator[](std::size_t index) -> entry & { - // C array is not bounds checked, therefore we have to check ourselves, to ensure no out of bounds reads, which - // could be incredibly hard to debug later. exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] Index out of bounds"); return entries[index]; } - auto page_table::next_table_address(std::size_t table_index) -> std::optional + auto page_table::operator[](std::size_t index) const -> entry const & + { + exception_handling::assert(index < PAGE_TABLE_ENTRY_COUNT, "[Page Table] Index out of bounds"); + return entries[index]; + } + + auto page_table::next_table_address(std::size_t table_index) const -> std::optional { auto const entry = this->operator[](table_index); @@ -130,6 +119,8 @@ namespace teachos::arch::memory::paging auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } + auto page_table_handle::is_empty() const -> bool { return handle->is_empty(); } + auto page_table_handle::next_table(std::size_t table_index) -> std::optional { exception_handling::assert(handle_level != page_table_handle::LEVEL1, -- cgit v1.2.3 From 0d16592c8dce876d8e621c73dea41c33339f3173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 29 Oct 2024 06:55:36 +0000 Subject: Use more virtual and physical address typedef where useful --- arch/x86_64/src/memory/paging/virtual_page.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/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 4221335..c0ec88d 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -17,7 +17,7 @@ namespace teachos::arch::memory::paging return virtual_page{address / allocator::PAGE_FRAME_SIZE}; } - auto virtual_page::start_address() const -> size_t { return page_number * allocator::PAGE_FRAME_SIZE; } + auto virtual_page::start_address() const -> virtual_address { return page_number * allocator::PAGE_FRAME_SIZE; } auto virtual_page::get_level_index(page_table_handle::level level) const -> size_t { -- cgit v1.2.3 From 4c4479a4b728fd7eaf007649e946f9435ee1e402 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Tue, 29 Oct 2024 07:32:24 +0000 Subject: implement page_table unmapping --- arch/x86_64/src/memory/paging/page_table.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 64a4fd9..0121e95 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -92,7 +92,8 @@ namespace teachos::arch::memory::paging : handle(handle) , handle_level(handle_level) { - exception_handling::assert(handle, "[Page Table] Attempted to pass nullptr as handle to page table handle method"); + exception_handling::assert(handle != nullptr, + "[Page Table] Attempted to pass nullptr as handle to page table handle method"); } auto page_table_handle::initialize_page_tables() -> void @@ -133,6 +134,8 @@ namespace teachos::arch::memory::paging return std::nullopt; } + auto page_table_handle::get_level() const -> page_table_handle::level { return handle_level; } + auto page_table_handle::operator[](std::size_t index) -> entry & { return handle->operator[](index); } auto operator--(page_table_handle::level & value) -> page_table_handle::level & -- cgit v1.2.3 From 4e9338075cf30702b922e5aecbc33c18bfd3f759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 29 Oct 2024 08:11:13 +0000 Subject: Remove not required methods --- arch/x86_64/src/memory/paging/page_mapper.cpp | 8 -------- arch/x86_64/src/memory/paging/page_table.cpp | 22 ---------------------- 2 files changed, 30 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index be4b259..2f2ee6a 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -9,16 +9,8 @@ namespace teachos::arch::memory::paging auto create_or_get() -> page_table_handle { - static auto initialized = false; static page_table_handle active_handle{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), page_table_handle::LEVEL4}; - - if (!initialized) - { - active_handle.initialize_page_tables(); - initialized = true; - } - return active_handle; } diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 0121e95..3ad1d54 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -96,28 +96,6 @@ namespace teachos::arch::memory::paging "[Page Table] Attempted to pass nullptr as handle to page table handle method"); } - auto page_table_handle::initialize_page_tables() -> void - { - // TODO: This should be done differently and only once the other methods are working see Rempaing the Kernel - /* - exception_handling::assert(handle_level == page_table_handle::LEVEL4, - "[Page Table] Attempted to initialize a page table of level 3 or lower"); - auto level3_page_table = std::construct_at(reinterpret_cast(&_end_of_image)); - for (std::size_t n = 1; n <= PAGE_TABLE_ENTRY_COUNT; ++n) - { - std::size_t offset = sizeof(page_table) * n; - std::construct_at(reinterpret_cast(&_end_of_image + offset)); - for (std::size_t m = 1; m <= PAGE_TABLE_ENTRY_COUNT; ++m) - { - std::size_t offset = sizeof(page_table) * ((PAGE_TABLE_ENTRY_COUNT * n) + m); - std::construct_at(reinterpret_cast(&_end_of_image + offset)); - } - } - std::size_t const flags = reinterpret_cast(level3_page_table); - this->operator[](0U) = entry{flags}; - */ - } - auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } auto page_table_handle::is_empty() const -> bool { return handle->is_empty(); } -- cgit v1.2.3 From 29c51a2cc30060bd904b06cbe6913755352c48c7 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Fri, 1 Nov 2024 10:08:50 +0000 Subject: create temporary page implementation --- arch/x86_64/src/memory/paging/page_table.cpp | 24 ++++++++-------- arch/x86_64/src/memory/paging/temporary_page.cpp | 35 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 arch/x86_64/src/memory/paging/temporary_page.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 3ad1d54..3c9942a 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -88,33 +88,33 @@ namespace teachos::arch::memory::paging return std::nullopt; } - page_table_handle::page_table_handle(page_table * handle, page_table_handle::level handle_level) - : handle(handle) - , handle_level(handle_level) + page_table_handle::page_table_handle(page_table * table, page_table_handle::level table_level) + : table(table) + , table_level(table_level) { - exception_handling::assert(handle != nullptr, - "[Page Table] Attempted to pass nullptr as handle to page table handle method"); + exception_handling::assert(table != nullptr, + "[Page Table] Attempted to pass nullptr as table to page table table method"); } - auto page_table_handle::zero_entries() -> void { handle->zero_entries(); } + auto page_table_handle::zero_entries() -> void { table->zero_entries(); } - auto page_table_handle::is_empty() const -> bool { return handle->is_empty(); } + auto page_table_handle::is_empty() const -> bool { return table->is_empty(); } auto page_table_handle::next_table(std::size_t table_index) -> std::optional { - exception_handling::assert(handle_level != page_table_handle::LEVEL1, + exception_handling::assert(table_level != page_table_handle::LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); - auto const next_table = handle->next_table(table_index); + auto const next_table = table->next_table(table_index); if (next_table.has_value()) { - return page_table_handle{next_table.value(), --handle_level}; + return page_table_handle{next_table.value(), --table_level}; } return std::nullopt; } - auto page_table_handle::get_level() const -> page_table_handle::level { return handle_level; } + auto page_table_handle::get_level() const -> page_table_handle::level { return table_level; } - auto page_table_handle::operator[](std::size_t index) -> entry & { return handle->operator[](index); } + auto page_table_handle::operator[](std::size_t index) -> entry & { return table->operator[](index); } auto operator--(page_table_handle::level & value) -> page_table_handle::level & { diff --git a/arch/x86_64/src/memory/paging/temporary_page.cpp b/arch/x86_64/src/memory/paging/temporary_page.cpp new file mode 100644 index 0000000..433f0ce --- /dev/null +++ b/arch/x86_64/src/memory/paging/temporary_page.cpp @@ -0,0 +1,35 @@ +#include "arch/memory/paging/temporary_page.hpp" + +#include "arch/memory/paging/page_entry.hpp" +#include "arch/memory/paging/page_mapper.hpp" + +namespace teachos::arch::memory::paging +{ + auto temporary_page::map_to_frame(allocator::physical_frame frame) -> virtual_address + { + exception_handling::assert(!translate_page(page).has_value(), "[Temporary page] Page is already mapped"); + + map_page_to_frame(allocator, page, frame, entry::WRITABLE); + return page.start_address(); + } + + auto temporary_page::unmap() -> void { unmap_page(allocator, page); } + + auto temporary_page::map_table_frame(allocator::physical_frame frame) -> page_table_handle + { + page_table_handle handle{reinterpret_cast(map_to_frame(frame)), page_table_handle::LEVEL1}; + return handle; + } + + auto temporary_page::zero_entries() -> void + { + auto frame = allocator.allocate_frame(); + exception_handling::assert(!frame.has_value(), "[Temporary Page] Tiny allocator could not allocate a frame"); + + page_table_handle handle = map_table_frame(frame.value()); + handle.zero_entries(); + handle[511].set_entry(frame.value(), entry::PRESENT | entry::WRITABLE); + + unmap(); + } +} // namespace teachos::arch::memory::paging \ No newline at end of file -- cgit v1.2.3 From defb727b2d0ac902e10e9736440779495b8b51a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 1 Nov 2024 12:56:22 +0000 Subject: Move methods into seperate class. --- arch/x86_64/src/memory/paging/page_mapper.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp index 2f2ee6a..30055e8 100644 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ b/arch/x86_64/src/memory/paging/page_mapper.cpp @@ -7,14 +7,15 @@ namespace teachos::arch::memory::paging std::size_t constexpr PAGE_TABLE_LEVEL_4_ADDRESS = 0xffffffff'fffff000; } // namespace - auto create_or_get() -> page_table_handle + auto active_page_table::create_or_get() -> active_page_table & { static page_table_handle active_handle{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), page_table_handle::LEVEL4}; - return active_handle; + static active_page_table active_page{active_handle}; + return active_page; } - auto translate_address(virtual_address address) -> std::optional + auto active_page_table::translate_address(virtual_address address) -> std::optional { auto const offset = address % allocator::PAGE_FRAME_SIZE; auto const page = virtual_page::containing_address(address); @@ -28,9 +29,9 @@ namespace teachos::arch::memory::paging return std::nullopt; } - auto translate_page(virtual_page page) -> std::optional + auto active_page_table::translate_page(virtual_page page) -> std::optional { - auto current_handle = create_or_get(); + auto current_handle = active_handle; for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; --level) { @@ -49,9 +50,9 @@ namespace teachos::arch::memory::paging return level1_entry.calculate_pointed_to_frame(); } - auto translate_huge_page(virtual_page page) -> std::optional + auto active_page_table::translate_huge_page(virtual_page page) -> std::optional { - auto current_handle = create_or_get(); + auto current_handle = active_handle; auto level3_handle = current_handle.next_table(page.get_level_index(page_table_handle::LEVEL4)); if (!level3_handle.has_value()) @@ -86,4 +87,10 @@ namespace teachos::arch::memory::paging } return std::nullopt; } + + active_page_table::active_page_table(page_table_handle active_handle) + : active_handle(active_handle) + { + // Nothing to do + } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From b0d917bc8ad95e282f25949c489dfc1c06b91b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 1 Nov 2024 13:02:14 +0000 Subject: Adjust temporary page to changes --- arch/x86_64/src/memory/paging/temporary_page.cpp | 25 +++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/temporary_page.cpp b/arch/x86_64/src/memory/paging/temporary_page.cpp index 433f0ce..4a8f4da 100644 --- a/arch/x86_64/src/memory/paging/temporary_page.cpp +++ b/arch/x86_64/src/memory/paging/temporary_page.cpp @@ -1,35 +1,38 @@ #include "arch/memory/paging/temporary_page.hpp" #include "arch/memory/paging/page_entry.hpp" -#include "arch/memory/paging/page_mapper.hpp" namespace teachos::arch::memory::paging { - auto temporary_page::map_to_frame(allocator::physical_frame frame) -> virtual_address + auto temporary_page::map_to_frame(allocator::physical_frame frame, + active_page_table & active_table) -> virtual_address { - exception_handling::assert(!translate_page(page).has_value(), "[Temporary page] Page is already mapped"); + exception_handling::assert(!active_table.translate_page(page).has_value(), + "[Temporary page] Page is already mapped"); - map_page_to_frame(allocator, page, frame, entry::WRITABLE); + active_table.map_page_to_frame(allocator, page, frame, entry::WRITABLE); return page.start_address(); } - auto temporary_page::unmap() -> void { unmap_page(allocator, page); } + auto temporary_page::unmap(active_page_table & active_table) -> void { active_table.unmap_page(allocator, page); } - auto temporary_page::map_table_frame(allocator::physical_frame frame) -> page_table_handle + auto temporary_page::map_table_frame(allocator::physical_frame frame, + active_page_table & active_table) -> page_table_handle { - page_table_handle handle{reinterpret_cast(map_to_frame(frame)), page_table_handle::LEVEL1}; + page_table_handle handle{reinterpret_cast(map_to_frame(frame, active_table)), + page_table_handle::LEVEL1}; return handle; } - auto temporary_page::zero_entries() -> void + auto temporary_page::zero_entries(active_page_table & active_table) -> void { auto frame = allocator.allocate_frame(); exception_handling::assert(!frame.has_value(), "[Temporary Page] Tiny allocator could not allocate a frame"); - page_table_handle handle = map_table_frame(frame.value()); + page_table_handle handle = map_table_frame(frame.value(), active_table); handle.zero_entries(); handle[511].set_entry(frame.value(), entry::PRESENT | entry::WRITABLE); - unmap(); + unmap(active_table); } -} // namespace teachos::arch::memory::paging \ No newline at end of file +} // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 31e1ac359eb4b84bdd81f768b2de327193976a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 1 Nov 2024 13:22:02 +0000 Subject: Remove static page mapper and replace with unique active and inactive page table classes. --- .../x86_64/src/memory/paging/active_page_table.cpp | 96 ++++++++++++++++++++++ .../src/memory/paging/inactive_page_table.cpp | 5 ++ arch/x86_64/src/memory/paging/page_mapper.cpp | 96 ---------------------- arch/x86_64/src/memory/paging/temporary_page.cpp | 2 +- 4 files changed, 102 insertions(+), 97 deletions(-) create mode 100644 arch/x86_64/src/memory/paging/active_page_table.cpp create mode 100644 arch/x86_64/src/memory/paging/inactive_page_table.cpp delete mode 100644 arch/x86_64/src/memory/paging/page_mapper.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp new file mode 100644 index 0000000..38696f8 --- /dev/null +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -0,0 +1,96 @@ +#include "arch/memory/paging/active_page_table.hpp" + +namespace teachos::arch::memory::paging +{ + namespace + { + std::size_t constexpr PAGE_TABLE_LEVEL_4_ADDRESS = 0xffffffff'fffff000; + } // namespace + + auto active_page_table::create_or_get() -> active_page_table & + { + static page_table_handle active_handle{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), + page_table_handle::LEVEL4}; + static active_page_table active_page{active_handle}; + return active_page; + } + + auto active_page_table::translate_address(virtual_address address) -> std::optional + { + auto const offset = address % allocator::PAGE_FRAME_SIZE; + auto const page = virtual_page::containing_address(address); + auto const frame = translate_page(page); + + if (frame.has_value()) + { + return frame.value().frame_number * allocator::PAGE_FRAME_SIZE + offset; + } + + return std::nullopt; + } + + auto active_page_table::translate_page(virtual_page page) -> std::optional + { + auto current_handle = active_handle; + + for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; --level) + { + auto const 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 (!next_handle.has_value()) + { + return translate_huge_page(page); + } + current_handle = next_handle.value(); + } + + auto const level1_index = page.get_level_index(page_table_handle::LEVEL1); + auto const level1_entry = current_handle[level1_index]; + return level1_entry.calculate_pointed_to_frame(); + } + + auto active_page_table::translate_huge_page(virtual_page page) -> std::optional + { + auto current_handle = active_handle; + auto level3_handle = current_handle.next_table(page.get_level_index(page_table_handle::LEVEL4)); + + if (!level3_handle.has_value()) + { + return std::nullopt; + } + + auto const level3_entry = level3_handle.value()[page.get_level_index(page_table_handle::LEVEL3)]; + auto const level3_frame = level3_entry.calculate_pointed_to_frame(); + if (level3_frame.has_value() && level3_entry.contains_flags(entry::HUGE_PAGE)) + { + exception_handling::assert( + level3_frame.value().frame_number % (PAGE_TABLE_ENTRY_COUNT * PAGE_TABLE_ENTRY_COUNT) == 0U, + "[Page Mapper] Physical address must be 1 GiB aligned"); + return allocator::physical_frame{level3_frame.value().frame_number + + page.get_level_index(page_table_handle::LEVEL2) * PAGE_TABLE_ENTRY_COUNT + + page.get_level_index(page_table_handle::LEVEL1)}; + } + + auto level2_handle = level3_handle.value().next_table(page.get_level_index(page_table_handle::LEVEL3)); + if (level2_handle.has_value()) + { + auto const level2_entry = level2_handle.value()[page.get_level_index(page_table_handle::LEVEL2)]; + auto const level2_frame = level2_entry.calculate_pointed_to_frame(); + if (level2_frame.has_value() && level2_entry.contains_flags(entry::HUGE_PAGE)) + { + exception_handling::assert(level2_frame.value().frame_number % PAGE_TABLE_ENTRY_COUNT == 0U, + "[Page Mapper] Physical address must be 2 MiB aligned"); + return allocator::physical_frame{level2_frame.value().frame_number + + page.get_level_index(page_table_handle::LEVEL1)}; + } + } + return std::nullopt; + } + + active_page_table::active_page_table(page_table_handle active_handle) + : active_handle(active_handle) + { + // Nothing to do + } +} // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/inactive_page_table.cpp b/arch/x86_64/src/memory/paging/inactive_page_table.cpp new file mode 100644 index 0000000..1f36aa3 --- /dev/null +++ b/arch/x86_64/src/memory/paging/inactive_page_table.cpp @@ -0,0 +1,5 @@ +#include "arch/memory/paging/inactive_page_table.hpp" + +namespace teachos::arch::memory::paging +{ +} // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/page_mapper.cpp b/arch/x86_64/src/memory/paging/page_mapper.cpp deleted file mode 100644 index 30055e8..0000000 --- a/arch/x86_64/src/memory/paging/page_mapper.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "arch/memory/paging/page_mapper.hpp" - -namespace teachos::arch::memory::paging -{ - namespace - { - std::size_t constexpr PAGE_TABLE_LEVEL_4_ADDRESS = 0xffffffff'fffff000; - } // namespace - - auto active_page_table::create_or_get() -> active_page_table & - { - static page_table_handle active_handle{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), - page_table_handle::LEVEL4}; - static active_page_table active_page{active_handle}; - return active_page; - } - - auto active_page_table::translate_address(virtual_address address) -> std::optional - { - auto const offset = address % allocator::PAGE_FRAME_SIZE; - auto const page = virtual_page::containing_address(address); - auto const frame = translate_page(page); - - if (frame.has_value()) - { - return frame.value().frame_number * allocator::PAGE_FRAME_SIZE + offset; - } - - return std::nullopt; - } - - auto active_page_table::translate_page(virtual_page page) -> std::optional - { - auto current_handle = active_handle; - - for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; --level) - { - auto const 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 (!next_handle.has_value()) - { - return translate_huge_page(page); - } - current_handle = next_handle.value(); - } - - auto const level1_index = page.get_level_index(page_table_handle::LEVEL1); - auto const level1_entry = current_handle[level1_index]; - return level1_entry.calculate_pointed_to_frame(); - } - - auto active_page_table::translate_huge_page(virtual_page page) -> std::optional - { - auto current_handle = active_handle; - auto level3_handle = current_handle.next_table(page.get_level_index(page_table_handle::LEVEL4)); - - if (!level3_handle.has_value()) - { - return std::nullopt; - } - - auto const level3_entry = level3_handle.value()[page.get_level_index(page_table_handle::LEVEL3)]; - auto const level3_frame = level3_entry.calculate_pointed_to_frame(); - if (level3_frame.has_value() && level3_entry.contains_flags(entry::HUGE_PAGE)) - { - exception_handling::assert( - level3_frame.value().frame_number % (PAGE_TABLE_ENTRY_COUNT * PAGE_TABLE_ENTRY_COUNT) == 0U, - "[Page Mapper] Physical address must be 1 GiB aligned"); - return allocator::physical_frame{level3_frame.value().frame_number + - page.get_level_index(page_table_handle::LEVEL2) * PAGE_TABLE_ENTRY_COUNT + - page.get_level_index(page_table_handle::LEVEL1)}; - } - - auto level2_handle = level3_handle.value().next_table(page.get_level_index(page_table_handle::LEVEL3)); - if (level2_handle.has_value()) - { - auto const level2_entry = level2_handle.value()[page.get_level_index(page_table_handle::LEVEL2)]; - auto const level2_frame = level2_entry.calculate_pointed_to_frame(); - if (level2_frame.has_value() && level2_entry.contains_flags(entry::HUGE_PAGE)) - { - exception_handling::assert(level2_frame.value().frame_number % PAGE_TABLE_ENTRY_COUNT == 0U, - "[Page Mapper] Physical address must be 2 MiB aligned"); - return allocator::physical_frame{level2_frame.value().frame_number + - page.get_level_index(page_table_handle::LEVEL1)}; - } - } - return std::nullopt; - } - - active_page_table::active_page_table(page_table_handle active_handle) - : active_handle(active_handle) - { - // Nothing to do - } -} // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/temporary_page.cpp b/arch/x86_64/src/memory/paging/temporary_page.cpp index 4a8f4da..180b4a8 100644 --- a/arch/x86_64/src/memory/paging/temporary_page.cpp +++ b/arch/x86_64/src/memory/paging/temporary_page.cpp @@ -27,7 +27,7 @@ namespace teachos::arch::memory::paging auto temporary_page::zero_entries(active_page_table & active_table) -> void { auto frame = allocator.allocate_frame(); - exception_handling::assert(!frame.has_value(), "[Temporary Page] Tiny allocator could not allocate a frame"); + exception_handling::assert(frame.has_value(), "[Temporary Page] Tiny allocator could not allocate a frame"); page_table_handle handle = map_table_frame(frame.value(), active_table); handle.zero_entries(); -- cgit v1.2.3 From b9fdcc729c1875858297c0f3fb9d8e6feff71374 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Fri, 1 Nov 2024 14:29:58 +0000 Subject: implement temporary_page and update active/inactive page tables --- arch/x86_64/src/memory/paging/active_page_table.cpp | 10 ++++++++++ arch/x86_64/src/memory/paging/inactive_page_table.cpp | 10 ++++++++++ 2 files changed, 20 insertions(+) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp index 38696f8..5f31f75 100644 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -93,4 +93,14 @@ namespace teachos::arch::memory::paging { // Nothing to do } + + auto active_page_table::with(inactive_page_table inactive_page_table, temporary_page temporary_page, + active_page_table::function f) -> void + { + active_handle[511].set_entry(inactive_page_table.page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); + invalidate_page_cache(PAGE_TABLE_LEVEL_4_ADDRESS); + + f(*mapper); + } + } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/inactive_page_table.cpp b/arch/x86_64/src/memory/paging/inactive_page_table.cpp index 1f36aa3..afba1f0 100644 --- a/arch/x86_64/src/memory/paging/inactive_page_table.cpp +++ b/arch/x86_64/src/memory/paging/inactive_page_table.cpp @@ -2,4 +2,14 @@ namespace teachos::arch::memory::paging { + inactive_page_table::inactive_page_table(allocator::physical_frame frame, active_page_table active_page_table, + temporary_page temporary_page) + : page_table_level_4_frame{frame} + { + auto table = temporary_page.map_table_frame(page_table_level_4_frame, active_page_table); + table.zero_entries(); + table[511].set_entry(page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); + + temporary_page.unmap(active_page_table); + } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 08875c9c42c94dd23b16baa70b2be60cf35eb253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 1 Nov 2024 14:53:31 +0000 Subject: Fix circular dependency issue --- arch/x86_64/src/memory/paging/active_page_table.cpp | 10 ---------- arch/x86_64/src/memory/paging/kernel_mapper.cpp | 13 +++++++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 arch/x86_64/src/memory/paging/kernel_mapper.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp index 5f31f75..38696f8 100644 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -93,14 +93,4 @@ namespace teachos::arch::memory::paging { // Nothing to do } - - auto active_page_table::with(inactive_page_table inactive_page_table, temporary_page temporary_page, - active_page_table::function f) -> void - { - active_handle[511].set_entry(inactive_page_table.page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); - invalidate_page_cache(PAGE_TABLE_LEVEL_4_ADDRESS); - - f(*mapper); - } - } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/kernel_mapper.cpp b/arch/x86_64/src/memory/paging/kernel_mapper.cpp new file mode 100644 index 0000000..9dfc5ad --- /dev/null +++ b/arch/x86_64/src/memory/paging/kernel_mapper.cpp @@ -0,0 +1,13 @@ +#include "arch/memory/paging/kernel_mapper.hpp" + +namespace teachos::arch::memory::paging +{ + auto with(inactive_page_table inactive_page_table, temporary_page temporary_page, + active_page_table::function f) -> void + { + /*active_handle[511].set_entry(inactive_page_table.page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); + invalidate_page_cache(PAGE_TABLE_LEVEL_4_ADDRESS); + + f(*this);*/ + } +} // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 86f19267c7ca4e14ac6169f758130b3c27e62cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 1 Nov 2024 15:10:30 +0000 Subject: Resolve compilation issues --- arch/x86_64/src/memory/paging/active_page_table.cpp | 5 ----- arch/x86_64/src/memory/paging/kernel_mapper.cpp | 17 ++++++++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp index 38696f8..844ae37 100644 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -2,11 +2,6 @@ namespace teachos::arch::memory::paging { - namespace - { - std::size_t constexpr PAGE_TABLE_LEVEL_4_ADDRESS = 0xffffffff'fffff000; - } // namespace - auto active_page_table::create_or_get() -> active_page_table & { static page_table_handle active_handle{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), diff --git a/arch/x86_64/src/memory/paging/kernel_mapper.cpp b/arch/x86_64/src/memory/paging/kernel_mapper.cpp index 9dfc5ad..59930fc 100644 --- a/arch/x86_64/src/memory/paging/kernel_mapper.cpp +++ b/arch/x86_64/src/memory/paging/kernel_mapper.cpp @@ -2,12 +2,19 @@ namespace teachos::arch::memory::paging { - auto with(inactive_page_table inactive_page_table, temporary_page temporary_page, - active_page_table::function f) -> void + kernel_mapper::kernel_mapper(active_page_table & active_table) + : active_table(active_table) { - /*active_handle[511].set_entry(inactive_page_table.page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); - invalidate_page_cache(PAGE_TABLE_LEVEL_4_ADDRESS); + // Nothing to do + } + + auto kernel_mapper::with(inactive_page_table inactive_page_table, temporary_page temporary_page, + active_page_table::function f) -> void + { + active_table.active_handle[511].set_entry(inactive_page_table.page_table_level_4_frame, + entry::PRESENT | entry::WRITABLE); + active_page_table::invalidate_page_cache(PAGE_TABLE_LEVEL_4_ADDRESS); - f(*this);*/ + f(active_table); } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 38cda6bf579f7b09d06ad73c9f4ab893939727a2 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Fri, 1 Nov 2024 15:32:24 +0000 Subject: extract tlb methods and finish implementation of with --- arch/x86_64/src/memory/paging/kernel_mapper.cpp | 13 ++++++++++++- arch/x86_64/src/memory/paging/tlb.cpp | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 arch/x86_64/src/memory/paging/tlb.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/kernel_mapper.cpp b/arch/x86_64/src/memory/paging/kernel_mapper.cpp index 59930fc..07d55ff 100644 --- a/arch/x86_64/src/memory/paging/kernel_mapper.cpp +++ b/arch/x86_64/src/memory/paging/kernel_mapper.cpp @@ -1,5 +1,8 @@ #include "arch/memory/paging/kernel_mapper.hpp" +#include "arch/memory/allocator/physical_frame.hpp" +#include "arch/memory/paging/tlb.hpp" + namespace teachos::arch::memory::paging { kernel_mapper::kernel_mapper(active_page_table & active_table) @@ -11,10 +14,18 @@ namespace teachos::arch::memory::paging auto kernel_mapper::with(inactive_page_table inactive_page_table, temporary_page temporary_page, active_page_table::function f) -> void { + auto backup = allocator::physical_frame::containing_address(PAGE_TABLE_LEVEL_4_ADDRESS); + auto page_table_level4 = temporary_page.map_table_frame(backup, active_table); + active_table.active_handle[511].set_entry(inactive_page_table.page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); - active_page_table::invalidate_page_cache(PAGE_TABLE_LEVEL_4_ADDRESS); + tlb_flush_all(); f(active_table); + + page_table_level4[511].set_entry(backup, entry::PRESENT | entry::WRITABLE); + tlb_flush_all(); + + temporary_page.unmap(active_table); } } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/tlb.cpp b/arch/x86_64/src/memory/paging/tlb.cpp new file mode 100644 index 0000000..b3255e9 --- /dev/null +++ b/arch/x86_64/src/memory/paging/tlb.cpp @@ -0,0 +1,7 @@ +#include "arch/memory/paging/tlb.hpp" + +namespace teachos::arch::memory::paging +{ + auto tlb_flush(virtual_address address) -> void { asm volatile("invlpg (%0)" ::"r"(address) : "memory"); } + auto tlb_flush_all() -> void { flush(PAGE_TABLE_LEVEL_4_ADDRESS); } +} // namespace teachos::arch::memory::paging -- cgit v1.2.3 From d488a0709c5a2701482130a3d9c972c0b468a1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 1 Nov 2024 15:34:03 +0000 Subject: Fix typo --- arch/x86_64/src/memory/paging/tlb.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/tlb.cpp b/arch/x86_64/src/memory/paging/tlb.cpp index b3255e9..c1160dc 100644 --- a/arch/x86_64/src/memory/paging/tlb.cpp +++ b/arch/x86_64/src/memory/paging/tlb.cpp @@ -3,5 +3,5 @@ namespace teachos::arch::memory::paging { auto tlb_flush(virtual_address address) -> void { asm volatile("invlpg (%0)" ::"r"(address) : "memory"); } - auto tlb_flush_all() -> void { flush(PAGE_TABLE_LEVEL_4_ADDRESS); } + auto tlb_flush_all() -> void { tlb_flush(PAGE_TABLE_LEVEL_4_ADDRESS); } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 5ffe7d69545bf098efdd70f105a5df83304b211a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 2 Nov 2024 11:49:38 +0000 Subject: Add physical frame iterator and continue implementing kernel mapping. --- .../src/memory/paging/inactive_page_table.cpp | 2 +- arch/x86_64/src/memory/paging/kernel_mapper.cpp | 31 ---------------------- 2 files changed, 1 insertion(+), 32 deletions(-) delete mode 100644 arch/x86_64/src/memory/paging/kernel_mapper.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/inactive_page_table.cpp b/arch/x86_64/src/memory/paging/inactive_page_table.cpp index afba1f0..de0421d 100644 --- a/arch/x86_64/src/memory/paging/inactive_page_table.cpp +++ b/arch/x86_64/src/memory/paging/inactive_page_table.cpp @@ -2,7 +2,7 @@ namespace teachos::arch::memory::paging { - inactive_page_table::inactive_page_table(allocator::physical_frame frame, active_page_table active_page_table, + inactive_page_table::inactive_page_table(allocator::physical_frame frame, active_page_table & active_page_table, temporary_page temporary_page) : page_table_level_4_frame{frame} { diff --git a/arch/x86_64/src/memory/paging/kernel_mapper.cpp b/arch/x86_64/src/memory/paging/kernel_mapper.cpp deleted file mode 100644 index 07d55ff..0000000 --- a/arch/x86_64/src/memory/paging/kernel_mapper.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "arch/memory/paging/kernel_mapper.hpp" - -#include "arch/memory/allocator/physical_frame.hpp" -#include "arch/memory/paging/tlb.hpp" - -namespace teachos::arch::memory::paging -{ - kernel_mapper::kernel_mapper(active_page_table & active_table) - : active_table(active_table) - { - // Nothing to do - } - - auto kernel_mapper::with(inactive_page_table inactive_page_table, temporary_page temporary_page, - active_page_table::function f) -> void - { - auto backup = allocator::physical_frame::containing_address(PAGE_TABLE_LEVEL_4_ADDRESS); - auto page_table_level4 = temporary_page.map_table_frame(backup, active_table); - - active_table.active_handle[511].set_entry(inactive_page_table.page_table_level_4_frame, - entry::PRESENT | entry::WRITABLE); - tlb_flush_all(); - - f(active_table); - - page_table_level4[511].set_entry(backup, entry::PRESENT | entry::WRITABLE); - tlb_flush_all(); - - temporary_page.unmap(active_table); - } -} // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 5ade8e0d5f190f2e439f81232b38b90c49c0cd1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 2 Nov 2024 12:38:22 +0000 Subject: Add comments and improve multiboot information struct usability. --- arch/x86_64/src/memory/paging/inactive_page_table.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/inactive_page_table.cpp b/arch/x86_64/src/memory/paging/inactive_page_table.cpp index de0421d..0b141e2 100644 --- a/arch/x86_64/src/memory/paging/inactive_page_table.cpp +++ b/arch/x86_64/src/memory/paging/inactive_page_table.cpp @@ -9,7 +9,6 @@ namespace teachos::arch::memory::paging auto table = temporary_page.map_table_frame(page_table_level_4_frame, active_page_table); table.zero_entries(); table[511].set_entry(page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); - temporary_page.unmap(active_page_table); } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From d4b1b8a85212f07df47217fe13d86956c7eb064f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 3 Nov 2024 10:47:33 +0000 Subject: Use passed allocator in inactive page instead of tiny. --- arch/x86_64/src/memory/paging/inactive_page_table.cpp | 2 +- arch/x86_64/src/memory/paging/temporary_page.cpp | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/inactive_page_table.cpp b/arch/x86_64/src/memory/paging/inactive_page_table.cpp index 0b141e2..f6ecb8b 100644 --- a/arch/x86_64/src/memory/paging/inactive_page_table.cpp +++ b/arch/x86_64/src/memory/paging/inactive_page_table.cpp @@ -9,6 +9,6 @@ namespace teachos::arch::memory::paging auto table = temporary_page.map_table_frame(page_table_level_4_frame, active_page_table); table.zero_entries(); table[511].set_entry(page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); - temporary_page.unmap(active_page_table); + temporary_page.unmap_page(active_page_table); } } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/memory/paging/temporary_page.cpp b/arch/x86_64/src/memory/paging/temporary_page.cpp index 180b4a8..7b065ab 100644 --- a/arch/x86_64/src/memory/paging/temporary_page.cpp +++ b/arch/x86_64/src/memory/paging/temporary_page.cpp @@ -14,7 +14,10 @@ namespace teachos::arch::memory::paging return page.start_address(); } - auto temporary_page::unmap(active_page_table & active_table) -> void { active_table.unmap_page(allocator, page); } + auto temporary_page::unmap_page(active_page_table & active_table) -> void + { + active_table.unmap_page(allocator, page); + } auto temporary_page::map_table_frame(allocator::physical_frame frame, active_page_table & active_table) -> page_table_handle @@ -33,6 +36,6 @@ namespace teachos::arch::memory::paging handle.zero_entries(); handle[511].set_entry(frame.value(), entry::PRESENT | entry::WRITABLE); - unmap(active_table); + unmap_page(active_table); } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 9292814545ab5df5aa69d4f75a6d9230f3e03f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 3 Nov 2024 11:03:34 +0000 Subject: Move possible implementation into cpp --- arch/x86_64/src/memory/paging/temporary_page.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/temporary_page.cpp b/arch/x86_64/src/memory/paging/temporary_page.cpp index 7b065ab..5f760a5 100644 --- a/arch/x86_64/src/memory/paging/temporary_page.cpp +++ b/arch/x86_64/src/memory/paging/temporary_page.cpp @@ -4,6 +4,14 @@ namespace teachos::arch::memory::paging { + auto temporary_page::map_table_frame(allocator::physical_frame frame, + active_page_table & active_table) -> page_table_handle + { + page_table_handle handle{reinterpret_cast(map_to_frame(frame, active_table)), + page_table_handle::LEVEL1}; + return handle; + } + auto temporary_page::map_to_frame(allocator::physical_frame frame, active_page_table & active_table) -> virtual_address { @@ -19,14 +27,6 @@ namespace teachos::arch::memory::paging active_table.unmap_page(allocator, page); } - auto temporary_page::map_table_frame(allocator::physical_frame frame, - active_page_table & active_table) -> page_table_handle - { - page_table_handle handle{reinterpret_cast(map_to_frame(frame, active_table)), - page_table_handle::LEVEL1}; - return handle; - } - auto temporary_page::zero_entries(active_page_table & active_table) -> void { auto frame = allocator.allocate_frame(); -- cgit v1.2.3 From be546b091795e740e9dd4c35fce6453fafe0319c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 3 Nov 2024 11:56:46 +0000 Subject: Make member variables private again. --- arch/x86_64/src/memory/paging/active_page_table.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp index 844ae37..033a363 100644 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -10,6 +10,8 @@ namespace teachos::arch::memory::paging return active_page; } + auto active_page_table::operator[](std::size_t index) -> entry & { return active_handle[index]; } + auto active_page_table::translate_address(virtual_address address) -> std::optional { auto const offset = address % allocator::PAGE_FRAME_SIZE; -- cgit v1.2.3 From 380bc85d1a4fdbef102132c726ef2ac7ac6355da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 3 Nov 2024 12:20:21 +0000 Subject: Make constructor constexpr for basic page and frame types --- arch/x86_64/src/memory/paging/virtual_page.cpp | 6 ------ 1 file changed, 6 deletions(-) (limited to 'arch/x86_64/src/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 c0ec88d..f798709 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -4,12 +4,6 @@ namespace teachos::arch::memory::paging { - virtual_page::virtual_page(std::size_t page_number) - : page_number(page_number) - { - // Nothing to do - } - auto virtual_page::containing_address(virtual_address address) -> virtual_page { exception_handling::assert(address < 0x00008000'00000000 || address >= 0xffff8000'00000000, -- cgit v1.2.3 From db70018fc76800dd56b4421be797bffd00d7619d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 3 Nov 2024 13:52:22 +0000 Subject: Convert elf section flags to entry flags --- arch/x86_64/src/memory/paging/page_entry.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 23c700f..5c4bc67 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -7,7 +7,8 @@ namespace teachos::arch::memory::paging namespace { std::size_t constexpr PHYSICAL_ADDRESS_MASK = 0x000fffff'fffff000; - } + std::size_t constexpr ENTRY_FLAGS_MASK = 0xfff00000'00000fff; + } // namespace entry::entry(uint64_t flags) : flags(flags) @@ -15,6 +16,23 @@ namespace teachos::arch::memory::paging // Nothing to do. } + entry::entry(multiboot::elf_section_flags elf_flags) + { + if (elf_flags.contains_flags(multiboot::elf_section_flags::OCCUPIES_MEMORY)) + { + flags = flags.to_ulong() | entry::PRESENT; + } + if (elf_flags.contains_flags(multiboot::elf_section_flags::WRITABLE)) + { + flags = flags.to_ulong() | entry::WRITABLE; + } + if (!elf_flags.contains_flags(multiboot::elf_section_flags::EXECUTABLE_CODE)) + { + // TODO: Ensure to set the NXE bit, if we don't using this entry flag causes crashes + // flags = flags.to_ulong() | entry::EXECUTING_CODE_FORBIDDEN; + } + } + auto entry::is_unused() const -> bool { return flags == 0U; } auto entry::set_unused() -> void { flags = 0U; } @@ -37,4 +55,6 @@ namespace teachos::arch::memory::paging "[Paging Entry] Start address is not aligned with page"); flags = frame.start_address() | additional_flags.to_ulong(); } + + auto entry::get_flags() -> std::bitset<64U> { return flags.to_ulong() & ENTRY_FLAGS_MASK; } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From e20b9a2acd77ab6df00ee4a9390aaef8c6941dc6 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 3 Nov 2024 13:55:59 +0000 Subject: remove elf flags mask --- arch/x86_64/src/memory/paging/page_entry.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 5c4bc67..f13e645 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -7,7 +7,6 @@ namespace teachos::arch::memory::paging namespace { std::size_t constexpr PHYSICAL_ADDRESS_MASK = 0x000fffff'fffff000; - std::size_t constexpr ENTRY_FLAGS_MASK = 0xfff00000'00000fff; } // namespace entry::entry(uint64_t flags) @@ -56,5 +55,5 @@ namespace teachos::arch::memory::paging flags = frame.start_address() | additional_flags.to_ulong(); } - auto entry::get_flags() -> std::bitset<64U> { return flags.to_ulong() & ENTRY_FLAGS_MASK; } + auto entry::get_flags() -> std::bitset<64U> { return flags.to_ulong() & ~PHYSICAL_ADDRESS_MASK; } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From c887e22cd7827ae53fd3eab6f5af41969cdd616c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 4 Nov 2024 13:55:22 +0000 Subject: Use auto where possible --- arch/x86_64/src/memory/paging/page_table.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 3c9942a..a39c235 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -46,7 +46,7 @@ namespace teachos::arch::memory::paging auto page_table::zero_entries() -> void { - std::ranges::for_each(entries, [](entry & entry) { entry.set_unused(); }); + std::ranges::for_each(entries, [](auto & entry) { entry.set_unused(); }); } auto page_table::is_empty() const -> bool -- cgit v1.2.3 From dc80a11864444cae275e9e7be9ae120a92433034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 5 Nov 2024 09:58:05 +0000 Subject: Move tlb into seperate subfolder and create cr3 header for reading and writing. --- arch/x86_64/src/memory/paging/active_page_table.cpp | 2 +- arch/x86_64/src/memory/paging/tlb.cpp | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) delete mode 100644 arch/x86_64/src/memory/paging/tlb.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp index 033a363..3f62419 100644 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -4,7 +4,7 @@ namespace teachos::arch::memory::paging { auto active_page_table::create_or_get() -> active_page_table & { - static page_table_handle active_handle{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), + static page_table_handle active_handle{reinterpret_cast(cpu::PAGE_TABLE_LEVEL_4_ADDRESS), page_table_handle::LEVEL4}; static active_page_table active_page{active_handle}; return active_page; diff --git a/arch/x86_64/src/memory/paging/tlb.cpp b/arch/x86_64/src/memory/paging/tlb.cpp deleted file mode 100644 index c1160dc..0000000 --- a/arch/x86_64/src/memory/paging/tlb.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "arch/memory/paging/tlb.hpp" - -namespace teachos::arch::memory::paging -{ - auto tlb_flush(virtual_address address) -> void { asm volatile("invlpg (%0)" ::"r"(address) : "memory"); } - auto tlb_flush_all() -> void { tlb_flush(PAGE_TABLE_LEVEL_4_ADDRESS); } -} // namespace teachos::arch::memory::paging -- cgit v1.2.3 From d18cf4a3e4abdea80992b8bba3d1ca50ae215253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 9 Nov 2024 14:51:10 +0000 Subject: Add switch method to kernel --- arch/x86_64/src/memory/paging/inactive_page_table.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/inactive_page_table.cpp b/arch/x86_64/src/memory/paging/inactive_page_table.cpp index f6ecb8b..4e0610e 100644 --- a/arch/x86_64/src/memory/paging/inactive_page_table.cpp +++ b/arch/x86_64/src/memory/paging/inactive_page_table.cpp @@ -2,8 +2,14 @@ namespace teachos::arch::memory::paging { + inactive_page_table::inactive_page_table(allocator::physical_frame frame) + : page_table_level_4_frame{frame} + { + // Nothing to do + } + inactive_page_table::inactive_page_table(allocator::physical_frame frame, active_page_table & active_page_table, - temporary_page temporary_page) + temporary_page & temporary_page) : page_table_level_4_frame{frame} { auto table = temporary_page.map_table_frame(page_table_level_4_frame, active_page_table); -- cgit v1.2.3 From 022e2555c233c13f990026ea86d164f56a9cd7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 9 Nov 2024 15:25:33 +0000 Subject: Remove unused method --- arch/x86_64/src/memory/paging/temporary_page.cpp | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/temporary_page.cpp b/arch/x86_64/src/memory/paging/temporary_page.cpp index 5f760a5..152241d 100644 --- a/arch/x86_64/src/memory/paging/temporary_page.cpp +++ b/arch/x86_64/src/memory/paging/temporary_page.cpp @@ -26,16 +26,4 @@ namespace teachos::arch::memory::paging { active_table.unmap_page(allocator, page); } - - auto temporary_page::zero_entries(active_page_table & active_table) -> void - { - auto frame = allocator.allocate_frame(); - exception_handling::assert(frame.has_value(), "[Temporary Page] Tiny allocator could not allocate a frame"); - - page_table_handle handle = map_table_frame(frame.value(), active_table); - handle.zero_entries(); - handle[511].set_entry(frame.value(), entry::PRESENT | entry::WRITABLE); - - unmap_page(active_table); - } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 4c030cbaee174a9f7f42d4f5ca7ddf6debbbe048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 9 Nov 2024 16:25:11 +0000 Subject: Fix flush all method and move crash. --- arch/x86_64/src/memory/paging/active_page_table.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp index 3f62419..0113869 100644 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -2,9 +2,14 @@ namespace teachos::arch::memory::paging { + namespace + { + paging::virtual_address constexpr PAGE_TABLE_LEVEL_4_ADDRESS = 0xffffffff'fffff000; + } + auto active_page_table::create_or_get() -> active_page_table & { - static page_table_handle active_handle{reinterpret_cast(cpu::PAGE_TABLE_LEVEL_4_ADDRESS), + static page_table_handle active_handle{reinterpret_cast(PAGE_TABLE_LEVEL_4_ADDRESS), page_table_handle::LEVEL4}; static active_page_table active_page{active_handle}; return active_page; -- cgit v1.2.3 From edc11135d83ef1f8fcbc1575a290b31ccbdb7e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 10 Nov 2024 09:34:21 +0000 Subject: Identity map memory map and vga text buffer,w hen setting up kernel --- arch/x86_64/src/memory/paging/page_entry.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index f13e645..aae5013 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -55,5 +55,5 @@ namespace teachos::arch::memory::paging flags = frame.start_address() | additional_flags.to_ulong(); } - auto entry::get_flags() -> std::bitset<64U> { return flags.to_ulong() & ~PHYSICAL_ADDRESS_MASK; } + auto entry::get_flags() const -> std::bitset<64U> { return flags.to_ulong() & ~PHYSICAL_ADDRESS_MASK; } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 50d99ddaff23f43c3b678021d1ebc0455860d998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 10 Nov 2024 12:09:30 +0000 Subject: Remove fixed todo --- arch/x86_64/src/memory/paging/page_entry.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index aae5013..08702cf 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -27,8 +27,7 @@ namespace teachos::arch::memory::paging } if (!elf_flags.contains_flags(multiboot::elf_section_flags::EXECUTABLE_CODE)) { - // TODO: Ensure to set the NXE bit, if we don't using this entry flag causes crashes - // flags = flags.to_ulong() | entry::EXECUTING_CODE_FORBIDDEN; + flags = flags.to_ulong() | entry::EXECUTING_CODE_FORBIDDEN; } } -- cgit v1.2.3 From 0a299adbb31605cc3018a5f37132113acd19bf4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 10 Nov 2024 13:57:29 +0000 Subject: Use |= operator where possible --- arch/x86_64/src/memory/paging/page_entry.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 08702cf..5aa0982 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -19,15 +19,15 @@ namespace teachos::arch::memory::paging { if (elf_flags.contains_flags(multiboot::elf_section_flags::OCCUPIES_MEMORY)) { - flags = flags.to_ulong() | entry::PRESENT; + flags |= entry::PRESENT; } if (elf_flags.contains_flags(multiboot::elf_section_flags::WRITABLE)) { - flags = flags.to_ulong() | entry::WRITABLE; + flags |= entry::WRITABLE; } if (!elf_flags.contains_flags(multiboot::elf_section_flags::EXECUTABLE_CODE)) { - flags = flags.to_ulong() | entry::EXECUTING_CODE_FORBIDDEN; + flags |= entry::EXECUTING_CODE_FORBIDDEN; } } -- cgit v1.2.3 From 325d540236476b50af78e5781e3afead9e910359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 11 Nov 2024 10:10:08 +0000 Subject: Add todos to section that cause the crash --- arch/x86_64/src/memory/paging/inactive_page_table.cpp | 1 + arch/x86_64/src/memory/paging/page_entry.cpp | 1 + 2 files changed, 2 insertions(+) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/inactive_page_table.cpp b/arch/x86_64/src/memory/paging/inactive_page_table.cpp index 4e0610e..d2eecb8 100644 --- a/arch/x86_64/src/memory/paging/inactive_page_table.cpp +++ b/arch/x86_64/src/memory/paging/inactive_page_table.cpp @@ -12,6 +12,7 @@ namespace teachos::arch::memory::paging temporary_page & temporary_page) : page_table_level_4_frame{frame} { + // TODO: Here the exact same mapping code auto table = temporary_page.map_table_frame(page_table_level_4_frame, active_page_table); table.zero_entries(); table[511].set_entry(page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index 5aa0982..ba3deb3 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -51,6 +51,7 @@ namespace teachos::arch::memory::paging { exception_handling::assert((frame.start_address() & ~PHYSICAL_ADDRESS_MASK) == 0, "[Paging Entry] Start address is not aligned with page"); + // TODO: Crash when trying to write invalid value at 0xffffff8657f5d5f0 flags = frame.start_address() | additional_flags.to_ulong(); } -- cgit v1.2.3 From 0f3c5c2bc02d7aa48f8edbe42a67dd91821032b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 11 Nov 2024 12:02:35 +0000 Subject: Mark the two methods that first work and then fail --- arch/x86_64/src/memory/paging/inactive_page_table.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/inactive_page_table.cpp b/arch/x86_64/src/memory/paging/inactive_page_table.cpp index d2eecb8..8ebc8ce 100644 --- a/arch/x86_64/src/memory/paging/inactive_page_table.cpp +++ b/arch/x86_64/src/memory/paging/inactive_page_table.cpp @@ -12,7 +12,7 @@ namespace teachos::arch::memory::paging temporary_page & temporary_page) : page_table_level_4_frame{frame} { - // TODO: Here the exact same mapping code + // TODO: Here the exact same mapping code but it actually works?, is it the different frame? auto table = temporary_page.map_table_frame(page_table_level_4_frame, active_page_table); table.zero_entries(); table[511].set_entry(page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); -- cgit v1.2.3 From 7db57d7a47671b4023e34413a2276611e1c65f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 12 Nov 2024 10:24:12 +0000 Subject: Add comment to flush all and remove now redudant todos. --- arch/x86_64/src/memory/paging/inactive_page_table.cpp | 1 - arch/x86_64/src/memory/paging/page_entry.cpp | 1 - 2 files changed, 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/inactive_page_table.cpp b/arch/x86_64/src/memory/paging/inactive_page_table.cpp index 8ebc8ce..4e0610e 100644 --- a/arch/x86_64/src/memory/paging/inactive_page_table.cpp +++ b/arch/x86_64/src/memory/paging/inactive_page_table.cpp @@ -12,7 +12,6 @@ namespace teachos::arch::memory::paging temporary_page & temporary_page) : page_table_level_4_frame{frame} { - // TODO: Here the exact same mapping code but it actually works?, is it the different frame? auto table = temporary_page.map_table_frame(page_table_level_4_frame, active_page_table); table.zero_entries(); table[511].set_entry(page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); diff --git a/arch/x86_64/src/memory/paging/page_entry.cpp b/arch/x86_64/src/memory/paging/page_entry.cpp index ba3deb3..5aa0982 100644 --- a/arch/x86_64/src/memory/paging/page_entry.cpp +++ b/arch/x86_64/src/memory/paging/page_entry.cpp @@ -51,7 +51,6 @@ namespace teachos::arch::memory::paging { exception_handling::assert((frame.start_address() & ~PHYSICAL_ADDRESS_MASK) == 0, "[Paging Entry] Start address is not aligned with page"); - // TODO: Crash when trying to write invalid value at 0xffffff8657f5d5f0 flags = frame.start_address() | additional_flags.to_ulong(); } -- cgit v1.2.3 From d10bc808b29b0b647cbbb6c92d253fbb8c5cf431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 12 Nov 2024 10:59:50 +0000 Subject: Fix next table overwriting old page handle --- arch/x86_64/src/memory/paging/page_table.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index a39c235..eb11810 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -100,14 +100,15 @@ namespace teachos::arch::memory::paging auto page_table_handle::is_empty() const -> bool { return table->is_empty(); } - auto page_table_handle::next_table(std::size_t table_index) -> std::optional + auto page_table_handle::next_table(std::size_t table_index) const -> std::optional { exception_handling::assert(table_level != page_table_handle::LEVEL1, "[Page Table] Attempted to call next_table on level 1 page table"); auto const next_table = table->next_table(table_index); if (next_table.has_value()) { - return page_table_handle{next_table.value(), --table_level}; + auto const new_level = static_cast(table_level - 1); + return page_table_handle{next_table.value(), new_level}; } return std::nullopt; } -- cgit v1.2.3 From 1cd666241b59b800818812220e28b8b8572e4263 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 19 Nov 2024 17:32:02 +0100 Subject: paging: de-templetize implementation --- .../x86_64/src/memory/paging/active_page_table.cpp | 67 +++++++++++++++ arch/x86_64/src/memory/paging/kernel_mapper.cpp | 94 ++++++++++++++++++++++ arch/x86_64/src/memory/paging/page_table.cpp | 21 +++++ arch/x86_64/src/memory/paging/temporary_page.cpp | 9 ++- 4 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 arch/x86_64/src/memory/paging/kernel_mapper.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp index 0113869..d2bf683 100644 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -1,5 +1,7 @@ #include "arch/memory/paging/active_page_table.hpp" +#include "arch/memory/cpu/tlb.hpp" + namespace teachos::arch::memory::paging { namespace @@ -15,6 +17,71 @@ namespace teachos::arch::memory::paging return active_page; } + auto active_page_table::map_page_to_frame(allocator::frame_allocator & allocator, virtual_page page, + allocator::physical_frame frame, std::bitset<64U> flags) -> void + { + auto current_handle = active_handle; + + for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; --level) + { + current_handle = current_handle.next_table_or_create(allocator, page.get_level_index(level)); + } + + auto & level1_entry = current_handle[page.get_level_index(page_table_handle::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"); + level1_entry.set_entry(frame, flags.to_ulong() | entry::PRESENT); + } + + auto active_page_table::identity_map(allocator::frame_allocator & allocator, allocator::physical_frame frame, + std::bitset<64U> flags) -> void + { + auto const page = virtual_page::containing_address(frame.start_address()); + map_page_to_frame(allocator, page, frame, flags); + } + + auto active_page_table::map_next_free_page_to_frame(allocator::frame_allocator & allocator, virtual_page page, + std::bitset<64U> flags) -> void + { + auto const frame = allocator.allocate_frame(); + exception_handling::assert(frame.has_value(), "[Page mapper] Out of memory exception"); + map_page_to_frame(allocator, page, frame.value(), flags); + } + + auto active_page_table::unmap_page(allocator::frame_allocator & allocator, virtual_page page) -> void + { + exception_handling::assert(translate_page(page).has_value(), + "[Page Mapper] Attempted to unmap page, which has not been mapped previously"); + + auto current_handle = active_handle; + + for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; --level) + { + auto const level_index = page.get_level_index(level); + auto const next_handle = current_handle.next_table(level_index); + // The next table method failed even tough the page has to be mapped already, because translate_page did not + // fail. This can only mean that we attempted to unmap a huge page, which is not supported in the first place. + exception_handling::assert(next_handle.has_value(), "[Page Mapper] Unable to unmap huge pages"); + current_handle = next_handle.value(); + } + + unmap_page_table_entry(allocator, page, current_handle); + cpu::tlb_flush(page.start_address()); + } + + auto active_page_table::unmap_page_table_entry(allocator::frame_allocator & allocator, virtual_page page, + page_table_handle & handle) -> void + { + auto level_index = page.get_level_index(handle.get_level()); + auto & entry = handle[level_index]; + auto const frame = entry.calculate_pointed_to_frame(); + exception_handling::assert(frame.has_value(), + "[Page Mapper] Attempted to unmap page, which has not been mapped previously"); + entry.set_unused(); + allocator.deallocate_frame(frame.value()); + } + auto active_page_table::operator[](std::size_t index) -> entry & { return active_handle[index]; } auto active_page_table::translate_address(virtual_address address) -> std::optional diff --git a/arch/x86_64/src/memory/paging/kernel_mapper.cpp b/arch/x86_64/src/memory/paging/kernel_mapper.cpp new file mode 100644 index 0000000..f938cb3 --- /dev/null +++ b/arch/x86_64/src/memory/paging/kernel_mapper.cpp @@ -0,0 +1,94 @@ +#include "arch/memory/paging/kernel_mapper.hpp" + +#include "arch/memory/cpu/control_register.hpp" +#include "arch/memory/cpu/tlb.hpp" +#include "arch/memory/paging/temporary_page.hpp" +#include "arch/video/vga/text.hpp" + +namespace teachos::arch::memory::paging +{ + + kernel_mapper::kernel_mapper(allocator::frame_allocator & allocator, multiboot::memory_information const & mem_info) + : allocator(allocator) + , mem_info(mem_info) + { + // Nothing to do + } + + auto kernel_mapper::remap_kernel() -> active_page_table & + { + temporary_page temporary_page{virtual_page{0xCAFEBABE}, allocator}; + auto & active_table = active_page_table::create_or_get(); + auto const frame = allocator.allocate_frame(); + exception_handling::assert(frame.has_value(), + "[Kernel Mapper] Frame could not be allocated and therefore kernel not mapped"); + inactive_page_table new_table{frame.value(), active_table, temporary_page}; + remap_elf_kernel_sections(new_table, temporary_page, active_table); + auto const old_table = switch_active_page_table(new_table); + auto const old_level_4_page = virtual_page::containing_address(old_table.page_table_level_4_frame.start_address()); + active_table.unmap_page(allocator, old_level_4_page); + return active_table; + } + + auto kernel_mapper::remap_elf_kernel_sections(inactive_page_table & inactive_table, temporary_page & temporary_page, + active_page_table & active_table) -> void + { + auto const backup = + allocator::physical_frame::containing_address(cpu::read_control_register(cpu::control_register::CR3)); + auto page_table_level4 = temporary_page.map_table_frame(backup, active_table); + + active_table[511].set_entry(inactive_table.page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); + cpu::tlb_flush_all(); + map_elf_kernel_sections(active_table); + + page_table_level4[511].set_entry(backup, entry::PRESENT | entry::WRITABLE); + cpu::tlb_flush_all(); + temporary_page.unmap_page(active_table); + } + + auto kernel_mapper::switch_active_page_table(inactive_page_table new_table) -> inactive_page_table + { + auto const backup = + allocator::physical_frame::containing_address(cpu::read_control_register(cpu::control_register::CR3)); + auto const old_table = inactive_page_table{backup}; + + auto const new_address = new_table.page_table_level_4_frame.start_address(); + cpu::write_control_register(cpu::control_register::CR3, new_address); + return old_table; + } + + auto kernel_mapper::map_elf_kernel_sections(active_page_table & active_table) -> void + { + exception_handling::assert(!mem_info.sections.empty(), "[Kernel Mapper] Kernel elf sections empty"); + for (auto const & section : mem_info.sections) + { + if (!section.flags.contains_flags(multiboot::elf_section_flags::OCCUPIES_MEMORY)) + { + continue; + } + exception_handling::assert(section.physical_address % allocator::PAGE_FRAME_SIZE == 0U, + "[Kernel Mapper] Section must be page aligned"); + auto const start_frame = allocator::physical_frame::containing_address(section.physical_address); + // End address is exclusive, so that it is not part of the section anymore (one past the last frame of this + // section). But end frame would now point to the actual last frame and not one past the last frame, therefore + // we increment by one to get one past the last frame of this section. + auto const end_frame = + ++(allocator::physical_frame::containing_address(section.physical_address + section.section_size - 1)); + + allocator::frame_container::iterator const begin{start_frame}; + allocator::frame_container::iterator const end{end_frame}; + allocator::frame_container const frames{begin, end}; + entry const entry{section.flags}; + + for (auto const & frame : frames) + { + active_table.identity_map(allocator, frame, entry.get_flags()); + } + } + + auto const vga_buffer_frame = + allocator::physical_frame::containing_address(video::vga::text::DEFAULT_VGA_TEXT_BUFFER_ADDRESS); + active_table.identity_map(allocator, vga_buffer_frame, entry::WRITABLE); + } + +} // namespace teachos::arch::memory::paging \ No newline at end of file diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index eb11810..48e6d4f 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -96,6 +96,27 @@ namespace teachos::arch::memory::paging "[Page Table] Attempted to pass nullptr as table to page table table method"); } + auto page_table_handle::next_table_or_create(allocator::frame_allocator & allocator, + std::size_t table_index) -> page_table_handle + { + auto next_handle = next_table(table_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_handle.has_value()) + { + auto const allocated_frame = allocator.allocate_frame(); + exception_handling::assert(allocated_frame.has_value(), "[Page mapper] Unable to allocate frame"); + this->operator[](table_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_handle = next_table(table_index); + exception_handling::assert(next_handle.has_value(), "[Page mapper] Unable to create new entry into page table"); + next_handle.value().zero_entries(); + } + return next_handle.value(); + } + auto page_table_handle::zero_entries() -> void { table->zero_entries(); } auto page_table_handle::is_empty() const -> bool { return table->is_empty(); } diff --git a/arch/x86_64/src/memory/paging/temporary_page.cpp b/arch/x86_64/src/memory/paging/temporary_page.cpp index 152241d..8439864 100644 --- a/arch/x86_64/src/memory/paging/temporary_page.cpp +++ b/arch/x86_64/src/memory/paging/temporary_page.cpp @@ -4,6 +4,13 @@ namespace teachos::arch::memory::paging { + temporary_page::temporary_page(virtual_page page, allocator::frame_allocator & allocator) + : page{page} + , allocator{allocator} + { + // Nothing to do + } + auto temporary_page::map_table_frame(allocator::physical_frame frame, active_page_table & active_table) -> page_table_handle { @@ -24,6 +31,6 @@ namespace teachos::arch::memory::paging auto temporary_page::unmap_page(active_page_table & active_table) -> void { - active_table.unmap_page(allocator, page); + active_table.unmap_page(this->allocator, page); } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 4c44f822eefa743649693e0a49a978291925ddff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 24 Nov 2024 09:17:53 +0000 Subject: Revert: de-templatize paging implementation --- .../x86_64/src/memory/paging/active_page_table.cpp | 67 --------------- arch/x86_64/src/memory/paging/kernel_mapper.cpp | 94 ---------------------- arch/x86_64/src/memory/paging/page_table.cpp | 21 ----- arch/x86_64/src/memory/paging/temporary_page.cpp | 9 +-- 4 files changed, 1 insertion(+), 190 deletions(-) delete mode 100644 arch/x86_64/src/memory/paging/kernel_mapper.cpp (limited to 'arch/x86_64/src/memory/paging') diff --git a/arch/x86_64/src/memory/paging/active_page_table.cpp b/arch/x86_64/src/memory/paging/active_page_table.cpp index d2bf683..0113869 100644 --- a/arch/x86_64/src/memory/paging/active_page_table.cpp +++ b/arch/x86_64/src/memory/paging/active_page_table.cpp @@ -1,7 +1,5 @@ #include "arch/memory/paging/active_page_table.hpp" -#include "arch/memory/cpu/tlb.hpp" - namespace teachos::arch::memory::paging { namespace @@ -17,71 +15,6 @@ namespace teachos::arch::memory::paging return active_page; } - auto active_page_table::map_page_to_frame(allocator::frame_allocator & allocator, virtual_page page, - allocator::physical_frame frame, std::bitset<64U> flags) -> void - { - auto current_handle = active_handle; - - for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; --level) - { - current_handle = current_handle.next_table_or_create(allocator, page.get_level_index(level)); - } - - auto & level1_entry = current_handle[page.get_level_index(page_table_handle::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"); - level1_entry.set_entry(frame, flags.to_ulong() | entry::PRESENT); - } - - auto active_page_table::identity_map(allocator::frame_allocator & allocator, allocator::physical_frame frame, - std::bitset<64U> flags) -> void - { - auto const page = virtual_page::containing_address(frame.start_address()); - map_page_to_frame(allocator, page, frame, flags); - } - - auto active_page_table::map_next_free_page_to_frame(allocator::frame_allocator & allocator, virtual_page page, - std::bitset<64U> flags) -> void - { - auto const frame = allocator.allocate_frame(); - exception_handling::assert(frame.has_value(), "[Page mapper] Out of memory exception"); - map_page_to_frame(allocator, page, frame.value(), flags); - } - - auto active_page_table::unmap_page(allocator::frame_allocator & allocator, virtual_page page) -> void - { - exception_handling::assert(translate_page(page).has_value(), - "[Page Mapper] Attempted to unmap page, which has not been mapped previously"); - - auto current_handle = active_handle; - - for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; --level) - { - auto const level_index = page.get_level_index(level); - auto const next_handle = current_handle.next_table(level_index); - // The next table method failed even tough the page has to be mapped already, because translate_page did not - // fail. This can only mean that we attempted to unmap a huge page, which is not supported in the first place. - exception_handling::assert(next_handle.has_value(), "[Page Mapper] Unable to unmap huge pages"); - current_handle = next_handle.value(); - } - - unmap_page_table_entry(allocator, page, current_handle); - cpu::tlb_flush(page.start_address()); - } - - auto active_page_table::unmap_page_table_entry(allocator::frame_allocator & allocator, virtual_page page, - page_table_handle & handle) -> void - { - auto level_index = page.get_level_index(handle.get_level()); - auto & entry = handle[level_index]; - auto const frame = entry.calculate_pointed_to_frame(); - exception_handling::assert(frame.has_value(), - "[Page Mapper] Attempted to unmap page, which has not been mapped previously"); - entry.set_unused(); - allocator.deallocate_frame(frame.value()); - } - auto active_page_table::operator[](std::size_t index) -> entry & { return active_handle[index]; } auto active_page_table::translate_address(virtual_address address) -> std::optional diff --git a/arch/x86_64/src/memory/paging/kernel_mapper.cpp b/arch/x86_64/src/memory/paging/kernel_mapper.cpp deleted file mode 100644 index f938cb3..0000000 --- a/arch/x86_64/src/memory/paging/kernel_mapper.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include "arch/memory/paging/kernel_mapper.hpp" - -#include "arch/memory/cpu/control_register.hpp" -#include "arch/memory/cpu/tlb.hpp" -#include "arch/memory/paging/temporary_page.hpp" -#include "arch/video/vga/text.hpp" - -namespace teachos::arch::memory::paging -{ - - kernel_mapper::kernel_mapper(allocator::frame_allocator & allocator, multiboot::memory_information const & mem_info) - : allocator(allocator) - , mem_info(mem_info) - { - // Nothing to do - } - - auto kernel_mapper::remap_kernel() -> active_page_table & - { - temporary_page temporary_page{virtual_page{0xCAFEBABE}, allocator}; - auto & active_table = active_page_table::create_or_get(); - auto const frame = allocator.allocate_frame(); - exception_handling::assert(frame.has_value(), - "[Kernel Mapper] Frame could not be allocated and therefore kernel not mapped"); - inactive_page_table new_table{frame.value(), active_table, temporary_page}; - remap_elf_kernel_sections(new_table, temporary_page, active_table); - auto const old_table = switch_active_page_table(new_table); - auto const old_level_4_page = virtual_page::containing_address(old_table.page_table_level_4_frame.start_address()); - active_table.unmap_page(allocator, old_level_4_page); - return active_table; - } - - auto kernel_mapper::remap_elf_kernel_sections(inactive_page_table & inactive_table, temporary_page & temporary_page, - active_page_table & active_table) -> void - { - auto const backup = - allocator::physical_frame::containing_address(cpu::read_control_register(cpu::control_register::CR3)); - auto page_table_level4 = temporary_page.map_table_frame(backup, active_table); - - active_table[511].set_entry(inactive_table.page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); - cpu::tlb_flush_all(); - map_elf_kernel_sections(active_table); - - page_table_level4[511].set_entry(backup, entry::PRESENT | entry::WRITABLE); - cpu::tlb_flush_all(); - temporary_page.unmap_page(active_table); - } - - auto kernel_mapper::switch_active_page_table(inactive_page_table new_table) -> inactive_page_table - { - auto const backup = - allocator::physical_frame::containing_address(cpu::read_control_register(cpu::control_register::CR3)); - auto const old_table = inactive_page_table{backup}; - - auto const new_address = new_table.page_table_level_4_frame.start_address(); - cpu::write_control_register(cpu::control_register::CR3, new_address); - return old_table; - } - - auto kernel_mapper::map_elf_kernel_sections(active_page_table & active_table) -> void - { - exception_handling::assert(!mem_info.sections.empty(), "[Kernel Mapper] Kernel elf sections empty"); - for (auto const & section : mem_info.sections) - { - if (!section.flags.contains_flags(multiboot::elf_section_flags::OCCUPIES_MEMORY)) - { - continue; - } - exception_handling::assert(section.physical_address % allocator::PAGE_FRAME_SIZE == 0U, - "[Kernel Mapper] Section must be page aligned"); - auto const start_frame = allocator::physical_frame::containing_address(section.physical_address); - // End address is exclusive, so that it is not part of the section anymore (one past the last frame of this - // section). But end frame would now point to the actual last frame and not one past the last frame, therefore - // we increment by one to get one past the last frame of this section. - auto const end_frame = - ++(allocator::physical_frame::containing_address(section.physical_address + section.section_size - 1)); - - allocator::frame_container::iterator const begin{start_frame}; - allocator::frame_container::iterator const end{end_frame}; - allocator::frame_container const frames{begin, end}; - entry const entry{section.flags}; - - for (auto const & frame : frames) - { - active_table.identity_map(allocator, frame, entry.get_flags()); - } - } - - auto const vga_buffer_frame = - allocator::physical_frame::containing_address(video::vga::text::DEFAULT_VGA_TEXT_BUFFER_ADDRESS); - active_table.identity_map(allocator, vga_buffer_frame, entry::WRITABLE); - } - -} // namespace teachos::arch::memory::paging \ No newline at end of file diff --git a/arch/x86_64/src/memory/paging/page_table.cpp b/arch/x86_64/src/memory/paging/page_table.cpp index 48e6d4f..eb11810 100644 --- a/arch/x86_64/src/memory/paging/page_table.cpp +++ b/arch/x86_64/src/memory/paging/page_table.cpp @@ -96,27 +96,6 @@ namespace teachos::arch::memory::paging "[Page Table] Attempted to pass nullptr as table to page table table method"); } - auto page_table_handle::next_table_or_create(allocator::frame_allocator & allocator, - std::size_t table_index) -> page_table_handle - { - auto next_handle = next_table(table_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_handle.has_value()) - { - auto const allocated_frame = allocator.allocate_frame(); - exception_handling::assert(allocated_frame.has_value(), "[Page mapper] Unable to allocate frame"); - this->operator[](table_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_handle = next_table(table_index); - exception_handling::assert(next_handle.has_value(), "[Page mapper] Unable to create new entry into page table"); - next_handle.value().zero_entries(); - } - return next_handle.value(); - } - auto page_table_handle::zero_entries() -> void { table->zero_entries(); } auto page_table_handle::is_empty() const -> bool { return table->is_empty(); } diff --git a/arch/x86_64/src/memory/paging/temporary_page.cpp b/arch/x86_64/src/memory/paging/temporary_page.cpp index 8439864..152241d 100644 --- a/arch/x86_64/src/memory/paging/temporary_page.cpp +++ b/arch/x86_64/src/memory/paging/temporary_page.cpp @@ -4,13 +4,6 @@ namespace teachos::arch::memory::paging { - temporary_page::temporary_page(virtual_page page, allocator::frame_allocator & allocator) - : page{page} - , allocator{allocator} - { - // Nothing to do - } - auto temporary_page::map_table_frame(allocator::physical_frame frame, active_page_table & active_table) -> page_table_handle { @@ -31,6 +24,6 @@ namespace teachos::arch::memory::paging auto temporary_page::unmap_page(active_page_table & active_table) -> void { - active_table.unmap_page(this->allocator, page); + active_table.unmap_page(allocator, page); } } // namespace teachos::arch::memory::paging -- cgit v1.2.3 From 47732f54474a083e9f98e52714c12c0ca1181174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 24 Nov 2024 13:27:15 +0000 Subject: Use container helper classes for heap remapping --- arch/x86_64/src/memory/paging/virtual_page.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'arch/x86_64/src/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 f798709..d374156 100644 --- a/arch/x86_64/src/memory/paging/virtual_page.cpp +++ b/arch/x86_64/src/memory/paging/virtual_page.cpp @@ -17,4 +17,17 @@ namespace teachos::arch::memory::paging { return (page_number >> (level * 9U)) & 0x1FF; } + + auto virtual_page::operator++(int) -> virtual_page + { + virtual_page const old_value = *this; + ++page_number; + return old_value; + } + + auto virtual_page::operator++() -> virtual_page & + { + ++page_number; + return *this; + } } // namespace teachos::arch::memory::paging -- cgit v1.2.3