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