aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/memory/main.cpp
diff options
context:
space:
mode:
authorMatteo Gmür <matteo.gmuer1@ost.ch>2025-02-18 10:59:05 +0100
committerMatteo Gmür <matteo.gmuer1@ost.ch>2025-02-18 10:59:05 +0100
commitcd42c21f2460751428b3e1b4ae07ea0b924967bc (patch)
treee3e410f399c3eead444f2a242a19448571fd979a /arch/x86_64/src/memory/main.cpp
parent47879f42d70755fcf5473ffb82798b515cb2e21b (diff)
parent3d488e53a1d15fcc01a7b1d23b9585ca7a724864 (diff)
downloadteachos-cd42c21f2460751428b3e1b4ae07ea0b924967bc.tar.xz
teachos-cd42c21f2460751428b3e1b4ae07ea0b924967bc.zip
Merge branch 'feat_memory_manager' into 'develop_sa'
Finish inital draft of Memory Manager See merge request teachos/kernel!3
Diffstat (limited to 'arch/x86_64/src/memory/main.cpp')
-rw-r--r--arch/x86_64/src/memory/main.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/arch/x86_64/src/memory/main.cpp b/arch/x86_64/src/memory/main.cpp
new file mode 100644
index 0000000..b978319
--- /dev/null
+++ b/arch/x86_64/src/memory/main.cpp
@@ -0,0 +1,53 @@
+#include "arch/memory/main.hpp"
+
+#include "arch/exception_handling/assert.hpp"
+#include "arch/memory/allocator/area_frame_allocator.hpp"
+#include "arch/memory/cpu/control_register.hpp"
+#include "arch/memory/cpu/msr.hpp"
+#include "arch/memory/heap/concept.hpp"
+#include "arch/memory/paging/active_page_table.hpp"
+#include "arch/memory/paging/kernel_mapper.hpp"
+
+namespace teachos::arch::memory
+{
+ namespace
+ {
+ auto remap_heap(allocator::area_frame_allocator allocator, paging::active_page_table & active_table) -> void
+ {
+ auto const start_page = paging::virtual_page::containing_address(memory::heap::HEAP_START);
+ auto const end_page =
+ ++(paging::virtual_page::containing_address(memory::heap::HEAP_START + memory::heap::HEAP_SIZE - 1));
+ paging::page_container::iterator const begin{start_page};
+ paging::page_container::iterator const end{end_page};
+ paging::page_container const pages{begin, end};
+
+ for (auto const & page : pages)
+ {
+ active_table.map_page_to_next_free_frame(allocator, page, paging::entry::WRITABLE);
+ }
+ }
+ } // namespace
+
+ auto initialize_memory_management() -> void
+ {
+ static bool has_been_called = false;
+ arch::exception_handling::assert(!has_been_called,
+ "[Initialization] Memory management has already been initialized");
+ has_been_called = true;
+
+ auto const memory_information = multiboot::read_multiboot2();
+ allocator::area_frame_allocator allocator(memory_information);
+
+ cpu::set_cr0_bit(memory::cpu::cr0_flags::WRITE_PROTECT);
+ cpu::set_efer_bit(memory::cpu::efer_flags::NXE);
+
+ paging::kernel_mapper kernel(allocator, memory_information);
+ auto & active_table = kernel.remap_kernel();
+ video::vga::text::write("Kernel remapping successful", video::vga::text::common_attributes::green_on_black);
+ video::vga::text::newline();
+
+ remap_heap(allocator, active_table);
+ video::vga::text::write("Heap remapping successful", video::vga::text::common_attributes::green_on_black);
+ video::vga::text::newline();
+ }
+} // namespace teachos::arch::memory