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 --- kapi/kapi/devices.hpp | 1 + kapi/kapi/devices/pci_express.hpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 kapi/kapi/devices/pci_express.hpp (limited to 'kapi') diff --git a/kapi/kapi/devices.hpp b/kapi/kapi/devices.hpp index c7de1830..d97e88b6 100644 --- a/kapi/kapi/devices.hpp +++ b/kapi/kapi/devices.hpp @@ -9,6 +9,7 @@ #include // IWYU pragma: export #include // IWYU pragma: export #include // IWYU pragma: export +#include // IWYU pragma: export #include // IWYU pragma: export #include // IWYU pragma: export diff --git a/kapi/kapi/devices/pci_express.hpp b/kapi/kapi/devices/pci_express.hpp new file mode 100644 index 00000000..6acb279f --- /dev/null +++ b/kapi/kapi/devices/pci_express.hpp @@ -0,0 +1,32 @@ +#ifndef TEACHOS_KAPI_DEVICES_PCI_EXPRESS_HPP +#define TEACHOS_KAPI_DEVICES_PCI_EXPRESS_HPP + +// IWYU pragma: private, include + +#include + +#include + +namespace kapi::devices +{ + + struct pcie_segment_descriptor + { + std::uintptr_t base_address; + std::uint8_t first_bus; + std::uint8_t last_bus; + }; + + //! @name Platform-defined API + //! @{ + + //! Get a list of all PCI Express segments in the system. + //! + //! @return a list of PCI Express segment descriptors. + auto get_pci_express_segment_descriptors() -> kstd::vector; + + //! @} + +} // namespace kapi::devices + +#endif \ 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 --- kapi/kapi/devices/pci_express.hpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'kapi') diff --git a/kapi/kapi/devices/pci_express.hpp b/kapi/kapi/devices/pci_express.hpp index 6acb279f..91cb65df 100644 --- a/kapi/kapi/devices/pci_express.hpp +++ b/kapi/kapi/devices/pci_express.hpp @@ -3,18 +3,27 @@ // IWYU pragma: private, include +#include + #include +#include #include namespace kapi::devices { + //! A descriptor for a single PCI Express segment. struct pcie_segment_descriptor { - std::uintptr_t base_address; - std::uint8_t first_bus; - std::uint8_t last_bus; + //! The physical base address, in MMIO-space, of the segment's configuration space. + memory::physical_address base_address{}; + //! The group number of the segment. + std::uint16_t group_number{}; + //! The first bus number of the segment. + std::uint8_t first_bus{}; + //! The number of buses in the segment. + std::size_t bus_count{}; }; //! @name Platform-defined API -- 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. --- kapi/kapi/memory.hpp | 3 ++- kapi/kapi/memory/page_mapper.hpp | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'kapi') diff --git a/kapi/kapi/memory.hpp b/kapi/kapi/memory.hpp index 4b14b553..2ae8b91b 100644 --- a/kapi/kapi/memory.hpp +++ b/kapi/kapi/memory.hpp @@ -82,7 +82,8 @@ namespace kapi::memory //! @warning This function will panic if no page mapper has been registered, or the page is not mapped. //! //! @param page The page to unmap - auto unmap(page page) -> void; + //! @return The frame the page was previously mapped to. + [[nodiscard]] auto unmap(page page) -> frame; //! Initialize the Memory-mapped I/O region system. //! diff --git a/kapi/kapi/memory/page_mapper.hpp b/kapi/kapi/memory/page_mapper.hpp index 5ac0210f..4a4827b6 100644 --- a/kapi/kapi/memory/page_mapper.hpp +++ b/kapi/kapi/memory/page_mapper.hpp @@ -11,6 +11,7 @@ #include #include +#include #include namespace kapi::memory @@ -54,13 +55,14 @@ namespace kapi::memory //! terminate execution via a kernel panic. //! //! @param page The page to unmap. - virtual auto unmap(page page) -> void = 0; + //! @return The frame the page was previously mapped to. + virtual auto unmap(page page) -> frame = 0; //! Try to unmap the given page. //! //! @param page The page to unmap - //! @return true iff. the page was successfully unmapped, false otherwise. - virtual auto try_unmap(page page) noexcept -> bool = 0; + //! @return The frame that was previously mapped to the page, if any. + virtual auto try_unmap(page page) noexcept -> std::optional = 0; //! @qualifier kernel-defined //! Map a page into a given frame, applyint the given 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 --- kapi/kapi/memory.hpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'kapi') 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 // IWYU pragma: export #include +#include #include #include @@ -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; //! 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; + -> kstd::result>; - //! 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. -- 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. --- kapi/kapi/memory.hpp | 7 +++++-- kapi/kapi/memory/page_mapper.hpp | 17 ++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'kapi') diff --git a/kapi/kapi/memory.hpp b/kapi/kapi/memory.hpp index 4cdd6ac1..7979c7e1 100644 --- a/kapi/kapi/memory.hpp +++ b/kapi/kapi/memory.hpp @@ -103,9 +103,12 @@ namespace kapi::memory //! Map a region of Memory-mapped I/O address space to a given hardware address using the given flags. //! //! @warning This function will panic if no page mapper has been registered, or the page has already been mapped. - //! This function will not ensure that the frame is not already in use. //! - //! This function will always set the @p uncached flag. + //! @warning This function will not ensure that the frame is not already in use. + //! + //! @warning This function will panic if an attempt is made to map an MMIO region as executable. + //! + //! This function will always set the @p uncached, @p global, and @p supervisor_only flags. //! //! @param region The region to map. //! @param hw_base The base of the hardware region. diff --git a/kapi/kapi/memory/page_mapper.hpp b/kapi/kapi/memory/page_mapper.hpp index 4a4827b6..889ef052 100644 --- a/kapi/kapi/memory/page_mapper.hpp +++ b/kapi/kapi/memory/page_mapper.hpp @@ -32,11 +32,18 @@ namespace kapi::memory enum struct flags : std::uint64_t { empty, - writable = 1 << 0, //! The page is writable. - executable = 1 << 1, //! The page contains executable instructions. - uncached = 1 << 2, //! The page contents must not be cached. - supervisor_only = 1 << 3, //! The page is only accessible in supervisor mode. - global = 1 << 4, //! The page translation persists across context switches. + //! The page is writable. + writable = 1 << 0, + //! The page contains executable instructions. + executable = 1 << 1, + //! The page contents must not be cached. + uncached = 1 << 2, + //! The page is only accessible in supervisor mode. + supervisor_only = 1 << 3, + //! The page translation persists across context switches. + //! + //! @note A given platform may not honor this flag. + global = 1 << 4, }; virtual ~page_mapper() = default; -- 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 --- kapi/kapi/memory.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'kapi') diff --git a/kapi/kapi/memory.hpp b/kapi/kapi/memory.hpp index 7979c7e1..ece85203 100644 --- a/kapi/kapi/memory.hpp +++ b/kapi/kapi/memory.hpp @@ -110,11 +110,12 @@ namespace kapi::memory //! //! This function will always set the @p uncached, @p global, and @p supervisor_only flags. //! - //! @param region The region to map. + //! @param region The region to map. This region must cover the in-page offset of hw_base plus the range to be + //! accessed. //! @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. + //! @return A pointer into the mapped region, including any frame offset, 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::result>; -- 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. --- kapi/kapi/memory.hpp | 46 +--------- kapi/kapi/memory/mmio.hpp | 187 ++++++++++++++++++++++++++++++++++++++ kapi/kapi/test_support/memory.hpp | 11 +++ 3 files changed, 199 insertions(+), 45 deletions(-) create mode 100644 kapi/kapi/memory/mmio.hpp create mode 100644 kapi/kapi/test_support/memory.hpp (limited to 'kapi') diff --git a/kapi/kapi/memory.hpp b/kapi/kapi/memory.hpp index ece85203..2abad38e 100644 --- a/kapi/kapi/memory.hpp +++ b/kapi/kapi/memory.hpp @@ -6,11 +6,11 @@ #include // IWYU pragma: export #include // IWYU pragma: export #include // IWYU pragma: export +#include // IWYU pragma: export #include // IWYU pragma: export #include // IWYU pragma: export #include -#include #include #include @@ -19,8 +19,6 @@ namespace kapi::memory { - using mmio_region = std::pair; - //! @addtogroup kapi-memory-kernel-defined //! @{ @@ -86,48 +84,6 @@ namespace kapi::memory //! @return The frame the page was previously mapped to. [[nodiscard]] auto unmap(page page) -> frame; - //! Initialize the Memory-mapped I/O region system. - //! - //! @param base The base address for the MMIO region. - //! @param page_count The number of pages the MMIO region is spans. - auto init_mmio(linear_address base, std::size_t page_count) -> void; - - //! 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. - //! @return The newly allocated MMIO region on success, an error otherwise. - auto allocate_mmio_region(std::size_t page_count) -> kstd::result; - - //! Map a region of Memory-mapped I/O address space to a given hardware address using the given flags. - //! - //! @warning This function will panic if no page mapper has been registered, or the page has already been mapped. - //! - //! @warning This function will not ensure that the frame is not already in use. - //! - //! @warning This function will panic if an attempt is made to map an MMIO region as executable. - //! - //! This function will always set the @p uncached, @p global, and @p supervisor_only flags. - //! - //! @param region The region to map. This region must cover the in-page offset of hw_base plus the range to be - //! accessed. - //! @param hw_base The base of the hardware region. - //! @param flags The flags to apply. - //! @return A pointer into the mapped region, including any frame offset, 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::result>; - - //! 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. - auto release_mmio_region(mmio_region region) -> void; - //! @} //! @addtogroup kapi-memory-platform-defined diff --git a/kapi/kapi/memory/mmio.hpp b/kapi/kapi/memory/mmio.hpp new file mode 100644 index 00000000..f1a7c6dc --- /dev/null +++ b/kapi/kapi/memory/mmio.hpp @@ -0,0 +1,187 @@ +#ifndef TEACHOS_KAPI_MEMORY_MMIO_HPP +#define TEACHOS_KAPI_MEMORY_MMIO_HPP + +// IWYU pragma: private, include + +#include + +#include +#include +#include + +#include +#include + +namespace kapi::memory +{ + + //! This concept models the requirements for a type to be used as a MMIO datum. + //! + //! A valid MMIO datum type must be an unsigned integral type, excluding bool, and must have a size of 1, 2, 4, or 8 + //! bytes. + template + concept mmio_datum = requires { + requires std::unsigned_integral; + requires(!std::same_as); + requires(sizeof(ValueType) == 1 || sizeof(ValueType) == 2 || sizeof(ValueType) == 4 || sizeof(ValueType) == 8); + }; + + //! @addtogroup kapi-memory-kernel-defined + //! @{ + + //! A window into memory-mapped I/O (MMIO) space. + //! + //! This structure represents a contiguous region of MMIO space that can be accessed according to the specified access + //! mode. The only permitted operations are read or write operations that access a single MMIO datum (see mmio_datum) + //! at a time. + //! + //! This type is move-only. A moved-from window is considered to have a size of zero and a base of null. All accesses + //! to a moved-from window are invalid and will panic. Destruction of a moved-from window does not change any mappings + //! or allocations. + struct mmio_window + { + //! The access mode for the MMIO window. + enum struct access : bool + { + //! The window can only be read from. + read_only, + //! The window can read from and written to. + writable, + }; + + //! Create a new MMIO window with the given properties. + //! + //! This function creates a new MMIO window of the specified size for the specified hardware MMIO address. The + //! mapping will always be created with the @p uncached, @p global, and @p supervisor_only page flags set. Depending + //! on the value of @p mode, the @p writable page flag will be set or not. The mapping covers every page containing + //! the requested range; accesses are nevertheless bounds-checked against @p size. All reads and writes are offset + //! from the page-aligned base address according to the in-page offset of the hardware base address. + //! + //! @warning This function will panic if the MMIO subsystem has not been initialized. + //! + //! @param hw_base The physical base address of the MMIO region. + //! @param size The size of this MMIO window. The size must not be zero. + //! @param mode The access mode for the MMIO region. + //! @return A new MMIO window on success, an error otherwise. + [[nodiscard]] auto static map(physical_address hw_base, kstd::bytes size, access mode) -> kstd::result; + + mmio_window(mmio_window const &) = delete; + + //! Move-construct a new MMIO window from an existing one. + //! + //! The moved-from window will be left with a zero-sized state and a base of null. All accesses to the moved-from + //! window are invalid and will panic. + mmio_window(mmio_window && other) noexcept; + + //! Destroy this MMIO window and release its resources. + //! + //! This will unmap the MMIO window and release any associated resources. If the window has been moved from, this + //! operation does nothing. + ~mmio_window(); + + auto operator=(mmio_window const &) -> mmio_window & = delete; + + //! Move-assign an existing MMIO window to this one. + //! + //! The moved-from window will be left with a zero-sized state and a base of null. All accesses to the moved-from + //! window are invalid and will panic. If this window has been previously mapped, it will be unmapped and its + //! resources released. + //! + //! Assigning a window to itself is safe and has no effect. + auto operator=(mmio_window && other) noexcept -> mmio_window &; + + //! Read a value, at a given offset, from this MMIO window. + //! + //! @warning This function will panic if the offset is out of bounds. + //! + //! @warning This function will panic if the operation would result in an unaligned access. The check is performed + //! against the absolute address (including the in-page offset). + //! + //! @warning This function will panic if this window has been moved from. + //! + //! @tparam ValueType The type of value to read from the MMIO window. + //! @param offset The offset within the MMIO window to read from. + //! @return The value read from the MMIO window. + template + [[nodiscard]] auto read(kstd::bytes offset) const -> ValueType + { + assert_valid_access(offset, kstd::size_of()); + auto read_pointer = reinterpret_cast(m_data_base.get() + offset.value); + return *read_pointer; + } + + //! Write a value, at a given offset, to this MMIO window. + //! + //! @warning This function will panic if the offset is out of bounds. + //! + //! @warning This function will panic if this window is read-only. + //! + //! @warning This function will panic if the operation would result in an unaligned access. The check is performed + //! against the absolute address (including the in-page offset). + //! + //! @warning This function will panic if this window has been moved from. + //! + //! @tparam ValueType The type of value to write to the MMIO window. + //! @param offset The offset within the MMIO window to write to. + //! @param value The value to write to the MMIO window. + template + auto write(kstd::bytes offset, ValueType value) -> void + { + assert_valid_access(offset, kstd::size_of()); + assert_writable(); + auto write_pointer = reinterpret_cast(m_data_base.get() + offset.value); + *write_pointer = value; + } + + private: + //! Construct a new MMIO window with the given mapping base, data base, size, and access mode. + //! + //! @param mapping_base The linear base address for the MMIO window. + //! @param data_base The offset adjusted base address for the MMIO window, as returned by the page mapper. + //! @param size The accessible size of the MMIO window. + //! @param mode The access mode of the MMIO window. + mmio_window(linear_address mapping_base, kstd::observer_ptr data_base, kstd::bytes size, access mode); + + //! Check if an access at the given offset and size is valid for this MMIO window. + //! + //! @warning This function will panic if the access is out of bounds. + //! + //! @warning This function will panic if the operation would result in an unaligned access. The check is performed + //! against the absolute address (including the in-page offset). + //! + //! @warning This function will panic if this window has been moved from. + //! + //! @param offset The offset within the MMIO window to check. + //! @param size The size of the access to check. + auto assert_valid_access(kstd::bytes offset, kstd::bytes size) const -> void; + + //! Check if this MMIO window is writable. + //! + //! @warning This function will panic if the window is not writable. + //! + auto assert_writable() const -> void; + + //! Release the resources associated with this MMIO window. + auto release() -> void; + + //! The linear base address of this MMIO window. + linear_address m_mapping_base; + //! The offset adjusted base address of this MMIO window. + kstd::observer_ptr m_data_base; + //! The accessible size of this MMIO window. + kstd::bytes m_data_size; + //! The access mode of this MMIO window. + access m_mode; + }; + + //! Initialize the Memory-mapped I/O region system. + //! + //! @param base The base address for the MMIO region. + //! @param page_count The number of pages the MMIO region spans. + auto init_mmio(linear_address base, std::size_t page_count) -> void; + + //! @} + +} // namespace kapi::memory + +#endif diff --git a/kapi/kapi/test_support/memory.hpp b/kapi/kapi/test_support/memory.hpp new file mode 100644 index 00000000..33c02bb9 --- /dev/null +++ b/kapi/kapi/test_support/memory.hpp @@ -0,0 +1,11 @@ +#ifndef TEACHOS_KAPI_TEST_SUPPORT_MEMORY_HPP +#define TEACHOS_KAPI_TEST_SUPPORT_MEMORY_HPP + +namespace kapi::test_support::memory +{ + + auto deinit_mmio() -> void; + +} // namespace kapi::test_support::memory + +#endif -- cgit v1.2.3