From 45bf10f2ac489cc8253eb2f145c569edc16fc45a Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 16 Nov 2026 10:20:33 +0100 Subject: 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. --- kernel/kapi/memory.cpp | 87 -------------------------------------------------- 1 file changed, 87 deletions(-) (limited to 'kernel/kapi/memory.cpp') diff --git a/kernel/kapi/memory.cpp b/kernel/kapi/memory.cpp index 12e9e23f..4160092d 100644 --- a/kernel/kapi/memory.cpp +++ b/kernel/kapi/memory.cpp @@ -1,14 +1,11 @@ #include #include -#include #include #include #include -#include -#include #include #include @@ -67,23 +64,10 @@ namespace kapi::memory constinit bad_frame_allocator bad_frame_allocator::instance{}; constinit bad_page_mapper bad_page_mapper::instance{}; auto constinit allocator = std::optional{}; - auto constinit mmio_allocator = std::optional{}; constinit auto active_frame_allocator = static_cast(&bad_frame_allocator::instance); constinit auto active_page_mapper = static_cast(&bad_page_mapper::instance); - auto unmap_range_if_mapped(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. Additionally, we use try unmap to avoid panicking if the page was not mapped. - std::ignore = active_page_mapper->try_unmap(current_page); - } - } } // namespace auto get_frame_allocator() -> frame_allocator & @@ -165,75 +149,4 @@ namespace kapi::memory kstd::println("[OS:MEM] Physical memory manager initialized."); } - auto init_mmio(linear_address base, std::size_t page_count) -> void - { - mmio_allocator.emplace(base, page_count); - } - - auto allocate_mmio_region(std::size_t page_count) -> kstd::result - { - if (!mmio_allocator) - { - system::panic("[OS:MEM] MMIO allocator not initialized!"); - } - - return mmio_allocator->allocate(page_count).transform([&](auto base) -> mmio_region { return {base, page_count}; }); - } - - auto map_mmio_region(mmio_region region, physical_address hw_base, page_mapper::flags flags) - -> kstd::result> - { - // Ensure that the MMIO region is not mapped as executable. Asking for an executable mapping of an MMIO region is a - // logical error, not a runtime issue. - if ((flags & page_mapper::flags::executable) != page_mapper::flags::empty) - { - system::panic("[OS:MEM] Attempt to map MMIO region at {:#018x} as executable!", region.first); - } - - auto [start, count] = region; - - auto start_page = page::containing(start); - auto start_frame = frame::containing(hw_base); - auto frame_offset = hw_base.raw() % frame::size.value; - - flags |= page_mapper::flags::uncached | page_mapper::flags::supervisor_only | page_mapper::flags::global; - - auto first_mapped_page = map(start_page, start_frame, flags); - - if (!first_mapped_page) - { - return kstd::failure(make_error_code(kstd::errc::not_enough_memory)); - } - - for (auto page_index = 1uz; page_index < count; ++page_index) - { - auto page = start_page + page_index; - auto frame = start_frame + page_index; - auto mapped = map(page, frame, flags); - - if (!mapped) - { - unmap_range_if_mapped(start_page, page_index); - return kstd::failure(make_error_code(kstd::errc::not_enough_memory)); - } - } - - return kstd::success(first_mapped_page + frame_offset); - } - - auto release_mmio_region(mmio_region region) -> void - { - if (!mmio_allocator) - { - system::panic("[OS:MEM] MMIO allocator not initialized!"); - } - - auto start_address = region.first; - auto start_page = kapi::memory::page::containing(start_address); - - unmap_range_if_mapped(start_page, region.second); - - mmio_allocator->release(region.first); - } - } // namespace kapi::memory -- cgit v1.2.3