From 8ac3b7c5dfd6917eac1ffb117ee48231032ce754 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 24 Jul 2026 23:12:23 +0200 Subject: kernel: remove boot modules registry --- arch/x86_64/kapi/boot_modules.cpp | 23 ++-- kapi/kapi/boot_modules.hpp | 21 ++-- kapi/kapi/boot_modules/bus.hpp | 71 ----------- kapi/kapi/boot_modules/device.hpp | 64 ++++++++++ kapi/kapi/boot_modules/module.hpp | 10 +- kapi/kapi/boot_modules/registry.hpp | 76 ------------ kapi/kapi/test_support/boot_modules.hpp | 17 --- kernel/CMakeLists.txt | 7 +- kernel/kapi/boot_module/bus.cpp | 97 --------------- kernel/kapi/boot_module/registry.cpp | 131 --------------------- kernel/kapi/boot_modules/device.cpp | 39 ++++++ kernel/kernel/bus/boot_modules.cpp | 60 ++++++++++ kernel/kernel/bus/boot_modules.hpp | 28 +++++ kernel/kernel/devices/storage.cpp | 9 +- kernel/kernel/devices/storage.hpp | 2 +- kernel/kernel/devices/storage.tests.cpp | 31 ++--- kernel/kernel/drivers/storage/ram_disk.cpp | 8 +- kernel/kernel/drivers/storage/ram_disk.hpp | 4 +- kernel/kernel/main.cpp | 8 +- .../filesystem/storage_boot_module_fixture.cpp | 42 +++---- .../filesystem/storage_boot_module_fixture.hpp | 8 +- kernel/kernel/test_support/kapi/boot_modules.cpp | 6 + .../kernel/test_support/state_reset_listener.cpp | 6 +- 23 files changed, 283 insertions(+), 485 deletions(-) delete mode 100644 kapi/kapi/boot_modules/bus.hpp create mode 100644 kapi/kapi/boot_modules/device.hpp delete mode 100644 kapi/kapi/boot_modules/registry.hpp delete mode 100644 kapi/kapi/test_support/boot_modules.hpp delete mode 100644 kernel/kapi/boot_module/bus.cpp delete mode 100644 kernel/kapi/boot_module/registry.cpp create mode 100644 kernel/kapi/boot_modules/device.cpp create mode 100644 kernel/kernel/bus/boot_modules.cpp create mode 100644 kernel/kernel/bus/boot_modules.hpp create mode 100644 kernel/kernel/test_support/kapi/boot_modules.cpp diff --git a/arch/x86_64/kapi/boot_modules.cpp b/arch/x86_64/kapi/boot_modules.cpp index 136eb30d..c86b9cf7 100644 --- a/arch/x86_64/kapi/boot_modules.cpp +++ b/arch/x86_64/kapi/boot_modules.cpp @@ -4,10 +4,12 @@ #include #include +#include #include -#include +#include #include +#include #include #include @@ -15,24 +17,27 @@ #include #include #include +#include namespace kapi::boot_modules { - auto init() -> void + auto init(kapi::devices::bus & bus) -> void { - registry::init(); - - kstd::println("[x86_64:BOOT_MODULES] Registering boot modules."); + kstd::println("[x86_64:BOOT_MODULES] Attaching boot modules."); auto modules = boot::bootstrap_information.mbi->modules(); - std::ranges::for_each(modules, [](auto const & module) { - registry::get().add(kapi::boot_modules::module{ + + std::ranges::for_each(std::views::enumerate(modules), [&bus](auto entry) { + auto [index, module] = entry; + auto boot_module = kapi::boot_modules::module{ .name = module.string(), .start_address = memory::linear_address{module.start_address + std::bit_cast(&arch::boot::TEACHOS_VMA)}, - .size = module.end_address - module.start_address, - }); + .size = module.end_address - module.start_address}; + auto device = kstd::make_shared(index, boot_module); + bus.add_child(device); }); } + } // namespace kapi::boot_modules \ No newline at end of file diff --git a/kapi/kapi/boot_modules.hpp b/kapi/kapi/boot_modules.hpp index 7f47c628..7f7d5727 100644 --- a/kapi/kapi/boot_modules.hpp +++ b/kapi/kapi/boot_modules.hpp @@ -1,21 +1,20 @@ #ifndef TEACHOS_KAPI_BOOT_MODULES_HPP #define TEACHOS_KAPI_BOOT_MODULES_HPP -#include // IWYU pragma: export -#include // IWYU pragma: export -#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include namespace kapi::boot_modules { - //! @qualifier platform-defined - //! Initialize the boot module registry. - //! - //! @note This function must be implemented by the target platform. - //! - //! This function initializes the boot module registry, which is responsible for keeping track of the modules loaded - //! by the bootloader, and providing access to them for the rest of the kernel. - auto init() -> void; + //! @addtogroup kapi-boot_modules-platform-defined + //! @{ + + //! Attach all boot modules to the boot module bus. + auto init(kapi::devices::bus & bus) -> void; + + //! @} } // namespace kapi::boot_modules #endif \ No newline at end of file diff --git a/kapi/kapi/boot_modules/bus.hpp b/kapi/kapi/boot_modules/bus.hpp deleted file mode 100644 index 3e982d85..00000000 --- a/kapi/kapi/boot_modules/bus.hpp +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef TEACHOS_KAPI_BOOT_MODULES_BUS_HPP -#define TEACHOS_KAPI_BOOT_MODULES_BUS_HPP - -// IWYU pragma: private, include - -#include -#include -#include - -#include - -#include -#include -#include - -namespace kapi::boot_modules -{ - - struct boot_module_identification - { - constexpr auto static id = devices::interface_id{"boot_module_identification"}; - - virtual ~boot_module_identification() = default; - - [[nodiscard]] auto virtual command_line() const -> std::string_view = 0; - - [[nodiscard]] auto virtual module() const -> module const & = 0; - }; - - struct boot_module_driver_identification - { - constexpr auto static id = devices::interface_id{"boot_module_driver_identification"}; - - virtual ~boot_module_driver_identification() = default; - - [[nodiscard]] virtual auto claimed_parameter() const -> std::pair = 0; - }; - - struct boot_module_device final : kapi::devices::device, boot_module_identification - { - boot_module_device(std::size_t index, struct module const & module); - - [[nodiscard]] auto command_line() const -> std::string_view override; - - [[nodiscard]] auto module() const -> struct module const & override; - - protected: - auto query_interface(kapi::devices::interface_id interface) -> void * override; - - private: - struct module m_module; - }; - - struct boot_module_bus final : kapi::devices::bus, kapi::devices::bus_protocol - { - explicit boot_module_bus(registry const * registry); - - auto enumerate(kapi::devices::bus & self) -> void override; - - [[nodiscard]] auto match(kapi::devices::device const & dev, kapi::devices::driver const & drv) const - -> kstd::result override; - - [[nodiscard]] auto protocol() -> kapi::devices::bus_protocol * override; - - private: - registry const * m_registry; - }; - -} // namespace kapi::boot_modules - -#endif \ No newline at end of file diff --git a/kapi/kapi/boot_modules/device.hpp b/kapi/kapi/boot_modules/device.hpp new file mode 100644 index 00000000..a6aba9b0 --- /dev/null +++ b/kapi/kapi/boot_modules/device.hpp @@ -0,0 +1,64 @@ +#ifndef TEACHOS_KAPI_BOOT_MODULES_DEVICE_HPP +#define TEACHOS_KAPI_BOOT_MODULES_DEVICE_HPP + +#include +#include +#include + +#include +#include + +namespace kapi::boot_modules +{ + + //! @addtogroup kapi-boot_modules-kernel-defined + //! @{ + + //! The signature interface of a boot module device. + //! + //! Boot module devices are "virtual" devices that get attached to the boot module bus. The purpose is to allow them + //! to materialize as "real" devices, e.g. a ram disk, and have a driver bound to them. + struct boot_module_signature + { + constexpr auto static id = kapi::devices::interface_id{"boot_module_signature"}; + + virtual ~boot_module_signature() = default; + + //! The command line associated with this module device. + [[nodiscard]] auto virtual command_line() const -> std::string_view = 0; + + //! The in-memory boot module associated with this device. + [[nodiscard]] auto virtual module() const -> kapi::boot_modules::module const & = 0; + }; + + //! The claim interface of a boot module driver. + struct boot_module_claim + { + constexpr auto static id = kapi::devices::interface_id{"boot_module_claim"}; + + virtual ~boot_module_claim() = default; + + //! The parameter this driver claims. + [[nodiscard]] virtual auto parameter() const -> std::pair = 0; + }; + + //! A generic boot module device. + struct device final : kapi::devices::device, boot_module_signature + { + device(std::size_t index, struct module const & module); + + [[nodiscard]] auto command_line() const -> std::string_view override; + + [[nodiscard]] auto module() const -> struct module const & override; + + protected: + auto query_interface(kapi::devices::interface_id interface) -> void * override; + + private: + struct module m_module; + }; + + //! @} + +} // namespace kapi::boot_modules +#endif \ No newline at end of file diff --git a/kapi/kapi/boot_modules/module.hpp b/kapi/kapi/boot_modules/module.hpp index 14898132..99e7b2ca 100644 --- a/kapi/kapi/boot_modules/module.hpp +++ b/kapi/kapi/boot_modules/module.hpp @@ -1,8 +1,6 @@ #ifndef TEACHOS_KAPI_BOOT_MODULES_MODULE_HPP #define TEACHOS_KAPI_BOOT_MODULES_MODULE_HPP -// IWYU pragma: private, include - #include #include @@ -10,6 +8,10 @@ namespace kapi::boot_modules { + + //! @addtogroup kapi-boot_modules + //! @{ + // ! The boot module struct // ! // ! The boot module struct represents a module loaded by the bootloader, and contains information about it, such as @@ -20,6 +22,8 @@ namespace kapi::boot_modules memory::linear_address start_address{}; std::size_t size{}; }; -} // namespace kapi::boot_modules + //! @} + +} // namespace kapi::boot_modules #endif \ No newline at end of file diff --git a/kapi/kapi/boot_modules/registry.hpp b/kapi/kapi/boot_modules/registry.hpp deleted file mode 100644 index 33dc2169..00000000 --- a/kapi/kapi/boot_modules/registry.hpp +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef TEACHOS_KAPI_BOOT_MODULES_REGISTRY_HPP -#define TEACHOS_KAPI_BOOT_MODULES_REGISTRY_HPP - -// IWYU pragma: private, include - -#include - -#include - -#include - -namespace kapi::boot_modules -{ - - //! @addtogroup kapi-boot_modules-kernel-defined - //! @{ - - //! The boot module registry - //! - //! The boot module registry is responsible for keeping track of the modules loaded by the bootloader, and - //! providing access to them for the rest of the kernel. - struct registry - { - using container = kstd::vector; - using value_type = container::value_type; - using const_reference = container::const_reference; - - using const_iterator = container::const_iterator; - using const_reverse_iterator = container::const_reverse_iterator; - using size_type = container::size_type; - - auto static init() -> void; - - auto static get() -> registry &; - - registry() = default; - - [[nodiscard]] auto begin() const noexcept -> const_iterator; - - [[nodiscard]] auto end() const noexcept -> const_iterator; - - [[nodiscard]] auto cbegin() const noexcept -> const_iterator; - - [[nodiscard]] auto cend() const noexcept -> const_iterator; - - [[nodiscard]] auto rbegin() const noexcept -> const_reverse_iterator; - - [[nodiscard]] auto rend() const noexcept -> const_reverse_iterator; - - [[nodiscard]] auto crbegin() const noexcept -> const_reverse_iterator; - - [[nodiscard]] auto crend() const noexcept -> const_reverse_iterator; - - [[nodiscard]] auto front() const noexcept -> const_reference; - - [[nodiscard]] auto back() const noexcept -> const_reference; - - [[nodiscard]] auto size() const noexcept -> std::size_t; - - [[nodiscard]] auto empty() const noexcept -> bool; - - [[nodiscard]] auto at(std::size_t index) const -> const_reference; - - [[nodiscard]] auto operator[](std::size_t index) const noexcept -> const_reference; - - auto add(module module) -> void; - - private: - container m_modules{}; - }; - - //! @} - -} // namespace kapi::boot_modules - -#endif \ No newline at end of file diff --git a/kapi/kapi/test_support/boot_modules.hpp b/kapi/kapi/test_support/boot_modules.hpp deleted file mode 100644 index 86a81307..00000000 --- a/kapi/kapi/test_support/boot_modules.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef KAPI_TEST_SUPPORT_BOOT_MODULES_HPP -#define KAPI_TEST_SUPPORT_BOOT_MODULES_HPP - -#include - -#include - -namespace kapi::test_support::boot_modules -{ - - auto deinit_registry() -> void; - - auto inject_registry(kapi::boot_modules::registry && registry) -> std::optional; - -} // namespace kapi::test_support::boot_modules - -#endif diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 5a0b42a8..0d6c4ba9 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -8,8 +8,7 @@ add_library("kernel::lib" ALIAS "kernel_lib") target_sources("kernel_lib" PRIVATE # Kernel-defined KAPI Implementation "kapi/acpi.cpp" - "kapi/boot_module/bus.cpp" - "kapi/boot_module/registry.cpp" + "kapi/boot_modules/device.cpp" "kapi/cio.cpp" "kapi/cpu.cpp" "kapi/devices.cpp" @@ -43,6 +42,9 @@ target_sources("kernel_lib" PRIVATE "kernel/devices/block_device_utils.cpp" "kernel/devices/root_bus.cpp" + # Bus Sumbsystem + "kernel/bus/boot_modules.cpp" + # Driver Subsystem "kernel/drivers/storage/ram_disk.cpp" "kernel/drivers/init.cpp" @@ -154,6 +156,7 @@ if(BUILD_TESTING) teachos_add_tests("kernel") target_sources("kernel_tests" PRIVATE + "kernel/test_support/kapi/boot_modules.cpp" "kernel/test_support/kapi/cpu.cpp" "kernel/test_support/kapi/cio.cpp" "kernel/test_support/kapi/interrupts.cpp" diff --git a/kernel/kapi/boot_module/bus.cpp b/kernel/kapi/boot_module/bus.cpp deleted file mode 100644 index 17052629..00000000 --- a/kernel/kapi/boot_module/bus.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include - -#include -#include - -#include -#include -#include -#include - -#include -#include -#include - -namespace kapi::boot_modules -{ - auto has_parameter(std::string_view cmdline, std::string_view key, std::string_view value) -> bool - { - for (auto token : std::views::split(cmdline, ' ')) - { - auto const text = std::string_view{token}; - auto const equals = text.find('='); - if (equals != std::string_view::npos && text.substr(0, equals) == key && text.substr(equals + 1) == value) - { - return true; - } - } - return false; - } - - boot_module_device::boot_module_device(std::size_t index, struct module const & module) - : kapi::devices::device{kstd::format("boot_module{}", index)} - , m_module(module) - {} - - auto boot_module_device::command_line() const -> std::string_view - { - return m_module.name; - } - - auto boot_module_device::module() const -> boot_modules::module const & - { - return m_module; - } - - auto boot_module_device::query_interface(kapi::devices::interface_id interface) -> void * - { - if (interface == boot_module_identification::id) - { - return static_cast(this); - } - - return kapi::devices::device::query_interface(interface); - } - - boot_module_bus::boot_module_bus(registry const * registry) - : kapi::devices::bus{"boot_modules"} - , m_registry(registry) - {} - - auto boot_module_bus::enumerate(kapi::devices::bus & self) -> void - { - std::size_t index = 0; - - for (auto const & module : *m_registry) - { - self.add_child(kstd::make_shared(index++, module)); - } - } - - auto boot_module_bus::match(kapi::devices::device const & dev, kapi::devices::driver const & drv) const - -> kstd::result - { - auto const * ident = dev.as(); - auto const * claims = drv.as(); - - if (!ident || !claims) - { - return kstd::failure(kapi::devices::driver_match_errc::no_match); - } - - auto const [key, value] = claims->claimed_parameter(); - - if (!has_parameter(ident->command_line(), key, value)) - { - return kstd::failure(kapi::devices::driver_match_errc::no_match); - } - - return 0u; - } - - auto boot_module_bus::protocol() -> kapi::devices::bus_protocol * - { - return this; - } - -} // namespace kapi::boot_modules diff --git a/kernel/kapi/boot_module/registry.cpp b/kernel/kapi/boot_module/registry.cpp deleted file mode 100644 index 9876866b..00000000 --- a/kernel/kapi/boot_module/registry.cpp +++ /dev/null @@ -1,131 +0,0 @@ -#include - -#include -#include -#include - -#include -#include -#include - -namespace kapi::boot_modules -{ - - namespace - { - constinit auto instance = std::optional{}; - } - - auto registry::init() -> void - { - if (instance) - { - system::panic("[OS] Boot module registry has already been initialized."); - } - - instance.emplace(); - } - - auto registry::get() -> registry & - { - if (!instance) - { - system::panic("[OS] Boot module registry has not been initialized."); - } - - return *instance; - } - - auto registry::begin() const noexcept -> const_iterator - { - return m_modules.begin(); - } - - auto registry::end() const noexcept -> const_iterator - { - return m_modules.end(); - } - - auto registry::cbegin() const noexcept -> const_iterator - { - return m_modules.cbegin(); - } - - auto registry::cend() const noexcept -> const_iterator - { - return m_modules.cend(); - } - - auto registry::rbegin() const noexcept -> const_reverse_iterator - { - return m_modules.rbegin(); - } - - auto registry::rend() const noexcept -> const_reverse_iterator - { - return m_modules.rend(); - } - - auto registry::crbegin() const noexcept -> const_reverse_iterator - { - return m_modules.crbegin(); - } - - auto registry::crend() const noexcept -> const_reverse_iterator - { - return m_modules.crend(); - } - - auto registry::front() const noexcept -> const_reference - { - return m_modules.front(); - } - - auto registry::back() const noexcept -> const_reference - { - return m_modules.back(); - } - - auto registry::size() const noexcept -> std::size_t - { - return m_modules.size(); - } - - auto registry::empty() const noexcept -> bool - { - return m_modules.empty(); - } - - auto registry::at(std::size_t index) const -> const_reference - { - return m_modules.at(index); - } - - auto registry::operator[](std::size_t index) const noexcept -> const_reference - { - return m_modules[index]; - } - - auto registry::add(module module) -> void - { - m_modules.push_back(module); - } - -} // namespace kapi::boot_modules - -namespace kapi::test_support::boot_modules -{ - - auto deinit_registry() -> void - { - kapi::boot_modules::instance.reset(); - } - - auto inject_registry(kapi::boot_modules::registry && registry) -> std::optional - { - auto old = std::move(kapi::boot_modules::instance); - kapi::boot_modules::instance.emplace(std::move(registry)); - return old; - } - -} // namespace kapi::test_support::boot_modules diff --git a/kernel/kapi/boot_modules/device.cpp b/kernel/kapi/boot_modules/device.cpp new file mode 100644 index 00000000..84a43c1c --- /dev/null +++ b/kernel/kapi/boot_modules/device.cpp @@ -0,0 +1,39 @@ +#include + +#include +#include + +#include + +#include +#include + +namespace kapi::boot_modules +{ + + device::device(std::size_t index, kapi::boot_modules::module const & module) + : kapi::devices::device{kstd::format("device{}", index)} + , m_module(module) + {} + + auto device::command_line() const -> std::string_view + { + return m_module.name; + } + + auto device::module() const -> boot_modules::module const & + { + return m_module; + } + + auto device::query_interface(kapi::devices::interface_id interface) -> void * + { + if (interface == boot_module_signature::id) + { + return static_cast(this); + } + + return kapi::devices::device::query_interface(interface); + } + +} // namespace kapi::boot_modules \ No newline at end of file diff --git a/kernel/kernel/bus/boot_modules.cpp b/kernel/kernel/bus/boot_modules.cpp new file mode 100644 index 00000000..ac5a2068 --- /dev/null +++ b/kernel/kernel/bus/boot_modules.cpp @@ -0,0 +1,60 @@ +#include + +#include +#include + +#include +#include + +#include +#include + +namespace kernel::bus +{ + auto has_parameter(std::string_view cmdline, std::string_view key, std::string_view value) -> bool + { + for (auto token : std::views::split(cmdline, ' ')) + { + auto const text = std::string_view{token}; + auto const equals = text.find('='); + if (equals != std::string_view::npos && text.substr(0, equals) == key && text.substr(equals + 1) == value) + { + return true; + } + } + return false; + } + + boot_modules::boot_modules() + : kapi::devices::bus{"boot_modules"} + {} + + auto boot_modules::enumerate(kapi::devices::bus &) -> void {} + + auto boot_modules::match(kapi::devices::device const & dev, kapi::devices::driver const & drv) const + -> kstd::result + { + auto signature = dev.as(); + auto claim = drv.as(); + + if (!signature || !claim) + { + return kstd::failure(kapi::devices::driver_match_errc::no_match); + } + + auto const [key, value] = claim->parameter(); + + if (!has_parameter(signature->command_line(), key, value)) + { + return kstd::failure(kapi::devices::driver_match_errc::no_match); + } + + return 0u; + } + + auto boot_modules::protocol() -> kapi::devices::bus_protocol * + { + return this; + } + +} // namespace kernel::bus diff --git a/kernel/kernel/bus/boot_modules.hpp b/kernel/kernel/bus/boot_modules.hpp new file mode 100644 index 00000000..147cfdf2 --- /dev/null +++ b/kernel/kernel/bus/boot_modules.hpp @@ -0,0 +1,28 @@ +#ifndef TEACHOS_KERNEL_BUS_BOOT_MODULES_HPP +#define TEACHOS_KERNEL_BUS_BOOT_MODULES_HPP + +#include + +#include + +namespace kernel::bus +{ + + //! The boot modules bus. + //! + //! Boot modules used to back devices may be attached to this bus. + struct boot_modules final : kapi::devices::bus, kapi::devices::bus_protocol + { + boot_modules(); + + auto enumerate(kapi::devices::bus & self) -> void override; + + [[nodiscard]] auto match(kapi::devices::device const & dev, kapi::devices::driver const & drv) const + -> kstd::result override; + + [[nodiscard]] auto protocol() -> kapi::devices::bus_protocol * override; + }; + +} // namespace kernel::bus + +#endif \ No newline at end of file diff --git a/kernel/kernel/devices/storage.cpp b/kernel/kernel/devices/storage.cpp index cbd350ce..a95b1840 100644 --- a/kernel/kernel/devices/storage.cpp +++ b/kernel/kernel/devices/storage.cpp @@ -1,17 +1,18 @@ #include -#include +#include + #include #include namespace kernel::devices::storage { - auto init() -> void + auto init() -> kstd::shared_ptr { - auto modules_bus = kstd::make_shared(&kapi::boot_modules::registry::get()); + auto modules_bus = kstd::make_shared(); kapi::devices::get_root_bus()->add_child(modules_bus); - modules_bus->enumerate(*modules_bus); + return modules_bus; } auto determine_boot_device() -> kstd::shared_ptr diff --git a/kernel/kernel/devices/storage.hpp b/kernel/kernel/devices/storage.hpp index 1d0362a3..bbbe8e6f 100644 --- a/kernel/kernel/devices/storage.hpp +++ b/kernel/kernel/devices/storage.hpp @@ -10,7 +10,7 @@ namespace kernel::devices::storage { //! Attach the boot-time storage controllers to the system device tree. - auto init() -> void; + [[nodiscard]] auto init() -> kstd::shared_ptr; //! @brief Determine the boot device. //! diff --git a/kernel/kernel/devices/storage.tests.cpp b/kernel/kernel/devices/storage.tests.cpp index 30cafe4a..2a06149a 100644 --- a/kernel/kernel/devices/storage.tests.cpp +++ b/kernel/kernel/devices/storage.tests.cpp @@ -1,9 +1,12 @@ #include #include +#include +#include #include #include -#include + +#include #include @@ -13,38 +16,24 @@ TEST_CASE("Storage devices attached with init() are reachable from the root bus", "[devices][storage]") { auto storage = std::vector{4096, std::byte{}}; - auto registry = kapi::boot_modules::registry{}; - - registry.add({ + auto module = kapi::boot_modules::module{ .name = "test_module type=ramdisk", .start_address = kapi::memory::linear_address{storage.data()}, .size = storage.size(), - }); + }; + auto device = kstd::make_shared(0, module); - kapi::test_support::boot_modules::inject_registry(std::move(registry)); + auto bus = static_pointer_cast(kapi::devices::device_registry::get().find("boot_modules")); + REQUIRE(bus); - kernel::devices::storage::init(); + bus->add_child(device); auto block_devices = kapi::devices::interface_registry::get().all(kapi::devices::block_device::id); REQUIRE(block_devices.size() == 1); CHECK(block_devices[0].name() == "ram0"); - auto root = kapi::devices::get_root_bus(); - auto boot_modules_bus_found = false; - - for (auto const & child : root->children()) - { - if (child->name() == "boot_modules") - { - boot_modules_bus_found = true; - } - } - CHECK(boot_modules_bus_found); - auto boot_device = kernel::devices::storage::determine_boot_device(); REQUIRE(boot_device); CHECK(boot_device->state() == kapi::devices::state::bound); CHECK(boot_device->bound_driver() != nullptr); - - kapi::test_support::boot_modules::deinit_registry(); } \ No newline at end of file diff --git a/kernel/kernel/drivers/storage/ram_disk.cpp b/kernel/kernel/drivers/storage/ram_disk.cpp index f4223c19..8bf06b66 100644 --- a/kernel/kernel/drivers/storage/ram_disk.cpp +++ b/kernel/kernel/drivers/storage/ram_disk.cpp @@ -95,7 +95,7 @@ namespace kernel::drivers::storage auto ram_disk::probe(kapi::devices::device & device) -> kstd::result { - auto interface = device.as(); + auto interface = device.as(); if (!interface) { return kstd::failure(make_error_code(kstd::errc::invalid_argument)); @@ -134,7 +134,7 @@ namespace kernel::drivers::storage device.set_driver_data(nullptr); } - auto ram_disk::claimed_parameter() const -> std::pair + auto ram_disk::parameter() const -> std::pair { return {"type", "ramdisk"}; } @@ -146,9 +146,9 @@ namespace kernel::drivers::storage auto ram_disk::query_interface(kapi::devices::interface_id interface) -> void * { - if (interface == kapi::boot_modules::boot_module_driver_identification::id) + if (interface == kapi::boot_modules::boot_module_claim::id) { - return static_cast(this); + return static_cast(this); } return kapi::devices::driver::query_interface(interface); diff --git a/kernel/kernel/drivers/storage/ram_disk.hpp b/kernel/kernel/drivers/storage/ram_disk.hpp index ab4c5a1b..ac8d2879 100644 --- a/kernel/kernel/drivers/storage/ram_disk.hpp +++ b/kernel/kernel/drivers/storage/ram_disk.hpp @@ -14,13 +14,13 @@ namespace kernel::drivers::storage { - struct ram_disk final : kapi::devices::driver, kapi::boot_modules::boot_module_driver_identification + struct ram_disk final : kapi::devices::driver, kapi::boot_modules::boot_module_claim { [[nodiscard]] auto probe(kapi::devices::device & device) -> kstd::result override; auto unbind(kapi::devices::device & device) -> void override; - [[nodiscard]] auto claimed_parameter() const -> std::pair override; + [[nodiscard]] auto parameter() const -> std::pair override; [[nodiscard]] auto claimed_major() const -> std::optional override; diff --git a/kernel/kernel/main.cpp b/kernel/kernel/main.cpp index aa12df1a..9d841f79 100644 --- a/kernel/kernel/main.cpp +++ b/kernel/kernel/main.cpp @@ -167,12 +167,12 @@ auto main() -> int kapi::interrupts::enable(); kstd::println("[OS] Interrupts enabled."); - kapi::boot_modules::init(); - kstd::println("[OS] Boot module registry initialized."); - - kernel::devices::storage::init(); + auto modules_bus = kernel::devices::storage::init(); kstd::println("[OS] Storage subsystem initialized."); + kapi::boot_modules::init(*modules_bus); + kstd::println("[OS] Boot modules attached."); + kernel::filesystem::open_file_table::init(); kstd::println("[OS] Global open file table initialized."); diff --git a/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.cpp b/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.cpp index 75b25011..644e38fe 100644 --- a/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.cpp +++ b/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.cpp @@ -1,11 +1,12 @@ #include +#include #include #include +#include #include #include -#include #include @@ -76,16 +77,14 @@ namespace kernel::tests::filesystem return *this; } - storage_boot_module_fixture::~storage_boot_module_fixture() - { - kapi::test_support::boot_modules::deinit_registry(); - } + storage_boot_module_fixture::~storage_boot_module_fixture() {} auto storage_boot_module_fixture::setup_modules(std::size_t module_count, std::size_t module_size) -> void { m_module_names.clear(); m_module_data.clear(); - m_registry = {}; + + m_boot_module_bus = kstd::make_shared(); m_module_names.reserve(module_count); m_module_data.reserve(module_count); @@ -94,22 +93,15 @@ namespace kernel::tests::filesystem { m_module_names.push_back(std::format("test_mod{} type=ramdisk", i)); m_module_data.emplace_back(module_size, std::byte{static_cast(0x40 + (i % 16))}); - } - for (std::size_t i = 0; i < module_count; ++i) - { - m_registry.add({ + auto module = kapi::boot_modules::module{ .name = m_module_names[i].c_str(), .start_address = kapi::memory::linear_address{m_module_data[i].data()}, .size = m_module_data[i].size(), - }); + }; + m_boot_module_bus->add_child(kstd::make_shared(i, module)); } - kapi::test_support::boot_modules::inject_registry(std::move(m_registry)); - - m_boot_module_bus = kstd::make_shared(&kapi::boot_modules::registry::get()); - m_boot_module_bus->enumerate(*m_boot_module_bus); - if (module_count > 0 && kapi::devices::interface_registry::get().all(kapi::devices::block_device::id).empty()) { throw std::runtime_error{"No RAM disk driver bound to any of the test fixture's boot modules."}; @@ -121,7 +113,8 @@ namespace kernel::tests::filesystem { m_module_names.clear(); m_module_data.clear(); - m_registry = {}; + + m_boot_module_bus = kstd::make_shared(); if (module_names.size() != img_paths.size()) { @@ -130,30 +123,27 @@ namespace kernel::tests::filesystem for (size_t i = 0; i < module_names.size(); ++i) { - setup_module_from_img(module_names[i], img_paths[i]); + setup_module_from_img(i, module_names[i], img_paths[i]); } - kapi::test_support::boot_modules::inject_registry(std::move(m_registry)); - - m_boot_module_bus = kstd::make_shared(&kapi::boot_modules::registry::get()); - m_boot_module_bus->enumerate(*m_boot_module_bus); - if (!module_names.empty() && kapi::devices::interface_registry::get().all(kapi::devices::block_device::id).empty()) { throw std::runtime_error{"No RAM disk driver bound to any of the test fixture's boot modules."}; } } - auto storage_boot_module_fixture::setup_module_from_img(std::string const & module_name, + auto storage_boot_module_fixture::setup_module_from_img(std::size_t index, std::string const & module_name, std::filesystem::path const & img_path) -> void { m_module_names.push_back(module_name + " type=ramdisk"); auto & mapped_image = m_mapped_images.emplace_back(img_path); - m_registry.add({ + auto module = kapi::boot_modules::module{ .name = m_module_names.back().c_str(), .start_address = kapi::memory::linear_address{mapped_image.mapping}, .size = mapped_image.size, - }); + }; + + m_boot_module_bus->add_child(kstd::make_shared(index, module)); } } // namespace kernel::tests::filesystem \ No newline at end of file diff --git a/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.hpp b/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.hpp index fdb50037..990d640a 100644 --- a/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.hpp +++ b/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.hpp @@ -2,6 +2,7 @@ #define TEACHOS_KERNEL_TEST_SUPPORT_FILESYSTEM_STORAGE_BOOT_MODULE_FIXTURE_HPP #include +#include #include @@ -37,15 +38,14 @@ namespace kernel::tests::filesystem std::size_t size; }; - kapi::boot_modules::registry m_registry{}; std::vector m_module_names{}; std::vector> m_module_data{}; std::vector m_mapped_images{}; - - kstd::shared_ptr m_boot_module_bus{}; + kstd::shared_ptr m_boot_module_bus{}; private: - auto setup_module_from_img(std::string const & module_name, std::filesystem::path const & img_path) -> void; + auto setup_module_from_img(std::size_t index, std::string const & module_name, + std::filesystem::path const & img_path) -> void; }; } // namespace kernel::tests::filesystem diff --git a/kernel/kernel/test_support/kapi/boot_modules.cpp b/kernel/kernel/test_support/kapi/boot_modules.cpp new file mode 100644 index 00000000..a064139f --- /dev/null +++ b/kernel/kernel/test_support/kapi/boot_modules.cpp @@ -0,0 +1,6 @@ +#include + +namespace kapi::boot_modules +{ + auto init(kapi::devices::bus &) -> void {} +} // namespace kapi::boot_modules diff --git a/kernel/kernel/test_support/state_reset_listener.cpp b/kernel/kernel/test_support/state_reset_listener.cpp index 45e8f5cb..13561644 100644 --- a/kernel/kernel/test_support/state_reset_listener.cpp +++ b/kernel/kernel/test_support/state_reset_listener.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #include #include @@ -25,6 +25,8 @@ #include #include +#include + struct state_reset_listener : Catch::EventListenerBase { using EventListenerBase::EventListenerBase; @@ -36,6 +38,7 @@ struct state_reset_listener : Catch::EventListenerBase kapi::memory::init(); kapi::devices::init(); + std::ignore = kernel::devices::storage::init(); kernel::filesystem::open_file_table::init(); kapi::devices::driver_registry::get().add(kstd::make_shared()); @@ -48,7 +51,6 @@ struct state_reset_listener : Catch::EventListenerBase kernel::tests::filesystem::type_registry::deinit(); kernel::tests::filesystem::open_file_table::deinit(); - kapi::test_support::boot_modules::deinit_registry(); kapi::test_support::devices::deinit(); kernel::tests::memory::deinit(); kernel::tests::cpu::deinit(); -- cgit v1.2.3