From 60ba364ec3c458b6bcf3d048781c8dd88937cd0e Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 13 Nov 2026 11:40:07 +0100 Subject: kapi/memory: fix page unmap logic Previously, the page mapper was expected to automatically free all frames that had become unused. However, this only works if all mappings refer to plain memory frames. In the case of MMIO frames, the mapper would still try to release the frame via the global frame allocator, which is incorrect, leading to undefined behavior in the worst case. This problem was discovered during work the PCI/PCIe subsystem, since we want to unmap any unused buses during enumeration. --- kernel/kapi/memory.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'kernel/kapi/memory.cpp') diff --git a/kernel/kapi/memory.cpp b/kernel/kapi/memory.cpp index 666910ba..c1b48ea3 100644 --- a/kernel/kapi/memory.cpp +++ b/kernel/kapi/memory.cpp @@ -51,14 +51,14 @@ namespace kapi::memory system::panic("[OS:MEM] Tried to map a page without an active mapper."); } - auto unmap(page) -> void override + auto unmap(page) -> kapi::memory::frame override { system::panic("[OS:MEM] Tried to unmap a page without an active mapper."); } - auto try_unmap(page) noexcept -> bool override + auto try_unmap(page) noexcept -> std::optional override { - return false; + return std::nullopt; } }; @@ -109,7 +109,7 @@ namespace kapi::memory return active_page_mapper->map(page, frame, flags); } - auto unmap(page page) -> void + auto unmap(page page) -> frame { return active_page_mapper->unmap(page); } -- cgit v1.2.3 From e5c8bf2146bf17fe051f86708e7fd7f2b366b0ed Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 13 Nov 2026 11:48:29 +0100 Subject: kapi/memory: unmap MMIO regions on release --- kernel/kapi/memory.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'kernel/kapi/memory.cpp') diff --git a/kernel/kapi/memory.cpp b/kernel/kapi/memory.cpp index c1b48ea3..cdc01512 100644 --- a/kernel/kapi/memory.cpp +++ b/kernel/kapi/memory.cpp @@ -182,6 +182,16 @@ namespace kapi::memory auto release_mmio_region(mmio_region region) -> void { + for (auto page_index = 0uz; page_index < region.second; ++page_index) + { + auto page_address = region.first + page_index * kapi::memory::page_size; + auto page = kapi::memory::page::containing(page_address); + + // 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 = kapi::memory::unmap(page); + } mmio_allocator->release(region.first); } -- cgit v1.2.3 From 9a1f24148e2b61ef1f9d54c0285aba7213b14a0c Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 13 Nov 2026 13:25:30 +0100 Subject: kapi/memory: map MMIO regions more securely --- kernel/kapi/memory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'kernel/kapi/memory.cpp') diff --git a/kernel/kapi/memory.cpp b/kernel/kapi/memory.cpp index cdc01512..ee8b45f0 100644 --- a/kernel/kapi/memory.cpp +++ b/kernel/kapi/memory.cpp @@ -167,7 +167,7 @@ namespace kapi::memory auto start_page = page::containing(region.first); auto start_frame = frame::containing(hw_base); - flags |= page_mapper::flags::uncached; + flags |= page_mapper::flags::uncached | page_mapper::flags::supervisor_only | page_mapper::flags::global; auto start = map(start_page, start_frame, flags); -- cgit v1.2.3 From 1071297ee44d5102feb92b7a5d5ceb44e90622b7 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 14 Nov 2026 17:20:53 +0100 Subject: memory: improve MMIO allocation/mapping robustness --- kernel/kapi/memory.cpp | 79 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 23 deletions(-) (limited to 'kernel/kapi/memory.cpp') diff --git a/kernel/kapi/memory.cpp b/kernel/kapi/memory.cpp index ee8b45f0..65f9f319 100644 --- a/kernel/kapi/memory.cpp +++ b/kernel/kapi/memory.cpp @@ -7,6 +7,8 @@ #include #include +#include +#include #include #include @@ -66,10 +68,23 @@ namespace kapi::memory constinit bad_page_mapper bad_page_mapper::instance{}; auto constinit allocator = std::optional{}; auto constinit mmio_allocator = std::optional{}; - } // namespace - constinit auto static active_frame_allocator = static_cast(&bad_frame_allocator::instance); - constinit auto static active_page_mapper = static_cast(&bad_page_mapper::instance); + 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 & { @@ -155,43 +170,61 @@ namespace kapi::memory mmio_allocator.emplace(base, page_count); } - auto allocate_mmio_region(std::size_t page_count) -> mmio_region + auto allocate_mmio_region(std::size_t page_count) -> kstd::result { - auto region = mmio_allocator->allocate(page_count); - return {region, page_count}; + 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::observer_ptr + -> kstd::result> { - auto start_page = page::containing(region.first); + auto [start, count] = region; + + auto start_page = page::containing(start); auto start_frame = frame::containing(hw_base); flags |= page_mapper::flags::uncached | page_mapper::flags::supervisor_only | page_mapper::flags::global; - auto start = map(start_page, start_frame, flags); + auto first_mapped_page = map(start_page, start_frame, flags); - std::ranges::for_each(std::views::iota(1uz, region.second), [&](auto index) { - auto page = page::containing(region.first + index * page::size); - auto frame = frame::containing(hw_base + index * page::size); - map(page, 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 start; + return kstd::success(first_mapped_page); } auto release_mmio_region(mmio_region region) -> void { - for (auto page_index = 0uz; page_index < region.second; ++page_index) + if (!mmio_allocator) { - auto page_address = region.first + page_index * kapi::memory::page_size; - auto page = kapi::memory::page::containing(page_address); - - // 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 = kapi::memory::unmap(page); + 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); } -- cgit v1.2.3 From dc5edf2193c14b17939ba9afe9eef4a907da053f Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 14 Nov 2026 17:54:57 +0100 Subject: memory: harden MMIO mappings and update docs. --- kernel/kapi/memory.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'kernel/kapi/memory.cpp') diff --git a/kernel/kapi/memory.cpp b/kernel/kapi/memory.cpp index 65f9f319..83b32f0d 100644 --- a/kernel/kapi/memory.cpp +++ b/kernel/kapi/memory.cpp @@ -183,6 +183,13 @@ namespace kapi::memory 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); -- cgit v1.2.3 From 9a17b94c3cc3397152092da7f40a17682440fd78 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 14 Nov 2026 19:03:39 +0100 Subject: kapi/memory: carry hw_base offset forward in mapping --- kernel/kapi/memory.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'kernel/kapi/memory.cpp') diff --git a/kernel/kapi/memory.cpp b/kernel/kapi/memory.cpp index 83b32f0d..12e9e23f 100644 --- a/kernel/kapi/memory.cpp +++ b/kernel/kapi/memory.cpp @@ -194,6 +194,7 @@ namespace kapi::memory 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; @@ -217,7 +218,7 @@ namespace kapi::memory } } - return kstd::success(first_mapped_page); + return kstd::success(first_mapped_page + frame_offset); } auto release_mmio_region(mmio_region region) -> void -- cgit v1.2.3 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