aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-11-23 12:19:54 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-11-23 12:19:54 +0100
commit3a628ca1fcc1799c55de2635024ac178961cf702 (patch)
tree13805cea0dfa726c3e1b21109f34ab1d6d1152aa
parent2f9972781b66fd350f1975b2975f8998d5d07f13 (diff)
downloadkernel-3a628ca1fcc1799c55de2635024ac178961cf702.tar.xz
kernel-3a628ca1fcc1799c55de2635024ac178961cf702.zip
tests: let ecam_builder set bridge bus numbers
This change extends the ecam_builder to support additional configuration data for PCI bridges, namely the secondary and subordinate bus numbers. This extension is required to support upcoming PCI bridge walk tests by allowing the tests to configure PCI bridge topologies. The builder test reads both of the new bus numbers to verify they are set correctly. Additionally, this change introduces a new test support function in kstd, called require_ok. require_ok asserts that a kstd::result contains a success value and moves that value out of the result. In the case of a failed result, the source location of the require_ok call as well as the error message are reported and the test case is aborted. Both ecam_builder::window_for and the ecam_builder tests use this new function.
-rw-r--r--kernel/kernel/test_support/devices/pci/ecam_builder.cpp17
-rw-r--r--kernel/kernel/test_support/devices/pci/ecam_builder.hpp14
-rw-r--r--kernel/kernel/test_support/devices/pci/ecam_builder.tests.cpp88
-rw-r--r--libs/kstd/kstd/test_support/result.hpp33
4 files changed, 106 insertions, 46 deletions
diff --git a/kernel/kernel/test_support/devices/pci/ecam_builder.cpp b/kernel/kernel/test_support/devices/pci/ecam_builder.cpp
index 78b0b32c..a9bb1256 100644
--- a/kernel/kernel/test_support/devices/pci/ecam_builder.cpp
+++ b/kernel/kernel/test_support/devices/pci/ecam_builder.cpp
@@ -2,6 +2,7 @@
#include <kapi/memory.hpp>
+#include <kstd/test_support/result.hpp>
#include <kstd/units.hpp>
#include <catch2/catch_test_macros.hpp>
@@ -62,6 +63,18 @@ namespace kernel::tests::devices::pci
return *this;
}
+ auto ecam_builder::function_builder::secondary_bus(std::uint8_t id) -> function_builder &
+ {
+ m_window.write<std::uint8_t>(secondary_bus_offset, id);
+ return *this;
+ }
+
+ auto ecam_builder::function_builder::subordinate_bus(std::uint8_t id) -> function_builder &
+ {
+ m_window.write<std::uint8_t>(subordinate_bus_offset, id);
+ return *this;
+ }
+
auto ecam_builder::function_builder::function(std::uint8_t bus, std::uint8_t device, std::uint8_t number)
-> function_builder
{
@@ -98,9 +111,7 @@ namespace kernel::tests::devices::pci
(function_number << function_number_address_shift));
auto window = mmio_window::map(address, pci_function_size, mmio_window::access::writable);
- REQUIRE(window);
-
- return std::move(*window);
+ return kstd::tests::require_ok(std::move(window));
}
} // namespace kernel::tests::devices::pci
diff --git a/kernel/kernel/test_support/devices/pci/ecam_builder.hpp b/kernel/kernel/test_support/devices/pci/ecam_builder.hpp
index 31334959..3c0cf036 100644
--- a/kernel/kernel/test_support/devices/pci/ecam_builder.hpp
+++ b/kernel/kernel/test_support/devices/pci/ecam_builder.hpp
@@ -30,6 +30,8 @@ namespace kernel::tests::devices::pci
constexpr auto static subclass_code_offset = kstd::bytes{0x0a};
constexpr auto static class_code_offset = kstd::bytes{0x0b};
constexpr auto static header_type_offset = kstd::bytes{0x0e};
+ constexpr auto static secondary_bus_offset = kstd::bytes{0x19};
+ constexpr auto static subordinate_bus_offset = kstd::bytes{0x1a};
struct function_builder
{
@@ -82,6 +84,18 @@ namespace kernel::tests::devices::pci
//! @return A reference to this function builder.
auto revision_id(std::uint8_t id) -> function_builder &;
+ //! Set the secondary bus number for this function.
+ //!
+ //! @param id The secondary bus number to set.
+ //! @return A reference to this function builder.
+ auto secondary_bus(std::uint8_t id) -> function_builder &;
+
+ //! Set the subordinate bus number for this function.
+ //!
+ //! @param id The subordinate bus number to set.
+ //! @return A reference to this function builder.
+ auto subordinate_bus(std::uint8_t id) -> function_builder &;
+
//! Begin building another function.
//!
//! @param bus The bus number the device is attached to.
diff --git a/kernel/kernel/test_support/devices/pci/ecam_builder.tests.cpp b/kernel/kernel/test_support/devices/pci/ecam_builder.tests.cpp
index 0ca6af69..3b300581 100644
--- a/kernel/kernel/test_support/devices/pci/ecam_builder.tests.cpp
+++ b/kernel/kernel/test_support/devices/pci/ecam_builder.tests.cpp
@@ -2,6 +2,7 @@
#include <kapi/memory.hpp>
+#include <kstd/test_support/result.hpp>
#include <kstd/units.hpp>
#include <catch2/catch_test_macros.hpp>
@@ -12,6 +13,7 @@
using kernel::tests::devices::pci::ecam_builder;
using mmio_window = kapi::memory::mmio_window;
+using kstd::tests::require_ok;
namespace
{
@@ -29,6 +31,8 @@ namespace
constexpr auto test_revision = 0x04;
constexpr auto test_header_type_endpoint = 0x00;
constexpr auto test_multifunction = 0x80;
+ constexpr auto test_secondary_bus = 0xa0;
+ constexpr auto test_subordinate_bus = 0xa1;
} // namespace
SCENARIO("Building PCI ECAM structures using the ecam_builder", "[smoke][devices][pci]")
@@ -44,31 +48,28 @@ SCENARIO("Building PCI ECAM structures using the ecam_builder", "[smoke][devices
THEN("the previous bus is unaffected")
{
auto base = test_ecam_base;
- auto window = mmio_window::map(base, bytes_per_bus, mmio_window::access::read_only);
- REQUIRE(window);
+ auto window = require_ok(mmio_window::map(base, bytes_per_bus, mmio_window::access::read_only));
- REQUIRE(window->read<std::uint32_t>(kstd::bytes{}) == 0);
- REQUIRE(window->read<std::uint32_t>(bytes_per_bus - kstd::bytes{4}) == 0);
+ REQUIRE(window.read<std::uint32_t>(kstd::bytes{}) == 0);
+ REQUIRE(window.read<std::uint32_t>(bytes_per_bus - kstd::bytes{4}) == 0);
}
THEN("the bus is cleared")
{
auto base = test_ecam_base + bytes_per_bus;
- auto window = mmio_window::map(base, bytes_per_bus, mmio_window::access::read_only);
- REQUIRE(window);
+ auto window = require_ok(mmio_window::map(base, bytes_per_bus, mmio_window::access::read_only));
- REQUIRE(window->read<std::uint32_t>(kstd::bytes{}) == 0xffff'ffff);
- REQUIRE(window->read<std::uint32_t>(bytes_per_bus - kstd::bytes{4}) == 0xffff'ffff);
+ REQUIRE(window.read<std::uint32_t>(kstd::bytes{}) == 0xffff'ffff);
+ REQUIRE(window.read<std::uint32_t>(bytes_per_bus - kstd::bytes{4}) == 0xffff'ffff);
}
THEN("the next bus is unaffected")
{
auto base = test_ecam_base + 2 * bytes_per_bus;
- auto window = mmio_window::map(base, bytes_per_bus, mmio_window::access::read_only);
- REQUIRE(window);
+ auto window = require_ok(mmio_window::map(base, bytes_per_bus, mmio_window::access::read_only));
- REQUIRE(window->read<std::uint32_t>(kstd::bytes{}) == 0);
- REQUIRE(window->read<std::uint32_t>(bytes_per_bus - kstd::bytes{4}) == 0);
+ REQUIRE(window.read<std::uint32_t>(kstd::bytes{}) == 0);
+ REQUIRE(window.read<std::uint32_t>(bytes_per_bus - kstd::bytes{4}) == 0);
}
}
@@ -85,22 +86,25 @@ SCENARIO("Building PCI ECAM structures using the ecam_builder", "[smoke][devices
.subclass_code(test_subclass_vga)
.programming_interface(test_prog_if_8514)
.header_type(test_header_type_endpoint | test_multifunction)
- .revision_id(test_revision);
+ .revision_id(test_revision)
+ .secondary_bus(test_secondary_bus)
+ .subordinate_bus(test_subordinate_bus);
THEN("the values are written correctly")
{
auto base = test_ecam_base + bus * bytes_per_bus + device * bytes_per_device + function * bytes_per_function;
- auto window = mmio_window::map(base, bytes_per_function, mmio_window::access::read_only);
- REQUIRE(window);
-
- REQUIRE(window->read<std::uint16_t>(ecam_builder::vendor_id_offset) == test_vendor);
- REQUIRE(window->read<std::uint16_t>(ecam_builder::device_id_offset) == test_device);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::class_code_offset) == test_class_display_ctrl);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::subclass_code_offset) == test_subclass_vga);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::programming_interface_offset) == test_prog_if_8514);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::revision_id_offset) == test_revision);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::header_type_offset) ==
+ auto window = require_ok(mmio_window::map(base, bytes_per_function, mmio_window::access::read_only));
+
+ REQUIRE(window.read<std::uint16_t>(ecam_builder::vendor_id_offset) == test_vendor);
+ REQUIRE(window.read<std::uint16_t>(ecam_builder::device_id_offset) == test_device);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::class_code_offset) == test_class_display_ctrl);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::subclass_code_offset) == test_subclass_vga);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::programming_interface_offset) == test_prog_if_8514);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::revision_id_offset) == test_revision);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::header_type_offset) ==
(test_header_type_endpoint | test_multifunction));
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::secondary_bus_offset) == test_secondary_bus);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::subordinate_bus_offset) == test_subordinate_bus);
}
}
@@ -130,32 +134,30 @@ SCENARIO("Building PCI ECAM structures using the ecam_builder", "[smoke][devices
THEN("the values are written correctly")
{
auto base = test_ecam_base + bus * bytes_per_bus + device * bytes_per_device + function * bytes_per_function;
- auto window = mmio_window::map(base, bytes_per_function, mmio_window::access::read_only);
- REQUIRE(window);
-
- REQUIRE(window->read<std::uint16_t>(ecam_builder::vendor_id_offset) == test_vendor);
- REQUIRE(window->read<std::uint16_t>(ecam_builder::device_id_offset) == test_device);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::class_code_offset) == test_class_display_ctrl);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::subclass_code_offset) == test_subclass_vga);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::programming_interface_offset) == test_prog_if_8514);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::revision_id_offset) == test_revision);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::header_type_offset) ==
+ auto window = require_ok(mmio_window::map(base, bytes_per_function, mmio_window::access::read_only));
+
+ REQUIRE(window.read<std::uint16_t>(ecam_builder::vendor_id_offset) == test_vendor);
+ REQUIRE(window.read<std::uint16_t>(ecam_builder::device_id_offset) == test_device);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::class_code_offset) == test_class_display_ctrl);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::subclass_code_offset) == test_subclass_vga);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::programming_interface_offset) == test_prog_if_8514);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::revision_id_offset) == test_revision);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::header_type_offset) ==
(test_header_type_endpoint | test_multifunction));
}
THEN("the first function is unchanged")
{
auto base = test_ecam_base + bytes_per_bus;
- auto window = mmio_window::map(base, bytes_per_function, mmio_window::access::read_only);
- REQUIRE(window);
-
- REQUIRE(window->read<std::uint16_t>(ecam_builder::vendor_id_offset) == 0);
- REQUIRE(window->read<std::uint16_t>(ecam_builder::device_id_offset) == 0);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::class_code_offset) == 0);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::subclass_code_offset) == 0);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::programming_interface_offset) == 0);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::revision_id_offset) == 0);
- REQUIRE(window->read<std::uint8_t>(ecam_builder::header_type_offset) == 0);
+ auto window = require_ok(mmio_window::map(base, bytes_per_function, mmio_window::access::read_only));
+
+ REQUIRE(window.read<std::uint16_t>(ecam_builder::vendor_id_offset) == 0);
+ REQUIRE(window.read<std::uint16_t>(ecam_builder::device_id_offset) == 0);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::class_code_offset) == 0);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::subclass_code_offset) == 0);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::programming_interface_offset) == 0);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::revision_id_offset) == 0);
+ REQUIRE(window.read<std::uint8_t>(ecam_builder::header_type_offset) == 0);
}
}
}
diff --git a/libs/kstd/kstd/test_support/result.hpp b/libs/kstd/kstd/test_support/result.hpp
new file mode 100644
index 00000000..7b6d765c
--- /dev/null
+++ b/libs/kstd/kstd/test_support/result.hpp
@@ -0,0 +1,33 @@
+#ifndef KSTD_TEST_SUPPORT_RESULT_HPP
+#define KSTD_TEST_SUPPORT_RESULT_HPP
+
+#include <kstd/result.hpp>
+#include <kstd/test_support/string_makers.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <source_location>
+#include <utility>
+
+namespace kstd::tests
+{
+
+ //! Require for a result to be successful.
+ //!
+ //! @param result The result to check.
+ //! @param where The source location to report in case of failure.
+ //! @return The successful value contained in the result.
+ template<typename ResultType>
+ auto require_ok(result<ResultType> result, std::source_location where = std::source_location::current()) -> ResultType
+ {
+ if (!result)
+ {
+ FAIL("expected success at " << where.file_name() << ':' << where.line() << ", got "
+ << error_to_string(result.error()));
+ }
+ return std::move(*result);
+ }
+
+} // namespace kstd::tests
+
+#endif