aboutsummaryrefslogtreecommitdiff
path: root/kernel/kapi/memory/mmio.cpp
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-11-16 10:20:33 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-11-22 14:20:36 +0100
commit45bf10f2ac489cc8253eb2f145c569edc16fc45a (patch)
tree0812a89514b78e2c3a9e2efe95393a1de71d2211 /kernel/kapi/memory/mmio.cpp
parenta15cd3183c35534561644bb1f615beb824ca6817 (diff)
downloadkernel-45bf10f2ac489cc8253eb2f145c569edc16fc45a.tar.xz
kernel-45bf10f2ac489cc8253eb2f145c569edc16fc45a.zip
kapi/memory: replace mmio_region API with mmio_window
Previously, allocation and mapping of MMIO regions was a multi-step process. The MMIO region API required its users to track and manage the lifetime of an MMIO region allocation manually. The new MMIO window API simplifies this by providing a single-step mapping interface and automatic lifetime management. Additionally, the new API adds strict bounds and alignment checks, to ensure that all MMIO accesses are within the requested range. The new API enforces these constraints at runtime via a system panic. Other contract violations, such as zero-sized mappings, are handled via runtime error codes. The new kapi::memory::mmio_window preserves the in-page offset of the hardware base within the window and bases all accesses on this offset. This means no user intervention is required to handle hardware ranges that are not aligned to a natural page boundary. The original pointer adjustment logic has been moved into the mmio_window class. Previous consumers of the region API (x86_64 LAPIC, PCIe) have been updated to the new mmio_window API. The behavior of the PCIe scan implementation has changed in that no windows are kept alive beyond the execution of the scan, since there are no consumers that would require persistent MMIO windows. Tests for the new infrastructure have been added, including a test support seam to allow for deinitialization of the MMIO subsystem in tests.
Diffstat (limited to 'kernel/kapi/memory/mmio.cpp')
-rw-r--r--kernel/kapi/memory/mmio.cpp192
1 files changed, 192 insertions, 0 deletions
diff --git a/kernel/kapi/memory/mmio.cpp b/kernel/kapi/memory/mmio.cpp
new file mode 100644
index 00000000..be99d9d7
--- /dev/null
+++ b/kernel/kapi/memory/mmio.cpp
@@ -0,0 +1,192 @@
+#include <kapi/memory/mmio.hpp>
+
+#include <kernel/memory/mmio_allocator.hpp>
+
+#include <kapi/memory.hpp>
+#include <kapi/system.hpp>
+#include <kapi/test_support/memory.hpp>
+
+#include <kstd/memory.hpp>
+#include <kstd/result.hpp>
+#include <kstd/system_error.hpp>
+#include <kstd/units.hpp>
+
+#include <cstddef>
+#include <cstdint>
+#include <limits>
+#include <optional>
+#include <tuple>
+#include <utility>
+
+using namespace kstd::units_literals;
+
+namespace kapi::memory
+{
+
+ namespace
+ {
+ auto constinit mmio_allocator = std::optional<kernel::memory::mmio_allocator>{};
+
+ auto unmap_range(page start, std::size_t count) -> void
+ {
+ for (auto page_index = 0uz; page_index < count; ++page_index)
+ {
+ auto const current_page = start + page_index;
+
+ // NOTE: We ignore the returned frame, since it has never been allocated by any kernel code. Rather, this frame
+ // corresponds to a frame which is "always present" since it is an abstraction of a memory mapped device,
+ // provided by the firmware.
+ std::ignore = memory::unmap(current_page);
+ }
+ }
+
+ constexpr auto determine_page_count(std::size_t in_page_offset, kstd::bytes size) -> std::size_t
+ {
+ return (kstd::bytes{in_page_offset} + size - 1_B) / page_size + 1;
+ }
+ } // namespace
+
+ auto mmio_window::map(physical_address hw_base, kstd::bytes size, access mode) -> kstd::result<mmio_window>
+ {
+ if (!mmio_allocator)
+ {
+ kapi::system::panic("[OS:MEM] Tried to map an MMIO window before the MMIO subsystem was initialized!");
+ }
+
+ if (size == 0_B || size.value - 1 > std::numeric_limits<std::uint64_t>::max() - hw_base.raw())
+ {
+ return kstd::failure(make_error_code(kstd::errc::invalid_argument));
+ }
+
+ auto const in_page_offset = hw_base.raw() % frame::size.value;
+ auto const page_count = determine_page_count(in_page_offset, size);
+ auto const virtual_base = mmio_allocator->allocate(page_count);
+
+ if (!virtual_base)
+ {
+ return kstd::failure(make_error_code(kstd::errc::not_enough_memory));
+ }
+
+ auto const first_page = page::containing(virtual_base.value());
+ auto const first_frame = frame::containing(hw_base);
+ auto const base_flags = mode == access::read_only ? page_mapper::flags::empty : page_mapper::flags::writable;
+ auto const flags =
+ base_flags | page_mapper::flags::uncached | page_mapper::flags::supervisor_only | page_mapper::flags::global;
+
+ auto const first_mapping = memory::map(first_page, first_frame, flags);
+
+ if (!first_mapping)
+ {
+ mmio_allocator->release(virtual_base.value());
+ return kstd::failure(make_error_code(kstd::errc::not_enough_memory));
+ }
+
+ for (auto index = 1uz; index < page_count; ++index)
+ {
+ auto const page = first_page + index;
+ auto const frame = first_frame + index;
+ auto const mapping = memory::map(page, frame, flags);
+
+ if (!mapping)
+ {
+ unmap_range(first_page, index);
+ mmio_allocator->release(virtual_base.value());
+ return kstd::failure(make_error_code(kstd::errc::not_enough_memory));
+ }
+ }
+
+ auto const linear_base = linear_address{virtual_base.value()};
+ auto const data_base = first_mapping + in_page_offset;
+
+ return kstd::success(mmio_window{linear_base, data_base, size, mode});
+ }
+
+ mmio_window::mmio_window(mmio_window && other) noexcept
+ : m_mapping_base(std::exchange(other.m_mapping_base, linear_address{}))
+ , m_data_base(std::exchange(other.m_data_base, nullptr))
+ , m_data_size(std::exchange(other.m_data_size, 0_B))
+ , m_mode(std::exchange(other.m_mode, access::read_only))
+ {}
+
+ auto mmio_window::operator=(mmio_window && other) noexcept -> mmio_window &
+ {
+ if (this != &other)
+ {
+ release();
+ m_mapping_base = std::exchange(other.m_mapping_base, linear_address{});
+ m_data_base = std::exchange(other.m_data_base, nullptr);
+ m_data_size = std::exchange(other.m_data_size, 0_B);
+ m_mode = std::exchange(other.m_mode, access::read_only);
+ }
+ return *this;
+ }
+
+ mmio_window::~mmio_window()
+ {
+ release();
+ }
+
+ mmio_window::mmio_window(linear_address mapping_base, kstd::observer_ptr<std::byte> data_base, kstd::bytes size,
+ access mode)
+ : m_mapping_base(mapping_base)
+ , m_data_base(data_base)
+ , m_data_size(size)
+ , m_mode(mode)
+ {}
+
+ auto mmio_window::assert_valid_access(kstd::bytes offset, kstd::bytes size) const -> void
+ {
+ if (!m_data_base)
+ {
+ system::panic("[OS:MEM] MMIO window has been moved from!");
+ }
+
+ if (size > m_data_size || offset > m_data_size - size)
+ {
+ system::panic("[OS:MEM] Tried to access {} bytes at offset {} in MMIO window of size {}", size, offset,
+ m_data_size);
+ }
+
+ auto const address = m_data_base.get() + offset.value;
+ if (reinterpret_cast<std::uintptr_t>(address) % size.value)
+ {
+ system::panic("[OS:MEM] Unaligned {} access in MMIO operation at {:#018x}!", size, address);
+ }
+ }
+
+ auto mmio_window::assert_writable() const -> void
+ {
+ if (m_mode != access::writable)
+ {
+ system::panic("[OS:MEM] Attempted to write to a read-only MMIO window!");
+ }
+ }
+
+ auto mmio_window::release() -> void
+ {
+ if (m_data_base)
+ {
+ auto const first_page = page::containing(m_mapping_base);
+ auto const in_page_offset = reinterpret_cast<std::uintptr_t>(m_data_base.get()) % page::size.value;
+ auto const page_count = determine_page_count(in_page_offset, m_data_size);
+ unmap_range(first_page, page_count);
+ mmio_allocator->release(m_mapping_base);
+ }
+ }
+
+ auto init_mmio(linear_address base, std::size_t page_count) -> void
+ {
+ mmio_allocator.emplace(base, page_count);
+ }
+
+} // namespace kapi::memory
+
+namespace kapi::test_support::memory
+{
+
+ auto deinit_mmio() -> void
+ {
+ kapi::memory::mmio_allocator.reset();
+ }
+
+} // namespace kapi::test_support::memory