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/memory/allocator/area_frame_allocator.hpp2
-rw-r--r--arch/x86_64/include/arch/memory/allocator/physical_frame.hpp128
-rw-r--r--arch/x86_64/include/arch/memory/multiboot/reader.hpp9
-rw-r--r--arch/x86_64/include/arch/memory/paging/active_page_table.hpp2
-rw-r--r--arch/x86_64/include/arch/memory/paging/inactive_page_table.hpp2
-rw-r--r--arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp73
-rw-r--r--arch/x86_64/include/arch/shared/container.hpp12
-rw-r--r--arch/x86_64/include/arch/shared/random_access_iterator.hpp11
8 files changed, 213 insertions, 26 deletions
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 4b6c2d4..f2b77f8 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
@@ -18,7 +18,7 @@ namespace teachos::arch::memory::allocator
*
* @param mem_info Structure containg all relevant information to map and allocate memory
*/
- area_frame_allocator(multiboot::memory_information mem_info);
+ area_frame_allocator(multiboot::memory_information const & mem_info);
/**
* @brief Allocate memory by finding and returning a free physical frame.
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 146e557..b95687c 100644
--- a/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp
+++ b/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp
@@ -3,6 +3,7 @@
#include <compare>
#include <cstdint>
+#include <iterator>
namespace teachos::arch::memory::allocator
{
@@ -17,6 +18,11 @@ namespace teachos::arch::memory::allocator
struct physical_frame
{
/**
+ * @brief Defaulted constructor.
+ */
+ physical_frame() = default;
+
+ /**
* @brief Constructor.
*
* @param frame_number Index number that should be assigned to this physical frame.
@@ -48,8 +54,126 @@ namespace teachos::arch::memory::allocator
*/
auto operator<=>(const physical_frame & other) const -> std::partial_ordering = default;
- std::size_t
- frame_number; ///< Index number of the current physical frame, used to distinguish it from other frames.
+ std::size_t frame_number =
+ {}; ///< Index number of the current physical frame, used to distinguish it from other frames.
+ };
+
+ /**
+ * @brief Iterator for the frame number
+ */
+ struct physical_frame_iterator
+ {
+ using iterator_category = std::random_access_iterator_tag; ///< Iterator category of this type.
+ using difference_type = std::ptrdiff_t; ///< Type when diving one instance of this iterator by another.
+ using value_type = physical_frame; ///< Underlying value pointed to by this iterator.
+
+ /**
+ * @brief Defaulted constructor.
+ */
+ physical_frame_iterator() = default;
+
+ /**
+ * @brief Constructor.
+ *
+ * @param value Underlying value the iterator should point too
+ */
+ explicit physical_frame_iterator(value_type value);
+
+ /**
+ * @brief Dereferences the initally given pointer to its value.
+ *
+ * @return Reference to the value.
+ */
+ auto operator*() -> value_type &;
+
+ /**
+ * @brief Get underlying value, which is the intially passed pointer.
+ *
+ * @return Underlying value passed intially.
+ */
+ auto operator->() -> value_type *;
+
+ /**
+ * @brief Post increment operator. Returns a copy of the value.
+ *
+ * @return Copy of the incremented underlying address.
+ */
+ auto operator++(int) -> physical_frame_iterator;
+
+ /**
+ * @brief Pre increment operator. Returns a reference to the changed value.
+ *
+ * @return Reference to the incremented underlying address.
+ */
+ auto operator++() -> physical_frame_iterator &;
+
+ /**
+ * @brief Addition assignment operator. Returns a reference to the changed value.
+ *
+ * @param operand Value we want to add to the underlying address.
+ * @return Reference to the changed underlying address.
+ */
+ auto operator+=(difference_type operand) -> physical_frame_iterator &;
+
+ /**
+ * @brief Subtraction assignment operator. Returns a reference to the changed value.
+ *
+ * @param operand Value we want to subtract from the underlying address.
+ * @return Reference to the changed underlying address.
+ */
+ auto operator-=(difference_type operand) -> physical_frame_iterator &;
+
+ /**
+ * @brief Addition operator. Returns the changed value.
+ *
+ * @param operand Value we want to add to a copy of the underlying address.
+ * @return Copy of underlying address incremented by the given value.
+ */
+ auto operator+(difference_type operand) const -> physical_frame_iterator;
+
+ /**
+ * @brief Subtraction operator. Returns the changed value.
+ *
+ * @param operand Value we want to subtrcat from a copy of the underlying address.
+ * @return Copy of underlying address decremented by the given value.
+ */
+ auto operator-(difference_type operand) const -> physical_frame_iterator;
+
+ /**
+ * @brief Subtraction operator. Returns the size difference between two iterators.
+ *
+ * @param other Other iterator we want to substract the underlying address with ours.
+ * @return Size difference between the underlying address of this instance and the given iterator.
+ */
+ auto operator-(const physical_frame_iterator & other) const -> difference_type;
+
+ /**
+ * @brief Index operator overload. Returns a reference to the value at the given index. Simply returns the
+ * dereferenced underlying pointer incremented by the given index.
+ *
+ * @param index Index we want to access and get the value from.
+ * @return Reference to the value at the given index.
+ */
+ auto operator[](difference_type index) const -> value_type;
+
+ /**
+ * @brief Defaulted comparsion operator. Simply compares the memory address of both iterators.
+ *
+ * @param other Other iterator to compare to.
+ * @return Whether both iterators point to the same underlying address in memory.
+ */
+ auto operator==(physical_frame_iterator const & other) const -> bool = default;
+
+ /**
+ * @brief Defaulted threeway comparsion operator. Simply compares the memory address of both iterators.
+ *
+ * @param other Other iterator to compare to.
+ * @return Whether the given iterator is smaller or larger than this iterator.
+ */
+ auto operator<=>(physical_frame_iterator const & other) const -> std::strong_ordering = default;
+
+ private:
+ value_type value = {}; ///< Underlying address the iterator is currently pointing too.
};
} // namespace teachos::arch::memory::allocator
diff --git a/arch/x86_64/include/arch/memory/multiboot/reader.hpp b/arch/x86_64/include/arch/memory/multiboot/reader.hpp
index baa49c9..a5c4872 100644
--- a/arch/x86_64/include/arch/memory/multiboot/reader.hpp
+++ b/arch/x86_64/include/arch/memory/multiboot/reader.hpp
@@ -1,6 +1,7 @@
#ifndef TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_READER_HPP
#define TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_READER_HPP
+#include "arch/memory/multiboot/elf_symbols_section.hpp"
#include "arch/memory/multiboot/memory_map.hpp"
#include <cstdint>
@@ -13,8 +14,12 @@ namespace teachos::arch::memory::multiboot
*/
struct memory_information
{
- std::size_t kernel_start; ///< Start address of the kernel code in memory.
- std::size_t kernel_end; ///< End address of the kernel code in memory.
+ std::size_t kernel_start; ///< Start address of the kernel code in memory.
+ std::size_t kernel_end; ///< End address of the kernel code in memory.
+ elf_section_header_container::iterator
+ begin_kernel; ///< Iterator containing non-owning pointer to the first element of all kernel sections.
+ elf_section_header_container::iterator
+ end_kernel; ///< Iterator pointing to one past the last element of all kernel sections.
std::size_t multiboot_start; ///< Start address of the multiboot code in memory.
std::size_t multiboot_end; ///< End address of the multiboot code in memory.
memory_area_container::iterator
diff --git a/arch/x86_64/include/arch/memory/paging/active_page_table.hpp b/arch/x86_64/include/arch/memory/paging/active_page_table.hpp
index 8e80b51..c183aff 100644
--- a/arch/x86_64/include/arch/memory/paging/active_page_table.hpp
+++ b/arch/x86_64/include/arch/memory/paging/active_page_table.hpp
@@ -17,8 +17,6 @@ namespace teachos::arch::memory::paging
*/
struct active_page_table
{
- typedef void (*function)(active_page_table & active_page_table);
-
/**
* @brief Creates a single instance of an active level 4 page table table and returns the created instance or
* alternatively returns the previously created instance instead. The instance is owned by this method and is
diff --git a/arch/x86_64/include/arch/memory/paging/inactive_page_table.hpp b/arch/x86_64/include/arch/memory/paging/inactive_page_table.hpp
index 62c196f..df3ba00 100644
--- a/arch/x86_64/include/arch/memory/paging/inactive_page_table.hpp
+++ b/arch/x86_64/include/arch/memory/paging/inactive_page_table.hpp
@@ -9,7 +9,7 @@ namespace teachos::arch::memory::paging
{
struct inactive_page_table
{
- inactive_page_table(allocator::physical_frame frame, active_page_table active_page_table,
+ inactive_page_table(allocator::physical_frame frame, active_page_table & active_page_table,
temporary_page temporary_page);
allocator::physical_frame page_table_level_4_frame;
diff --git a/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp b/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp
index 0583692..0e2411a 100644
--- a/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp
+++ b/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp
@@ -7,15 +7,80 @@
namespace teachos::arch::memory::paging
{
+ namespace
+ {
+ std::size_t constexpr UNUSED_VIRTUAL_ADDRESS = 0xCAFEBABE;
+ } // namespace
+
+ typedef shared::container<allocator::physical_frame_iterator> frame_container;
+
+ template<allocator::FrameAllocator T>
struct kernel_mapper
{
- kernel_mapper(active_page_table & active_table);
+ kernel_mapper(T & allocator, multiboot::memory_information const & mem_info)
+ : allocator(allocator)
+ , mem_info(mem_info)
+ {
+ // Nothing to do
+ }
- auto with(inactive_page_table inactive_page_table, temporary_page temporary_page,
- active_page_table::function f) -> void;
+ auto remap_kernel() -> void
+ {
+ temporary_page temp_page{virtual_page{UNUSED_VIRTUAL_ADDRESS}, 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");
+ auto const inactive_table = inactive_page_table{frame.value(), active_table, temp_page};
+ map_elf_sections(inactive_table, temp_page, active_table);
+ }
private:
- active_page_table & active_table;
+ auto map_elf_sections(inactive_page_table inactive_page_table, temporary_page temporary_page,
+ active_page_table & active_table) -> void
+ {
+ auto const 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();
+ map_kernel_sections(active_table);
+
+ page_table_level4[511].set_entry(backup, entry::PRESENT | entry::WRITABLE);
+ tlb_flush_all();
+ temporary_page.unmap(active_table);
+ }
+
+ auto map_kernel_sections(active_page_table & active_table) -> void
+ {
+ multiboot::elf_section_header_container sections{mem_info.begin_kernel, mem_info.end_kernel};
+ for (auto const & section : sections)
+ {
+ if (!section.flags.contains_flags(multiboot::elf_section_flags::OCCUPIES_MEMORY))
+ {
+ continue;
+ }
+ exception_handling::assert(section.virtual_address % allocator::PAGE_FRAME_SIZE == 0U,
+ "[Kernel Mapper] Section must be page aligned");
+ auto const start_frame = allocator::physical_frame::containing_address(section.virtual_address);
+ auto const end_frame =
+ allocator::physical_frame::containing_address(section.virtual_address + section.section_size);
+
+ allocator::physical_frame_iterator const begin{start_frame};
+ allocator::physical_frame_iterator const end{end_frame};
+ frame_container frames{begin, end};
+
+ for (auto frame : frames)
+ {
+ // TODO: Use actual elf section flags, convert from one to the other flag type.
+ active_table.identity_map(allocator, frame, entry::WRITABLE);
+ }
+ }
+ }
+
+ T & allocator;
+ multiboot::memory_information const & mem_info;
};
} // namespace teachos::arch::memory::paging
diff --git a/arch/x86_64/include/arch/shared/container.hpp b/arch/x86_64/include/arch/shared/container.hpp
index 8ea0d08..bc2a5f6 100644
--- a/arch/x86_64/include/arch/shared/container.hpp
+++ b/arch/x86_64/include/arch/shared/container.hpp
@@ -20,6 +20,11 @@ namespace teachos::arch::shared
using size_type = std::size_t; ///< Maximum size of this container.
/**
+ * @brief Defaulted constructor.
+ */
+ container() = default;
+
+ /**
* @brief Constructor.
*
* @param begin Iterator containing non-owning pointer to the first element of all memory areas.
@@ -29,8 +34,7 @@ namespace teachos::arch::shared
: begin_itr(begin)
, end_itr(end)
{
- exception_handling::assert(begin != iterator{}, "[Memory Area] Attempted to pass nullptr as begin iterator");
- exception_handling::assert(end != iterator{}, "[Memory Area] Attempted to pass nullptr as end iterator");
+ // Nothing to do
}
/**
@@ -65,8 +69,8 @@ namespace teachos::arch::shared
auto empty() const -> bool { return size() == 0; }
private:
- iterator begin_itr; ///< Pointer to the first element of the given template type.
- iterator end_itr; ///< Pointer to one pas the last element of the given template type.
+ iterator begin_itr = {}; ///< Pointer to the first element of the given template type.
+ iterator end_itr = {}; ///< Pointer to one pas the last element of the given template type.
};
} // namespace teachos::arch::shared
diff --git a/arch/x86_64/include/arch/shared/random_access_iterator.hpp b/arch/x86_64/include/arch/shared/random_access_iterator.hpp
index 13deb68..b1a800c 100644
--- a/arch/x86_64/include/arch/shared/random_access_iterator.hpp
+++ b/arch/x86_64/include/arch/shared/random_access_iterator.hpp
@@ -26,8 +26,7 @@ namespace teachos::arch::shared
/**
* @brief Constructor.
*
- * @param p Underlying address the iterator should point too, ensure to not pass an invalid pointer or the
- * constructor will halt execution.
+ * @param p Underlying address the iterator should point too.
*/
explicit random_access_iterator(value_type * p)
: ptr(p)
@@ -144,14 +143,6 @@ namespace teachos::arch::shared
auto operator==(random_access_iterator const & other) const -> bool = default;
/**
- * @brief Defaulted negated comparsion operator. Simply compares the memory address of both iterators.
- *
- * @param other Other iterator to compare to.
- * @return Whether both iterators don't point to the same underlying address in memory.
- */
- auto operator!=(random_access_iterator const & other) const -> bool = default;
-
- /**
* @brief Defaulted threeway comparsion operator. Simply compares the memory address of both iterators.
*
* @param other Other iterator to compare to.