From 056f27ea9f4f228007e3704b4376b815fcdfa6e7 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 11 Nov 2026 17:09:35 +0100 Subject: libs/acpi: improve vla_table interface --- libs/acpi/acpi/common/vla_table.hpp | 236 +++++++++++++++++++++++++++++------- libs/acpi/acpi/data/madt.hpp | 3 + libs/acpi/acpi/data/madt.tests.cpp | 5 + libs/acpi/acpi/data/mcfg.cpp | 22 ---- libs/acpi/acpi/data/mcfg.hpp | 22 +--- libs/acpi/acpi/data/rsdt.hpp | 3 + libs/acpi/acpi/data/rsdt.tests.cpp | 5 + libs/acpi/acpi/data/xsdt.hpp | 3 + libs/acpi/acpi/data/xsdt.tests.cpp | 5 + 9 files changed, 221 insertions(+), 83 deletions(-) (limited to 'libs') diff --git a/libs/acpi/acpi/common/vla_table.hpp b/libs/acpi/acpi/common/vla_table.hpp index 7993e2db..e003c4ca 100644 --- a/libs/acpi/acpi/common/vla_table.hpp +++ b/libs/acpi/acpi/common/vla_table.hpp @@ -3,89 +3,231 @@ #include +#include + +#include #include #include namespace acpi { - template + namespace detail + { + + template + struct common_vla_table_iterator_base + { + using value_type = EntryType; + using pointer = value_type *; + using reference = value_type &; + using difference_type = std::ptrdiff_t; + + constexpr auto operator++(int) noexcept -> IteratorType + { + auto self = static_cast(this); + auto copy = *self; + ++*self; + return copy; + } + + constexpr auto operator--(int) noexcept -> IteratorType + requires requires { --*static_cast(this); } + { + auto self = static_cast(this); + auto copy = *self; + --*self; + return copy; + } + + constexpr auto operator*() const noexcept -> reference + { + return *this->m_entry; + } + + constexpr auto operator->() const noexcept -> pointer + { + return this->m_entry; + } + + constexpr auto friend operator==(common_vla_table_iterator_base const & lhs, + common_vla_table_iterator_base const & rhs) noexcept -> bool + { + return lhs.m_entry == rhs.m_entry || (lhs.is_end() && rhs.is_end()); + } + + private: + friend IteratorType; + + constexpr common_vla_table_iterator_base() noexcept = default; + + constexpr common_vla_table_iterator_base(pointer entry, pointer limit) noexcept + : m_entry{entry} + , m_limit{limit} + {} + + [[nodiscard]] constexpr auto is_end() const noexcept -> bool + { + return m_entry == m_limit || m_entry == nullptr; + } + + pointer m_entry{}; + pointer m_limit{}; + }; + + } // namespace detail + + template struct vla_table_iterator + : detail::common_vla_table_iterator_base> { + using super = detail::common_vla_table_iterator_base>; + using iterator_category = std::forward_iterator_tag; - using value_type = EntryType; - using pointer = value_type *; - using reference = value_type &; - using difference_type = std::ptrdiff_t; + using iterator_concept = std::forward_iterator_tag; + using typename super::difference_type; + using typename super::pointer; + using typename super::reference; + using typename super::value_type; + + using super::operator++; + + constexpr vla_table_iterator() noexcept = default; + + constexpr vla_table_iterator(pointer entry, pointer limit) noexcept + : super{entry, limit} + {} + + auto operator++() noexcept -> vla_table_iterator & + { + auto decayed = std::bit_cast(this->m_entry); + decayed += this->m_entry->length(); + this->m_entry = std::bit_cast(decayed); + return *this; + } + }; + + template + struct vla_table_iterator + : detail::common_vla_table_iterator_base> + { + using super = detail::common_vla_table_iterator_base>; + + using iterator_category = std::random_access_iterator_tag; + using iterator_concept = std::contiguous_iterator_tag; + using typename super::difference_type; + using typename super::pointer; + using typename super::reference; + using typename super::value_type; + + using super::operator++; + using super::operator--; constexpr vla_table_iterator() noexcept = default; constexpr vla_table_iterator(pointer entry, pointer limit) noexcept - : m_entry{entry} - , m_limit{limit} + : super{entry, limit} {} + constexpr auto operator<=>(vla_table_iterator const & other) const noexcept -> std::strong_ordering + { + return this->m_entry <=> other.m_entry; + } + + constexpr auto operator==(vla_table_iterator const & other) const noexcept -> bool + { + return this->m_entry == other.m_entry; + } + + constexpr auto operator==(std::default_sentinel_t) const noexcept -> bool + { + return this->m_entry == nullptr || this->m_entry == this->m_limit; + } + constexpr auto operator++() noexcept -> vla_table_iterator & { - auto decayed = std::bit_cast(m_entry); - decayed += m_entry->length(); - m_entry = std::bit_cast(decayed); + ++this->m_entry; return *this; } - constexpr auto operator++(int) noexcept -> vla_table_iterator + constexpr auto operator--() noexcept -> vla_table_iterator & + { + --this->m_entry; + return *this; + } + + constexpr auto operator-(vla_table_iterator const & other) const noexcept -> difference_type + { + return this->m_entry - other.m_entry; + } + + constexpr auto operator+=(difference_type n) noexcept -> vla_table_iterator & + { + this->m_entry += n; + return *this; + } + + constexpr auto operator+(difference_type n) const noexcept -> vla_table_iterator { auto copy = *this; - ++*this; + copy += n; return copy; } - constexpr auto operator*() const noexcept -> reference + constexpr auto friend operator+(difference_type n, vla_table_iterator const & it) noexcept -> vla_table_iterator { - return *m_entry; + return it + n; } - constexpr auto operator->() const noexcept -> pointer + constexpr auto operator-=(difference_type n) noexcept -> vla_table_iterator & { - return m_entry; + this->m_entry -= n; + return *this; } - constexpr auto operator==(vla_table_iterator const & other) const noexcept -> bool + constexpr auto operator-(difference_type n) const noexcept -> vla_table_iterator { - return m_entry == other.m_entry || (is_end() && other.is_end()); + auto copy = *this; + copy -= n; + return copy; } - private: - [[nodiscard]] constexpr auto is_end() const noexcept -> bool + constexpr auto operator[](difference_type n) const noexcept -> reference { - return m_entry == m_limit; + return *(*this + n); } - - pointer m_entry{}; - pointer m_limit{}; }; template struct vla_table : basic_table { using entry_type = EntryType; - using iterator = vla_table_iterator; - using const_iterator = vla_table_iterator; + using iterator = vla_table_iterator; + using const_iterator = iterator; + using reference = entry_type &; + using const_reference = entry_type const &; [[nodiscard]] auto begin() noexcept -> iterator { - auto base = vla_base(); - auto limit = std::bit_cast(this); - limit += this->length().value; - return iterator{base, std::bit_cast(limit)}; + return cbegin(); } [[nodiscard]] auto end() noexcept -> iterator { - return iterator{}; + return cend(); } [[nodiscard]] auto begin() const noexcept -> const_iterator + { + return cbegin(); + } + + [[nodiscard]] auto end() const noexcept -> const_iterator + { + return cend(); + } + + [[nodiscard]] auto cbegin() const noexcept -> const_iterator { auto base = std::bit_cast(this); base += sizeof(TableType); @@ -94,33 +236,43 @@ namespace acpi return const_iterator{std::bit_cast(base), std::bit_cast(limit)}; } - [[nodiscard]] auto end() const noexcept -> const_iterator + [[nodiscard]] auto cend() const noexcept -> const_iterator { - return const_iterator{}; + auto limit = std::bit_cast(this); + limit += this->length().value; + return const_iterator{std::bit_cast(limit), std::bit_cast(limit)}; } - [[nodiscard]] auto cbegin() const noexcept -> const_iterator + [[nodiscard]] auto size() const noexcept -> std::size_t { - return begin(); + return std::ranges::distance(cbegin(), cend()); } - [[nodiscard]] auto cend() const noexcept -> const_iterator + [[nodiscard]] auto operator[](std::size_t index) const noexcept -> const_reference { - return end(); + return *(std::next(cbegin(), index)); } - protected: - [[nodiscard]] auto vla_base() const noexcept -> entry_type * + [[nodiscard]] auto at(std::size_t index) const -> const_reference { - auto base = std::bit_cast(this); - base += sizeof(TableType); - return reinterpret_cast(base); + if (index >= size()) + { + kstd::os::panic("[ACPI] VLA table: index out of range"); + } + return (*this)[index]; } private: friend TableType; constexpr vla_table() noexcept = default; + + [[nodiscard]] auto vla_base() const noexcept -> entry_type * + { + auto base = std::bit_cast(this); + base += sizeof(TableType); + return reinterpret_cast(base); + } }; } // namespace acpi diff --git a/libs/acpi/acpi/data/madt.hpp b/libs/acpi/acpi/data/madt.hpp index 4a929a42..1fca05da 100644 --- a/libs/acpi/acpi/data/madt.hpp +++ b/libs/acpi/acpi/data/madt.hpp @@ -34,6 +34,9 @@ namespace acpi struct madt_entry { + //! Indicate that MADT entries have a variable size. + constexpr auto static fixed_size = false; + //! The type of an MADT entry. enum struct type : std::uint8_t { diff --git a/libs/acpi/acpi/data/madt.tests.cpp b/libs/acpi/acpi/data/madt.tests.cpp index 9ec0180a..345523bb 100644 --- a/libs/acpi/acpi/data/madt.tests.cpp +++ b/libs/acpi/acpi/data/madt.tests.cpp @@ -33,6 +33,11 @@ SCENARIO("MADT parsing", "[madt]") REQUIRE(std::distance(madt->begin(), madt->end()) == 1); } + THEN("the size of the table is 1") + { + REQUIRE(madt->size() == 1); + } + THEN("the LAPIC address is 0xfee00000") { REQUIRE(madt->local_interrupt_controller_address() == 0xfee0'0000); diff --git a/libs/acpi/acpi/data/mcfg.cpp b/libs/acpi/acpi/data/mcfg.cpp index 9e1b9d4d..008a8ca3 100644 --- a/libs/acpi/acpi/data/mcfg.cpp +++ b/libs/acpi/acpi/data/mcfg.cpp @@ -1,8 +1,5 @@ #include -#include - -#include #include namespace @@ -45,23 +42,4 @@ namespace acpi return read(); } - auto mcfg::size() const noexcept -> std::size_t - { - return (this->length().value - sizeof(*this)) / sizeof(mcfg_entry); - } - - auto mcfg::operator[](std::size_t index) const noexcept -> mcfg_entry const & - { - return vla_base()[index]; - } - - auto mcfg::at(std::size_t index) const -> mcfg_entry const & - { - if (index >= size()) - { - kstd::os::panic("[ACPI] MCFG table: index out of bounds"); - } - return (*this)[index]; - } - } // namespace acpi \ No newline at end of file diff --git a/libs/acpi/acpi/data/mcfg.hpp b/libs/acpi/acpi/data/mcfg.hpp index 881f479a..24097b44 100644 --- a/libs/acpi/acpi/data/mcfg.hpp +++ b/libs/acpi/acpi/data/mcfg.hpp @@ -32,6 +32,9 @@ namespace acpi //! An entry in the MCFG table. struct mcfg_entry { + //! Indicate that MCFG entries have a fixed size. + constexpr auto static fixed_size = true; + //! Get the processor-relative base address of the memory-mapped configuration space, this entry describes. //! //! This is address represents the physical address of the memory-mapped configuration space. It is the @@ -87,25 +90,6 @@ namespace acpi //! This table provides information about the memory-mapped configuration space for PCI and PCIe devices. struct mcfg : vla_table { - //! Get the number of entries in this MCFG table. - [[nodiscard]] auto size() const noexcept -> std::size_t; - - //! Get the nth entry in this MCFG table. - //! - //! @warning This function does not perform bounds checking. - //! - //! @param index The index of the entry to retrieve. - //! @return The MCFG entry at the specified index. - [[nodiscard]] auto operator[](std::size_t index) const noexcept -> mcfg_entry const &; - - //! Get the nth entry in this MCFG table. - //! - //! @warning This function panics if the index is out of bounds. - //! - //! @param index The index of the entry to retrieve. - //! @return The MCFG entry at the specified index. - [[nodiscard]] auto at(std::size_t index) const -> mcfg_entry const &; - private: //! The reserved bytes at the end of the MCFG table header. [[maybe_unused]] std::array m_reserved{}; diff --git a/libs/acpi/acpi/data/rsdt.hpp b/libs/acpi/acpi/data/rsdt.hpp index a6611875..c19a9ea4 100644 --- a/libs/acpi/acpi/data/rsdt.hpp +++ b/libs/acpi/acpi/data/rsdt.hpp @@ -26,6 +26,9 @@ namespace acpi struct rsdt_entry { + //! Indicate that all RSDT entries have a fixed size. + constexpr auto static fixed_size = true; + [[nodiscard]] auto address() const noexcept -> table_header *; [[nodiscard]] auto length() const noexcept -> std::size_t; diff --git a/libs/acpi/acpi/data/rsdt.tests.cpp b/libs/acpi/acpi/data/rsdt.tests.cpp index 826d0b4f..c111f749 100644 --- a/libs/acpi/acpi/data/rsdt.tests.cpp +++ b/libs/acpi/acpi/data/rsdt.tests.cpp @@ -34,6 +34,11 @@ SCENARIO("RSDT parsing", "[rsdt]") REQUIRE(std::distance(rsdt->begin(), rsdt->end()) == 8); } + THEN("the size of the table is 8") + { + REQUIRE(rsdt->size() == 8); + } + THEN("the first entry has address 0x10") { REQUIRE(rsdt->cbegin()->address() == reinterpret_cast(0x10)); diff --git a/libs/acpi/acpi/data/xsdt.hpp b/libs/acpi/acpi/data/xsdt.hpp index 965f23c0..f09e2e5e 100644 --- a/libs/acpi/acpi/data/xsdt.hpp +++ b/libs/acpi/acpi/data/xsdt.hpp @@ -26,6 +26,9 @@ namespace acpi struct xsdt_entry { + //! Indicate that all RSDT entries have a fixed size. + constexpr auto static fixed_size = true; + [[nodiscard]] auto address() const noexcept -> table_header *; [[nodiscard]] auto length() const noexcept -> std::size_t; diff --git a/libs/acpi/acpi/data/xsdt.tests.cpp b/libs/acpi/acpi/data/xsdt.tests.cpp index 19d9bcdb..e5eaed67 100644 --- a/libs/acpi/acpi/data/xsdt.tests.cpp +++ b/libs/acpi/acpi/data/xsdt.tests.cpp @@ -34,6 +34,11 @@ SCENARIO("XSDT parsing", "[xsdt]") REQUIRE(std::distance(xsdt->begin(), xsdt->end()) == 8); } + THEN("the size of the table is 8") + { + REQUIRE(xsdt->size() == 8); + } + THEN("the first entry has address 0x10") { REQUIRE(xsdt->cbegin()->address() == reinterpret_cast(0x10)); -- cgit v1.2.3