aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/memory
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2024-11-24 13:10:21 +0000
committerFabian Imhof <fabian.imhof@ost.ch>2024-11-24 13:10:21 +0000
commit55f32173e97fdcf4a45006b66cc4b20329a5c7af (patch)
treed9b53c5037caf3a353f3e9cd93e7b0d6d5187240 /arch/x86_64/src/memory
parent77146c6e2dbd02661636d9424b7e51562eac30c9 (diff)
downloadteachos-55f32173e97fdcf4a45006b66cc4b20329a5c7af.tar.xz
teachos-55f32173e97fdcf4a45006b66cc4b20329a5c7af.zip
implement basic heap and remap it
Diffstat (limited to 'arch/x86_64/src/memory')
-rw-r--r--arch/x86_64/src/memory/heap/allocator.cpp27
-rw-r--r--arch/x86_64/src/memory/main.cpp47
2 files changed, 74 insertions, 0 deletions
diff --git a/arch/x86_64/src/memory/heap/allocator.cpp b/arch/x86_64/src/memory/heap/allocator.cpp
new file mode 100644
index 0000000..c9ddd78
--- /dev/null
+++ b/arch/x86_64/src/memory/heap/allocator.cpp
@@ -0,0 +1,27 @@
+#include "arch/memory/heap/allocator.hpp"
+
+#include "arch/exception_handling/assert.hpp"
+
+namespace teachos::arch::memory::heap
+{
+ auto bump_allocator::allocate(std::size_t size) -> void *
+ {
+ // Uses some sort of alignment orignally:
+ // https://github.com/phil-opp/blog_os/blob/7f6576c9dc34e360b81236c54c25c7827fd6a2df/src/memory/heap_allocator.rs#L24
+ auto alloc_start = next;
+ auto alloc_end = next + size;
+
+ arch::exception_handling::assert(alloc_end <= heap_end, "[Heap Allocator] Out of memory!");
+
+ next = alloc_end;
+ return reinterpret_cast<void *>(alloc_start);
+ }
+
+ auto bump_allocator::deallocate(uint8_t * pointer, std::size_t size) -> void
+ {
+ // Memory leak
+ if (pointer || size)
+ {
+ }
+ }
+} // namespace teachos::arch::memory::heap \ No newline at end of file
diff --git a/arch/x86_64/src/memory/main.cpp b/arch/x86_64/src/memory/main.cpp
new file mode 100644
index 0000000..ce07115
--- /dev/null
+++ b/arch/x86_64/src/memory/main.cpp
@@ -0,0 +1,47 @@
+#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/allocator.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 heap_start_page = paging::virtual_page::containing_address(memory::heap::HEAP_START);
+ auto heap_end_page =
+ paging::virtual_page::containing_address(memory::heap::HEAP_START + memory::heap::HEAP_SIZE - 1);
+
+ for (auto i = heap_start_page.page_number; i <= heap_end_page.page_number; i++)
+ {
+ active_table.map_page_to_next_free_frame(allocator, paging::virtual_page{i}, 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_cr2_bit(memory::cpu::cr2_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 successfull", video::vga::text::common_attributes::green_on_black);
+
+ remap_heap(allocator, active_table);
+ }
+} // namespace teachos::arch::memory \ No newline at end of file