aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/test_support/simulated_memory.cpp
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-04-02 09:51:44 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-04-02 09:52:13 +0200
commitf7ff847498d629c05bb206b41a172f6735e2afe6 (patch)
tree874d35673d59d0f1e4ebcf01d61d9160ba655f9c /kernel/src/test_support/simulated_memory.cpp
parent0c01a95325b26151ff3c9a70142f5dc83ff7d53f (diff)
downloadkernel-f7ff847498d629c05bb206b41a172f6735e2afe6.tar.xz
kernel-f7ff847498d629c05bb206b41a172f6735e2afe6.zip
kernel/tests: clean up implementation structure
Diffstat (limited to 'kernel/src/test_support/simulated_memory.cpp')
-rw-r--r--kernel/src/test_support/simulated_memory.cpp77
1 files changed, 47 insertions, 30 deletions
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 <kstd/units>
+#include <cerrno>
#include <cstddef>
#include <cstring>
+#include <format>
#include <stdexcept>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
-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<off_t>(m_size.value)) < 0)
+ if (ftruncate(m_descriptor, static_cast<off_t>(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<std::byte *>(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<std::byte *>(physical_storage);
m_virtual_base = static_cast<std::byte *>(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<std::byte *>(mapped_ptr);
}
} // namespace kernel::tests \ No newline at end of file