aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2025-12-02 19:39:03 +0100
committerFelix Morgner <felix.morgner@ost.ch>2025-12-02 19:39:03 +0100
commit9331afdcbbe95bc1bd79d657f0d7c5b91a19a375 (patch)
tree09053882abb3b2dddfa7883931f43405f544407b
parent148c54a3d470c6019ebebe1387a7d889a2b8808e (diff)
downloadteachos-9331afdcbbe95bc1bd79d657f0d7c5b91a19a375.tar.xz
teachos-9331afdcbbe95bc1bd79d657f0d7c5b91a19a375.zip
x86_64/memory: fix temporary page unmapping
-rw-r--r--arch/x86_64/src/kapi/memory.cpp34
-rw-r--r--arch/x86_64/src/memory/scoped_mapping.cpp2
2 files changed, 35 insertions, 1 deletions
diff --git a/arch/x86_64/src/kapi/memory.cpp b/arch/x86_64/src/kapi/memory.cpp
index 703b3e1..0e4b78e 100644
--- a/arch/x86_64/src/kapi/memory.cpp
+++ b/arch/x86_64/src/kapi/memory.cpp
@@ -8,6 +8,7 @@
#include "x86_64/memory/buffered_allocator.hpp"
#include "x86_64/memory/mmu.hpp"
#include "x86_64/memory/page_table.hpp"
+#include "x86_64/memory/page_utilities.hpp"
#include "x86_64/memory/paging_root.hpp"
#include "x86_64/memory/region_allocator.hpp"
#include "x86_64/memory/scoped_mapping.hpp"
@@ -63,15 +64,46 @@ namespace teachos::memory
using namespace x86_64;
using entry_flags = page_table::entry::flags;
- auto temporary_mapper = scoped_mapping{page::containing(unused_page_address), allocator};
+ auto page = page::containing(unused_page_address);
+
+ auto temporary_mapper = scoped_mapping{page, allocator};
auto new_pml4_frame = allocator.allocate();
auto pml4 = std::construct_at(temporary_mapper.map_as<page_table>(*new_pml4_frame, entry_flags::writable));
(*pml4)[recursive_page_map_index].frame(new_pml4_frame.value(), entry_flags::present | entry_flags::writable);
+
+ auto pml4_index = pml_index<4>(page);
+ auto & old_pml4 = paging_root::get();
+ auto pml4_entry = old_pml4[pml4_index];
+
+ auto pml3_index = pml_index<3>(page);
+ auto old_pml3 = old_pml4.next(pml4_index);
+ auto pml3_entry = (**old_pml3)[pml3_index];
+
+ auto pml2_index = pml_index<2>(page);
+ auto old_pml2 = (**old_pml3).next(pml3_index);
+ auto pml2_entry = (**old_pml2)[pml2_index];
+
+ auto pml1_index = pml_index<1>(page);
+ auto old_pml1 = (**old_pml2).next(pml2_index);
+ auto pml1_entry = (**old_pml1)[pml1_index];
+
paging_root::get()[recursive_page_map_index].frame(new_pml4_frame.value(),
entry_flags::present | entry_flags::writable);
tlb_flush_all();
+
+ auto & new_pml4 = paging_root::get();
+ new_pml4[pml4_index] = pml4_entry;
+
+ auto new_pml3 = new_pml4.next(pml4_index);
+ (**new_pml3)[pml3_index] = pml3_entry;
+
+ auto new_pml2 = (**new_pml3).next(pml3_index);
+ (**new_pml2)[pml2_index] = pml2_entry;
+
+ auto new_pml1 = (**new_pml2).next(pml2_index);
+ (**new_pml1)[pml1_index] = pml1_entry;
}
} // namespace
diff --git a/arch/x86_64/src/memory/scoped_mapping.cpp b/arch/x86_64/src/memory/scoped_mapping.cpp
index 602198e..191a7ad 100644
--- a/arch/x86_64/src/memory/scoped_mapping.cpp
+++ b/arch/x86_64/src/memory/scoped_mapping.cpp
@@ -89,6 +89,8 @@ namespace teachos::memory::x86_64
auto pml1_index = pml_index<1>(m_page);
(*pml1)[pml1_index].frame(frame, page_table::entry::flags::present | flags);
+ m_mapped = true;
+
return static_cast<std::byte *>(frame.start_address());
}