From 27a58a338ad2f61a063df02488fc497405afc492 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 12 Nov 2026 22:46:57 +0100 Subject: dev/pcie: implement initial detection --- arch/x86_64/CMakeLists.txt | 1 + arch/x86_64/kapi/devices/pci_express.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 arch/x86_64/kapi/devices/pci_express.cpp (limited to 'arch') diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt index 59df016e..b54fce95 100644 --- a/arch/x86_64/CMakeLists.txt +++ b/arch/x86_64/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources("x86_64" PRIVATE "kapi/cio.cpp" "kapi/cpu.cpp" "kapi/devices.cpp" + "kapi/devices/pci_express.cpp" "kapi/interrupts.cpp" "kapi/memory.cpp" "kapi/system.cpp" diff --git a/arch/x86_64/kapi/devices/pci_express.cpp b/arch/x86_64/kapi/devices/pci_express.cpp new file mode 100644 index 00000000..ca566bf0 --- /dev/null +++ b/arch/x86_64/kapi/devices/pci_express.cpp @@ -0,0 +1,30 @@ +#include + +#include + +#include + +#include +#include + +#include + +namespace kapi::devices +{ + + auto get_pci_express_segment_descriptors() -> kstd::vector + { + auto mcfg_table = kapi::acpi::get_table<::acpi::table_signature_v<::acpi::mcfg>>(); + if (!mcfg_table) + { + return {}; + } + + return kstd::vector{kstd::from_range, *mcfg_table | std::views::transform([](auto & entry) { + return pcie_segment_descriptor{.base_address = entry.base_address(), + .first_bus = entry.start_bus_number(), + .last_bus = entry.end_bus_number()}; + })}; + } + +} // namespace kapi::devices \ No newline at end of file -- cgit v1.2.3 From c0e5d076cfdeddde0a83197e2ebcee2df17934f8 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 13 Nov 2026 07:09:06 +0100 Subject: dev/pcie: extend segment descriptor --- arch/x86_64/kapi/devices/pci_express.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'arch') diff --git a/arch/x86_64/kapi/devices/pci_express.cpp b/arch/x86_64/kapi/devices/pci_express.cpp index ca566bf0..2fac3185 100644 --- a/arch/x86_64/kapi/devices/pci_express.cpp +++ b/arch/x86_64/kapi/devices/pci_express.cpp @@ -1,12 +1,15 @@ #include #include +#include #include #include #include +#include +#include #include namespace kapi::devices @@ -20,10 +23,16 @@ namespace kapi::devices return {}; } - return kstd::vector{kstd::from_range, *mcfg_table | std::views::transform([](auto & entry) { - return pcie_segment_descriptor{.base_address = entry.base_address(), + return kstd::vector{kstd::from_range, *mcfg_table | std::views::transform([number = 0](auto & entry) mutable { + auto base_address = memory::physical_address{entry.base_address()}; + auto bus_count = + static_cast(entry.end_bus_number() - entry.start_bus_number() + 1); + auto group_number = static_cast(number++); + + return pcie_segment_descriptor{.base_address = base_address, + .group_number = group_number, .first_bus = entry.start_bus_number(), - .last_bus = entry.end_bus_number()}; + .bus_count = bus_count}; })}; } -- cgit v1.2.3 From 3ee3b357cc8ba6ef59cb045bcaa050c4cb9d698d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 13 Nov 2026 08:06:55 +0100 Subject: x86_64/pcie: fix bus group number --- arch/x86_64/kapi/devices/pci_express.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch') diff --git a/arch/x86_64/kapi/devices/pci_express.cpp b/arch/x86_64/kapi/devices/pci_express.cpp index 2fac3185..a88931b9 100644 --- a/arch/x86_64/kapi/devices/pci_express.cpp +++ b/arch/x86_64/kapi/devices/pci_express.cpp @@ -23,11 +23,11 @@ namespace kapi::devices return {}; } - return kstd::vector{kstd::from_range, *mcfg_table | std::views::transform([number = 0](auto & entry) mutable { + return kstd::vector{kstd::from_range, *mcfg_table | std::views::transform([](auto & entry) { auto base_address = memory::physical_address{entry.base_address()}; auto bus_count = static_cast(entry.end_bus_number() - entry.start_bus_number() + 1); - auto group_number = static_cast(number++); + auto group_number = static_cast(entry.segment_group_number()); return pcie_segment_descriptor{.base_address = base_address, .group_number = group_number, -- cgit v1.2.3 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. --- arch/x86_64/arch/memory/higher_half_mapper.cpp | 36 ++++++++++++++++++-------- arch/x86_64/arch/memory/higher_half_mapper.hpp | 5 ++-- 2 files changed, 28 insertions(+), 13 deletions(-) (limited to 'arch') diff --git a/arch/x86_64/arch/memory/higher_half_mapper.cpp b/arch/x86_64/arch/memory/higher_half_mapper.cpp index 8a069c86..9019e25b 100644 --- a/arch/x86_64/arch/memory/higher_half_mapper.cpp +++ b/arch/x86_64/arch/memory/higher_half_mapper.cpp @@ -8,10 +8,10 @@ #include -#include #include #include #include +#include #include #include @@ -44,15 +44,17 @@ namespace arch::memory return static_cast>(page.start_address()); } - auto higher_half_mapper::unmap(kapi::memory::page page) -> void + auto higher_half_mapper::unmap(kapi::memory::page page) -> kapi::memory::frame { - if (!try_unmap(page)) + auto frame = try_unmap(page); + if (!frame) { kapi::system::panic("[ARCH:MEM] Tried to unmap a page that is not mapped!"); } + return *frame; } - auto higher_half_mapper::try_unmap(kapi::memory::page page) noexcept -> bool + auto higher_half_mapper::try_unmap(kapi::memory::page page) noexcept -> std::optional { auto table_path = std::array, std::size_t>, 4>{}; table_path[0] = std::pair{m_root, pml_index(4, page)}; @@ -64,15 +66,20 @@ namespace arch::memory if (!entry.present()) { - return false; + return std::nullopt; } auto next_table = to_higher_half_pointer(entry.frame()->start_address()); - auto next_index = pml_index(4 - level - 1, page); - table_path[4 - level - 1] = std::pair{next_table, next_index}; + auto next_index = pml_index(level - 1, page); + table_path[4 - (level - 1)] = std::pair{next_table, next_index}; } - std::ranges::for_each(std::views::reverse(table_path), [previous_was_empty = true](auto & step) mutable { + auto release_path = std::views::reverse(table_path); + auto previous_was_empty = true; + auto unmapped_frame = std::optional{}; + + for (auto & step : release_path) + { auto [table, index] = step; auto & entry = (*table)[index]; auto frame = entry.frame(); @@ -81,11 +88,18 @@ namespace arch::memory { entry.clear(); previous_was_empty = table->empty(); - kapi::memory::get_frame_allocator().release(*frame); + if (!unmapped_frame) + { + unmapped_frame = *frame; + } + else + { + kapi::memory::get_frame_allocator().release(*frame); + } } - }); + } - return true; + return unmapped_frame; } auto higher_half_mapper::get_or_create_page_table(kapi::memory::page page) noexcept -> kstd::observer_ptr diff --git a/arch/x86_64/arch/memory/higher_half_mapper.hpp b/arch/x86_64/arch/memory/higher_half_mapper.hpp index 45ff100a..f46e6f3c 100644 --- a/arch/x86_64/arch/memory/higher_half_mapper.hpp +++ b/arch/x86_64/arch/memory/higher_half_mapper.hpp @@ -8,6 +8,7 @@ #include #include +#include namespace arch::memory { @@ -24,10 +25,10 @@ namespace arch::memory auto map(kapi::memory::page page, kapi::memory::frame frame, flags flags) -> kstd::observer_ptr override; //! @copydoc kapi::memory::page_mapper::unmap - auto unmap(kapi::memory::page page) -> void override; + auto unmap(kapi::memory::page page) -> kapi::memory::frame override; //! @copydoc kapi::memory::page_mapper::try_unmap - auto try_unmap(kapi::memory::page page) noexcept -> bool override; + auto try_unmap(kapi::memory::page page) noexcept -> std::optional override; private: //! Try to retrieve the the PML1 responsible for mapping this page, creating one if necessary. -- cgit v1.2.3 From 700123106b48f23fd083cfbc0f8a6d9d7364c1cb Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 13 Nov 2026 17:57:23 +0100 Subject: x86_64/memory: flush TLB on page unmap --- arch/x86_64/arch/memory/higher_half_mapper.cpp | 2 ++ arch/x86_64/arch/memory/mmu.hpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'arch') diff --git a/arch/x86_64/arch/memory/higher_half_mapper.cpp b/arch/x86_64/arch/memory/higher_half_mapper.cpp index 9019e25b..ee11d50a 100644 --- a/arch/x86_64/arch/memory/higher_half_mapper.cpp +++ b/arch/x86_64/arch/memory/higher_half_mapper.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -99,6 +100,7 @@ namespace arch::memory } } + tlb_flush(page.start_address()); return unmapped_frame; } diff --git a/arch/x86_64/arch/memory/mmu.hpp b/arch/x86_64/arch/memory/mmu.hpp index 9bd95b43..934aa63f 100644 --- a/arch/x86_64/arch/memory/mmu.hpp +++ b/arch/x86_64/arch/memory/mmu.hpp @@ -7,7 +7,7 @@ namespace arch::memory { //! Invalidate a translation lookaside buffer (TLB) entry for a given address //! - //! @param address The physical address for which to flush the TLB entries + //! @param address The linear address for which to flush the TLB entries auto tlb_flush(kapi::memory::linear_address address) -> void; //! Invalidate all translation lookaside buffer (TLB) entries -- cgit v1.2.3 From 718d7f46a22220a7c4a53195dadc7291431ff912 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 13 Nov 2026 18:18:07 +0100 Subject: x86_64/memory: check leaf entry before unmapping --- arch/x86_64/arch/memory/higher_half_mapper.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'arch') diff --git a/arch/x86_64/arch/memory/higher_half_mapper.cpp b/arch/x86_64/arch/memory/higher_half_mapper.cpp index ee11d50a..814cc991 100644 --- a/arch/x86_64/arch/memory/higher_half_mapper.cpp +++ b/arch/x86_64/arch/memory/higher_half_mapper.cpp @@ -75,6 +75,14 @@ namespace arch::memory table_path[4 - (level - 1)] = std::pair{next_table, next_index}; } + auto [leaf_table, leaf_index] = table_path.back(); + if (!(*leaf_table)[leaf_index].present()) + { + return std::nullopt; + } + + // If execution reaches this point, all page table entries leading to, and including, the leaf entry are present. It + // is therefore safe to access their `frame` value without additional checks. auto release_path = std::views::reverse(table_path); auto previous_was_empty = true; auto unmapped_frame = std::optional{}; -- cgit v1.2.3 From 36a2fd5eb75e87bfe6f6f6c775ebac3d80902110 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 14 Nov 2026 15:59:11 +0100 Subject: x86_64/memory: panic on huge page mappings The x86_64 platform's higher half memory mapper currently has not support for huge page mappings. However, the previous implementation did not explicitly exclude huge page mappings, instead potentially corrupting the contents of huge pages by treating them as regular pages. The kernel now panics whenever an attempt is made to map a page inside a region covered by a huge page, or when trying to unmap a huge page or a range covered by a huge page. --- arch/x86_64/arch/memory/higher_half_mapper.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'arch') diff --git a/arch/x86_64/arch/memory/higher_half_mapper.cpp b/arch/x86_64/arch/memory/higher_half_mapper.cpp index 814cc991..cd0f943a 100644 --- a/arch/x86_64/arch/memory/higher_half_mapper.cpp +++ b/arch/x86_64/arch/memory/higher_half_mapper.cpp @@ -70,6 +70,11 @@ namespace arch::memory return std::nullopt; } + if (entry.huge()) + { + kapi::system::panic("[ARCH:MEM] Tried to unmap a huge page, which is not supported!"); + } + auto next_table = to_higher_half_pointer(entry.frame()->start_address()); auto next_index = pml_index(level - 1, page); table_path[4 - (level - 1)] = std::pair{next_table, next_index}; @@ -121,6 +126,11 @@ namespace arch::memory auto index = pml_index(level, page); auto & entry = (*table)[index]; + if (entry.huge()) + { + kapi::system::panic("[ARCH:MEM] Tried map an entry in a huge page, which is not supported!"); + } + if (!entry.present()) { auto table_frame = kapi::memory::allocate_frame(); -- 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 --- arch/x86_64/arch/drivers/cpu/lapic.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'arch') 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; } -- 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. --- arch/x86_64/arch/memory/mmu.hpp | 2 ++ arch/x86_64/arch/memory/page_table.hpp | 34 ++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) (limited to 'arch') diff --git a/arch/x86_64/arch/memory/mmu.hpp b/arch/x86_64/arch/memory/mmu.hpp index 934aa63f..f824ba53 100644 --- a/arch/x86_64/arch/memory/mmu.hpp +++ b/arch/x86_64/arch/memory/mmu.hpp @@ -11,6 +11,8 @@ namespace arch::memory auto tlb_flush(kapi::memory::linear_address address) -> void; //! Invalidate all translation lookaside buffer (TLB) entries + //! + //! @note Once CR4.PGE is set, this function will no longer flush TLB entries for global pages. auto tlb_flush_all() -> void; } // namespace arch::memory diff --git a/arch/x86_64/arch/memory/page_table.hpp b/arch/x86_64/arch/memory/page_table.hpp index 0c0f1a77..c34f72e0 100644 --- a/arch/x86_64/arch/memory/page_table.hpp +++ b/arch/x86_64/arch/memory/page_table.hpp @@ -40,16 +40,30 @@ namespace arch::memory enum struct flags : std::uint64_t { empty = 0, - present = 1uz << 0, //!< The page is mapped. - writable = 1uz << 1, //!< The page is writable. - user_accessible = 1uz << 2, //!< The page is accessible in user mode. - write_through = 1uz << 3, //!< Any writes to the page must immediately hit memory. - disable_cache = 1uz << 4, //!< Any writes to the page must never be cached. - accessed = 1uz << 5, //!< The page was accessed. - dirty = 1uz << 6, //!< The page was written to. - huge_page = 1uz << 7, //!< The page is huge. - global = 1uz << 8, //!< The TLB entry for this page must not be flushed on context switches. - no_execute = 1uz << 63, //!< The data in this page must not be executed. + //! The page is mapped. + present = 1uz << 0, + //! The page is writable. + writable = 1uz << 1, + //! The page is accessible in user mode. + user_accessible = 1uz << 2, + //! Any writes to the page must immediately hit memory. + write_through = 1uz << 3, + //! Any writes to the page must never be cached. + disable_cache = 1uz << 4, + //! The page was accessed. + accessed = 1uz << 5, + //! The page was written to. + dirty = 1uz << 6, + //! The page is huge. + huge_page = 1uz << 7, + //! The TLB entry for this page must not be flushed on context switches. + //! + //! @note This flag is currently not honored by this implementation, since the CR4.PGE bit is not set. As a + //! consequence, any CR3 reload or a call to arch::memory::tlb_flush_all will still flush this entry on context + //! switches. + global = 1uz << 8, + //! The data in this page must not be executed. + no_execute = 1uz << 63, }; //! Construct an empty entry. -- 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. --- arch/x86_64/arch/drivers/cpu/lapic.cpp | 30 ++++++++++-------------------- arch/x86_64/arch/drivers/cpu/lapic.hpp | 4 ++-- 2 files changed, 12 insertions(+), 22 deletions(-) (limited to 'arch') diff --git a/arch/x86_64/arch/drivers/cpu/lapic.cpp b/arch/x86_64/arch/drivers/cpu/lapic.cpp index 02d2f529..996b99c5 100644 --- a/arch/x86_64/arch/drivers/cpu/lapic.cpp +++ b/arch/x86_64/arch/drivers/cpu/lapic.cpp @@ -118,27 +118,20 @@ namespace arch::drivers::cpu auto guard = kstd::lock_guard{m_lock}; - if (!m_is_mapped) + if (!m_mmio_window) { - auto const page_count = (mmio->size + kapi::memory::page_size - 1_B) / kapi::memory::page_size; - auto mmio_region = kapi::memory::allocate_mmio_region(page_count); + auto const mmio_size = mmio->size; + auto const mmio_base = kapi::memory::physical_address{mmio->start}; + auto mmio_window = + kapi::memory::mmio_window::map(mmio_base, mmio_size, kapi::memory::mmio_window::access::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) + if (!mmio_window) { kstd::println(kstd::print_sink::stderr, "[ARCH:DRV] LAPIC {} MMIO mapping failed!", signature->hardware_id()); - kapi::memory::release_mmio_region(*mmio_region); - return kstd::failure(result.error()); + return kstd::failure(mmio_window.error()); } - m_mapped_region = *mmio_region; - m_is_mapped = true; + m_mmio_window = std::move(*mmio_window); } if (signature->is_bsp()) @@ -170,15 +163,12 @@ namespace arch::drivers::cpu auto lapic::read_register(registers id) const -> std::uint32_t { - auto reg = - static_cast>(m_mapped_region.first + std::to_underlying(id)); - return *reg; + return m_mmio_window->read(kstd::bytes(std::to_underlying(id))); } auto lapic::write_register(registers id, std::uint32_t value) -> void { - auto reg = static_cast>(m_mapped_region.first + std::to_underlying(id)); - *reg = value; + m_mmio_window->write(kstd::bytes(std::to_underlying(id)), value); } auto lapic::query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr diff --git a/arch/x86_64/arch/drivers/cpu/lapic.hpp b/arch/x86_64/arch/drivers/cpu/lapic.hpp index 52533d9a..e0b3bb71 100644 --- a/arch/x86_64/arch/drivers/cpu/lapic.hpp +++ b/arch/x86_64/arch/drivers/cpu/lapic.hpp @@ -13,6 +13,7 @@ #include #include +#include #include namespace arch::drivers::cpu @@ -37,8 +38,7 @@ namespace arch::drivers::cpu auto write_register(registers id, std::uint32_t value) -> void; mutable kapi::tracked_mutex m_lock{}; - kapi::memory::mmio_region m_mapped_region{}; - bool m_is_mapped{}; + std::optional m_mmio_window{}; }; } // namespace arch::drivers::cpu -- cgit v1.2.3