diff options
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/kernel/devices/pci/init.cpp | 34 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/scan.cpp | 83 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/scan.hpp | 15 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/scan.tests.cpp | 477 | ||||
| -rw-r--r-- | kernel/kernel/test_support/cio.hpp | 14 | ||||
| -rw-r--r-- | kernel/kernel/test_support/devices/pci/ecam_fixture.cpp | 2 | ||||
| -rw-r--r-- | kernel/kernel/test_support/kapi/cio.cpp | 22 |
7 files changed, 621 insertions, 26 deletions
diff --git a/kernel/kernel/devices/pci/init.cpp b/kernel/kernel/devices/pci/init.cpp index 6abf758c..f3efaa87 100644 --- a/kernel/kernel/devices/pci/init.cpp +++ b/kernel/kernel/devices/pci/init.cpp @@ -7,12 +7,8 @@ #include <kapi/devices.hpp> #include <kstd/print.hpp> -#include <kstd/system_error.hpp> #include <kstd/vector.hpp> -#include <cstddef> -#include <cstdint> - namespace kernel::devices::pci { @@ -30,28 +26,18 @@ namespace kernel::devices::pci for (auto const & descriptor : descriptors) { auto segment = ecam_segment{descriptor}; + auto functions = scan_segment(segment); + if (!functions) + { + kstd::println(kstd::print_sink::stderr, "[OS:DEV] Failed to scan segment {}: {}", segment.group(), + functions.error()); + continue; + } - for (std::size_t bus = segment.first_bus(); segment.contains(bus); ++bus) + for (auto const & function : *functions) { - auto functions = scan_bus(segment, static_cast<std::uint8_t>(bus)); - if (!functions) - { - if (functions.error() == kstd::errc::not_enough_memory) - { - kstd::println(kstd::print_sink::stderr, "[OS:DEV] Failed to map MMIO window for bus {}", bus); - } - else - { - kstd::println(kstd::print_sink::stderr, "[OS:DEV] Failed to scan bus {}: {}", bus, functions.error()); - } - continue; - } - - for (auto const & function : *functions) - { - kstd::println("[OS:DEV] Detected PCI Express device: {} [{:04x}:{:04x}]", function.address, - function.vendor_id, function.device_id); - } + kstd::println("[OS:DEV] Detected PCI Express device: {} [{:04x}:{:04x}]", function.address, function.vendor_id, + function.device_id); } } } diff --git a/kernel/kernel/devices/pci/scan.cpp b/kernel/kernel/devices/pci/scan.cpp index da3523ec..0ff7d03e 100644 --- a/kernel/kernel/devices/pci/scan.cpp +++ b/kernel/kernel/devices/pci/scan.cpp @@ -6,10 +6,13 @@ #include <kapi/memory.hpp> +#include <kstd/print.hpp> #include <kstd/result.hpp> #include <kstd/vector.hpp> +#include <bitset> #include <cstdint> +#include <limits> #include <optional> #include <utility> @@ -102,4 +105,84 @@ namespace kernel::devices::pci return kstd::success(std::move(functions)); } + auto scan_segment(ecam_segment const & segment) -> kstd::result<kstd::vector<function_info>> + { + auto root_bus = segment.first_bus(); + if (!segment.contains(root_bus)) + { + return kstd::vector<function_info>{}; + } + + auto functions = scan_bus(segment, root_bus); + if (!functions) + { + return kstd::failure(functions.error()); + } + + auto scanned_buses = std::bitset<std::numeric_limits<std::uint8_t>::max() + 1>{}; + scanned_buses[root_bus] = true; + + for (auto function = 0uz; function < functions->size(); ++function) + { + auto maybe_bridge = functions->at(function); + if (maybe_bridge.type() != header_type::pci_to_pci_bridge) + { + continue; + } + + auto secondary_bus = maybe_bridge.secondary_bus; + auto subordinate_bus = maybe_bridge.subordinate_bus; + + if (!secondary_bus) + { + kstd::println(kstd::print_sink::stderr, "[OS:DEV] ignoring bridge {} with 0 secondary bus", + maybe_bridge.address); + continue; + } + + if (!segment.contains(secondary_bus)) + { + kstd::println(kstd::print_sink::stderr, "[OS:DEV] ignoring bridge {} with secondary bus {} outside of segment", + maybe_bridge.address, secondary_bus); + continue; + } + + if (secondary_bus <= maybe_bridge.address.bus) + { + kstd::println( + kstd::print_sink::stderr, + "[OS:DEV] ignoring bridge {} with secondary bus {} not greater than the bus the bridge is attached to {}", + maybe_bridge.address, secondary_bus, maybe_bridge.address.bus); + continue; + } + + if (subordinate_bus < secondary_bus) + { + kstd::println(kstd::print_sink::stderr, + "[OS:DEV] ignoring bridge {} with subordinate bus {} less than secondary bus {}", + maybe_bridge.address, subordinate_bus, secondary_bus); + continue; + } + + if (scanned_buses[secondary_bus]) + { + kstd::println(kstd::print_sink::stderr, + "[OS:DEV] ignoring bridge {} with secondary bus {} that has already been scanned", + maybe_bridge.address, secondary_bus); + continue; + } + scanned_buses[secondary_bus] = true; + + auto children = scan_bus(segment, secondary_bus); + if (!children) + { + return kstd::failure(children.error()); + } + + functions->insert_range(functions->begin() + function + 1, std::move(*children)); + } + + return functions; + } + } // namespace kernel::devices::pci diff --git a/kernel/kernel/devices/pci/scan.hpp b/kernel/kernel/devices/pci/scan.hpp index 1c10d979..799a1c09 100644 --- a/kernel/kernel/devices/pci/scan.hpp +++ b/kernel/kernel/devices/pci/scan.hpp @@ -24,6 +24,21 @@ namespace kernel::devices::pci [[nodiscard]] auto scan_bus(ecam_segment const & segment, std::uint8_t bus_number) -> kstd::result<kstd::vector<function_info>>; + //! Scan an entire ECAM segment for available PCI functions. + //! + //! This function recursively scans all buses within the given ECAM segment for available functions. All found + //! functions are returned in topological order, bridges before their children, children in ascending address order. + //! Functions violating the PCI specification are ignored. + //! + //! @note This function currently assumes that there is a single root bus per ECAM segment. Furthermore, it assumes + //! the root bus for a segment is the first bus in that segment. Proper handling of multiple root buses requires an + //! AML interpreter to process the _CRS resource description. + //! + //! @param segment The ECAM segment to scan. + //! @return A vector of function_info objects representing the functions found in the segment on success, an error + //! otherwise + [[nodiscard]] auto scan_segment(ecam_segment const & segment) -> kstd::result<kstd::vector<function_info>>; + } // namespace kernel::devices::pci #endif diff --git a/kernel/kernel/devices/pci/scan.tests.cpp b/kernel/kernel/devices/pci/scan.tests.cpp index ca370533..455a25c5 100644 --- a/kernel/kernel/devices/pci/scan.tests.cpp +++ b/kernel/kernel/devices/pci/scan.tests.cpp @@ -3,7 +3,9 @@ #include <kernel/devices/pci/ecam_segment.hpp> #include <kernel/devices/pci/function_address.hpp> #include <kernel/devices/pci/function_info.hpp> +#include <kernel/test_support/cio.hpp> #include <kernel/test_support/devices/pci/ecam_fixture.hpp> +#include <kernel/test_support/devices/pci/function_addresses.hpp> #include <kernel/test_support/devices/pci/function_spec.hpp> #include <kernel/test_support/memory.hpp> @@ -14,10 +16,16 @@ #include <catch2/catch_test_macros.hpp> +#include <algorithm> #include <cstdint> +#include <string> using kernel::devices::pci::ecam_segment; using kernel::devices::pci::function_address; +using kernel::devices::pci::scan_segment; +using kernel::tests::cio::require_log_message; +using kernel::tests::devices::pci::addresses; +using kernel::tests::devices::pci::addresses_of; using kernel::tests::devices::pci::function_spec; using kstd::tests::require_ok; @@ -58,6 +66,7 @@ namespace constexpr auto distinct_subordinate_bus = 0x42; constexpr auto devices_per_bus = 32; + constexpr auto functions_per_device = 8; auto amd_fch_sata_controller(std::uint8_t bus, std::uint8_t device, std::uint8_t function = 0) -> function_spec { @@ -468,3 +477,471 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Reading bridge bus n } } } + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Walking the bridges of a PCI segment", "[devices][pci]") +{ + GIVEN("A root bus without bridges") + { + place({.bus = 0, .device = 0}); + place({.bus = 0, .device = 1}); + + THEN("the walk finds exactly the functions of the root bus") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {0, 1, 0} + })); + } + } + + GIVEN("A bridge with nothing behind it") + { + place(bridge({.bus = 0, .device = 1}, 1, 1)); + + THEN("the walk finds only the bridge") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 1, 0} + })); + } + } + + GIVEN("A bridge with an endpoint behind it") + { + place(bridge({.bus = 0, .device = 0}, 1, 1)); + place({.bus = 1, .device = 0}); + + THEN("the endpoint follows its bridge") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {1, 0, 0} + })); + } + } + + GIVEN("A chain of bridges across all four buses and an endpoint next to the first bridge") + { + place(bridge({.bus = 0, .device = 0}, 1, 3)); + place({.bus = 0, .device = 1}); + place(bridge({.bus = 1, .device = 0}, 2, 3)); + place(bridge({.bus = 2, .device = 0}, 3, 3)); + place({.bus = 3, .device = 0}); + + THEN("the whole chain is listed before the endpoint next to its first bridge") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {1, 0, 0}, + {2, 0, 0}, + {3, 0, 0}, + {0, 1, 0} + })); + } + } + + GIVEN("Two sibling bridges with an endpoint behind each") + { + place(bridge({.bus = 0, .device = 0}, 1, 1)); + place(bridge({.bus = 0, .device = 1}, 2, 2)); + place({.bus = 1, .device = 0}); + place({.bus = 2, .device = 0}); + + THEN("each bridge is followed by its own subtree before the next sibling") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {1, 0, 0}, + {0, 1, 0}, + {2, 0, 0} + })); + } + } + + GIVEN("An endpoint on the root bus between two bridges") + { + place(bridge({.bus = 0, .device = 0}, 1, 1)); + place({.bus = 0, .device = 1}); + place(bridge({.bus = 0, .device = 2}, 2, 2)); + place({.bus = 1, .device = 0}); + place({.bus = 2, .device = 0}); + + THEN("the functions of the root bus keep their address order around the subtrees") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {1, 0, 0}, + {0, 1, 0}, + {0, 2, 0}, + {2, 0, 0} + })); + } + } + + GIVEN("A nested bridge with an endpoint next to it on the same bus") + { + place(bridge({.bus = 0, .device = 0}, 1, 2)); + place(bridge({.bus = 1, .device = 0}, 2, 2)); + place({.bus = 1, .device = 1}); + place({.bus = 2, .device = 0}); + + THEN("the nested subtree is listed before the endpoint that follows its bridge") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {1, 0, 0}, + {2, 0, 0}, + {1, 1, 0} + })); + } + } + + GIVEN("A segment that starts at bus 1 with a function on bus 0") + { + place({.bus = 0, .device = 0}); + place({.bus = 1, .device = 0}); + place(bridge({.bus = 1, .device = 1}, 2, 2)); + place({.bus = 2, .device = 0}); + + THEN("the walk starts at the first bus of the segment and ignores bus 0") + { + auto segment = ecam_segment{make_descriptor(0, 1, 3)}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {1, 0, 0}, + {1, 1, 0}, + {2, 0, 0} + })); + } + } + + GIVEN("A segment with a segment group other than 0 and a bridge") + { + place(bridge({.bus = 0, .device = 0}, 1, 1)); + place({.bus = 1, .device = 0}); + + THEN("the functions behind the bridge carry the segment group as well") + { + auto segment = ecam_segment{make_descriptor(5, 0, 4)}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of( + { + {0, 0, 0}, + {1, 0, 0} + }, + 5)); + } + } + + GIVEN("A bridge whose subordinate bus number exceeds the buses it leads to") + { + place(bridge({.bus = 0, .device = 0}, 1, 3)); + place({.bus = 1, .device = 0}); + place({.bus = 2, .device = 0}); + place({.bus = 3, .device = 0}); + + THEN("only the secondary bus is scanned") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {1, 0, 0} + })); + } + } + + GIVEN("A function on a bus that no bridge leads to") + { + place({.bus = 0, .device = 0}); + place({.bus = 1, .device = 0}); + + THEN("the function is not found") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0} + })); + } + } + + GIVEN("A segment that is empty because its bus count is invalid") + { + place({.bus = 0, .device = 0}); + + THEN("the walk succeeds and finds nothing") + { + auto segment = ecam_segment{make_descriptor(0, 0, 257)}; // NOLINT(readability-magic-numbers) + auto functions = require_ok(scan_segment(segment)); + + require_log_message("[OS:DEV] Invalid PCI bus count '257' for start bus '0'"); + + REQUIRE(functions.empty()); + } + } +} + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Walking a PCI segment with invalid bridges", + "[devices][pci]") +{ + GIVEN("A bridge without a secondary bus number") + { + place(bridge({.bus = 0, .device = 0}, 0, 0)); + place({.bus = 0, .device = 1}); + + THEN("the bridge is skipped and the rest of its bus is still listed") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + require_log_message("[OS:DEV] ignoring bridge 0000:00:00.0 with 0 secondary bus"); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {0, 1, 0} + })); + } + } + + GIVEN("A bridge whose secondary bus lies outside the segment") + { + place(bridge({.bus = 0, .device = 0}, 3, 3)); + place({.bus = 0, .device = 1}); + place({.bus = 3, .device = 0}); + + THEN("the bridge is skipped instead of failing the walk") + { + auto segment = ecam_segment{make_descriptor(0, 0, 2)}; + auto functions = require_ok(scan_segment(segment)); + + require_log_message("[OS:DEV] ignoring bridge 0000:00:00.0 with secondary bus 3 outside of segment"); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {0, 1, 0} + })); + } + } + + GIVEN("A bridge on bus 2 whose secondary bus is below its own bus and has not been scanned") + { + place(bridge({.bus = 0, .device = 0}, 2, 2)); + place(bridge({.bus = 2, .device = 0}, 1, 1)); + place({.bus = 1, .device = 0}); + + THEN("the bridge is skipped and the lower bus is never scanned") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + require_log_message( + "[OS:DEV] ignoring bridge 0000:02:00.0 with secondary bus 1 not greater than the bus the bridge " + "is attached to 2"); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {2, 0, 0} + })); + } + } + + GIVEN("A bridge on bus 1 whose secondary bus is its own bus") + { + place(bridge({.bus = 0, .device = 0}, 1, 1)); + place(bridge({.bus = 1, .device = 0}, 1, 1)); + place({.bus = 1, .device = 1}); + + THEN("the bridge is skipped and no function is listed twice") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + require_log_message( + "[OS:DEV] ignoring bridge 0000:01:00.0 with secondary bus 1 not greater than the bus the bridge " + "is attached to 1"); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {1, 0, 0}, + {1, 1, 0} + })); + } + } + + GIVEN("A bridge whose subordinate bus is below its secondary bus, followed by a valid bridge") + { + place(bridge({.bus = 0, .device = 0}, 1, 0)); + place({.bus = 1, .device = 0}); + place(bridge({.bus = 0, .device = 1}, 2, 2)); + place({.bus = 2, .device = 0}); + + THEN("only the invalid bridge is skipped") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + require_log_message("[OS:DEV] ignoring bridge 0000:00:00.0 with subordinate bus 0 less than secondary bus 1"); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {0, 1, 0}, + {2, 0, 0} + })); + } + } + + GIVEN("Two bridges that lead to the same bus") + { + place(bridge({.bus = 0, .device = 0}, 1, 1)); + place(bridge({.bus = 0, .device = 1}, 1, 1)); + place({.bus = 1, .device = 0}); + + THEN("the bus is listed once, behind the first bridge") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + require_log_message("[OS:DEV] ignoring bridge 0000:00:01.0 with secondary bus 1 that has already been scanned"); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {1, 0, 0}, + {0, 1, 0} + })); + } + } + + GIVEN("A CardBus bridge with non-zero bytes where a PCI-to-PCI bridge would keep its bus numbers") + { + place({.bus = 0, .device = 0, .header_type = header_type_cardbus_bridge, .secondary_bus = 1, .subordinate_bus = 1}); + place({.bus = 0, .device = 1}); + place({.bus = 1, .device = 0}); + + THEN("the CardBus bridge is listed but not descended into") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + + REQUIRE(addresses(functions) == addresses_of({ + {0, 0, 0}, + {0, 1, 0} + })); + } + } +} + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Failing to walk a PCI segment", "[devices][pci]") +{ + GIVEN("A segment whose root bus lies outside the MMIO space") + { + THEN("the walk fails with Not Enough Memory") + { + auto descriptor = make_descriptor(0, 0, 32); + descriptor.base_address = builder.base + kernel::tests::memory::mmio_size; + + auto segment = ecam_segment{descriptor}; + auto functions = scan_segment(segment); + + REQUIRE(!functions); + REQUIRE(functions.error() == kstd::errc::not_enough_memory); + } + } + + GIVEN("A bridge whose secondary bus is inside the segment but outside the MMIO space") + { + place(bridge({.bus = 0, .device = 0}, 4, 4)); + place({.bus = 0, .device = 1}); + + THEN("the walk fails with Not Enough Memory instead of returning a partial list") + { + auto segment = ecam_segment{make_descriptor(0, 0, 8)}; + auto functions = scan_segment(segment); + + REQUIRE(!functions); + REQUIRE(functions.error() == kstd::errc::not_enough_memory); + } + } +} + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Walking a PCI segment with more than 256 functions", + "[devices][pci]") +{ + auto fill_root_bus = [this](bool last_function_is_bridge) { + for (auto device = std::uint8_t{0}; device < devices_per_bus; ++device) + { + place(multifunction(endpoint(amd_fch_sata_controller(0, device, 0)))); + + for (auto function = std::uint8_t{1}; function < functions_per_device; ++function) + { + if (last_function_is_bridge && device == devices_per_bus - 1 && function == functions_per_device - 1) + { + place(bridge({.bus = 0, .device = device, .function = function}, 1, 1)); + } + else + { + place(endpoint(amd_fch_sata_controller(0, device, function))); + } + } + } + }; + + GIVEN("A root bus on which every function of every device is present") + { + fill_root_bus(false); + + THEN("the walk lists all 256 functions in address order") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + auto found = addresses(functions); + + REQUIRE(found.size() == 256); + REQUIRE(std::ranges::is_sorted(found)); + REQUIRE(found.front() == function_address{.segment_group = 0, .bus = 0, .device = 0, .function = 0}); + REQUIRE(found.back() == function_address{.segment_group = 0, .bus = 0, .device = 31, .function = 7}); + } + } + + GIVEN("A full root bus whose very last function is a bridge to a bus with an endpoint") + { + fill_root_bus(true); + place({.bus = 1, .device = 0}); + + THEN("the walk still descends into the bridge and lists 257 functions") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(scan_segment(segment)); + auto found = addresses(functions); + + REQUIRE(found.size() == 257); + REQUIRE(found.at(255) == function_address{.segment_group = 0, .bus = 0, .device = 31, .function = 7}); + REQUIRE(found.at(256) == function_address{.segment_group = 0, .bus = 1, .device = 0, .function = 0}); + } + } +} diff --git a/kernel/kernel/test_support/cio.hpp b/kernel/kernel/test_support/cio.hpp index 71564727..cd42a0ae 100644 --- a/kernel/kernel/test_support/cio.hpp +++ b/kernel/kernel/test_support/cio.hpp @@ -5,7 +5,10 @@ #include <kapi/cio.hpp> +#include <cstddef> #include <mutex> +#include <source_location> +#include <string> #include <string_view> namespace kernel::tests::cio @@ -37,6 +40,15 @@ namespace kernel::tests::cio //! @throws std::runtime_error if no output device has been registered. //! @return The currently active device's log buffer. [[nodiscard]] auto log_buffer() -> kernel::tests::log_buffer &; + + //! Require that a specific message exists in the log buffer. + //! + //! @param message The message to require. + //! @param starting_at The position to start the search at. + //! @param where The source location where this requirement is checked. + //! @return The position of the message in the flat messages string. + auto require_log_message(std::string const & message, std::size_t starting_at = 0, + std::source_location where = std::source_location::current()) -> std::size_t; } // namespace kernel::tests::cio -#endif
\ No newline at end of file +#endif diff --git a/kernel/kernel/test_support/devices/pci/ecam_fixture.cpp b/kernel/kernel/test_support/devices/pci/ecam_fixture.cpp index 402d5eb3..28f0694a 100644 --- a/kernel/kernel/test_support/devices/pci/ecam_fixture.cpp +++ b/kernel/kernel/test_support/devices/pci/ecam_fixture.cpp @@ -1,5 +1,6 @@ #include <kernel/test_support/devices/pci/ecam_fixture.hpp> +#include <kernel/test_support/cio.hpp> #include <kernel/test_support/devices/pci/function_spec.hpp> #include <kernel/test_support/memory.hpp> @@ -36,6 +37,7 @@ namespace kernel::tests::devices::pci ecam_fixture::ecam_fixture() { kernel::tests::memory::fill_mmio(std::byte{0xff}); + kernel::tests::cio::log_buffer().clear(); } auto ecam_fixture::make_descriptor(std::uint16_t group, std::uint8_t first_bus, std::size_t count) diff --git a/kernel/kernel/test_support/kapi/cio.cpp b/kernel/kernel/test_support/kapi/cio.cpp index 98bc99d7..701eb043 100644 --- a/kernel/kernel/test_support/kapi/cio.cpp +++ b/kernel/kernel/test_support/kapi/cio.cpp @@ -4,9 +4,14 @@ #include <kapi/cio.hpp> +#include <catch2/catch_test_macros.hpp> + #include <atomic> +#include <cstddef> #include <optional> +#include <source_location> #include <stdexcept> +#include <string> namespace { @@ -51,4 +56,19 @@ namespace kernel::tests::cio return device->log_buffer(); } -} // namespace kernel::tests::cio
\ No newline at end of file + auto require_log_message(std::string const & message, std::size_t starting_at, std::source_location where) + -> std::size_t + { + auto all = kernel::tests::cio::log_buffer().flat_messages(); + auto found = all.find(message, starting_at); + + if (found == std::string::npos) + { + FAIL("expected message \"" << message << "\" in log\n" + << all << " (at " << where.file_name() << ":" << where.line() << ")"); + } + + return found; + } + +} // namespace kernel::tests::cio |
