aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/devices/storage/ram_disk
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/src/devices/storage/ram_disk')
-rw-r--r--kernel/src/devices/storage/ram_disk/controller.cpp27
-rw-r--r--kernel/src/devices/storage/ram_disk/device.cpp71
-rw-r--r--kernel/src/devices/storage/ram_disk/device.tests.cpp116
3 files changed, 214 insertions, 0 deletions
diff --git a/kernel/src/devices/storage/ram_disk/controller.cpp b/kernel/src/devices/storage/ram_disk/controller.cpp
new file mode 100644
index 0000000..30441fa
--- /dev/null
+++ b/kernel/src/devices/storage/ram_disk/controller.cpp
@@ -0,0 +1,27 @@
+#include <kernel/devices/storage/ram_disk/controller.hpp>
+
+#include <kernel/devices/storage/ram_disk/device.hpp>
+
+#include <kapi/boot_module/boot_module_registry.hpp>
+
+#include <kstd/memory>
+
+#include <algorithm>
+#include <cstddef>
+
+namespace kernel::devices::storage::ram_disk
+{
+ controller::controller(kapi::boot_modules::boot_module_registry const * registry)
+ : m_boot_module_registry(registry)
+ {}
+
+ auto controller::probe() -> void
+ {
+ size_t current_device_index = 0;
+
+ std::ranges::for_each(*m_boot_module_registry, [this, &current_device_index](auto const & module) {
+ auto const minor = current_device_index++ * m_minors_per_device;
+ m_devices.push_back(kstd::make_shared<device>(module, m_major, minor));
+ });
+ }
+} // namespace kernel::devices::storage::ram_disk \ No newline at end of file
diff --git a/kernel/src/devices/storage/ram_disk/device.cpp b/kernel/src/devices/storage/ram_disk/device.cpp
new file mode 100644
index 0000000..1557204
--- /dev/null
+++ b/kernel/src/devices/storage/ram_disk/device.cpp
@@ -0,0 +1,71 @@
+#include <kernel/devices/storage/ram_disk/device.hpp>
+
+#include <kernel/devices/block_device.hpp>
+
+#include <kapi/boot_module/boot_module.hpp>
+#include <kapi/system.hpp>
+
+#include <kstd/cstring>
+#include <kstd/string>
+
+#include <cstddef>
+
+namespace kernel::devices::storage::ram_disk
+{
+ namespace
+ {
+ constexpr size_t ram_disk_block_size = 512uz;
+ } // namespace
+
+ device::device(kapi::boot_modules::boot_module const & module, size_t major, size_t minor)
+ : block_device(major, minor, "ram" + kstd::to_string(minor), ram_disk_block_size)
+ , m_boot_module(module)
+ {}
+
+ auto device::init() -> bool
+ {
+ return m_boot_module.start_address.raw() != 0 && m_boot_module.size > 0;
+ }
+
+ auto device::read_block(size_t block_index, void * buffer) const -> void
+ {
+ if (buffer == nullptr)
+ {
+ kapi::system::panic("[RAM DISK DEVICE] read_block called with null buffer.");
+ }
+
+ auto const info = calculate_transfer(block_index);
+
+ if (info.to_transfer > 0)
+ {
+ auto const src = static_cast<std::byte const *>(m_boot_module.start_address) + info.offset;
+ kstd::libc::memcpy(buffer, src, info.to_transfer);
+ }
+
+ if (info.remainder > 0)
+ {
+ kstd::libc::memset(static_cast<std::byte *>(buffer) + info.to_transfer, 0, info.remainder);
+ }
+ }
+
+ auto device::write_block(size_t block_index, void const * buffer) -> void
+ {
+ if (buffer == nullptr)
+ {
+ kapi::system::panic("[RAM DISK DEVICE] write_block called with null buffer.");
+ }
+
+ auto const info = calculate_transfer(block_index);
+
+ if (info.to_transfer > 0)
+ {
+ auto const dest = static_cast<std::byte *>(m_boot_module.start_address) + info.offset;
+ kstd::libc::memcpy(dest, buffer, info.to_transfer);
+ }
+ }
+
+ auto device::size() const -> size_t
+ {
+ return m_boot_module.size;
+ }
+} // namespace kernel::devices::storage::ram_disk \ No newline at end of file
diff --git a/kernel/src/devices/storage/ram_disk/device.tests.cpp b/kernel/src/devices/storage/ram_disk/device.tests.cpp
new file mode 100644
index 0000000..d0fab76
--- /dev/null
+++ b/kernel/src/devices/storage/ram_disk/device.tests.cpp
@@ -0,0 +1,116 @@
+#include <kernel/devices/storage/ram_disk/device.hpp>
+
+#include <kapi/boot_module/boot_module.hpp>
+#include <kapi/memory.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/matchers/catch_matchers.hpp>
+#include <catch2/matchers/catch_matchers_range_equals.hpp>
+
+#include <cstddef>
+#include <ranges>
+#include <stdexcept>
+#include <vector>
+
+SCENARIO("RAM Disk Device Construction and Initialization", "[ram_disk_device]")
+{
+ GIVEN("an empty boot module")
+ {
+ kapi::boot_modules::boot_module boot_module{};
+ boot_module.start_address = kapi::memory::linear_address{nullptr};
+ boot_module.size = 0;
+
+ WHEN("constructing the device")
+ {
+ kernel::devices::storage::ram_disk::device device{boot_module, 0, 0};
+
+ THEN("init return false")
+ {
+ REQUIRE_FALSE(device.init());
+ }
+ }
+ }
+
+ GIVEN("a boot module with a valid start address and size")
+ {
+ kapi::boot_modules::boot_module boot_module{};
+ boot_module.start_address = kapi::memory::linear_address{reinterpret_cast<std::byte *>(0x1000)};
+ boot_module.size = 512;
+
+ WHEN("constructing the device")
+ {
+ kernel::devices::storage::ram_disk::device device{boot_module, 0, 0};
+
+ THEN("init return true")
+ {
+ REQUIRE(device.init());
+ }
+ }
+ }
+}
+
+SCENARIO("RAM Disk Device Read and Write", "[ram_disk_device]")
+{
+ GIVEN("a device initialized with a valid boot module")
+ {
+ auto storage = std::vector{4096, std::byte{0xff}};
+ auto module =
+ kapi::boot_modules::boot_module{"test_module", kapi::memory::linear_address{storage.data()}, storage.size()};
+ auto device = kernel::devices::storage::ram_disk::device{module, 0, 0};
+ REQUIRE(device.init());
+
+ WHEN("reading a full block from the device")
+ {
+ auto buffer = std::vector<std::byte>(device.block_size());
+ device.read_block(0, buffer.data());
+
+ THEN("the buffer is filled with the module data")
+ {
+ REQUIRE_THAT(buffer, Catch::Matchers::RangeEquals(std::views::take(storage, device.block_size())));
+ }
+ }
+
+ WHEN("reading from a block index beyond the module size")
+ {
+ auto buffer = std::vector<std::byte>(device.block_size());
+ device.read_block(10, buffer.data());
+
+ THEN("the buffer is filled with zeros")
+ {
+ REQUIRE_THAT(buffer, Catch::Matchers::RangeEquals(std::vector<std::byte>(device.block_size(), std::byte{0})));
+ }
+ }
+
+ WHEN("reading into a null buffer")
+ {
+ REQUIRE_THROWS_AS(device.read_block(0, nullptr), std::runtime_error);
+ }
+
+ WHEN("writing to a full block")
+ {
+ auto buffer = std::vector<std::byte>(device.block_size(), std::byte{0x01});
+ device.write_block(0, buffer.data());
+
+ THEN("the module data is updated")
+ {
+ REQUIRE_THAT(std::views::take(storage, device.block_size()), Catch::Matchers::RangeEquals(buffer));
+ }
+ }
+
+ WHEN("writing to a block index beyond the module size")
+ {
+ auto buffer = std::vector<std::byte>(device.block_size(), std::byte{0x01});
+ device.write_block(10, buffer.data());
+
+ THEN("the module data is not updated")
+ {
+ REQUIRE_THAT(storage, Catch::Matchers::RangeEquals(std::vector{4096, std::byte{0xff}}));
+ }
+ }
+
+ WHEN("writing from a null buffer")
+ {
+ REQUIRE_THROWS_AS(device.write_block(0, nullptr), std::runtime_error);
+ }
+ }
+} \ No newline at end of file