aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatteo Gmür <matteo.gmuer1@ost.ch>2024-10-20 07:01:53 +0000
committerMatteo Gmür <matteo.gmuer1@ost.ch>2024-10-20 07:01:53 +0000
commitd728052d62470799f73f6d9a2b8baa2b0b357383 (patch)
treec73e113315b27b3caaca2dac2f8d451d1a260b2d
parent7ebfe9e09efa84044d1470132b7f55ebf53a7f89 (diff)
downloadteachos-d728052d62470799f73f6d9a2b8baa2b0b357383.tar.xz
teachos-d728052d62470799f73f6d9a2b8baa2b0b357383.zip
Add helper methods to phyisca frame
-rw-r--r--arch/x86_64/CMakeLists.txt1
-rw-r--r--arch/x86_64/include/arch/memory/allocator/physical_frame.hpp12
-rw-r--r--arch/x86_64/include/arch/memory/paging/virtual_page.hpp25
-rw-r--r--arch/x86_64/src/memory/allocator/area_frame_allocator.cpp4
-rw-r--r--arch/x86_64/src/memory/allocator/physical_frame.cpp5
-rw-r--r--arch/x86_64/src/memory/multiboot/reader.cpp15
-rw-r--r--arch/x86_64/src/memory/paging/page_entry.cpp4
-rw-r--r--arch/x86_64/src/memory/paging/page_table.cpp6
-rw-r--r--arch/x86_64/src/memory/paging/virtual_page.cpp22
9 files changed, 68 insertions, 26 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt
index 016215b..fe83548 100644
--- a/arch/x86_64/CMakeLists.txt
+++ b/arch/x86_64/CMakeLists.txt
@@ -48,6 +48,7 @@ target_sources("_memory" PRIVATE
"src/memory/allocator/physical_frame.cpp"
"src/memory/paging/page_entry.cpp"
"src/memory/paging/page_table.cpp"
+ "src/memory/paging/virtual_page.cpp"
)
#[============================================================================[
diff --git a/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp b/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp
index 5e99d10..e013e0d 100644
--- a/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp
+++ b/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp
@@ -6,6 +6,8 @@
namespace teachos::arch::memory::allocator
{
+ constexpr std::size_t PAGE_FRAME_SIZE = 4096U; ///< Default page size of x86_84 is always 4KiB.
+
/**
* @brief Specific physical frame containing helper functions to determine if a specific address is in that
* physical frame or not.
@@ -15,22 +17,22 @@ namespace teachos::arch::memory::allocator
/**
* @brief Constructor.
*
- * @param frame_number Index number that should be assigned to this physical_frame.
+ * @param frame_number Index number that should be assigned to this physical frame.
*/
explicit physical_frame(std::size_t frame_number);
/**
- * @brief Returns the physical_frame the given address is contained in.
+ * @brief Returns the physical frame the given address is contained in.
*
- * @param address Address we want to get the corresponding physical_frame for.
+ * @param physical_address Physical address we want to get the corresponding physical frame for.
* @return Frame the given address is contained in.
*/
- static auto containing_address(std::size_t address) -> physical_frame;
+ static auto containing_address(std::size_t physical_address) -> physical_frame;
/**
* @brief Evaluates the start address of the physical frame.
*
- * @return start address of the physical frame.
+ * @return Start address of the physical frame.
*/
auto start_address() const -> uint64_t;
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 12af510..7871e4b 100644
--- a/arch/x86_64/include/arch/memory/paging/virtual_page.hpp
+++ b/arch/x86_64/include/arch/memory/paging/virtual_page.hpp
@@ -2,6 +2,7 @@
#define TEACHOS_ARCH_X86_64_MEMORY_PAGING_VIRTUAL_PAGE_HPP
#include <compare>
+#include <cstdint>
namespace teachos::arch::memory::paging
{
@@ -10,7 +11,27 @@ namespace teachos::arch::memory::paging
*/
struct virtual_page
{
- std::size_t number; ///< Index number of the current virtual page, used to distinguish it from other pages.
+ /**
+ * @brief Constructor.
+ *
+ * @param page_number Index number of the current virtual page, used to distinguish it from other pages.
+ */
+ explicit virtual_page(std::size_t page_number);
+
+ /**
+ * @brief Returns the virtual page the given address is contained in.
+ *
+ * @param virtual_address Virtual address we want to get the corresponding virtual page for.
+ * @return Frame the given address is contained in.
+ */
+ static auto containing_address(std::size_t virtual_address) -> virtual_page;
+
+ /**
+ * @brief Evaluates the start address of the virtual page.
+ *
+ * @return Start address of the virtual page.
+ */
+ auto start_address() const -> uint64_t;
/**
* @brief Defaulted equals operator.
@@ -21,6 +42,8 @@ namespace teachos::arch::memory::paging
* @brief Defaulted three-way comparsion operator.
*/
constexpr auto operator<=>(const virtual_page & other) const -> std::partial_ordering = default;
+
+ std::size_t page_number; ///< Index number of the current virtual page, used to distinguish it from other pages.
};
} // namespace teachos::arch::memory::paging
diff --git a/arch/x86_64/src/memory/allocator/area_frame_allocator.cpp b/arch/x86_64/src/memory/allocator/area_frame_allocator.cpp
index 9c344d8..c2cafce 100644
--- a/arch/x86_64/src/memory/allocator/area_frame_allocator.cpp
+++ b/arch/x86_64/src/memory/allocator/area_frame_allocator.cpp
@@ -87,8 +87,8 @@ namespace teachos::arch::memory::allocator
auto area_frame_allocator::deallocate_frame(physical_frame physical_frame) -> void
{
- arch::exception_handling::assert(false && physical_frame.frame_number == 0,
- "[deallocate_frame] Not implemented Exception");
+ exception_handling::assert(false && physical_frame.frame_number == 0,
+ "[deallocate_frame] Not implemented Exception");
}
auto area_frame_allocator::begin() -> multiboot::memory_area_iterator { return area_begin; }
diff --git a/arch/x86_64/src/memory/allocator/physical_frame.cpp b/arch/x86_64/src/memory/allocator/physical_frame.cpp
index 03ec193..03bd965 100644
--- a/arch/x86_64/src/memory/allocator/physical_frame.cpp
+++ b/arch/x86_64/src/memory/allocator/physical_frame.cpp
@@ -2,11 +2,6 @@
namespace teachos::arch::memory::allocator
{
- namespace
- {
- constexpr std::size_t PAGE_FRAME_SIZE = 4096U; ///< Default page size of x86_84 is always 4KiB.
- }
-
physical_frame::physical_frame(std::size_t frame_number)
: frame_number(frame_number)
{
diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp
index 8741b44..156b437 100644
--- a/arch/x86_64/src/memory/multiboot/reader.cpp
+++ b/arch/x86_64/src/memory/multiboot/reader.cpp
@@ -19,7 +19,7 @@ namespace teachos::arch::memory::multiboot
{
auto expected_entry_size = mminfo->entry_size;
constexpr auto actual_entry_size = sizeof(memory_area);
- arch::exception_handling::assert(expected_entry_size == actual_entry_size, "Unexpected memory_area entry size");
+ exception_handling::assert(expected_entry_size == actual_entry_size, "Unexpected memory_area entry size");
auto total_size = mminfo->info.size;
auto total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size;
@@ -34,19 +34,18 @@ namespace teachos::arch::memory::multiboot
{
auto expected_entry_size = symbol->entry_size;
constexpr auto actual_entry_size = sizeof(elf_section_header);
- arch::exception_handling::assert(expected_entry_size == actual_entry_size,
- "Unexpected elf_section_header entry size");
+ exception_handling::assert(expected_entry_size == actual_entry_size, "Unexpected elf_section_header entry size");
auto expected_total_size = symbol->info.size;
auto actual_total_entry_size = actual_entry_size * symbol->number_of_sections;
constexpr auto actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t);
auto actual_total_size = actual_total_entry_size + actual_total_section_size;
- arch::exception_handling::assert(expected_total_size == actual_total_size,
- "Unexpected elf_symbols_section_header total size");
+ exception_handling::assert(expected_total_size == actual_total_size,
+ "Unexpected elf_symbols_section_header total size");
auto begin = reinterpret_cast<elf_section_header *>(&symbol->end);
auto end = begin + symbol->number_of_sections;
- arch::exception_handling::assert(begin->is_null(), "Missing elf_section_header begin");
+ exception_handling::assert(begin->is_null(), "Missing elf_section_header begin");
std::size_t symbol_table_section_count = 0U;
std::size_t dynamic_section_count = 0U;
@@ -80,8 +79,8 @@ namespace teachos::arch::memory::multiboot
}
}
- arch::exception_handling::assert(symbol_table_section_count == 1U, "Unexpected symbol_table_count value");
- arch::exception_handling::assert(dynamic_section_count <= 1U, "Unexpected dynamic_section_count value");
+ exception_handling::assert(symbol_table_section_count == 1U, "Unexpected symbol_table_count value");
+ exception_handling::assert(dynamic_section_count <= 1U, "Unexpected dynamic_section_count value");
}
} // namespace
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