aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-03-16 19:03:53 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-03-16 19:03:53 +0100
commit69a92ce3077f01ce43daef5681db298594a4badc (patch)
treede3a97018853d453c550bba7a1507211ae28fd31 /arch/x86_64/src
parentcc5f6ef95acb7a0024c43eba314eb1f3563b41b8 (diff)
downloadteachos-69a92ce3077f01ce43daef5681db298594a4badc.tar.xz
teachos-69a92ce3077f01ce43daef5681db298594a4badc.zip
x86_64/memory: remove recursive mapping
Diffstat (limited to 'arch/x86_64/src')
-rw-r--r--arch/x86_64/src/boot/boot32.S4
-rw-r--r--arch/x86_64/src/memory/paging_root.cpp19
-rw-r--r--arch/x86_64/src/memory/recursive_page_mapper.cpp121
-rw-r--r--arch/x86_64/src/memory/scoped_mapping.cpp69
4 files changed, 0 insertions, 213 deletions
diff --git a/arch/x86_64/src/boot/boot32.S b/arch/x86_64/src/boot/boot32.S
index 056cd8e..694b8b7 100644
--- a/arch/x86_64/src/boot/boot32.S
+++ b/arch/x86_64/src/boot/boot32.S
@@ -319,10 +319,6 @@ _prepare_page_maps:
call _clear_page_map_memory
lea (page_map_level_4 - 0b)(%esi), %edi
- mov %edi, %eax
- or $0b11, %eax
- mov %eax, (510 * 8)(%edi)
-
lea (page_map_level_3_low - 0b)(%esi), %eax
or $0b11, %eax
mov %eax, (%edi)
diff --git a/arch/x86_64/src/memory/paging_root.cpp b/arch/x86_64/src/memory/paging_root.cpp
deleted file mode 100644
index 41f40ed..0000000
--- a/arch/x86_64/src/memory/paging_root.cpp
+++ /dev/null
@@ -1,19 +0,0 @@
-#include "arch/memory/paging_root.hpp"
-
-#include <bit>
-#include <cstdint>
-
-namespace arch::memory
-{
-
- namespace
- {
- constexpr auto recursive_base = std::uintptr_t{0177777'776'776'776'776'0000uz};
- } // namespace
-
- auto paging_root::get() -> paging_root *
- {
- return std::bit_cast<paging_root *>(recursive_base);
- }
-
-} // namespace arch::memory \ No newline at end of file
diff --git a/arch/x86_64/src/memory/recursive_page_mapper.cpp b/arch/x86_64/src/memory/recursive_page_mapper.cpp
deleted file mode 100644
index c7c5341..0000000
--- a/arch/x86_64/src/memory/recursive_page_mapper.cpp
+++ /dev/null
@@ -1,121 +0,0 @@
-#include "arch/memory/recursive_page_mapper.hpp"
-
-#include "kapi/memory.hpp"
-#include "kapi/system.hpp"
-
-#include "arch/memory/page_table.hpp"
-#include "arch/memory/page_utilities.hpp"
-#include "arch/memory/paging_root.hpp"
-
-#include <cstddef>
-#include <optional>
-
-namespace arch::memory
-{
- namespace
- {
- //! Perform the actual mapping of the page, via the recursive page map.
- //!
- //! On any level above PML1, the entries need to not be no_execute, because the image is densely packed. The entries
- //! also need to be writable, since the mapping is being performed through the recursive page map hierarchy. When
- //! setting the final entry in the PML1, the actually desired flags are set as is, with the present bit
- //! added, thus still enforcing non-writability and non-execution of the affected page.
- template<std::size_t Level>
- requires(Level > 1uz && Level <= PLATFORM_PAGING_LEVELS)
- auto do_map(recursive_page_table<Level> * pml, kapi::memory::page page, kapi::memory::frame_allocator & allocator,
- kapi::memory::page_mapper::flags flags)
- {
- auto index = pml_index<Level>(page);
- auto entry_flags = to_table_flags(flags);
-
- entry_flags = entry_flags & ~page_table::entry::flags::no_execute;
- entry_flags = entry_flags | page_table::entry::flags::writable;
- if (!(*pml)[index].present())
- {
- auto new_table_frame = allocator.allocate();
- (*pml)[index].frame(new_table_frame.value(), page_table::entry::flags::present | entry_flags);
- auto new_table = std::optional{std::construct_at(*pml->next(index))};
- return new_table;
- }
- (*pml)[index] |= entry_flags;
- return pml->next(index);
- }
-
- //! Perform the actual PML1 update.
- auto do_map(page_table * pml, kapi::memory::page page, kapi::memory::frame frame,
- kapi::memory::page_mapper::flags flags) -> std::optional<std::byte *>
- {
- auto index = pml_index<1>(page);
- if ((*pml)[index].present())
- {
- kapi::system::panic("[x86_64:MEM] Tried to map a page that is already mapped");
- }
- (*pml)[index].frame(frame, page_table::entry::flags::present | to_table_flags(flags));
- return std::optional{static_cast<std::byte *>(page.start_address())};
- }
-
- } // namespace
-
- recursive_page_mapper::recursive_page_mapper() {}
-
- auto recursive_page_mapper::map(kapi::memory::page page, kapi::memory::frame frame, flags flags) -> std::byte *
- {
- auto pml4 = static_cast<recursive_page_table<4> *>((paging_root::get()));
- auto & allocator = kapi::memory::get_frame_allocator();
-
- return std::optional{pml4}
- .and_then([&](auto pml) -> auto { return do_map(pml, page, allocator, flags); })
- .and_then([&](auto pml) -> auto { return do_map(pml, page, allocator, flags); })
- .and_then([&](auto pml) -> auto { return do_map(pml, page, allocator, flags); })
- .and_then([&](auto pml) -> auto { return do_map(pml, page, frame, flags); })
- .value_or(nullptr);
- }
-
- auto recursive_page_mapper::unmap(kapi::memory::page page) -> void
- {
- if (!try_unmap(page))
- {
- kapi::system::panic("[x86_64:MEM] Tried to unmap a page that was not mapped.");
- }
- }
-
- auto recursive_page_mapper::try_unmap(kapi::memory::page page) noexcept -> bool
- {
- if (!paging_root::get()->translate(page))
- {
- return false;
- }
-
- auto pml4 = paging_root::get();
- auto pml3 = pml4->next(pml_index<4>(page)).value();
- auto pml2 = pml3->next(pml_index<3>(page)).value();
- auto pml1 = pml2->next(pml_index<2>(page)).value();
- auto & allocator = kapi::memory::get_frame_allocator();
-
- (*pml1)[pml_index<1>(page)].clear();
-
- if (pml1->empty())
- {
- auto pml1_frame = (*pml2)[pml_index<2>(page)].frame().value();
- allocator.release(pml1_frame);
- (*pml2)[pml_index<2>(page)].clear();
- }
-
- if (pml2->empty())
- {
- auto pml2_frame = (*pml3)[pml_index<3>(page)].frame().value();
- allocator.release(pml2_frame);
- (*pml3)[pml_index<3>(page)].clear();
- }
-
- if (pml3->empty())
- {
- auto pml3_frame = (*pml4)[pml_index<4>(page)].frame().value();
- allocator.release(pml3_frame);
- (*pml4)[pml_index<4>(page)].clear();
- }
-
- return true;
- }
-
-} // namespace arch::memory \ No newline at end of file
diff --git a/arch/x86_64/src/memory/scoped_mapping.cpp b/arch/x86_64/src/memory/scoped_mapping.cpp
deleted file mode 100644
index dde1dda..0000000
--- a/arch/x86_64/src/memory/scoped_mapping.cpp
+++ /dev/null
@@ -1,69 +0,0 @@
-#include "arch/memory/scoped_mapping.hpp"
-
-#include "kapi/memory.hpp"
-#include "kapi/system.hpp"
-
-#include "arch/memory/mmu.hpp"
-#include "arch/memory/page_table.hpp"
-#include "arch/memory/paging_root.hpp"
-
-#include <cstddef>
-#include <utility>
-
-namespace arch::memory
-{
-
- scoped_mapping::scoped_mapping(scoped_mapping && other) noexcept
- : m_page{std::exchange(other.m_page, kapi::memory::page{})}
- , m_mapper{std::exchange(other.m_mapper, nullptr)}
- , m_mapped{std::exchange(other.m_mapped, false)}
- {}
-
- scoped_mapping::scoped_mapping(kapi::memory::page page, kapi::memory::page_mapper & mapper)
- : m_page{page}
- , m_mapper{&mapper}
- , m_mapped{false}
- {
- if (paging_root::get()->translate(page))
- {
- kapi::system::panic("[MEM] Tried to map a page that is already mapped!");
- }
- }
-
- scoped_mapping::~scoped_mapping() noexcept
- {
- if (m_mapped)
- {
- unmap();
- tlb_flush(m_page.start_address());
- }
- }
-
- auto scoped_mapping::operator=(scoped_mapping && other) noexcept -> scoped_mapping &
- {
- swap(*this, other);
- return *this;
- }
-
- auto scoped_mapping::map(kapi::memory::frame frame, page_table::entry::flags flags) -> std::byte *
- {
- auto result = m_mapper->map(m_page, frame, to_mapper_flags(flags));
- m_mapped = true;
- return result;
- }
-
- auto scoped_mapping::unmap() -> void
- {
- m_mapper->unmap(m_page);
- m_mapped = false;
- }
-
- auto swap(scoped_mapping & lhs, scoped_mapping & rhs) -> void
- {
- using std::swap;
- swap(lhs.m_page, rhs.m_page);
- swap(lhs.m_mapper, rhs.m_mapper);
- swap(lhs.m_mapped, rhs.m_mapped);
- }
-
-} // namespace arch::memory \ No newline at end of file