From 203355e51690073e571d4906d53f2494c3dad41b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 1 Dec 2025 19:32:19 +0100 Subject: x86_64/memory: prepare scoped_mapping extraction --- arch/x86_64/src/memory/scoped_mapping.cpp | 73 +++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 arch/x86_64/src/memory/scoped_mapping.cpp (limited to 'arch/x86_64/src/memory') diff --git a/arch/x86_64/src/memory/scoped_mapping.cpp b/arch/x86_64/src/memory/scoped_mapping.cpp new file mode 100644 index 0000000..c436ee5 --- /dev/null +++ b/arch/x86_64/src/memory/scoped_mapping.cpp @@ -0,0 +1,73 @@ +#include "x86_64/memory/scoped_mapping.hpp" + +#include "kapi/memory.hpp" +#include "kapi/system.hpp" + +#include "x86_64/memory/mmu.hpp" + +#include + +namespace teachos::memory::x86_64 +{ + + scoped_mapping::scoped_mapping(scoped_mapping && other) + : m_address{std::exchange(other.m_address, linear_address{})} + , m_allocator{std::exchange(other.m_allocator, nullptr)} + , m_mapped{std::exchange(other.m_mapped, false)} + {} + + scoped_mapping::scoped_mapping(linear_address address, frame_allocator & allocator) + : m_address{address} + , m_allocator{&allocator} + , m_mapped{false} + {} + + scoped_mapping::~scoped_mapping() + { + if (m_mapped) + { + unmap(); + x86_64::tlb_flush(m_address); + } + } + + auto scoped_mapping::operator=(scoped_mapping && other) -> scoped_mapping & + { + if (&other == this) + { + return *this; + } + + using std::swap; + + swap(m_address, other.m_address); + swap(m_allocator, other.m_allocator); + swap(m_mapped, other.m_mapped); + + return *this; + } + + auto scoped_mapping::map(frame frame, page_table::entry::flags flags) -> std::byte * + { + static_cast(frame); + static_cast(flags); + + m_mapped = true; + + return nullptr; + } + + auto scoped_mapping::unmap() -> void + { + if (!m_mapped) + { + system::panic("[MEM] Tried to release an unmapped temporary mapping!"); + } + + // TODO: scan pages + // TODO: remove mapping + // TODO: release temporary table frames + m_mapped = false; + } + +} // namespace teachos::memory::x86_64 \ No newline at end of file -- cgit v1.2.3