From f7ff847498d629c05bb206b41a172f6735e2afe6 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Apr 2026 09:51:44 +0200 Subject: kernel/tests: clean up implementation structure --- kernel/src/test_support/simulated_memory.cpp | 77 +++++++++++++++++----------- 1 file changed, 47 insertions(+), 30 deletions(-) (limited to 'kernel/src/test_support/simulated_memory.cpp') diff --git a/kernel/src/test_support/simulated_memory.cpp b/kernel/src/test_support/simulated_memory.cpp index fa3d36c..0648df2 100644 --- a/kernel/src/test_support/simulated_memory.cpp +++ b/kernel/src/test_support/simulated_memory.cpp @@ -4,50 +4,50 @@ #include +#include #include #include +#include #include #include #include #include -using namespace kstd::units_literals; - namespace kernel::tests { - namespace - { - constexpr auto virtual_size = 1_GiB; - } - simulated_memory::simulated_memory(kstd::units::bytes size) - : m_memory_descriptor{memfd_create("teachos_simulated_memory", 0)} - , m_size{size} + simulated_memory::simulated_memory(kstd::units::bytes physical_size, kstd::units::bytes virtual_size) + : m_descriptor{memfd_create("teachos_simulated_memory", 0)} + , m_physical_size{physical_size} + , m_virtual_size{virtual_size} { - if (m_memory_descriptor < 0) + if (m_descriptor < 0) { - throw std::runtime_error("Failed to create simulated memory"); + auto error = std::format("Failed to allocate backing memory: {}", strerror(errno)); + throw std::runtime_error(error); } - if (ftruncate(m_memory_descriptor, static_cast(m_size.value)) < 0) + if (ftruncate(m_descriptor, static_cast(m_physical_size.value)) < 0) { - throw std::runtime_error("Failed to resize simulated memory"); + auto error = std::format("Failed to reserve backing memory: {}", strerror(errno)); + throw std::runtime_error(error); } - auto mapped_pointer = mmap(nullptr, m_size.value, PROT_READ | PROT_WRITE, MAP_SHARED, m_memory_descriptor, 0); - if (mapped_pointer == MAP_FAILED) + auto physical_storage = mmap(nullptr, m_physical_size.value, PROT_READ | PROT_WRITE, MAP_SHARED, m_descriptor, 0); + if (physical_storage == MAP_FAILED) { - throw std::runtime_error("Failed to map simulated memory"); + auto error = std::format("Failed to map backing memory: {}", strerror(errno)); + throw std::runtime_error(error); } - m_physical_base = static_cast(mapped_pointer); - auto virtual_pointer = mmap(nullptr, virtual_size.value, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (virtual_pointer == MAP_FAILED) { - throw std::runtime_error("Failed to map simulated memory"); + auto error = std::format("Failed to reserve virtual memory: {}", strerror(errno)); + throw std::runtime_error(error); } + m_physical_base = static_cast(physical_storage); m_virtual_base = static_cast(virtual_pointer); clear(); @@ -55,39 +55,56 @@ namespace kernel::tests simulated_memory::~simulated_memory() { - munmap(m_physical_base, m_size.value); - munmap(m_virtual_base, virtual_size.value); - close(m_memory_descriptor); + munmap(m_virtual_base, m_virtual_size.value); + munmap(m_physical_base, m_physical_size.value); + close(m_descriptor); } auto simulated_memory::clear() -> void { - std::memset(m_physical_base, 0, m_size.value); + std::memset(m_physical_base, 0, m_physical_size.value); } - auto simulated_memory::ram_base() noexcept -> std::byte * + auto simulated_memory::physical_base() noexcept -> std::byte * { return m_physical_base; } - auto simulated_memory::ram_base() const noexcept -> std::byte const * + auto simulated_memory::physical_base() const noexcept -> std::byte const * { return m_physical_base; } - auto simulated_memory::heap_base() const noexcept -> kapi::memory::linear_address + auto simulated_memory::physical_size() const noexcept -> kstd::units::bytes + { + return m_physical_size; + } + + auto simulated_memory::virtual_base() const noexcept -> kapi::memory::linear_address { return kapi::memory::linear_address{m_virtual_base}; } - auto simulated_memory::heap_size() const noexcept -> kstd::units::bytes + auto simulated_memory::virtual_size() const noexcept -> kstd::units::bytes { - return virtual_size; + return m_virtual_size; } - auto simulated_memory::memory_descriptor() const noexcept -> int + auto simulated_memory::descriptor() const noexcept -> int { - return m_memory_descriptor; + return m_descriptor; + } + + auto simulated_memory::map(kstd::units::bytes size, std::byte * to, off_t offset) -> std::byte * + { + auto mapped_ptr = mmap(to, size.value, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, m_descriptor, offset); + if (mapped_ptr == MAP_FAILED) + { + auto error = std::format("Failed to map page: {}", strerror(errno)); + throw std::runtime_error(error); + } + + return static_cast(mapped_ptr); } } // namespace kernel::tests \ No newline at end of file -- cgit v1.2.3