aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-11-14 17:20:53 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-11-22 14:20:36 +0100
commit1071297ee44d5102feb92b7a5d5ceb44e90622b7 (patch)
treee9a255b01d2ad15449d74a1b1213979373b700da /kernel
parent36a2fd5eb75e87bfe6f6f6c775ebac3d80902110 (diff)
downloadkernel-1071297ee44d5102feb92b7a5d5ceb44e90622b7.tar.xz
kernel-1071297ee44d5102feb92b7a5d5ceb44e90622b7.zip
memory: improve MMIO allocation/mapping robustness
Diffstat (limited to 'kernel')
-rw-r--r--kernel/kapi/memory.cpp79
-rw-r--r--kernel/kernel/devices/pci_express.cpp19
-rw-r--r--kernel/kernel/memory/mmio_allocator.cpp14
-rw-r--r--kernel/kernel/memory/mmio_allocator.hpp4
4 files changed, 81 insertions, 35 deletions
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 <kstd/memory.hpp>
#include <kstd/print.hpp>
+#include <kstd/result.hpp>
+#include <kstd/system_error.hpp>
#include <kstd/units.hpp>
#include <algorithm>
@@ -66,10 +68,23 @@ namespace kapi::memory
constinit bad_page_mapper bad_page_mapper::instance{};
auto constinit allocator = std::optional<kernel::memory::bitmap_frame_allocator>{};
auto constinit mmio_allocator = std::optional<kernel::memory::mmio_allocator>{};
- } // namespace
- constinit auto static active_frame_allocator = static_cast<frame_allocator *>(&bad_frame_allocator::instance);
- constinit auto static active_page_mapper = static_cast<page_mapper *>(&bad_page_mapper::instance);
+ constinit auto active_frame_allocator = static_cast<frame_allocator *>(&bad_frame_allocator::instance);
+ constinit auto active_page_mapper = static_cast<page_mapper *>(&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<mmio_region>
{
- 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<std::byte>
+ -> kstd::result<kstd::observer_ptr<std::byte>>
{
- 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);
}
diff --git a/kernel/kernel/devices/pci_express.cpp b/kernel/kernel/devices/pci_express.cpp
index e2c13f08..4bd09306 100644
--- a/kernel/kernel/devices/pci_express.cpp
+++ b/kernel/kernel/devices/pci_express.cpp
@@ -51,14 +51,27 @@ namespace kernel::devices
auto bus_pages = devices_per_bus * functions_per_device;
auto bus_mmio_region = kapi::memory::allocate_mmio_region(bus_pages);
+ if (!bus_mmio_region)
+ {
+ kstd::println("[OS:DEV] Failed to allocate MMIO region for bus {}", bus);
+ continue;
+ }
+
auto bus_address = kapi::memory::physical_address{segment_base + (bus << bus_address_shift)};
- auto bus_config = kapi::memory::map_mmio_region(bus_mmio_region, bus_address);
+ auto bus_config = kapi::memory::map_mmio_region(*bus_mmio_region, bus_address);
+
+ if (!bus_config)
+ {
+ kstd::println("[OS:DEV] Failed to map MMIO region for bus {}", bus);
+ kapi::memory::release_mmio_region(*bus_mmio_region);
+ continue;
+ }
auto has_devices = false;
for (auto device = 0; device < devices_per_bus; ++device)
{
- auto device_base = bus_config + (device << device_address_shift);
+ auto device_base = *bus_config + (device << device_address_shift);
auto device_config = reinterpret_pointer_cast<pcie_config_space_header>(device_base);
if (device_config->vendor_id == invalid_vendor_id)
@@ -89,7 +102,7 @@ namespace kernel::devices
if (!has_devices)
{
- kapi::memory::release_mmio_region(bus_mmio_region);
+ kapi::memory::release_mmio_region(*bus_mmio_region);
}
}
}
diff --git a/kernel/kernel/memory/mmio_allocator.cpp b/kernel/kernel/memory/mmio_allocator.cpp
index 17f436f8..49cb31e9 100644
--- a/kernel/kernel/memory/mmio_allocator.cpp
+++ b/kernel/kernel/memory/mmio_allocator.cpp
@@ -1,9 +1,10 @@
#include <kernel/memory/mmio_allocator.hpp>
#include <kapi/memory.hpp>
-#include <kapi/system.hpp>
#include <kstd/allocator.hpp>
+#include <kstd/result.hpp>
+#include <kstd/system_error.hpp>
#include <cstddef>
#include <memory>
@@ -16,11 +17,11 @@ namespace kernel::memory
: m_head{make_node(base, pages, nullptr, nullptr, true)}
{}
- auto mmio_allocator::allocate(std::size_t count) -> kapi::memory::linear_address
+ auto mmio_allocator::allocate(std::size_t count) -> kstd::result<kapi::memory::linear_address>
{
- if (count == 0 || !m_head)
+ if (count == 0)
{
- return {};
+ return kstd::failure(make_error_code(kstd::errc::invalid_argument));
}
auto current = m_head;
@@ -42,13 +43,12 @@ namespace kernel::memory
}
current->is_free = false;
- return current->base;
+ return kstd::success(current->base);
}
current = current->next;
}
- kapi::system::panic("[OS:MEM] MMIO alloctor out of memory!");
- return {};
+ return kstd::failure(make_error_code(kstd::errc::not_enough_memory));
}
auto mmio_allocator::release(kapi::memory::linear_address base) -> void
diff --git a/kernel/kernel/memory/mmio_allocator.hpp b/kernel/kernel/memory/mmio_allocator.hpp
index 862188fa..268ca40b 100644
--- a/kernel/kernel/memory/mmio_allocator.hpp
+++ b/kernel/kernel/memory/mmio_allocator.hpp
@@ -4,7 +4,7 @@
#include <kapi/memory.hpp>
#include <kstd/allocator.hpp>
-#include <kstd/memory.hpp>
+#include <kstd/result.hpp>
#include <cstddef>
@@ -15,7 +15,7 @@ namespace kernel::memory
{
mmio_allocator(kapi::memory::linear_address base, std::size_t pages);
- [[nodiscard]] auto allocate(std::size_t page_count) -> kapi::memory::linear_address;
+ [[nodiscard]] auto allocate(std::size_t page_count) -> kstd::result<kapi::memory::linear_address>;
auto release(kapi::memory::linear_address base) -> void;
private: