diff options
| -rw-r--r-- | arch/x86_64/arch/drivers/cpu/lapic.cpp | 16 | ||||
| -rw-r--r-- | kapi/kapi/memory.hpp | 14 | ||||
| -rw-r--r-- | kernel/kapi/memory.cpp | 79 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci_express.cpp | 19 | ||||
| -rw-r--r-- | kernel/kernel/memory/mmio_allocator.cpp | 14 | ||||
| -rw-r--r-- | kernel/kernel/memory/mmio_allocator.hpp | 4 |
6 files changed, 104 insertions, 42 deletions
diff --git a/arch/x86_64/arch/drivers/cpu/lapic.cpp b/arch/x86_64/arch/drivers/cpu/lapic.cpp index 5cc62d4c..02d2f529 100644 --- a/arch/x86_64/arch/drivers/cpu/lapic.cpp +++ b/arch/x86_64/arch/drivers/cpu/lapic.cpp @@ -121,15 +121,23 @@ namespace arch::drivers::cpu if (!m_is_mapped) { auto const page_count = (mmio->size + kapi::memory::page_size - 1_B) / kapi::memory::page_size; - m_mapped_region = kapi::memory::allocate_mmio_region(page_count); + auto mmio_region = kapi::memory::allocate_mmio_region(page_count); - if (!kapi::memory::map_mmio_region(m_mapped_region, kapi::memory::physical_address{mmio->start}, - kapi::memory::page_mapper::flags::writable)) + if (!mmio_region) + { + return kstd::failure(mmio_region.error()); + } + + if (auto result = kapi::memory::map_mmio_region(*mmio_region, kapi::memory::physical_address{mmio->start}, + kapi::memory::page_mapper::flags::writable); + !result) { kstd::println(kstd::print_sink::stderr, "[ARCH:DRV] LAPIC {} MMIO mapping failed!", signature->hardware_id()); - return kstd::failure(make_error_code(kstd::errc::io_error)); + kapi::memory::release_mmio_region(*mmio_region); + return kstd::failure(result.error()); } + m_mapped_region = *mmio_region; m_is_mapped = true; } diff --git a/kapi/kapi/memory.hpp b/kapi/kapi/memory.hpp index 2ae8b91b..4cdd6ac1 100644 --- a/kapi/kapi/memory.hpp +++ b/kapi/kapi/memory.hpp @@ -10,6 +10,7 @@ #include <kapi/memory/page_mapper.hpp> // IWYU pragma: export #include <kstd/memory.hpp> +#include <kstd/result.hpp> #include <cstddef> #include <optional> @@ -94,8 +95,10 @@ namespace kapi::memory //! Allocate a Memory-mapped I/O region of the given size. //! //! @warning This function will panic if the MMIO system has not been initialized! + //! //! @param page_count The number of pages to allocate. - auto allocate_mmio_region(std::size_t page_count) -> mmio_region; + //! @return The newly allocated MMIO region on success, an error otherwise. + auto allocate_mmio_region(std::size_t page_count) -> kstd::result<mmio_region>; //! Map a region of Memory-mapped I/O address space to a given hardware address using the given flags. //! @@ -107,10 +110,15 @@ namespace kapi::memory //! @param region The region to map. //! @param hw_base The base of the hardware region. //! @param flags The flags to apply. + //! @return A result containing a pointer to the first byte of the mapped region on success, an error otherwise, in + //! which case any partial mappings have been rolled back. 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>>; - //! Release a Memory-mapped I/O region. + //! Release (unmap and free) a Memory-mapped I/O region. + //! + //! This function is tolerant of partially mapped regions and will unmap any mapped pages within the region before + //! releasing the MMIO region. //! //! @warning This function will panic if the MMIO system has not been initialized! //! @param region The region to release. 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: |
