aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/include
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/include')
-rw-r--r--arch/x86_64/include/arch/exception_handling/assert.hpp2
-rw-r--r--arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp6
-rw-r--r--arch/x86_64/include/arch/memory/paging/page_mapper.hpp29
-rw-r--r--arch/x86_64/include/arch/memory/paging/page_table.hpp65
-rw-r--r--arch/x86_64/include/arch/memory/paging/virtual_page.hpp2
5 files changed, 67 insertions, 37 deletions
diff --git a/arch/x86_64/include/arch/exception_handling/assert.hpp b/arch/x86_64/include/arch/exception_handling/assert.hpp
index bfc205c..58c1f33 100644
--- a/arch/x86_64/include/arch/exception_handling/assert.hpp
+++ b/arch/x86_64/include/arch/exception_handling/assert.hpp
@@ -1,8 +1,6 @@
#ifndef TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_ASSERT_HPP
#define TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_ASSERT_HPP
-#include "arch/exception_handling/panic.hpp"
-
namespace teachos::arch::exception_handling
{
/**
diff --git a/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp b/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp
index f99b7c8..7b1bb16 100644
--- a/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp
+++ b/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp
@@ -1,17 +1,17 @@
#ifndef TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_AREA_FRAME_ALLOCATOR_HPP
#define TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_AREA_FRAME_ALLOCATOR_HPP
+#include "arch/memory/allocator/physical_frame.hpp"
#include "arch/memory/multiboot/reader.hpp"
-#include "physical_frame.hpp"
#include <optional>
namespace teachos::arch::memory::allocator
{
template<typename T>
- concept FrameAllocator = requires(T t) {
+ concept FrameAllocator = requires(T t, physical_frame a) {
{ t.allocate_frame() } -> std::same_as<std::optional<physical_frame>>;
- { t.deallocate_frame() } -> std::same_as<void>;
+ { t.deallocate_frame(a) } -> std::same_as<void>;
};
/**
diff --git a/arch/x86_64/include/arch/memory/paging/page_mapper.hpp b/arch/x86_64/include/arch/memory/paging/page_mapper.hpp
index ebd044a..a874f75 100644
--- a/arch/x86_64/include/arch/memory/paging/page_mapper.hpp
+++ b/arch/x86_64/include/arch/memory/paging/page_mapper.hpp
@@ -10,13 +10,13 @@
namespace teachos::arch::memory::paging
{
/**
- * @brief Creates a single instance of the level 4 page table table and returns it or alternatively returns the
- * previously created instance. The instance is owned by this method and is static, meaning it lives on for the
- * complete lifetime of the program.
+ * @brief Creates a single instance of the level 4 page table table and returns handle to it or alternatively returns
+ * the previously created handle instead. The instance is owned by this method and is static, meaning it lives on for
+ * the complete lifetime of the program.
*
- * @return Single unique instance of the level 4 page table.
+ * @return Handle to the single unique instance of the level 4 page table.
*/
- auto create_or_get() -> page_table *;
+ auto create_or_get() -> page_table_handle;
/**
* @brief Translates page into physical frame, will first attempt to parse normally using default page size and if it
@@ -58,32 +58,31 @@ namespace teachos::arch::memory::paging
auto map_page_to_frame(T & allocator, virtual_page page, allocator::physical_frame frame,
std::bitset<64U> flags) -> void
{
- page_table * current_page_table = create_or_get();
+ auto current_handle = create_or_get();
for (auto level = page_table::LEVEL4; level != page_table::LEVEL1; level--)
{
auto level_index = page.get_level_index(level);
- auto next_page_table = current_page_table->next_table(level_index);
+ auto next_handle = current_handle.next_table(level_index);
// If the next table method failed then it means that the page level of the frame we want allocate has not yet
// been created itself. So we have to do that before we are able to allocate the wanted frame. This has to be done
// for every level, meaning we potenitally create a level 4, level 3 and level 2 page entry, each pointing to a
// page table one level below.
- if (!next_page_table)
+ if (!next_handle)
{
auto allocated_frame = allocator.allocate_frame();
exception_handling::assert(!allocated_frame.has_value(), "[Page mapper] Unable to allocate frame");
- current_page_table->operator[](level_index)
- .set_entry(allocated_frame.value(), entry::PRESENT | entry::WRITABLE);
+ current_handle[level_index].set_entry(allocated_frame.value(), entry::PRESENT | entry::WRITABLE);
// There should now be an entry at the previously not existent index, therefore we can simply access it again.
- next_page_table = current_page_table->next_table(page.get_level_index(level));
- exception_handling::assert(!next_page_table.has_value(),
+ next_handle = current_handle.next_table(page.get_level_index(level));
+ exception_handling::assert(!next_handle.has_value(),
"[Page mapper] Unable to create new entry into page table");
- next_page_table.value()->zero_entries();
+ next_handle.value().zero_entries();
}
- current_page_table = next_page_table.value();
+ current_handle = next_handle.value();
}
- auto level1_entry = current_page_table->operator[](page.get_level_index(page_table::LEVEL1));
+ auto level1_entry = current_handle[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");
diff --git a/arch/x86_64/include/arch/memory/paging/page_table.hpp b/arch/x86_64/include/arch/memory/paging/page_table.hpp
index 3439127..a1c8abe 100644
--- a/arch/x86_64/include/arch/memory/paging/page_table.hpp
+++ b/arch/x86_64/include/arch/memory/paging/page_table.hpp
@@ -1,7 +1,7 @@
#ifndef TEACHOS_ARCH_X86_64_MEMORY_PAGING_PAGE_TABLE_HPP
#define TEACHOS_ARCH_X86_64_MEMORY_PAGING_PAGE_TABLE_HPP
-#include "page_entry.hpp"
+#include "arch/memory/paging/page_entry.hpp"
namespace teachos::arch::memory::paging
{
@@ -41,27 +41,20 @@ namespace teachos::arch::memory::paging
/**
* @brief Returns the next page table level from the given page table index. Meaning we
* use an index into a Level 4 page table to get the according Level 3 page table. This method should not be called
- * on a Level 1 page table or it will return invalid addresses and cause hard to debug issues.
+ * 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 higher.
+ * @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<page_table *>;
+ auto next_table(std::size_t table_index) const -> std::optional<page_table *>;
/**
- * @brief Index operator overload to access specific mutable entry directy.
+ * @brief Index operator overload to access specific entries directy.
*
- * @param index Index of the entry we want to access and change.
+ * @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 &;
-
- /**
- * @brief Index operator overload to access specific immutable entry directy.
- *
- * @param index Index of the entry we want to access and only read.
- * @return Entry at the given table index.
- */
- auto operator[](std::size_t index) const -> entry const &;
+ auto operator[](std::size_t index) const -> entry;
private:
/**
@@ -72,13 +65,53 @@ 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<std::size_t>;
+ auto next_table_address(std::size_t table_index) const -> std::optional<std::size_t>;
entry entries[PAGE_TABLE_ENTRY_COUNT]; ///< Entries containing addresses to page tables of a level below or actual
///< virtual addresses for the level 1 page table.
};
auto operator--(page_table::level & level, int) -> page_table::level;
+
+ /**
+ * @brief Handle that ensures accessing the page table is safe because it adds additional checks to the next_table
+ * method and ensures it can only be called if the table level is not LEVEL1.
+ */
+ struct page_table_handle
+ {
+ /**
+ * @brief Constructor.
+ */
+ page_table_handle(page_table * handle, page_table::level handle_level);
+
+ /**
+ * @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. If this method is called with a
+ * Level 1 page table it will instead assert and halt execution, 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<page_table_handle>;
+
+ /**
+ * @brief Index operator overload to access specific immutable entry directy.
+ *
+ * @param index Index of the entry we want to access and only read.
+ * @return Entry at the given table index.
+ */
+ auto operator[](std::size_t index) const -> entry;
+
+ private:
+ page_table * handle; ///< Handle to underlying page table, can never be null (invariant ensured by constructor)
+ page_table::level handle_level; ///< Level page table is currently on, depends on how often next_level was
+ ///< called successfully.
+ };
} // namespace teachos::arch::memory::paging
#endif // TEACHOS_ARCH_X86_64_MEMORY_PAGING_PAGE_TABLE_HPP
diff --git a/arch/x86_64/include/arch/memory/paging/virtual_page.hpp b/arch/x86_64/include/arch/memory/paging/virtual_page.hpp
index 6b9a641..f8dfbf0 100644
--- a/arch/x86_64/include/arch/memory/paging/virtual_page.hpp
+++ b/arch/x86_64/include/arch/memory/paging/virtual_page.hpp
@@ -2,8 +2,8 @@
#define TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP
#include "arch/memory/allocator/physical_frame.hpp"
+#include "arch/memory/paging/page_table.hpp"
-#include "page_table.hpp"
#include <compare>
#include <cstdint>
#include <optional>