diff options
26 files changed, 797 insertions, 133 deletions
diff --git a/cmake/Modules/BuildHostTests.cmake b/cmake/Modules/BuildHostTests.cmake index f124728b..c254e30a 100644 --- a/cmake/Modules/BuildHostTests.cmake +++ b/cmake/Modules/BuildHostTests.cmake @@ -1,78 +1,80 @@ include_guard(GLOBAL) -function("teachos_add_tests" NAME) - add_executable("${NAME}_tests") - add_executable("${NAME}::tests" ALIAS "${NAME}_tests") +function("_teachos_add_tests_impl" NAME TYPE) + add_executable("${NAME}_${TYPE}") + add_executable("${NAME}::${TYPE}" ALIAS "${NAME}_${TYPE}") - file(GLOB_RECURSE - TEST_SOURCES - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - CONFIGURE_DEPENDS - "*.tests.cpp" - ) + get_target_property(LIBRARY_TARGET "${NAME}::lib" ALIASED_TARGET) - set_source_files_properties(${TEST_SOURCES} PROPERTIES - SKIP_LINTING YES + file(GLOB_RECURSE + TEST_SUPPORT_SOURCES + RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" + CONFIGURE_DEPENDS + "**/test_support/**.cpp" ) - target_sources("${NAME}_tests" PRIVATE - ${TEST_SOURCES} - ) + list(FILTER TEST_SUPPORT_SOURCES EXCLUDE REGEX ".tests.cpp") - target_link_libraries("${NAME}_tests" PRIVATE - "Catch2::Catch2WithMain" - "${NAME}::lib" - ) + message(STATUS "Registering test support sources for ${NAME}::${TYPE}") + foreach(SOURCE IN LISTS TEST_SUPPORT_SOURCES) + message(DEBUG "${CMAKE_MESSAGE_INDENT}${SOURCE}") + endforeach() - set_target_properties("${NAME}_tests" PROPERTIES - EXCLUDE_FROM_ALL NO - ) + target_sources("${LIBRARY_TARGET}" PRIVATE + ${TEST_SUPPORT_SOURCES} + ) - file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bht_results") + if(TYPE STREQUAL "stress_tests") + set(FILE_COMPONENT "stress") + else() + set(FILE_COMPONENT "tests") + endif() - catch_discover_tests("${NAME}::tests" - REPORTER junit - EXTRA_ARGS --reporter console::out=-::colour-mode=ansi - OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/bht_results" - OUTPUT_SUFFIX ".xml" - ADD_TAGS_AS_LABELS - ) -endfunction() + file(GLOB_RECURSE + TEST_SOURCES + RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" + CONFIGURE_DEPENDS + "*.${FILE_COMPONENT}.cpp" + ) -function("teachos_add_stress_tests" NAME) - add_executable("${NAME}_stress_tests") - add_executable("${NAME}::stress_tests" ALIAS "${NAME}_stress_tests") + message(STATUS "Registering test sources for ${NAME}::${TYPE}") + foreach(SOURCE IN LISTS TEST_SOURCES) + message(DEBUG "${CMAKE_MESSAGE_INDENT}${SOURCE}") + endforeach() - file(GLOB_RECURSE - TEST_SOURCES - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - CONFIGURE_DEPENDS - "*.stress.cpp" - ) set_source_files_properties(${TEST_SOURCES} PROPERTIES - SKIP_LINTING YES + SKIP_LINTING YES ) - target_sources("${NAME}_stress_tests" PRIVATE - ${TEST_SOURCES} - ) + target_sources("${NAME}_${TYPE}" PRIVATE + ${TEST_SOURCES} + ) - target_link_libraries("${NAME}_stress_tests" PRIVATE - "Catch2::Catch2WithMain" - "${NAME}::lib" - ) + target_link_libraries("${NAME}_${TYPE}" PRIVATE + "Catch2::Catch2WithMain" + "${NAME}::lib" + ) - set_target_properties("${NAME}_stress_tests" PROPERTIES - EXCLUDE_FROM_ALL NO - ) + set_target_properties("${NAME}_${TYPE}" PROPERTIES + EXCLUDE_FROM_ALL NO + ) file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bht_results") - catch_discover_tests("${NAME}::stress_tests" - REPORTER junit - OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/bht_results" - OUTPUT_SUFFIX ".xml" - ADD_TAGS_AS_LABELS - ) + catch_discover_tests("${NAME}::${TYPE}" + REPORTER junit + EXTRA_ARGS "--reporter console::out=-::colour-mode=ansi" + OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/bht_results" + OUTPUT_SUFFIX ".xml" + ADD_TAGS_AS_LABELS + ) +endfunction() + +function("teachos_add_tests" NAME) + _teachos_add_tests_impl("${NAME}" "tests") +endfunction() + +function("teachos_add_stress_tests" NAME) + _teachos_add_tests_impl("${NAME}" "stress_tests") endfunction() diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 2ed3ac62..e105ae62 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -170,20 +170,6 @@ if(BUILD_TESTING) set(TEST_TARGET "kernel_tests") endif() - file(GLOB_RECURSE KERNEL_TEST_SUPPORT_SOURCES - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - CONFIGURE_DEPENDS - "kernel/test_support/**.[ch]pp" - ) - - set_source_files_properties(${KERNEL_TEST_SUPPORT_SOURCES} PROPERTIES - SKIP_LINTING YES - ) - - target_sources("${TEST_TARGET}" PRIVATE - ${KERNEL_TEST_SUPPORT_SOURCES} - ) - target_compile_options("${TEST_TARGET}" PRIVATE "$<$<CXX_COMPILER_ID:Clang,GNU>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/kernel/test_support/string_makers.hpp>" ) diff --git a/kernel/kernel/acpi/manager.cpp b/kernel/kernel/acpi/manager.cpp index 2ba6d558..25ef52b8 100644 --- a/kernel/kernel/acpi/manager.cpp +++ b/kernel/kernel/acpi/manager.cpp @@ -10,7 +10,6 @@ #include <algorithm> #include <cstddef> -#include <cstdint> #include <ranges> #include <string_view> @@ -55,7 +54,7 @@ namespace kernel::acpi } auto check_and_register_table = [&](auto table_address) -> void { - auto physical_table_address = kapi::memory::physical_address{reinterpret_cast<std::uintptr_t>(table_address)}; + auto physical_table_address = kapi::memory::physical_address{table_address}; auto mapped_table = kapi::memory::hhdm_to_linear(physical_table_address); auto table = static_cast<kstd::observer_ptr<::acpi::table_header const>>(mapped_table); diff --git a/kernel/kernel/acpi/manager.hpp b/kernel/kernel/acpi/manager.hpp index de344dd8..8ce5368d 100644 --- a/kernel/kernel/acpi/manager.hpp +++ b/kernel/kernel/acpi/manager.hpp @@ -5,7 +5,6 @@ #include <kstd/flat_map.hpp> #include <kstd/memory.hpp> -#include <kstd/vector.hpp> #include <string_view> diff --git a/libs/acpi/CMakeLists.txt b/libs/acpi/CMakeLists.txt index 9fc8c719..c613178e 100644 --- a/libs/acpi/CMakeLists.txt +++ b/libs/acpi/CMakeLists.txt @@ -25,6 +25,7 @@ target_sources("acpi" PRIVATE "acpi/common/checksum.cpp" "acpi/common/table_header.cpp" "acpi/data/madt.cpp" + "acpi/data/mcfg.cpp" "acpi/data/rsdt.cpp" "acpi/data/xsdt.cpp" "acpi/pointers.cpp" @@ -68,6 +69,7 @@ if(BUILD_TESTING) set(TEST_TABLES "basic_madt" + "basic_mcfg" "basic_rsdt" "basic_rsdp" "basic_xsdt" diff --git a/libs/acpi/acpi/acpi.hpp b/libs/acpi/acpi/acpi.hpp index 0da44a85..292e8fdb 100644 --- a/libs/acpi/acpi/acpi.hpp +++ b/libs/acpi/acpi/acpi.hpp @@ -5,6 +5,7 @@ #include <acpi/common/table_signature.hpp> // IWYU pragma: export #include <acpi/common/table_type.hpp> // IWYU pragma: export #include <acpi/data/madt.hpp> // IWYU pragma: export +#include <acpi/data/mcfg.hpp> // IWYU pragma: export #include <acpi/data/rsdt.hpp> // IWYU pragma: export #include <acpi/data/xsdt.hpp> // IWYU pragma: export #include <acpi/pointers.hpp> // IWYU pragma: export diff --git a/libs/acpi/acpi/common/basic_table.hpp b/libs/acpi/acpi/common/basic_table.hpp index f5b5b271..e005b90d 100644 --- a/libs/acpi/acpi/common/basic_table.hpp +++ b/libs/acpi/acpi/common/basic_table.hpp @@ -14,16 +14,25 @@ namespace acpi template<typename TableType> struct basic_table : table_header { + //! Validate the checksum of this table. + //! + //! @return @p true iff. the checksum of this table is valid, @p false otherwise. [[nodiscard]] auto validate_checksum() const noexcept -> bool { return acpi::validate_checksum(as_span()); } + //! Get a read-only span representing the raw bytes of this table. + //! + //! @return A span of bytes covering the entire table. [[nodiscard]] auto as_span() const noexcept -> std::span<std::byte const> { return {reinterpret_cast<std::byte const *>(this), length().value}; } + //! Validate the integrity of this table. + //! + //! @return @p true iff. the table has a valid signature and checksum, @p false otherwise. [[nodiscard]] auto validate() const noexcept -> bool { return signature() == table_signature_v<TableType> && validate_checksum(); diff --git a/libs/acpi/acpi/common/table_signature.hpp b/libs/acpi/acpi/common/table_signature.hpp index f0c2d7a3..103b96a8 100644 --- a/libs/acpi/acpi/common/table_signature.hpp +++ b/libs/acpi/acpi/common/table_signature.hpp @@ -6,9 +6,15 @@ namespace acpi { + //! The base template for table signatures. + //! + //! A developer may choose to specialize this template for each ACPI table type to provide its signature. template<typename TableType> struct table_signature; + //! The signature for a given ACPI table type. + //! + //! @tparam TableType The ACPI table type. template<typename TableType> constexpr auto table_signature_v = table_signature<TableType>::value; diff --git a/libs/acpi/acpi/common/table_type.hpp b/libs/acpi/acpi/common/table_type.hpp index bc427c7b..ea66d461 100644 --- a/libs/acpi/acpi/common/table_type.hpp +++ b/libs/acpi/acpi/common/table_type.hpp @@ -6,9 +6,16 @@ namespace acpi { + //! The base template for table types. + //! + //! A developer may choose to specialize this template for each ACPI table signature to provide its corresponding + //! table type. template<char const *> struct table_type; + //! The table type corresponding to a given ACPI table signature. + //! + //! @tparam Signature The ACPI table signature. template<char const * Signature> using table_type_t = typename table_type<Signature>::type; diff --git a/libs/acpi/acpi/common/vla_table.hpp b/libs/acpi/acpi/common/vla_table.hpp index d3f33a7c..e236c58b 100644 --- a/libs/acpi/acpi/common/vla_table.hpp +++ b/libs/acpi/acpi/common/vla_table.hpp @@ -3,91 +3,346 @@ #include <acpi/common/basic_table.hpp> +#include <kstd/os/error.hpp> + +#include <compare> #include <cstddef> #include <iterator> namespace acpi { - template<typename EntryType> + namespace detail + { + + template<typename EntryType, typename IteratorType> + struct common_vla_table_iterator_base + { + using value_type = EntryType; + using pointer = value_type *; + using reference = value_type &; + using difference_type = std::ptrdiff_t; + + //! Advance the iterator to the next entry in the VLA table. + //! + //! @return A copy of the iterator before it was advanced. + constexpr auto operator++(int) noexcept -> IteratorType + { + auto self = static_cast<IteratorType *>(this); + auto copy = *self; + ++*self; + return copy; + } + + //! Move the iterator to the previous entry in the VLA table. + //! + //! @note This operation requires that the iterator supports the pre-decrement operator. + //! + //! @return A copy of the iterator before it was moved backward. + constexpr auto operator--(int) noexcept -> IteratorType + requires requires { --*static_cast<IteratorType *>(this); } + { + auto self = static_cast<IteratorType *>(this); + auto copy = *self; + --*self; + return copy; + } + + //! Access the entry at the current iterator position. + //! + //! @return A reference to the entry at the current iterator position. + constexpr auto operator*() const noexcept -> reference + { + return *this->m_entry; + } + + //! Access a member of the entry at the current iterator position. + //! + //! @return A pointer to the entry at the current iterator position. + constexpr auto operator->() const noexcept -> pointer + { + return this->m_entry; + } + + //! Compare two iterators for equality. + //! + //! @param lhs The left-hand side iterator. + //! @param rhs The right-hand side iterator. + //! @return @p true iff. the iterators are equal, @p false otherwise. + 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; + + //! Construct a sentinel iterator. + constexpr common_vla_table_iterator_base() noexcept = default; + + //! Construct an iterator pointing to a specific entry in the VLA table. + //! + //! @param entry A pointer to the entry to start the iterator at. + //! @param limit A pointer to the end of the VLA table. + constexpr common_vla_table_iterator_base(pointer entry, pointer limit) noexcept + : m_entry{entry} + , m_limit{limit} + {} + + //! Check if the iterator has reached the end of the VLA table. + //! + //! @return @p true if the iterator is at the end, @p false otherwise. + [[nodiscard]] constexpr auto is_end() const noexcept -> bool + { + return m_entry == m_limit || m_entry == nullptr; + } + + //! The current entry the iterator is pointing to. + pointer m_entry{}; + //! The end of the VLA table. + pointer m_limit{}; + }; + + } // namespace detail + + //! An iterator for variable-length array (VLA) tables with variable-sized entries. + template<typename EntryType, bool FixedSize> struct vla_table_iterator + : detail::common_vla_table_iterator_base<EntryType, vla_table_iterator<EntryType, FixedSize>> { + using super = detail::common_vla_table_iterator_base<EntryType, vla_table_iterator<EntryType, FixedSize>>; + 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++; + + //! Construct a sentinel iterator for the end of the VLA table. + constexpr vla_table_iterator() noexcept = default; + + //! Construct an iterator pointing to a specific entry in the VLA table. + //! + //! @param entry A pointer to the entry to start the iterator at. + //! @param limit A pointer to the end of the VLA table. + constexpr vla_table_iterator(pointer entry, pointer limit) noexcept + : super{entry, limit} + {} + + //! Advance the iterator to the next entry in the VLA table. + //! + //! @return A reference to the updated iterator. + auto operator++() noexcept -> vla_table_iterator & + { + auto decayed = std::bit_cast<std::byte *>(this->m_entry); + decayed += this->m_entry->length(); + this->m_entry = std::bit_cast<pointer>(decayed); + return *this; + } + }; + + //! An iterator for variable-length array (VLA) tables with fixed-sized entries. + template<typename EntryType> + struct vla_table_iterator<EntryType, true> + : detail::common_vla_table_iterator_base<EntryType, vla_table_iterator<EntryType, true>> + { + using super = detail::common_vla_table_iterator_base<EntryType, vla_table_iterator<EntryType, true>>; + + 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--; + + //! Construct a sentinel iterator for the end of the VLA table. constexpr vla_table_iterator() noexcept = default; + //! Construct an iterator pointing to a specific entry in the VLA table. + //! + //! @param entry A pointer to the entry to start the iterator at. + //! @param limit A pointer to the end of the VLA table. constexpr vla_table_iterator(pointer entry, pointer limit) noexcept - : m_entry{entry} - , m_limit{limit} + : super{entry, limit} {} + //! Compare two VLA table iterators for ordering. + //! + //! @param lhs The left-hand side iterator. + //! @param rhs The right-hand side iterator. + //! @return A strong ordering indicating the relative order of the iterators. + constexpr auto friend operator<=>(vla_table_iterator const & lhs, vla_table_iterator const & rhs) noexcept + -> std::strong_ordering + { + return lhs.m_entry <=> rhs.m_entry; + } + + //! Compare two VLA table iterators for equality. + //! + //! @param lhs The left-hand side iterator. + //! @param rhs The right-hand side iterator. + //! @return @p true iff. the iterators are equal, @p false otherwise. + constexpr auto friend operator==(vla_table_iterator const & lhs, vla_table_iterator const & rhs) noexcept -> bool + { + return lhs.m_entry == rhs.m_entry; + } + + //! Compare the iterator with a sentinel for equality. + //! + //! @param it The iterator to compare. + //! @param sent The sentinel to compare with. + //! @return @p true iff. the iterator has reached the end, @p false otherwise. + constexpr auto friend operator==(vla_table_iterator const & it, std::default_sentinel_t) noexcept -> bool + { + return it.m_entry == nullptr || it.m_entry == it.m_limit; + } + + //! Advance the iterator to the next entry. + //! + //! @return A reference to the updated iterator. constexpr auto operator++() noexcept -> vla_table_iterator & { - auto decayed = std::bit_cast<std::byte *>(m_entry); - decayed += m_entry->length(); - m_entry = std::bit_cast<pointer>(decayed); + ++this->m_entry; return *this; } - constexpr auto operator++(int) noexcept -> vla_table_iterator + //! Move the iterator to the previous entry. + //! + //! @return A reference to the updated iterator. + constexpr auto operator--() noexcept -> vla_table_iterator & { - auto copy = *this; - ++*this; - return copy; + --this->m_entry; + return *this; } - constexpr auto operator*() const noexcept -> reference + //! Compute the distance between two iterators. + //! + //! @param lhs The left-hand side iterator. + //! @param rhs The right-hand side iterator. + //! @return The number of entries between this iterator and the other iterator. + constexpr auto friend operator-(vla_table_iterator const & lhs, vla_table_iterator const & rhs) noexcept + -> difference_type { - return *m_entry; + return lhs.m_entry - rhs.m_entry; } - constexpr auto operator->() const noexcept -> pointer + //! Advance the iterator by a given number of entries. + //! + //! @param lhs The iterator to advance. + //! @param n The number of entries to advance. + //! @return A reference to the updated iterator. + constexpr auto friend operator+=(vla_table_iterator & lhs, difference_type n) noexcept -> vla_table_iterator & { - return m_entry; + lhs.m_entry += n; + return lhs; } - constexpr auto operator==(vla_table_iterator const & other) const noexcept -> bool + //! Compute a new iterator by adding a given number of entries to an existing iterator. + //! + //! @param it The iterator to add to. + //! @param n The number of entries to add. + //! @return A new iterator advanced by the given number of entries. + constexpr auto friend operator+(vla_table_iterator it, difference_type n) noexcept -> vla_table_iterator { - return m_entry == other.m_entry || (is_end() && other.is_end()); + it += n; + return it; } - private: - [[nodiscard]] constexpr auto is_end() const noexcept -> bool + //! Compute a new iterator by adding a given number of entries to an existing iterator. + //! + //! @param n The number of entries to add. + //! @param it The iterator to add to. + //! @return A new iterator advanced by the given number of entries. + constexpr auto friend operator+(difference_type n, vla_table_iterator it) noexcept -> vla_table_iterator + { + return it + n; + } + + //! Move the iterator backward by a given number of entries. + //! + //! @param it The iterator to move backward. + //! @param n The number of entries to move backward. + //! @return A reference to the updated iterator. + constexpr auto friend operator-=(vla_table_iterator & it, difference_type n) noexcept -> vla_table_iterator & + { + it.m_entry -= n; + return it; + } + + //! Compute a new iterator by subtracting a given number of entries from an existing iterator. + //! + //! @param it The iterator to subtract from. + //! @param n The number of entries to subtract. + //! @return A new iterator moved backward by the given number of entries. + constexpr auto friend operator-(vla_table_iterator it, difference_type n) noexcept -> vla_table_iterator { - return m_entry == m_limit; + it -= n; + return it; } - pointer m_entry{}; - pointer m_limit{}; + //! Access an entry at a given offset from the current iterator. + //! + //! @param n The offset from the current iterator. + //! @return A reference to the entry at the given offset. + constexpr auto operator[](difference_type n) const noexcept -> reference + { + return *(*this + n); + } }; template<typename EntryType, typename TableType> struct vla_table : basic_table<TableType> { using entry_type = EntryType; - using iterator = vla_table_iterator<EntryType>; - using const_iterator = vla_table_iterator<EntryType const>; + using iterator = vla_table_iterator<EntryType const, EntryType::fixed_size>; + using const_iterator = iterator; + using reference = entry_type &; + using const_reference = entry_type const &; + //! Get an iterator to the beginning of the entries of this table. + //! + //! @return An iterator pointing to the first entry. [[nodiscard]] auto begin() noexcept -> iterator { - auto base = std::bit_cast<std::byte *>(this); - base += sizeof(TableType); - auto limit = std::bit_cast<std::byte *>(this); - limit += this->length().value; - return iterator{std::bit_cast<entry_type *>(base), std::bit_cast<entry_type *>(limit)}; + return cbegin(); } + //! Get an iterator to the end of the entries of this table. + //! + //! @return An iterator pointing past the last entry. [[nodiscard]] auto end() noexcept -> iterator { - return iterator{}; + return cend(); } + //! Get a read-only iterator to the beginning of the entries of this table. + //! + //! @return An iterator pointing to the first entry. [[nodiscard]] auto begin() const noexcept -> const_iterator { + return cbegin(); + } + + //! Get a read-only iterator to the end of the entries of this table. + //! + //! @return An iterator pointing past the last entry. + [[nodiscard]] auto end() const noexcept -> const_iterator + { + return cend(); + } + + //! Get a read-only iterator to the beginning of the entries of this table. + //! + //! @return An iterator pointing to the first entry. + [[nodiscard]] auto cbegin() const noexcept -> const_iterator + { auto base = std::bit_cast<std::byte *>(this); base += sizeof(TableType); auto limit = std::bit_cast<std::byte *>(this); @@ -95,25 +350,67 @@ namespace acpi return const_iterator{std::bit_cast<entry_type const *>(base), std::bit_cast<entry_type const *>(limit)}; } - [[nodiscard]] auto end() const noexcept -> const_iterator + //! Get a read-only iterator to the end of the entries of this table. + //! + //! @return An iterator pointing past the last entry. + [[nodiscard]] auto cend() const noexcept -> const_iterator { - return const_iterator{}; + auto limit = std::bit_cast<std::byte *>(this); + limit += this->length().value; + return const_iterator{std::bit_cast<entry_type const *>(limit), std::bit_cast<entry_type const *>(limit)}; } - [[nodiscard]] auto cbegin() const noexcept -> const_iterator + //! Get the number of entries in this table. + //! + //! @note For tables with variable-sized entries, this function will compute the number of entries in linear time. + //! + //! @return The number of entries. + [[nodiscard]] auto size() const noexcept -> std::size_t { - return begin(); + return std::ranges::distance(cbegin(), cend()); } - [[nodiscard]] auto cend() const noexcept -> const_iterator + //! Access an entry by its index. + //! + //! @warning This function does not perform bounds checking. Use `at()` for bounds-checked access. + //! + //! @param index The index of the entry. + //! @return A reference to the entry at the specified index. + [[nodiscard]] auto operator[](std::size_t index) const noexcept -> const_reference { - return end(); + return *(std::next(cbegin(), index)); + } + + //! Access an entry by its index with bounds checking. + //! + //! @warning This function will panic if the index is out-of-bounds for this table. + //! + //! @param index The index of the entry. + //! @return A reference to the entry at the specified index. + [[nodiscard]] auto at(std::size_t index) const -> const_reference + { + if (index >= size()) + { + kstd::os::panic("[ACPI] VLA table: index out of range"); + } + return (*this)[index]; } private: friend TableType; + //! Construct a VLA table. constexpr vla_table() noexcept = default; + + //! Get a pointer to the base of the variable-length array entries. + //! + //! @return A pointer to the first entry in the variable-length array. + [[nodiscard]] auto vla_base() const noexcept -> entry_type * + { + auto base = std::bit_cast<std::byte *>(this); + base += sizeof(TableType); + return reinterpret_cast<entry_type *>(base); + } }; } // namespace acpi diff --git a/libs/acpi/acpi/data/madt.cpp b/libs/acpi/acpi/data/madt.cpp index fe6bae16..64900bd0 100644 --- a/libs/acpi/acpi/data/madt.cpp +++ b/libs/acpi/acpi/data/madt.cpp @@ -13,7 +13,7 @@ namespace acpi struct madt_data { std::uint32_t local_interrupt_controller_address; - std::uint32_t flags; + enum madt::flags flags; }; static_assert(sizeof(madt_data) == 8); @@ -33,7 +33,7 @@ namespace acpi return value; } - auto madt::flags() const noexcept -> std::uint32_t + auto madt::flags() const noexcept -> enum madt::flags { using type = decltype(madt_data::flags); diff --git a/libs/acpi/acpi/data/madt.hpp b/libs/acpi/acpi/data/madt.hpp index 4a929a42..50fd09e1 100644 --- a/libs/acpi/acpi/data/madt.hpp +++ b/libs/acpi/acpi/data/madt.hpp @@ -9,7 +9,6 @@ #include <kstd/bitfield_enum.hpp> #include <kstd/os/error.hpp> -#include <kstd/units.hpp> #include <array> #include <cstddef> @@ -20,20 +19,28 @@ namespace acpi { + //! The MADT (Multiple APIC Description Table) table signature. template<> struct table_signature<struct madt> { constexpr char static const value[] = "APIC"; // NOLINT }; + //! The MADT table type. template<> struct table_type<table_signature_v<struct madt>> { using type = struct madt; }; + //! An entry in the MADT table. + //! + //! MADT entries have a variable size and describe various components of the system's interrupt controllers. 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 { @@ -54,8 +61,9 @@ namespace acpi //! Cast this entry to the given concrete entry type. //! - //! @warning This function will terminate execution if the desired target type does not match up with this entry's - //! actual type. + //! @warning This function will panic if the desired target type does not match up with this entry's actual type. + //! + //! @return A reference to the specific entry. template<typename EntryType> [[nodiscard]] auto as() const -> EntryType const & { @@ -67,17 +75,38 @@ namespace acpi } private: + //! The raw header data of an MADT entry. std::array<std::byte, 2> m_data{}; }; //! The Multiple APIC Description Table (MADT) + //! + //! This table provides information about the system's interrupt controllers, including local APICs and I/O APICs. struct madt : vla_table<madt_entry, madt> { - //! Get the physical address of the local interrupt controllers described by this entry. + //! The flag bits of the MADT table. + enum struct flags : std::uint32_t + { + //! Indicates the system has a PC-AT compatible dual 8259 interrupt controller setup. + //! + //! If this bit is set, the interrupts of the legacy PIC must be masked before enabling the APICs. + dual_8259_pic = 1, + }; + + //! Get the physical address of the local interrupt controller for each CPU. + //! + //! @return The physical address of the local interrupt controller. [[nodiscard]] auto local_interrupt_controller_address() const noexcept -> std::uintptr_t; - [[nodiscard]] auto flags() const noexcept -> std::uint32_t; + //! Get the flags of the MADT table. + //! + //! @return The flags of the MADT table. + [[nodiscard]] auto flags() const noexcept -> flags; + //! Get a view of all entries of the specified type in the MADT table. + //! + //! @tparam EntryType The type of MADT entries to filter. + //! @return A view of all entries of the specified type, if any exist. An empty view otherwise. template<typename EntryType> [[nodiscard]] auto only() const noexcept { @@ -86,29 +115,56 @@ namespace acpi } private: + //! The raw, post-header, data of the MADT table, excluding any table entries. std::array<std::byte, 8> m_data{}; }; + //! A processor local APIC entry in the MADT table. + //! + //! This entry describes a local APIC associated with a processor, including its ID, flags, and processor ID. struct processor_local_apic_entry : madt_entry { + //! The type of this MADT entry. constexpr auto static this_type = type::processor_local_apic; + //! The flag bits of the processor local APIC entry. enum struct flags : std::uint32_t { + //! Indicates that the processor is enabled. processor_enabled = 1, + //! Indicates that the processor is capable of being brought online. online_capable = 2, }; + //! Get the ID of the local APIC. + //! + //! @return The ID of the local APIC. [[nodiscard]] auto id() const noexcept -> std::uint8_t; + + //! Get the flags of the processor local APIC entry. + //! + //! @return The flags of the processor local APIC entry. [[nodiscard]] auto flags() const noexcept -> flags; + + //! Get the processor ID associated with this local APIC entry. + //! + //! @return The processor ID associated with this local APIC entry. [[nodiscard]] auto processor_id() const noexcept -> std::uint32_t; private: + //! The raw data of this entry. std::array<std::byte, 6> m_data{}; }; } // namespace acpi +//! Mark the MADT flags enum as a bitfield enum. +template<> +struct kstd::is_bitfield_enum<enum acpi::madt::flags> : std::true_type +{ +}; + +//! Mark the processor local APIC entry flags enum as a bitfield enum. template<> struct kstd::is_bitfield_enum<enum acpi::processor_local_apic_entry::flags> : std::true_type { 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 new file mode 100644 index 00000000..2c79104c --- /dev/null +++ b/libs/acpi/acpi/data/mcfg.cpp @@ -0,0 +1,56 @@ +#include <acpi/data/mcfg.hpp> + +#include <kstd/cstring.hpp> + +#include <array> +#include <cstddef> +#include <cstdint> + +namespace +{ + + template<typename ValueType, std::size_t Offset, std::size_t ArraySize> + [[nodiscard]] auto read(std::array<std::byte, ArraySize> const & m_data) noexcept -> ValueType + { + static_assert(Offset + sizeof(ValueType) <= ArraySize, "Read out of bounds"); + auto result = ValueType{}; + kstd::libc::memcpy(&result, m_data.data() + Offset, sizeof(ValueType)); + return result; + } + + //! The structure of a single MCFG entry. + struct mcfg_entry_data + { + std::uint64_t base_address; + std::uint16_t segment_group_number; + std::uint8_t start_bus_number; + std::uint8_t end_bus_number; + }; + +} // namespace + +namespace acpi +{ + + auto mcfg_entry::base_address() const noexcept -> std::uintptr_t + { + return read<decltype(mcfg_entry_data::base_address), offsetof(mcfg_entry_data, base_address)>(m_data); + } + + auto mcfg_entry::segment_group_number() const noexcept -> std::uint16_t + { + return read<decltype(mcfg_entry_data::segment_group_number), offsetof(mcfg_entry_data, segment_group_number)>( + m_data); + } + + auto mcfg_entry::start_bus_number() const noexcept -> std::uint8_t + { + return read<decltype(mcfg_entry_data::start_bus_number), offsetof(mcfg_entry_data, start_bus_number)>(m_data); + } + + auto mcfg_entry::end_bus_number() const noexcept -> std::uint8_t + { + return read<decltype(mcfg_entry_data::end_bus_number), offsetof(mcfg_entry_data, end_bus_number)>(m_data); + } + +} // namespace acpi
\ No newline at end of file diff --git a/libs/acpi/acpi/data/mcfg.hpp b/libs/acpi/acpi/data/mcfg.hpp new file mode 100644 index 00000000..b0661154 --- /dev/null +++ b/libs/acpi/acpi/data/mcfg.hpp @@ -0,0 +1,85 @@ +#ifndef ACPI_ACPI_DATA_MCFG_HPP +#define ACPI_ACPI_DATA_MCFG_HPP + +// IWYU pragma: private, include <acpi/acpi.hpp> + +#include <acpi/common/table_signature.hpp> +#include <acpi/common/table_type.hpp> +#include <acpi/common/vla_table.hpp> + +#include <array> +#include <cstddef> +#include <cstdint> + +namespace acpi +{ + + //! The Memory-Mapped Configuration Space (MCFG) table signature. + template<> + struct table_signature<struct mcfg> + { + constexpr char static const value[] = "MCFG"; // NOLINT(modernize-avoid-c-arrays) + }; + + //! The type of the MCFG table. + template<> + struct table_type<table_signature_v<struct mcfg>> + { + using type = struct mcfg; + }; + + //! 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 + //! responsibility of the operating system to map this address appropriately. + //! + //! @return The physical address of the memory-mapped configuration space. + [[nodiscard]] auto base_address() const noexcept -> std::uintptr_t; + + //! Get the PCI segment group number this entry applies to. + //! + //! @return The segment group number. + [[nodiscard]] auto segment_group_number() const noexcept -> std::uint16_t; + + //! Get the number of the first bus this entry applies to. + //! + //! @return The number of the first bus. + [[nodiscard]] auto start_bus_number() const noexcept -> std::uint8_t; + + //! Get the number of the last bus this entry applies to. + //! + //! @return The number of the last bus. + [[nodiscard]] auto end_bus_number() const noexcept -> std::uint8_t; + + //! Get the length of this entry. + //! + //! @return The length of this entry in bytes. + [[nodiscard]] constexpr auto length() const noexcept -> std::size_t + { + return 16; + } + + private: + //! The raw data of this entry. + std::array<std::byte, 16> m_data; + }; + + //! The Memory-Mapped Configuration Space (MCFG) table. + //! + //! This table provides information about the memory-mapped configuration space for PCI and PCIe devices. + struct mcfg : vla_table<mcfg_entry, mcfg> + { + private: + //! The reserved bytes at the end of the MCFG table header. + [[maybe_unused]] std::array<std::byte, 8> m_reserved{}; + }; + +} // namespace acpi + +#endif // ACPI_ACPI_DATA_MCFG_HPP
\ No newline at end of file diff --git a/libs/acpi/acpi/data/mcfg.tests.cpp b/libs/acpi/acpi/data/mcfg.tests.cpp new file mode 100644 index 00000000..e0eb59c7 --- /dev/null +++ b/libs/acpi/acpi/data/mcfg.tests.cpp @@ -0,0 +1,79 @@ +#include <acpi/data/mcfg.hpp> + +#include <acpi/test_data/tables.hpp> + +#include <kstd/test_support/os_panic.hpp> + +#include <catch2/catch_test_macros.hpp> + +#include <iterator> + +SCENARIO("MCFG parsing", "[acpi][mcfg]") +{ + GIVEN("The basic compiled MCFG containing a single entry") + { + auto data = acpi::test_data::tables::basic_mcfg(); + + WHEN("parsing the table") + { + auto mcfg = reinterpret_cast<acpi::mcfg const *>(data.data()); + + THEN("the signature is correct") + { + REQUIRE(mcfg->signature() == "MCFG"); + } + + THEN("validate returns true") + { + REQUIRE(mcfg->validate()); + } + + THEN("there is a single entry in the table") + { + REQUIRE(std::ranges::distance(*mcfg) == 1); + } + + THEN("size is 1") + { + REQUIRE(mcfg->size() == 1); + } + + THEN("the first entry has base address 0x00000000f0000000") + { + REQUIRE(mcfg->cbegin()->base_address() == 0x0000'0000'f000'0000); + } + + THEN("the first entry has segment group 0") + { + REQUIRE(mcfg->cbegin()->segment_group_number() == 0); + } + + THEN("the first entry has start bus number 0") + { + REQUIRE(mcfg->cbegin()->start_bus_number() == 0); + } + + THEN("the first entry has end bus number 0x7f") + { + REQUIRE(mcfg->cbegin()->end_bus_number() == 0x7f); + } + + THEN("accessing the first entry via subscript operator is possible") + { + auto entry = (*mcfg)[0]; + REQUIRE(entry.base_address() == mcfg->cbegin()->base_address()); + } + + THEN("accessing the first entry via at() is possible") + { + auto entry = (*mcfg).at(0); + REQUIRE(entry.base_address() == mcfg->cbegin()->base_address()); + } + + THEN("trying to access the second entry panics") + { + REQUIRE_THROWS_AS((*mcfg).at(1), kstd::tests::os_panic); + } + } + } +}
\ No newline at end of file diff --git a/libs/acpi/acpi/data/rsdt.cpp b/libs/acpi/acpi/data/rsdt.cpp index a8626742..99090beb 100644 --- a/libs/acpi/acpi/data/rsdt.cpp +++ b/libs/acpi/acpi/data/rsdt.cpp @@ -1,7 +1,5 @@ #include <acpi/data/rsdt.hpp> -#include <acpi/common/table_header.hpp> - #include <kstd/cstring.hpp> #include <cstddef> @@ -16,7 +14,7 @@ namespace acpi std::uint32_t address; }; - [[nodiscard]] auto rsdt_entry::address() const noexcept -> table_header * + [[nodiscard]] auto rsdt_entry::address() const noexcept -> std::uintptr_t { using type = decltype(rsdt_entry_data::address); @@ -28,7 +26,7 @@ namespace acpi kstd::libc::memcpy(&value, data.data(), size); - return reinterpret_cast<table_header *>(static_cast<uintptr_t>(value)); + return static_cast<std::uintptr_t>(value); } [[nodiscard]] auto rsdt_entry::length() const noexcept -> std::size_t diff --git a/libs/acpi/acpi/data/rsdt.hpp b/libs/acpi/acpi/data/rsdt.hpp index a6611875..843e6cf2 100644 --- a/libs/acpi/acpi/data/rsdt.hpp +++ b/libs/acpi/acpi/data/rsdt.hpp @@ -1,38 +1,59 @@ #ifndef ACPI_DATA_RSDT_HPP #define ACPI_DATA_RSDT_HPP -#include <acpi/common/table_header.hpp> #include <acpi/common/table_signature.hpp> #include <acpi/common/table_type.hpp> #include <acpi/common/vla_table.hpp> #include <array> #include <cstddef> +#include <cstdint> namespace acpi { + //! The RSDT (Root System Description Table) table signature. template<> struct table_signature<struct rsdt> { constexpr char static const value[] = "RSDT"; // NOLINT }; + //! The RSDT table type. template<> struct table_type<table_signature_v<struct rsdt>> { using type = struct rsdt; }; + //! An entry in the RSDT table. + //! + //! Each entry in the RSDT table points to another ACPI table, providing a way to locate all system description + //! tables. struct rsdt_entry { - [[nodiscard]] auto address() const noexcept -> table_header *; + //! Indicate that all RSDT entries have a fixed size. + constexpr auto static fixed_size = true; + + //! Get the address of the ACPI table pointed to by this entry. + //! + //! @return The physical address of the ACPI table header of the referenced table. + [[nodiscard]] auto address() const noexcept -> std::uintptr_t; + + //! Get the length of the ACPI table pointed to by this entry. + //! + //! @return The length of the referenced ACPI table. [[nodiscard]] auto length() const noexcept -> std::size_t; private: + //! The raw data of this RSDT entry. std::array<std::byte, 4> m_data{}; }; + //! The Root System Description Table (RSDT) + //! + //! This table provides a list of pointers to other ACPI tables, allowing the system to locate all system description + //! tables. struct rsdt : vla_table<rsdt_entry, rsdt> { }; diff --git a/libs/acpi/acpi/data/rsdt.tests.cpp b/libs/acpi/acpi/data/rsdt.tests.cpp index 826d0b4f..691fd631 100644 --- a/libs/acpi/acpi/data/rsdt.tests.cpp +++ b/libs/acpi/acpi/data/rsdt.tests.cpp @@ -34,9 +34,14 @@ 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<acpi::table_header *>(0x10)); + REQUIRE(rsdt->cbegin()->address() == 0x10); } THEN("the length is sizeof(rsdt) + 8 * sizeof(rsdt_entry)") diff --git a/libs/acpi/acpi/data/xsdt.cpp b/libs/acpi/acpi/data/xsdt.cpp index c4b109b8..6f280889 100644 --- a/libs/acpi/acpi/data/xsdt.cpp +++ b/libs/acpi/acpi/data/xsdt.cpp @@ -1,7 +1,5 @@ #include <acpi/data/xsdt.hpp> -#include <acpi/common/table_header.hpp> - #include <kstd/cstring.hpp> #include <cstddef> @@ -16,7 +14,7 @@ namespace acpi std::uint64_t address; }; - [[nodiscard]] auto xsdt_entry::address() const noexcept -> table_header * + [[nodiscard]] auto xsdt_entry::address() const noexcept -> std::uintptr_t { using type = decltype(xsdt_entry_data::address); @@ -28,7 +26,7 @@ namespace acpi kstd::libc::memcpy(&value, data.data(), size); - return reinterpret_cast<table_header *>(static_cast<uintptr_t>(value)); + return static_cast<std::uintptr_t>(value); } [[nodiscard]] auto xsdt_entry::length() const noexcept -> std::size_t diff --git a/libs/acpi/acpi/data/xsdt.hpp b/libs/acpi/acpi/data/xsdt.hpp index 965f23c0..f8c0a775 100644 --- a/libs/acpi/acpi/data/xsdt.hpp +++ b/libs/acpi/acpi/data/xsdt.hpp @@ -1,38 +1,59 @@ #ifndef ACPI_DATA_XSDT_HPP #define ACPI_DATA_XSDT_HPP -#include <acpi/common/table_header.hpp> #include <acpi/common/table_signature.hpp> #include <acpi/common/table_type.hpp> #include <acpi/common/vla_table.hpp> #include <array> #include <cstddef> +#include <cstdint> namespace acpi { + //! The XSDT (Extended System Description Table) table signature. template<> struct table_signature<struct xsdt> { constexpr char static const value[] = "XSDT"; // NOLINT }; + //! The XSDT table type. template<> struct table_type<table_signature_v<struct xsdt>> { using type = struct xsdt; }; + //! An entry in the XSDT table. + //! + //! Each entry in the XSDT table points to another ACPI table, providing a way to locate all system description + //! tables. struct xsdt_entry { - [[nodiscard]] auto address() const noexcept -> table_header *; + //! Indicate that all XSDT entries have a fixed size. + constexpr auto static fixed_size = true; + + //! Get the address of the ACPI table pointed to by this entry. + //! + //! @return The physical address of the ACPI table header of the referenced table. + [[nodiscard]] auto address() const noexcept -> std::uintptr_t; + + //! Get the length of the ACPI table pointed to by this entry. + //! + //! @return The length of the referenced ACPI table. [[nodiscard]] auto length() const noexcept -> std::size_t; private: + //! The raw data of this XSDT entry. std::array<std::byte, 8> m_data{}; }; + //! The Extended System Description Table (XSDT) + //! + //! This table provides a list of pointers to other ACPI tables, allowing the system to locate all system description + //! tables. struct xsdt : vla_table<xsdt_entry, xsdt> { }; diff --git a/libs/acpi/acpi/data/xsdt.tests.cpp b/libs/acpi/acpi/data/xsdt.tests.cpp index 19d9bcdb..955c08df 100644 --- a/libs/acpi/acpi/data/xsdt.tests.cpp +++ b/libs/acpi/acpi/data/xsdt.tests.cpp @@ -34,9 +34,14 @@ 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<acpi::table_header *>(0x10)); + REQUIRE(xsdt->cbegin()->address() == 0x10); } THEN("the length is sizeof(xsdt) + 8 * sizeof(xsdt_entry)") diff --git a/libs/acpi/acpi/test_data/tables.S b/libs/acpi/acpi/test_data/tables.S index 641db6a8..b60b21be 100644 --- a/libs/acpi/acpi/test_data/tables.S +++ b/libs/acpi/acpi/test_data/tables.S @@ -9,6 +9,7 @@ name##_end: TABLE(basic_madt, "basic_madt.aml") +TABLE(basic_mcfg, "basic_mcfg.aml") TABLE(basic_rsdt, "basic_rsdt.aml") TABLE(basic_rsdp, "basic_rsdp.aml") TABLE(basic_xsdt, "basic_xsdt.aml") diff --git a/libs/acpi/acpi/test_data/tables.hpp b/libs/acpi/acpi/test_data/tables.hpp index e91f1a5a..60c6dcbe 100644 --- a/libs/acpi/acpi/test_data/tables.hpp +++ b/libs/acpi/acpi/test_data/tables.hpp @@ -16,6 +16,7 @@ namespace acpi::test_data::tables { TABLE(basic_madt); + TABLE(basic_mcfg); TABLE(basic_rsdt); TABLE(basic_rsdp); TABLE(basic_xsdt); diff --git a/libs/acpi/test_data/basic_mcfg.asl b/libs/acpi/test_data/basic_mcfg.asl new file mode 100644 index 00000000..3daaecc2 --- /dev/null +++ b/libs/acpi/test_data/basic_mcfg.asl @@ -0,0 +1,25 @@ +/* + * Intel ACPI Component Architecture + * iASL Compiler/Disassembler version 20251212 (64-bit version) + * Copyright (c) 2000 - 2025 Intel Corporation + * + * Template for [MCFG] ACPI Table (static data table) + * Format: [ByteLength] FieldName : HexFieldValue + */ +[0004] Signature : "MCFG" [Memory Mapped Configuration Table] +[0004] Table Length : 0000003C +[0001] Revision : 01 +[0001] Checksum : BE +[0006] Oem ID : "INTEL " +[0008] Oem Table ID : "Template" +[0004] Oem Revision : 00000001 +[0004] Asl Compiler ID : "INTL" +[0004] Asl Compiler Revision : 20100528 + +[0008] Reserved : 0000000000000000 + +[0008] Base Address : 00000000F0000000 +[0002] Segment Group Number : 0000 +[0001] Start Bus Number : 00 +[0001] End Bus Number : 7F +[0004] Reserved : 00000000 diff --git a/libs/kstd/kstd/test_support/os_panic.tests.cpp b/libs/kstd/kstd/test_support/os_panic.cpp index 17af74a7..17af74a7 100644 --- a/libs/kstd/kstd/test_support/os_panic.tests.cpp +++ b/libs/kstd/kstd/test_support/os_panic.cpp |
