aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/include
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2025-12-10 16:51:44 +0100
committerFelix Morgner <felix.morgner@ost.ch>2025-12-10 16:51:44 +0100
commitb9d445bf92725d79269becf978059e040519c00a (patch)
tree768eb7158b8a106a2577d87661cde1a4e5ee108a /arch/x86_64/include
parent448a79328544e3ecb72d0b3b95c0b9b0d49faafc (diff)
downloadteachos-b9d445bf92725d79269becf978059e040519c00a.tar.xz
teachos-b9d445bf92725d79269becf978059e040519c00a.zip
x86_64/memory: implement simple kernel remapper
Diffstat (limited to 'arch/x86_64/include')
-rw-r--r--arch/x86_64/include/x86_64/memory/kernel_mapper.hpp33
-rw-r--r--arch/x86_64/include/x86_64/memory/page_table.hpp16
-rw-r--r--arch/x86_64/include/x86_64/memory/paging_root.hpp8
3 files changed, 57 insertions, 0 deletions
diff --git a/arch/x86_64/include/x86_64/memory/kernel_mapper.hpp b/arch/x86_64/include/x86_64/memory/kernel_mapper.hpp
new file mode 100644
index 0000000..4b681ae
--- /dev/null
+++ b/arch/x86_64/include/x86_64/memory/kernel_mapper.hpp
@@ -0,0 +1,33 @@
+#ifndef TEACHOS_X86_64_KERNEL_MAPPER_HPP
+#define TEACHOS_X86_64_KERNEL_MAPPER_HPP
+
+#include "kapi/memory.hpp"
+
+#include <elf/format.hpp>
+#include <elf/section_header.hpp>
+#include <multiboot2/information.hpp>
+
+#include <string_view>
+
+namespace teachos::memory::x86_64
+{
+
+ struct kernel_mapper
+ {
+ using section_header_type = elf::section_header<elf::format::elf64>;
+
+ kernel_mapper(frame_allocator & allocator, multiboot2::information_view const * mbi);
+
+ auto remap_kernel() -> void;
+
+ private:
+ auto map_section(section_header_type const & section, std::string_view name) -> void;
+
+ frame_allocator * m_allocator;
+ multiboot2::information_view const * m_mbi;
+ std::uintptr_t m_kernel_load_base;
+ };
+
+} // namespace teachos::memory::x86_64
+
+#endif \ No newline at end of file
diff --git a/arch/x86_64/include/x86_64/memory/page_table.hpp b/arch/x86_64/include/x86_64/memory/page_table.hpp
index c5102a2..ce34e23 100644
--- a/arch/x86_64/include/x86_64/memory/page_table.hpp
+++ b/arch/x86_64/include/x86_64/memory/page_table.hpp
@@ -51,6 +51,12 @@ namespace teachos::memory::x86_64
//! Get all flags present in this entry.
[[nodiscard]] auto all_flags() const -> flags;
+ //! Set the flags of this entry to the given set.
+ auto all_flags(flags flags) -> void;
+
+ //! Additively set the given flags in this entry.
+ auto operator|=(flags rhs) -> entry &;
+
//! Get the frame number associated with this entry, if the referenced page is present.
[[nodiscard]] auto frame() const -> std::optional<frame>;
@@ -86,11 +92,21 @@ namespace teachos::memory::x86_64
return std::bit_cast<page_table::entry::flags>(std::to_underlying(lhs) | std::to_underlying(rhs));
}
+ constexpr auto operator|=(page_table::entry::flags & lhs, page_table::entry::flags rhs) -> page_table::entry::flags &
+ {
+ return lhs = lhs | rhs;
+ }
+
constexpr auto operator&(page_table::entry::flags lhs, page_table::entry::flags rhs) -> page_table::entry::flags
{
return std::bit_cast<page_table::entry::flags>(std::to_underlying(lhs) & std::to_underlying(rhs));
}
+ constexpr auto operator~(page_table::entry::flags flags)
+ {
+ return std::bit_cast<page_table::entry::flags>(~std::to_underlying(flags));
+ }
+
//! A recursively mapped page table.
template<std::size_t Level>
requires(Level > 0uz && Level < 5uz)
diff --git a/arch/x86_64/include/x86_64/memory/paging_root.hpp b/arch/x86_64/include/x86_64/memory/paging_root.hpp
index a9ab6a6..d4aa023 100644
--- a/arch/x86_64/include/x86_64/memory/paging_root.hpp
+++ b/arch/x86_64/include/x86_64/memory/paging_root.hpp
@@ -25,6 +25,14 @@ namespace teachos::memory::x86_64
[[nodiscard]] auto translate(linear_address address) const -> std::optional<physical_address>;
[[nodiscard]] auto translate(page page) const -> std::optional<frame>;
+ //! Map the given page into the given frame using the given flags.
+ //!
+ //! @param page A page to map.
+ //! @param frame The frame into which to map the page.
+ //! @param flags The flags to apply to the mapping.
+ //! @param allocator The frame allocator used to allocate any required page tables.
+ auto map(page page, frame frame, entry::flags flags, frame_allocator & allocator) -> std::optional<std::byte *>;
+
private:
paging_root() = default;
};