aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2025-12-15 17:04:15 +0100
committerFelix Morgner <felix.morgner@ost.ch>2025-12-15 17:04:15 +0100
commita9eeec745e29d89afd48ee43d09432eb6fc35be7 (patch)
tree6fc71a253c8b0325d303bd34c95b564ba536ed14
parent1945dd16716392e70e74efacd19e779f121bd1da (diff)
downloadteachos-a9eeec745e29d89afd48ee43d09432eb6fc35be7.tar.xz
teachos-a9eeec745e29d89afd48ee43d09432eb6fc35be7.zip
kapi/memory: initialize memory subsystem
-rw-r--r--arch/x86_64/include/x86_64/memory/buffered_allocator.hpp3
-rw-r--r--arch/x86_64/src/kapi/memory.cpp36
-rw-r--r--arch/x86_64/src/memory/region_allocator.cpp1
3 files changed, 25 insertions, 15 deletions
diff --git a/arch/x86_64/include/x86_64/memory/buffered_allocator.hpp b/arch/x86_64/include/x86_64/memory/buffered_allocator.hpp
index d7e9491..90ac878 100644
--- a/arch/x86_64/include/x86_64/memory/buffered_allocator.hpp
+++ b/arch/x86_64/include/x86_64/memory/buffered_allocator.hpp
@@ -6,6 +6,7 @@
#include <algorithm>
#include <array>
#include <cstddef>
+#include <optional>
namespace teachos::memory::x86_64
{
@@ -20,7 +21,7 @@ namespace teachos::memory::x86_64
}
buffered_allocator(buffered_allocator const &) = delete;
- buffered_allocator(buffered_allocator &&) = default;
+ buffered_allocator(buffered_allocator && other) noexcept = delete;
~buffered_allocator() override
{
diff --git a/arch/x86_64/src/kapi/memory.cpp b/arch/x86_64/src/kapi/memory.cpp
index abfb32e..8c53c4c 100644
--- a/arch/x86_64/src/kapi/memory.cpp
+++ b/arch/x86_64/src/kapi/memory.cpp
@@ -24,6 +24,7 @@
#include <cstddef>
#include <cstdint>
#include <memory>
+#include <optional>
#include <span>
#include <utility>
@@ -36,7 +37,7 @@ namespace teachos::memory
constexpr auto static recursive_page_map_index = x86_64::page_table::entry_count - 2;
//! Instantiate a basic, memory region based, early frame allocator for remapping.
- auto create_early_frame_allocator()
+ auto collect_memory_information()
{
auto memory_map = boot::bootstrap_information.mbi->maybe_memory_map();
if (!memory_map)
@@ -48,12 +49,10 @@ namespace teachos::memory
auto mbi_span = std::span{std::bit_cast<std::byte *>(mbi), mbi->size_bytes()};
auto image_span = std::span{&boot::x86_64::_start_physical, &boot::x86_64::_end_physical};
- return x86_64::region_allocator{
- {
- .image_range = std::make_pair(physical_address{&image_span.front()}, physical_address{&image_span.back()}),
- .mbi_range = std::make_pair(physical_address{&mbi_span.front()}, physical_address{&mbi_span.back()}),
- .memory_map = *memory_map,
- }
+ return x86_64::region_allocator::memory_information{
+ .image_range = std::make_pair(physical_address{&image_span.front()}, physical_address{&image_span.back()}),
+ .mbi_range = std::make_pair(physical_address{&mbi_span.front()}, physical_address{&mbi_span.back()}),
+ .memory_map = *memory_map,
};
}
@@ -150,6 +149,12 @@ namespace teachos::memory
} // namespace
+ // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
+ auto constinit region_based_allocator = std::optional<x86_64::region_allocator>{};
+ auto constinit buffered_allocator = std::optional<x86_64::buffered_allocator<4>>{};
+ auto constinit recursive_page_mapper = std::optional<x86_64::recursive_page_mapper>{};
+ // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
+
auto init() -> void
{
auto static constinit is_initialized = std::atomic_flag{};
@@ -163,23 +168,26 @@ namespace teachos::memory
enable_cpu_protections();
- auto early_allocator = create_early_frame_allocator();
- auto allocation_buffer = x86_64::buffered_allocator<4>{&early_allocator};
- auto recursive_mapper = x86_64::recursive_page_mapper{allocation_buffer};
+ region_based_allocator.emplace(collect_memory_information());
+ buffered_allocator.emplace(&*region_based_allocator);
+ recursive_page_mapper.emplace(*buffered_allocator);
cio::println("[x86_64:MEM] Preparing new paging hierarchy.");
- auto new_pml4_frame = inject_faux_pml4(allocation_buffer, recursive_mapper);
+ auto new_pml4_frame = inject_faux_pml4(*buffered_allocator, *recursive_page_mapper);
- remap_kernel(recursive_mapper);
- remap_vga_text_mode_buffer(recursive_mapper);
- remap_multiboot_information(recursive_mapper);
+ remap_kernel(*recursive_page_mapper);
+ remap_vga_text_mode_buffer(*recursive_page_mapper);
+ remap_multiboot_information(*recursive_page_mapper);
cio::println("[x86_64:MEM] Switching to new paging hierarchy.");
auto cr3 = cpu::x86_64::cr3::read();
cr3.address(new_pml4_frame.start_address());
cpu::x86_64::cr3::write(cr3);
+
+ set_frame_allocator(*buffered_allocator);
+ set_page_mapper(*recursive_page_mapper);
}
} // namespace teachos::memory
diff --git a/arch/x86_64/src/memory/region_allocator.cpp b/arch/x86_64/src/memory/region_allocator.cpp
index dbe14cd..0f65b3a 100644
--- a/arch/x86_64/src/memory/region_allocator.cpp
+++ b/arch/x86_64/src/memory/region_allocator.cpp
@@ -8,6 +8,7 @@
#include <algorithm>
#include <optional>
#include <ranges>
+#include <utility>
namespace teachos::memory::x86_64
{