From 7676db54f6c3f5e19d78b2d2b0250e1838c30ef6 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 11 Nov 2026 10:20:52 +0100 Subject: libs/acpi: add basic test data for MCFG table --- libs/acpi/CMakeLists.txt | 1 + libs/acpi/acpi/test_data/tables.S | 1 + libs/acpi/acpi/test_data/tables.hpp | 1 + libs/acpi/test_data/basic_mcfg.asl | 25 +++++++++++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 libs/acpi/test_data/basic_mcfg.asl diff --git a/libs/acpi/CMakeLists.txt b/libs/acpi/CMakeLists.txt index 9fc8c719..847223a8 100644 --- a/libs/acpi/CMakeLists.txt +++ b/libs/acpi/CMakeLists.txt @@ -68,6 +68,7 @@ if(BUILD_TESTING) set(TEST_TABLES "basic_madt" + "basic_mcfg" "basic_rsdt" "basic_rsdp" "basic_xsdt" 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 -- cgit v1.2.3 From f639297d1b96b6aeb3d3822a63b9a602e1f814fb Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 11 Nov 2026 14:29:25 +0100 Subject: libs/acpi: implement MCFG table support --- libs/acpi/CMakeLists.txt | 1 + libs/acpi/acpi/acpi.hpp | 1 + libs/acpi/acpi/data/mcfg.cpp | 38 ++++++++++++++++++++++ libs/acpi/acpi/data/mcfg.hpp | 65 ++++++++++++++++++++++++++++++++++++++ libs/acpi/acpi/data/mcfg.tests.cpp | 55 ++++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+) create mode 100644 libs/acpi/acpi/data/mcfg.cpp create mode 100644 libs/acpi/acpi/data/mcfg.hpp create mode 100644 libs/acpi/acpi/data/mcfg.tests.cpp diff --git a/libs/acpi/CMakeLists.txt b/libs/acpi/CMakeLists.txt index 847223a8..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" 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 // IWYU pragma: export #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/libs/acpi/acpi/data/mcfg.cpp b/libs/acpi/acpi/data/mcfg.cpp new file mode 100644 index 00000000..ffb2933d --- /dev/null +++ b/libs/acpi/acpi/data/mcfg.cpp @@ -0,0 +1,38 @@ +#include + +#include + +namespace +{ + + constexpr auto static base_address_offset = 0uz; + constexpr auto static segment_group_number_offset = 8uz; + constexpr auto static start_bus_number_offset = 10uz; + constexpr auto static end_bus_number_offset = 11uz; + +} // namespace + +namespace acpi +{ + + auto mcfg_entry::base_address() const noexcept -> std::uintptr_t + { + return read(); + } + + auto mcfg_entry::segment_group_number() const noexcept -> std::uint16_t + { + return read(); + } + + auto mcfg_entry::start_bus_number() const noexcept -> std::uint8_t + { + return read(); + } + + auto mcfg_entry::end_bus_number() const noexcept -> std::uint8_t + { + return read(); + } + +} // 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..32d5fce4 --- /dev/null +++ b/libs/acpi/acpi/data/mcfg.hpp @@ -0,0 +1,65 @@ +#ifndef ACPI_ACPI_DATA_MCFG_HPP +#define ACPI_ACPI_DATA_MCFG_HPP + +// IWYU pragma: private, include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +namespace acpi +{ + + template<> + struct table_signature + { + constexpr char static const value[] = "MCFG"; // NOLINT(modernize-avoid-c-arrays) + }; + + template<> + struct table_type> + { + using type = struct mcfg; + }; + + struct mcfg_entry + { + [[nodiscard]] auto base_address() const noexcept -> std::uintptr_t; + [[nodiscard]] auto segment_group_number() const noexcept -> std::uint16_t; + [[nodiscard]] auto start_bus_number() const noexcept -> std::uint8_t; + [[nodiscard]] auto end_bus_number() const noexcept -> std::uint8_t; + + [[nodiscard]] constexpr auto length() const noexcept -> std::size_t + { + return 16; + } + + private: + template + [[nodiscard]] auto read() const noexcept -> ValueType + { + static_assert(Offset + sizeof(ValueType) <= std::tuple_size_v, "Read out of bounds"); + auto result = ValueType{}; + kstd::libc::memcpy(&result, m_data.data() + Offset, sizeof(ValueType)); + return result; + } + + std::array m_data; + }; + + struct mcfg : vla_table + { + private: + [[maybe_unused]] std::array 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..9cbe475e --- /dev/null +++ b/libs/acpi/acpi/data/mcfg.tests.cpp @@ -0,0 +1,55 @@ +#include + +#include + +#include + +#include + +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(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("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); + } + } + } +} \ No newline at end of file -- cgit v1.2.3 From ef6bf8e59c7d86d4c3b3f43233755a060bd4f891 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 11 Nov 2026 15:06:37 +0100 Subject: libs/acpi: extend MCFG table interface --- cmake/Modules/BuildHostTests.cmake | 18 +++++++++ kernel/CMakeLists.txt | 18 --------- libs/acpi/acpi/common/vla_table.hpp | 13 +++++-- libs/acpi/acpi/data/mcfg.cpp | 29 +++++++++++++++ libs/acpi/acpi/data/mcfg.hpp | 51 ++++++++++++++++++++++++++ libs/acpi/acpi/data/mcfg.tests.cpp | 24 ++++++++++++ libs/kstd/kstd/test_support/os_panic.cpp | 15 ++++++++ libs/kstd/kstd/test_support/os_panic.tests.cpp | 15 -------- 8 files changed, 147 insertions(+), 36 deletions(-) create mode 100644 libs/kstd/kstd/test_support/os_panic.cpp delete mode 100644 libs/kstd/kstd/test_support/os_panic.tests.cpp diff --git a/cmake/Modules/BuildHostTests.cmake b/cmake/Modules/BuildHostTests.cmake index f124728b..65003e70 100644 --- a/cmake/Modules/BuildHostTests.cmake +++ b/cmake/Modules/BuildHostTests.cmake @@ -4,6 +4,24 @@ function("teachos_add_tests" NAME) add_executable("${NAME}_tests") add_executable("${NAME}::tests" ALIAS "${NAME}_tests") + get_target_property(LIBRARY_TARGET + "${NAME}::lib" + ALIASED_TARGET + ) + + file(GLOB_RECURSE + TEST_SUPPORT_SOURCES + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + CONFIGURE_DEPENDS + "**/test_support/**.cpp" + ) + + message(STATUS "Library target: ${LIBRARY_TARGET} \n TEST_SUPPORT_SOURCES: ${TEST_SUPPORT_SOURCES}") + + target_sources("${LIBRARY_TARGET}" PRIVATE + ${TEST_SUPPORT_SOURCES} + ) + file(GLOB_RECURSE TEST_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 2ed3ac62..d76185bf 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -170,24 +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 - "$<$:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/kernel/test_support/string_makers.hpp>" - ) - target_compile_definitions("${TEST_TARGET}" PRIVATE KERNEL_TEST_ASSETS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/kernel/test_support/vfs/test_assets" ) diff --git a/libs/acpi/acpi/common/vla_table.hpp b/libs/acpi/acpi/common/vla_table.hpp index d3f33a7c..7993e2db 100644 --- a/libs/acpi/acpi/common/vla_table.hpp +++ b/libs/acpi/acpi/common/vla_table.hpp @@ -74,11 +74,10 @@ namespace acpi [[nodiscard]] auto begin() noexcept -> iterator { - auto base = std::bit_cast(this); - base += sizeof(TableType); + auto base = vla_base(); auto limit = std::bit_cast(this); limit += this->length().value; - return iterator{std::bit_cast(base), std::bit_cast(limit)}; + return iterator{base, std::bit_cast(limit)}; } [[nodiscard]] auto end() noexcept -> iterator @@ -110,6 +109,14 @@ namespace acpi return end(); } + protected: + [[nodiscard]] auto vla_base() const noexcept -> entry_type * + { + auto base = std::bit_cast(this); + base += sizeof(TableType); + return reinterpret_cast(base); + } + private: friend TableType; diff --git a/libs/acpi/acpi/data/mcfg.cpp b/libs/acpi/acpi/data/mcfg.cpp index ffb2933d..9e1b9d4d 100644 --- a/libs/acpi/acpi/data/mcfg.cpp +++ b/libs/acpi/acpi/data/mcfg.cpp @@ -1,13 +1,23 @@ #include +#include + +#include #include namespace { + //! The offset of the base address field within an MCFG entry. constexpr auto static base_address_offset = 0uz; + + //! The offset of the segment group number field within an MCFG entry. constexpr auto static segment_group_number_offset = 8uz; + + //! The offset of the start bus number field within an MCFG entry. constexpr auto static start_bus_number_offset = 10uz; + + //! The offset of the end bus number field within an MCFG entry. constexpr auto static end_bus_number_offset = 11uz; } // namespace @@ -35,4 +45,23 @@ 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 32d5fce4..881f479a 100644 --- a/libs/acpi/acpi/data/mcfg.hpp +++ b/libs/acpi/acpi/data/mcfg.hpp @@ -29,19 +29,46 @@ namespace acpi using type = struct mcfg; }; + //! An entry in the MCFG table. struct mcfg_entry { + //! 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: + //! Read a value of a specified type at a specified offset within this entry. + //! + //! @tparam ValueType The type of value to read. + //! @tparam Offset The offset within the entry to read from. + //! @return The value read from the specified offset. template [[nodiscard]] auto read() const noexcept -> ValueType { @@ -51,12 +78,36 @@ namespace acpi return result; } + //! The raw data of this entry. std::array 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 { + //! 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/mcfg.tests.cpp b/libs/acpi/acpi/data/mcfg.tests.cpp index 9cbe475e..e0eb59c7 100644 --- a/libs/acpi/acpi/data/mcfg.tests.cpp +++ b/libs/acpi/acpi/data/mcfg.tests.cpp @@ -2,6 +2,8 @@ #include +#include + #include #include @@ -31,6 +33,11 @@ SCENARIO("MCFG parsing", "[acpi][mcfg]") 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); @@ -50,6 +57,23 @@ SCENARIO("MCFG parsing", "[acpi][mcfg]") { 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/kstd/kstd/test_support/os_panic.cpp b/libs/kstd/kstd/test_support/os_panic.cpp new file mode 100644 index 00000000..17af74a7 --- /dev/null +++ b/libs/kstd/kstd/test_support/os_panic.cpp @@ -0,0 +1,15 @@ +#include + +#include +#include +#include + +namespace kstd::os +{ + + auto panic(std::string_view message, std::source_location location) -> void + { + throw kstd::tests::os_panic{std::string{message}, location}; + } + +} // namespace kstd::os \ No newline at end of file diff --git a/libs/kstd/kstd/test_support/os_panic.tests.cpp b/libs/kstd/kstd/test_support/os_panic.tests.cpp deleted file mode 100644 index 17af74a7..00000000 --- a/libs/kstd/kstd/test_support/os_panic.tests.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#include -#include -#include - -namespace kstd::os -{ - - auto panic(std::string_view message, std::source_location location) -> void - { - throw kstd::tests::os_panic{std::string{message}, location}; - } - -} // namespace kstd::os \ No newline at end of file -- cgit v1.2.3 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(-) 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 From deb8f19936ead3e995d418d2c87ea030d7fe434d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 11 Nov 2026 17:52:12 +0100 Subject: libs/acpi: improve common documentation --- libs/acpi/acpi/common/basic_table.hpp | 9 ++ libs/acpi/acpi/common/table_signature.hpp | 6 + libs/acpi/acpi/common/table_type.hpp | 7 ++ libs/acpi/acpi/common/vla_table.hpp | 184 ++++++++++++++++++++++++++---- 4 files changed, 183 insertions(+), 23 deletions(-) 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 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 { return {reinterpret_cast(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 && 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 struct table_signature; + //! The signature for a given ACPI table type. + //! + //! @tparam TableType The ACPI table type. template constexpr auto table_signature_v = table_signature::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 struct table_type; + //! The table type corresponding to a given ACPI table signature. + //! + //! @tparam Signature The ACPI table signature. template using table_type_t = typename table_type::type; diff --git a/libs/acpi/acpi/common/vla_table.hpp b/libs/acpi/acpi/common/vla_table.hpp index e003c4ca..e236c58b 100644 --- a/libs/acpi/acpi/common/vla_table.hpp +++ b/libs/acpi/acpi/common/vla_table.hpp @@ -23,6 +23,9 @@ namespace acpi 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(this); @@ -31,6 +34,11 @@ namespace acpi 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(this); } { @@ -40,16 +48,27 @@ namespace acpi 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 { @@ -59,24 +78,35 @@ namespace acpi 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 struct vla_table_iterator : detail::common_vla_table_iterator_base> @@ -92,12 +122,20 @@ namespace acpi 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(this->m_entry); @@ -107,6 +145,7 @@ namespace acpi } }; + //! An iterator for variable-length array (VLA) tables with fixed-sized entries. template struct vla_table_iterator : detail::common_vla_table_iterator_base> @@ -123,75 +162,135 @@ namespace acpi 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 : super{entry, limit} {} - constexpr auto operator<=>(vla_table_iterator const & other) const noexcept -> std::strong_ordering + //! 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 this->m_entry <=> other.m_entry; + return lhs.m_entry <=> rhs.m_entry; } - constexpr auto operator==(vla_table_iterator const & other) const noexcept -> bool + //! 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 this->m_entry == other.m_entry; + return lhs.m_entry == rhs.m_entry; } - constexpr auto operator==(std::default_sentinel_t) const noexcept -> bool + //! 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 this->m_entry == nullptr || this->m_entry == this->m_limit; + 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 & { ++this->m_entry; return *this; } + //! Move the iterator to the previous entry. + //! + //! @return A reference to the updated 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 + //! 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 this->m_entry - other.m_entry; + return lhs.m_entry - rhs.m_entry; } - constexpr auto operator+=(difference_type n) noexcept -> vla_table_iterator & + //! 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 & { - this->m_entry += n; - return *this; + lhs.m_entry += n; + return lhs; } - constexpr auto operator+(difference_type n) const noexcept -> vla_table_iterator + //! 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 { - auto copy = *this; - copy += n; - return copy; + it += n; + return it; } - constexpr auto friend operator+(difference_type n, vla_table_iterator const & it) noexcept -> vla_table_iterator + //! 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; } - constexpr auto operator-=(difference_type n) noexcept -> vla_table_iterator & + //! 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 & { - this->m_entry -= n; - return *this; + it.m_entry -= n; + return it; } - constexpr auto operator-(difference_type n) const noexcept -> vla_table_iterator + //! 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 { - auto copy = *this; - copy -= n; - return copy; + it -= n; + return it; } + //! 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); @@ -207,26 +306,41 @@ namespace acpi 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 { 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 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(this); @@ -236,6 +350,9 @@ namespace acpi return const_iterator{std::bit_cast(base), std::bit_cast(limit)}; } + //! 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 { auto limit = std::bit_cast(this); @@ -243,16 +360,33 @@ namespace acpi return const_iterator{std::bit_cast(limit), std::bit_cast(limit)}; } + //! 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 std::ranges::distance(cbegin(), cend()); } + //! 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 *(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()) @@ -265,8 +399,12 @@ namespace acpi 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(this); -- cgit v1.2.3 From ab8de113fb03736e49d89b2cccc31f9cae355500 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 12 Nov 2026 10:10:10 +0100 Subject: libs/acpi: improve data docs --- libs/acpi/acpi/data/madt.cpp | 4 +-- libs/acpi/acpi/data/madt.hpp | 63 ++++++++++++++++++++++++++++++++++++++++---- libs/acpi/acpi/data/mcfg.hpp | 2 ++ libs/acpi/acpi/data/rsdt.hpp | 18 +++++++++++++ libs/acpi/acpi/data/xsdt.hpp | 20 +++++++++++++- 5 files changed, 99 insertions(+), 8 deletions(-) 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 1fca05da..50fd09e1 100644 --- a/libs/acpi/acpi/data/madt.hpp +++ b/libs/acpi/acpi/data/madt.hpp @@ -9,7 +9,6 @@ #include #include -#include #include #include @@ -20,18 +19,23 @@ namespace acpi { + //! The MADT (Multiple APIC Description Table) table signature. template<> struct table_signature { constexpr char static const value[] = "APIC"; // NOLINT }; + //! The MADT table type. template<> struct table_type> { 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. @@ -57,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 [[nodiscard]] auto as() const -> EntryType const & { @@ -70,17 +75,38 @@ namespace acpi } private: + //! The raw header data of an MADT entry. std::array 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 { - //! 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 [[nodiscard]] auto only() const noexcept { @@ -89,29 +115,56 @@ namespace acpi } private: + //! The raw, post-header, data of the MADT table, excluding any table entries. std::array 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 m_data{}; }; } // namespace acpi +//! Mark the MADT flags enum as a bitfield enum. +template<> +struct kstd::is_bitfield_enum : std::true_type +{ +}; + +//! Mark the processor local APIC entry flags enum as a bitfield enum. template<> struct kstd::is_bitfield_enum : std::true_type { diff --git a/libs/acpi/acpi/data/mcfg.hpp b/libs/acpi/acpi/data/mcfg.hpp index 24097b44..21f3293c 100644 --- a/libs/acpi/acpi/data/mcfg.hpp +++ b/libs/acpi/acpi/data/mcfg.hpp @@ -17,12 +17,14 @@ namespace acpi { + //! The Memory-Mapped Configuration Space (MCFG) table signature. template<> struct table_signature { constexpr char static const value[] = "MCFG"; // NOLINT(modernize-avoid-c-arrays) }; + //! The type of the MCFG table. template<> struct table_type> { diff --git a/libs/acpi/acpi/data/rsdt.hpp b/libs/acpi/acpi/data/rsdt.hpp index c19a9ea4..ca36b33d 100644 --- a/libs/acpi/acpi/data/rsdt.hpp +++ b/libs/acpi/acpi/data/rsdt.hpp @@ -12,30 +12,48 @@ namespace acpi { + //! The RSDT (Root System Description Table) table signature. template<> struct table_signature { constexpr char static const value[] = "RSDT"; // NOLINT }; + //! The RSDT table type. template<> struct table_type> { 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 { //! 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 A pointer to the ACPI table header of the referenced table. [[nodiscard]] auto address() const noexcept -> table_header *; + + //! 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 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 { }; diff --git a/libs/acpi/acpi/data/xsdt.hpp b/libs/acpi/acpi/data/xsdt.hpp index f09e2e5e..ef268d5f 100644 --- a/libs/acpi/acpi/data/xsdt.hpp +++ b/libs/acpi/acpi/data/xsdt.hpp @@ -12,30 +12,48 @@ namespace acpi { + //! The XSDT (Extended System Description Table) table signature. template<> struct table_signature { constexpr char static const value[] = "XSDT"; // NOLINT }; + //! The XSDT table type. template<> struct table_type> { 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 { - //! Indicate that all RSDT entries have a fixed size. + //! 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 A pointer to the ACPI table header of the referenced table. [[nodiscard]] auto address() const noexcept -> table_header *; + + //! 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 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 { }; -- cgit v1.2.3 From 83510526b66c17ed26ac86e75eb1632ed0630138 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 12 Nov 2026 10:15:32 +0100 Subject: libs/acpi: don't convert addresses to pointer Previously, ACPI table addresses in both the RSDT and XSDT were converted to pointers. However, this is not actually correct, since within the object model of the kernel, pointers always refer to objects by their linear address, and the addresses encoded in ACPI tables are, by necessity, physical addresses. The API should therefore be honest about the fact that the addresses returned by accessors of the RSDT and XSDT are physical addresses, not pointers to live objects. --- kernel/kernel/acpi/manager.cpp | 3 +-- kernel/kernel/acpi/manager.hpp | 1 - libs/acpi/acpi/data/rsdt.cpp | 6 ++---- libs/acpi/acpi/data/rsdt.hpp | 6 +++--- libs/acpi/acpi/data/rsdt.tests.cpp | 2 +- libs/acpi/acpi/data/xsdt.cpp | 6 ++---- libs/acpi/acpi/data/xsdt.hpp | 6 +++--- libs/acpi/acpi/data/xsdt.tests.cpp | 2 +- 8 files changed, 13 insertions(+), 19 deletions(-) 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 #include -#include #include #include @@ -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(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>(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 #include -#include #include 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 -#include - #include #include @@ -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(static_cast(value)); + return static_cast(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 ca36b33d..843e6cf2 100644 --- a/libs/acpi/acpi/data/rsdt.hpp +++ b/libs/acpi/acpi/data/rsdt.hpp @@ -1,13 +1,13 @@ #ifndef ACPI_DATA_RSDT_HPP #define ACPI_DATA_RSDT_HPP -#include #include #include #include #include #include +#include namespace acpi { @@ -37,8 +37,8 @@ namespace acpi //! Get the address of the ACPI table pointed to by this entry. //! - //! @return A pointer to the ACPI table header of the referenced table. - [[nodiscard]] auto address() const noexcept -> table_header *; + //! @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. //! diff --git a/libs/acpi/acpi/data/rsdt.tests.cpp b/libs/acpi/acpi/data/rsdt.tests.cpp index c111f749..691fd631 100644 --- a/libs/acpi/acpi/data/rsdt.tests.cpp +++ b/libs/acpi/acpi/data/rsdt.tests.cpp @@ -41,7 +41,7 @@ SCENARIO("RSDT parsing", "[rsdt]") THEN("the first entry has address 0x10") { - REQUIRE(rsdt->cbegin()->address() == reinterpret_cast(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 -#include - #include #include @@ -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(static_cast(value)); + return static_cast(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 ef268d5f..f8c0a775 100644 --- a/libs/acpi/acpi/data/xsdt.hpp +++ b/libs/acpi/acpi/data/xsdt.hpp @@ -1,13 +1,13 @@ #ifndef ACPI_DATA_XSDT_HPP #define ACPI_DATA_XSDT_HPP -#include #include #include #include #include #include +#include namespace acpi { @@ -37,8 +37,8 @@ namespace acpi //! Get the address of the ACPI table pointed to by this entry. //! - //! @return A pointer to the ACPI table header of the referenced table. - [[nodiscard]] auto address() const noexcept -> table_header *; + //! @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. //! diff --git a/libs/acpi/acpi/data/xsdt.tests.cpp b/libs/acpi/acpi/data/xsdt.tests.cpp index e5eaed67..955c08df 100644 --- a/libs/acpi/acpi/data/xsdt.tests.cpp +++ b/libs/acpi/acpi/data/xsdt.tests.cpp @@ -41,7 +41,7 @@ SCENARIO("XSDT parsing", "[xsdt]") THEN("the first entry has address 0x10") { - REQUIRE(xsdt->cbegin()->address() == reinterpret_cast(0x10)); + REQUIRE(xsdt->cbegin()->address() == 0x10); } THEN("the length is sizeof(xsdt) + 8 * sizeof(xsdt_entry)") -- cgit v1.2.3 From 1328752bd06900d7b0b45a2cb7e821b8fe58ea37 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 12 Nov 2026 10:24:17 +0100 Subject: libs/acpi: clean up MCFG entry reading code --- libs/acpi/acpi/data/mcfg.cpp | 39 +++++++++++++++++++++++++-------------- libs/acpi/acpi/data/mcfg.hpp | 17 ----------------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/libs/acpi/acpi/data/mcfg.cpp b/libs/acpi/acpi/data/mcfg.cpp index 008a8ca3..2c79104c 100644 --- a/libs/acpi/acpi/data/mcfg.cpp +++ b/libs/acpi/acpi/data/mcfg.cpp @@ -1,21 +1,31 @@ #include +#include + +#include +#include #include namespace { - //! The offset of the base address field within an MCFG entry. - constexpr auto static base_address_offset = 0uz; - - //! The offset of the segment group number field within an MCFG entry. - constexpr auto static segment_group_number_offset = 8uz; - - //! The offset of the start bus number field within an MCFG entry. - constexpr auto static start_bus_number_offset = 10uz; + template + [[nodiscard]] auto read(std::array 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 offset of the end bus number field within an MCFG entry. - constexpr auto static end_bus_number_offset = 11uz; + //! 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 @@ -24,22 +34,23 @@ namespace acpi auto mcfg_entry::base_address() const noexcept -> std::uintptr_t { - return read(); + return read(m_data); } auto mcfg_entry::segment_group_number() const noexcept -> std::uint16_t { - return read(); + return read( + m_data); } auto mcfg_entry::start_bus_number() const noexcept -> std::uint8_t { - return read(); + return read(m_data); } auto mcfg_entry::end_bus_number() const noexcept -> std::uint8_t { - return read(); + return read(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 index 21f3293c..b0661154 100644 --- a/libs/acpi/acpi/data/mcfg.hpp +++ b/libs/acpi/acpi/data/mcfg.hpp @@ -7,12 +7,9 @@ #include #include -#include - #include #include #include -#include namespace acpi { @@ -69,20 +66,6 @@ namespace acpi } private: - //! Read a value of a specified type at a specified offset within this entry. - //! - //! @tparam ValueType The type of value to read. - //! @tparam Offset The offset within the entry to read from. - //! @return The value read from the specified offset. - template - [[nodiscard]] auto read() const noexcept -> ValueType - { - static_assert(Offset + sizeof(ValueType) <= std::tuple_size_v, "Read out of bounds"); - auto result = ValueType{}; - kstd::libc::memcpy(&result, m_data.data() + Offset, sizeof(ValueType)); - return result; - } - //! The raw data of this entry. std::array m_data; }; -- cgit v1.2.3 From 875024f7113a6ee229a8283af10ccb2091a812ae Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 12 Nov 2026 13:43:18 +0100 Subject: build: fix stress test linking --- cmake/Modules/BuildHostTests.cmake | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cmake/Modules/BuildHostTests.cmake b/cmake/Modules/BuildHostTests.cmake index 65003e70..27a92f25 100644 --- a/cmake/Modules/BuildHostTests.cmake +++ b/cmake/Modules/BuildHostTests.cmake @@ -16,8 +16,6 @@ function("teachos_add_tests" NAME) "**/test_support/**.cpp" ) - message(STATUS "Library target: ${LIBRARY_TARGET} \n TEST_SUPPORT_SOURCES: ${TEST_SUPPORT_SOURCES}") - target_sources("${LIBRARY_TARGET}" PRIVATE ${TEST_SUPPORT_SOURCES} ) @@ -61,6 +59,22 @@ function("teachos_add_stress_tests" NAME) add_executable("${NAME}_stress_tests") add_executable("${NAME}::stress_tests" ALIAS "${NAME}_stress_tests") + get_target_property(LIBRARY_TARGET + "${NAME}::lib" + ALIASED_TARGET + ) + + file(GLOB_RECURSE + TEST_SUPPORT_SOURCES + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + CONFIGURE_DEPENDS + "**/test_support/**.cpp" + ) + + target_sources("${LIBRARY_TARGET}" PRIVATE + ${TEST_SUPPORT_SOURCES} + ) + file(GLOB_RECURSE TEST_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} -- cgit v1.2.3 From 6227946bcae27887c31b54e823ce56976c439bbf Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 12 Nov 2026 13:56:29 +0100 Subject: cmake: clean up test target generation --- cmake/Modules/BuildHostTests.cmake | 135 ++++++++++++++----------------------- 1 file changed, 49 insertions(+), 86 deletions(-) diff --git a/cmake/Modules/BuildHostTests.cmake b/cmake/Modules/BuildHostTests.cmake index 27a92f25..fab89bb4 100644 --- a/cmake/Modules/BuildHostTests.cmake +++ b/cmake/Modules/BuildHostTests.cmake @@ -1,110 +1,73 @@ 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}") - get_target_property(LIBRARY_TARGET - "${NAME}::lib" - ALIASED_TARGET - ) + get_target_property(LIBRARY_TARGET "${NAME}::lib" ALIASED_TARGET) file(GLOB_RECURSE - TEST_SUPPORT_SOURCES - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - CONFIGURE_DEPENDS - "**/test_support/**.cpp" - ) + TEST_SUPPORT_SOURCES + RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" + CONFIGURE_DEPENDS + "**/test_support/**.cpp" + ) + + message(STATUS "Registering test support sources for ${NAME}::${TYPE}") + foreach(SOURCE IN LISTS TEST_SUPPORT_SOURCES) + message(DEBUG "${CMAKE_MESSAGE_INDENT}${SOURCE}") + endforeach() + target_sources("${LIBRARY_TARGET}" PRIVATE ${TEST_SUPPORT_SOURCES} - ) + ) file(GLOB_RECURSE - TEST_SOURCES - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - CONFIGURE_DEPENDS - "*.tests.cpp" - ) - - set_source_files_properties(${TEST_SOURCES} PROPERTIES - SKIP_LINTING YES + TEST_SOURCES + RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" + CONFIGURE_DEPENDS + "*.tests.cpp" ) - target_sources("${NAME}_tests" PRIVATE - ${TEST_SOURCES} - ) - - target_link_libraries("${NAME}_tests" PRIVATE - "Catch2::Catch2WithMain" - "${NAME}::lib" - ) + message(STATUS "Registering test sources for ${NAME}::${TYPE}") + foreach(SOURCE IN LISTS TEST_SOURCES) + message(DEBUG "${CMAKE_MESSAGE_INDENT}${SOURCE}") + endforeach() - set_target_properties("${NAME}_tests" PROPERTIES - EXCLUDE_FROM_ALL NO - ) - file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bht_results") - - 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() + set_source_files_properties(${TEST_SOURCES} PROPERTIES + SKIP_LINTING YES + ) -function("teachos_add_stress_tests" NAME) - add_executable("${NAME}_stress_tests") - add_executable("${NAME}::stress_tests" ALIAS "${NAME}_stress_tests") + target_sources("${NAME}_${TYPE}" PRIVATE + ${TEST_SOURCES} + ) - get_target_property(LIBRARY_TARGET + target_link_libraries("${NAME}_${TYPE}" PRIVATE + "Catch2::Catch2WithMain" "${NAME}::lib" - ALIASED_TARGET - ) - - file(GLOB_RECURSE - TEST_SUPPORT_SOURCES - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - CONFIGURE_DEPENDS - "**/test_support/**.cpp" - ) - - target_sources("${LIBRARY_TARGET}" PRIVATE - ${TEST_SUPPORT_SOURCES} - ) - - file(GLOB_RECURSE - TEST_SOURCES - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - CONFIGURE_DEPENDS - "*.stress.cpp" - ) - - set_source_files_properties(${TEST_SOURCES} PROPERTIES - SKIP_LINTING YES ) - target_sources("${NAME}_stress_tests" PRIVATE - ${TEST_SOURCES} - ) + set_target_properties("${NAME}_${TYPE}" PROPERTIES + EXCLUDE_FROM_ALL NO + ) - target_link_libraries("${NAME}_stress_tests" PRIVATE - "Catch2::Catch2WithMain" - "${NAME}::lib" - ) + file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bht_results") - set_target_properties("${NAME}_stress_tests" PROPERTIES - EXCLUDE_FROM_ALL NO - ) + 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() - file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bht_results") +function("teachos_add_tests" NAME) + _teachos_add_tests_impl("${NAME}" "tests") +endfunction() - catch_discover_tests("${NAME}::stress_tests" - REPORTER junit - OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/bht_results" - OUTPUT_SUFFIX ".xml" - ADD_TAGS_AS_LABELS - ) +function("teachos_add_stress_tests" NAME) + _teachos_add_tests_impl("${NAME}" "stress_tests") endfunction() -- cgit v1.2.3 From a70ad14527076b556778eea40034fb297eaceb04 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 12 Nov 2026 20:01:30 +0100 Subject: build: fix test executables --- cmake/Modules/BuildHostTests.cmake | 13 ++++++++++--- kernel/CMakeLists.txt | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cmake/Modules/BuildHostTests.cmake b/cmake/Modules/BuildHostTests.cmake index fab89bb4..c254e30a 100644 --- a/cmake/Modules/BuildHostTests.cmake +++ b/cmake/Modules/BuildHostTests.cmake @@ -13,21 +13,28 @@ function("_teachos_add_tests_impl" NAME TYPE) "**/test_support/**.cpp" ) + list(FILTER TEST_SUPPORT_SOURCES EXCLUDE REGEX ".tests.cpp") + message(STATUS "Registering test support sources for ${NAME}::${TYPE}") foreach(SOURCE IN LISTS TEST_SUPPORT_SOURCES) message(DEBUG "${CMAKE_MESSAGE_INDENT}${SOURCE}") endforeach() - target_sources("${LIBRARY_TARGET}" PRIVATE ${TEST_SUPPORT_SOURCES} ) + if(TYPE STREQUAL "stress_tests") + set(FILE_COMPONENT "stress") + else() + set(FILE_COMPONENT "tests") + endif() + file(GLOB_RECURSE TEST_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS - "*.tests.cpp" + "*.${FILE_COMPONENT}.cpp" ) message(STATUS "Registering test sources for ${NAME}::${TYPE}") @@ -57,7 +64,7 @@ function("_teachos_add_tests_impl" NAME TYPE) catch_discover_tests("${NAME}::${TYPE}" REPORTER junit - EXTRA_ARGS --reporter console::out=-::colour-mode=ansi + EXTRA_ARGS "--reporter console::out=-::colour-mode=ansi" OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/bht_results" OUTPUT_SUFFIX ".xml" ADD_TAGS_AS_LABELS diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index d76185bf..e105ae62 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -170,6 +170,10 @@ if(BUILD_TESTING) set(TEST_TARGET "kernel_tests") endif() + target_compile_options("${TEST_TARGET}" PRIVATE + "$<$:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/kernel/test_support/string_makers.hpp>" + ) + target_compile_definitions("${TEST_TARGET}" PRIVATE KERNEL_TEST_ASSETS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/kernel/test_support/vfs/test_assets" ) -- cgit v1.2.3