diff options
Diffstat (limited to 'kapi')
| -rw-r--r-- | kapi/kapi/devices.hpp | 1 | ||||
| -rw-r--r-- | kapi/kapi/devices/pci_express.hpp | 41 | ||||
| -rw-r--r-- | kapi/kapi/memory.hpp | 37 | ||||
| -rw-r--r-- | kapi/kapi/memory/mmio.hpp | 187 | ||||
| -rw-r--r-- | kapi/kapi/memory/page_mapper.hpp | 25 | ||||
| -rw-r--r-- | kapi/kapi/test_support/memory.hpp | 11 |
6 files changed, 260 insertions, 42 deletions
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 <kapi/devices/driver_registry.hpp> // IWYU pragma: export #include <kapi/devices/error.hpp> // IWYU pragma: export #include <kapi/devices/facet_registry.hpp> // IWYU pragma: export +#include <kapi/devices/pci_express.hpp> // IWYU pragma: export #include <kapi/devices/power.hpp> // IWYU pragma: export #include <kapi/devices/resource.hpp> // 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..91cb65df --- /dev/null +++ b/kapi/kapi/devices/pci_express.hpp @@ -0,0 +1,41 @@ +#ifndef TEACHOS_KAPI_DEVICES_PCI_EXPRESS_HPP +#define TEACHOS_KAPI_DEVICES_PCI_EXPRESS_HPP + +// IWYU pragma: private, include <kapi/devices.hpp> + +#include <kapi/memory.hpp> + +#include <kstd/vector.hpp> + +#include <cstddef> +#include <cstdint> + +namespace kapi::devices +{ + + //! A descriptor for a single PCI Express segment. + struct pcie_segment_descriptor + { + //! 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 + //! @{ + + //! 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<pcie_segment_descriptor>; + + //! @} + +} // namespace kapi::devices + +#endif
\ No newline at end of file diff --git a/kapi/kapi/memory.hpp b/kapi/kapi/memory.hpp index 4b14b553..2abad38e 100644 --- a/kapi/kapi/memory.hpp +++ b/kapi/kapi/memory.hpp @@ -6,6 +6,7 @@ #include <kapi/memory/frame.hpp> // IWYU pragma: export #include <kapi/memory/frame_allocator.hpp> // IWYU pragma: export #include <kapi/memory/layout.hpp> // IWYU pragma: export +#include <kapi/memory/mmio.hpp> // IWYU pragma: export #include <kapi/memory/page.hpp> // IWYU pragma: export #include <kapi/memory/page_mapper.hpp> // IWYU pragma: export @@ -18,8 +19,6 @@ namespace kapi::memory { - using mmio_region = std::pair<linear_address, std::size_t>; - //! @addtogroup kapi-memory-kernel-defined //! @{ @@ -82,38 +81,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; - - //! 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. - auto allocate_mmio_region(std::size_t page_count) -> mmio_region; - - //! 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. - //! - //! @param region The region to map. - //! @param hw_base The base of the hardware region. - //! @param flags The flags to apply. - auto map_mmio_region(mmio_region region, physical_address hw_base, page_mapper::flags flags = {}) - -> kstd::observer_ptr<std::byte>; - - //! Release a Memory-mapped I/O 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; + //! @return The frame the page was previously mapped to. + [[nodiscard]] auto unmap(page page) -> frame; //! @} 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 <kapi/memory.hpp> + +#include <kapi/memory/address.hpp> + +#include <kstd/memory.hpp> +#include <kstd/result.hpp> +#include <kstd/units.hpp> + +#include <concepts> +#include <cstddef> + +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<typename ValueType> + concept mmio_datum = requires { + requires std::unsigned_integral<ValueType>; + requires(!std::same_as<bool, ValueType>); + 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(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<mmio_datum ValueType> + [[nodiscard]] auto read(kstd::bytes offset) const -> ValueType + { + assert_valid_access(offset, kstd::size_of<ValueType>()); + auto read_pointer = reinterpret_cast<ValueType volatile const *>(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<mmio_datum ValueType> + auto write(kstd::bytes offset, ValueType value) -> void + { + assert_valid_access(offset, kstd::size_of<ValueType>()); + assert_writable(); + auto write_pointer = reinterpret_cast<ValueType volatile *>(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<std::byte> 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<std::byte> 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/memory/page_mapper.hpp b/kapi/kapi/memory/page_mapper.hpp index 5ac0210f..889ef052 100644 --- a/kapi/kapi/memory/page_mapper.hpp +++ b/kapi/kapi/memory/page_mapper.hpp @@ -11,6 +11,7 @@ #include <cstddef> #include <cstdint> +#include <optional> #include <type_traits> namespace kapi::memory @@ -31,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; @@ -54,13 +62,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<frame> = 0; //! @qualifier kernel-defined //! Map a page into a given frame, applyint the given flags. 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 |
