aboutsummaryrefslogtreecommitdiff
path: root/arch
diff options
context:
space:
mode:
Diffstat (limited to 'arch')
-rw-r--r--arch/x86_64/include/arch/memory/multiboot.hpp4
-rw-r--r--arch/x86_64/include/arch/memory/paging.hpp43
-rw-r--r--arch/x86_64/src/memory/paging.cpp47
3 files changed, 64 insertions, 30 deletions
diff --git a/arch/x86_64/include/arch/memory/multiboot.hpp b/arch/x86_64/include/arch/memory/multiboot.hpp
index 9a753fa..9cca8bc 100644
--- a/arch/x86_64/include/arch/memory/multiboot.hpp
+++ b/arch/x86_64/include/arch/memory/multiboot.hpp
@@ -194,7 +194,9 @@ namespace teachos::arch::memory
auto operator==(elf_section_flags const & other) const -> bool = default;
private:
- std::bitset<64U> flags;
+ std::bitset<64U> flags; ///< Underlying bitset used to read the flags from. Bits 21 - 28 are reserved for operating
+ ///< system specific semantics and bits 29 - 32 are reserved for processor specific
+ ///< semantics. Bits 33 - 64 are unused for compatability with ELF32.
};
/**
diff --git a/arch/x86_64/include/arch/memory/paging.hpp b/arch/x86_64/include/arch/memory/paging.hpp
index 4092d18..d55835e 100644
--- a/arch/x86_64/include/arch/memory/paging.hpp
+++ b/arch/x86_64/include/arch/memory/paging.hpp
@@ -111,40 +111,53 @@ namespace teachos::arch::memory
struct page_table
{
/**
+ * @brief Constructor.
+ */
+ page_table();
+
+ /**
* @brief Set every entry of the page to unused
*/
auto zero_entries() -> void;
/**
- * @brief Find and return the next table address
+ * @brief Convert the address of a page_table into a page_table
*
- * The next table address is only valid if the corresponding entry is present
- * and does not create a huge page
+ * @param index Index of the entry we want to access
+ * @return An optional of the next page table or null
+ */
+ auto next_table(std::size_t index) const -> std::optional<const page_table *>;
+
+ /**
+ * @brief Index operator overload to access specific mutable entry directy
*
- * @param index
- * @return An optional of the address of the next page table or null
+ * @param index Index of the entry we want to access and change
+ * @return Entry at the given table index
*/
- auto next_table_address(std::size_t index) const -> std::optional<std::size_t>;
+ auto operator[](std::size_t index) -> entry &;
/**
- * @brief Convert the address of a page_table into a page_table
+ * @brief Index operator overload to access specific immutable entry directy
*
- * @param index
- * @return An optional of the next page table or null
+ * @param index Index of the entry we want to access and only read
+ * @return Entry at the given table index
*/
- auto next_table(std::size_t index) const -> std::optional<const page_table *>;
+ auto operator[](std::size_t index) const -> entry const &;
+ private:
/**
- * @brief Index operator overload to access specific entries directy
+ * @brief Find and return the next table address
+ *
+ * The next table address is only valid if the corresponding entry is present
+ * and does not create a huge page
*
* @param index
- * @return The address of the accessed entry
+ * @return An optional of the address of the next page table or null
*/
- entry & operator[](std::size_t index);
+ auto next_table_address(std::size_t index) const -> std::optional<std::size_t>;
- private:
entry entries[PAGE_TABLE_ENTRY_COUNT];
- page_table const * p4 = reinterpret_cast<page_table *>(0xfffffffffffff000);
+ page_table const * p4;
};
} // namespace teachos::arch::memory
diff --git a/arch/x86_64/src/memory/paging.cpp b/arch/x86_64/src/memory/paging.cpp
index a07b2c0..a8f2c40 100644
--- a/arch/x86_64/src/memory/paging.cpp
+++ b/arch/x86_64/src/memory/paging.cpp
@@ -40,25 +40,21 @@ namespace teachos::arch::memory
flags = std::bitset<64U>(frame.start_address()) | flags;
}
- auto page_table::zero_entries() -> void
+ page_table::page_table()
+ : entries()
+ , p4(reinterpret_cast<page_table *>(0xfffffffffffff000))
{
- for (size_t i = 0; i < sizeof(entries) / sizeof(entries[0]); ++i)
- {
- entries[i].set_unused();
- }
+ // Nothing to do
}
- auto page_table::next_table_address(std::size_t index) const -> std::optional<std::size_t>
+ auto page_table::zero_entries() -> void
{
- auto entry = entries[index];
-
- if (entry.contains_flags(entry.PRESENT) && !entry.contains_flags(entry.HUGE_PAGE))
+ constexpr size_t entry_amount = sizeof(entries) / sizeof(entries[0]);
+ for (size_t i = 0; i < entry_amount; ++i)
{
- std::size_t table_address = reinterpret_cast<std::size_t>(this);
- return (table_address << 9) | (index << 12);
+ auto entry = this->operator[](i);
+ entry.set_unused();
}
- // TODO: Implement behaviour for huge pages currently not done
- return std::nullopt;
}
auto page_table::next_table(std::size_t index) const -> std::optional<page_table const *>
@@ -73,9 +69,32 @@ namespace teachos::arch::memory
return std::nullopt;
}
- entry & page_table::operator[](std::size_t index)
+ 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 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 entries[index];
}
+
+ auto page_table::next_table_address(std::size_t index) const -> std::optional<std::size_t>
+ {
+ auto entry = this->operator[](index);
+
+ if (entry.contains_flags(entry::PRESENT) && !entry.contains_flags(entry::HUGE_PAGE))
+ {
+ std::size_t const table_address = reinterpret_cast<std::size_t>(this);
+ return (table_address << 9) | (index << 12);
+ }
+ // TODO: Implement behaviour for huge pages currently not done
+ return std::nullopt;
+ }
} // namespace teachos::arch::memory