From 7d241968ff4feb83bc23636df3a7ddb80f4bb4a9 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 24 Jul 2026 00:55:57 +0200 Subject: kapi/device: remove init() --- arch/x86_64/arch/devices/legacy_pit.cpp | 5 --- arch/x86_64/arch/devices/legacy_pit.hpp | 3 -- arch/x86_64/arch/devices/local_apic.cpp | 7 +--- arch/x86_64/arch/devices/local_apic.hpp | 2 -- kapi/kapi/boot_module/bus.hpp | 2 -- kapi/kapi/devices/bus.hpp | 14 -------- kapi/kapi/devices/device.hpp | 6 ---- kernel/kapi/boot_module/bus.cpp | 8 ++--- kernel/kapi/devices.cpp | 1 - kernel/kapi/devices.tests.cpp | 5 --- kernel/kapi/devices/bus.cpp | 40 ---------------------- kernel/kapi/devices/bus.tests.cpp | 10 ------ kernel/kapi/devices/interface_registry.tests.cpp | 5 --- .../kernel/test_support/devices/block_device.cpp | 5 --- .../kernel/test_support/devices/block_device.hpp | 2 -- .../test_support/devices/character_device.cpp | 5 --- .../test_support/devices/character_device.hpp | 2 -- .../filesystem/storage_boot_module_fixture.cpp | 25 ++++++++------ .../filesystem/storage_boot_module_fixture.hpp | 5 ++- .../kernel/test_support/state_reset_listener.cpp | 6 ++++ libs/kstd/kstd/libc/string.cpp | 10 ++---- 21 files changed, 29 insertions(+), 139 deletions(-) diff --git a/arch/x86_64/arch/devices/legacy_pit.cpp b/arch/x86_64/arch/devices/legacy_pit.cpp index 5e968a6b..b2a30ef9 100644 --- a/arch/x86_64/arch/devices/legacy_pit.cpp +++ b/arch/x86_64/arch/devices/legacy_pit.cpp @@ -42,11 +42,6 @@ namespace arch::devices return "pit"; } - auto pit_device::init() -> bool - { - return true; - } - auto pit_device::query_interface(kapi::devices::interface interface) -> void * { if (interface == arch::bus::isa_identification::id) diff --git a/arch/x86_64/arch/devices/legacy_pit.hpp b/arch/x86_64/arch/devices/legacy_pit.hpp index 1bb41e2b..f0a18ecb 100644 --- a/arch/x86_64/arch/devices/legacy_pit.hpp +++ b/arch/x86_64/arch/devices/legacy_pit.hpp @@ -22,9 +22,6 @@ namespace arch::devices { pit_device(); - // TODO: remove - auto init() -> bool override; - //! Get the synthetic ISA identifier for this device. //! //! @return the string "pit" diff --git a/arch/x86_64/arch/devices/local_apic.cpp b/arch/x86_64/arch/devices/local_apic.cpp index e7108f8f..2b32c864 100644 --- a/arch/x86_64/arch/devices/local_apic.cpp +++ b/arch/x86_64/arch/devices/local_apic.cpp @@ -82,9 +82,6 @@ namespace arch::devices , m_hardware_id{hardware_id} , m_base{base} , m_is_bsp{is_bsp} - {} - - auto local_apic::init() -> bool { auto static shared_virtual_base = kapi::memory::allocate_mmio_region(1); auto static is_mapped = false; @@ -94,7 +91,7 @@ namespace arch::devices if (!kapi::memory::map_mmio_region(shared_virtual_base, m_base, kapi::memory::page_mapper::flags::writable)) { kstd::println("[x86_64:DEV] LAPIC {} MMIO mapping failed!", m_hardware_id); - return false; + return; } is_mapped = true; } @@ -117,8 +114,6 @@ namespace arch::devices { kstd::println("[x86_64:DEV] LAPIC {} is not on the BSP, deferring initialization.", m_hardware_id); } - - return true; } auto local_apic::core_index() const noexcept -> std::uint64_t diff --git a/arch/x86_64/arch/devices/local_apic.hpp b/arch/x86_64/arch/devices/local_apic.hpp index 4b82050b..61fc99be 100644 --- a/arch/x86_64/arch/devices/local_apic.hpp +++ b/arch/x86_64/arch/devices/local_apic.hpp @@ -14,8 +14,6 @@ namespace arch::devices { local_apic(std::uint64_t core_index, std::uint64_t hardware_id, kapi::memory::physical_address base, bool is_bsp); - auto init() -> bool override; - [[nodiscard]] auto core_index() const noexcept -> std::uint64_t; private: diff --git a/kapi/kapi/boot_module/bus.hpp b/kapi/kapi/boot_module/bus.hpp index e569b253..4d195111 100644 --- a/kapi/kapi/boot_module/bus.hpp +++ b/kapi/kapi/boot_module/bus.hpp @@ -38,8 +38,6 @@ namespace kapi::boot_modules { boot_module_device(std::size_t index, boot_module const & module); - auto init() -> bool override; - auto command_line() const -> std::string_view override; auto module() const -> boot_module const & override; diff --git a/kapi/kapi/devices/bus.hpp b/kapi/kapi/devices/bus.hpp index 10b9c39d..af9f2c33 100644 --- a/kapi/kapi/devices/bus.hpp +++ b/kapi/kapi/devices/bus.hpp @@ -10,7 +10,6 @@ #include #include -#include #include namespace kapi::devices @@ -27,11 +26,6 @@ namespace kapi::devices //! @param name The name of the bus. explicit bus(kstd::string const & name); - //! Initialize the bus and all of its children. - //! - //! @return true iff. the bus and all of its children are healthy, false otherwise. - auto init() -> bool final; - //! Attach a child device to this bus. //! //! Whenever a device is attached to a bus, the bus takes sole ownership of the device. @@ -46,16 +40,8 @@ namespace kapi::devices //! @return this bus's protocol facet if it has one, nullptr otherwise. [[nodiscard]] virtual auto protocol() -> struct bus_protocol *; - protected: - //! Enumerate the devices on the bus. - //! - //! @return true iff. the bus hardware is healthy, false otherwise. - auto virtual enumerate_old() -> bool; - private: kstd::vector> m_devices; - std::atomic_flag m_init_was_called{}; - std::atomic_flag m_initialized{}; }; //! @} diff --git a/kapi/kapi/devices/device.hpp b/kapi/kapi/devices/device.hpp index c1b2065b..70c5bd15 100644 --- a/kapi/kapi/devices/device.hpp +++ b/kapi/kapi/devices/device.hpp @@ -39,12 +39,6 @@ namespace kapi::devices */ virtual ~device() = default; - /** - * @brief Initialize the device. - * @return true on success, false otherwise. - */ - virtual auto init() -> bool = 0; - template [[nodiscard]] constexpr auto as() -> InterfaceType * { diff --git a/kernel/kapi/boot_module/bus.cpp b/kernel/kapi/boot_module/bus.cpp index b917866c..3e95a3ac 100644 --- a/kernel/kapi/boot_module/bus.cpp +++ b/kernel/kapi/boot_module/bus.cpp @@ -20,7 +20,8 @@ namespace kapi::boot_modules { auto const text = std::string_view{token}; auto const equals = text.find('='); - if (equals != std::string_view::npos && text.substr(equals) == key && text.substr(equals + 1) == value) + if (equals != std::string_view::npos && text.substr(0, equals) == key && + text.substr(equals + 1, value.size()) == value) { return true; } @@ -33,11 +34,6 @@ namespace kapi::boot_modules , m_module(module) {} - auto boot_module_device::init() -> bool - { - return true; - } - auto boot_module_device::command_line() const -> std::string_view { return m_module.name; diff --git a/kernel/kapi/devices.cpp b/kernel/kapi/devices.cpp index 5eed1b87..267bfb4d 100644 --- a/kernel/kapi/devices.cpp +++ b/kernel/kapi/devices.cpp @@ -35,7 +35,6 @@ namespace kapi::devices root_bus = kstd::make_shared(); register_device(root_bus); - root_bus->init(); } auto get_root_bus() -> kstd::shared_ptr diff --git a/kernel/kapi/devices.tests.cpp b/kernel/kapi/devices.tests.cpp index 236a812d..627c5ce0 100644 --- a/kernel/kapi/devices.tests.cpp +++ b/kernel/kapi/devices.tests.cpp @@ -15,11 +15,6 @@ namespace struct test_device final : kapi::devices::device { using kapi::devices::device::device; - - auto init() -> bool override - { - return true; - } }; } // namespace diff --git a/kernel/kapi/devices/bus.cpp b/kernel/kapi/devices/bus.cpp index 25e35733..db693eac 100644 --- a/kernel/kapi/devices/bus.cpp +++ b/kernel/kapi/devices/bus.cpp @@ -5,11 +5,9 @@ #include #include -#include #include #include -#include #include #include @@ -19,30 +17,6 @@ namespace kapi::devices : device{name} {} - auto bus::init() -> bool - { - if (m_init_was_called.test_and_set()) - { - kstd::println(kstd::print_sink::stderr, "[OS:DEV] Bus {} already initialized", name()); - return true; - } - - if (!enumerate_old()) - { - kstd::println(kstd::print_sink::stderr, "[OS:DEV] Bus {} enumeration failed", name()); - return false; - } - - auto child_status = std::ranges::fold_left(m_devices, true, [&](bool acc, auto & child) -> bool { - kstd::println("[OS:DEV] Initializing child device {}@{}", child->name(), name()); - return child->init() && acc; - }); - - m_initialized.test_and_set(); - - return child_status; - } - auto bus::add_child(kstd::shared_ptr child) -> void { child->set_parent(kstd::static_pointer_cast(shared_from_this())); @@ -56,15 +30,6 @@ namespace kapi::devices attached->set_state(state::present); driver_registry::get().device_attached(attached); - - if (m_initialized.test()) - { - kstd::println("[OS:DEV] Initializing child device {}@{}", attached->name(), name()); - if (!attached->init()) - { - kapi::system::panic("[OS:DEV] Failed to initialize child device"); - } - } } [[nodiscard]] auto bus::children() const -> std::span const> @@ -77,9 +42,4 @@ namespace kapi::devices return nullptr; } - auto bus::enumerate_old() -> bool - { - return true; - } - } // namespace kapi::devices \ No newline at end of file diff --git a/kernel/kapi/devices/bus.tests.cpp b/kernel/kapi/devices/bus.tests.cpp index d5a2d1cf..9f95513b 100644 --- a/kernel/kapi/devices/bus.tests.cpp +++ b/kernel/kapi/devices/bus.tests.cpp @@ -16,11 +16,6 @@ namespace struct test_device final : kapi::devices::device { using kapi::devices::device::device; - - auto init() -> bool override - { - return true; - } }; struct flagging_device final : kapi::devices::device @@ -35,11 +30,6 @@ namespace *m_destroyed_flag = true; } - auto init() -> bool override - { - return true; - } - private: bool * m_destroyed_flag; }; diff --git a/kernel/kapi/devices/interface_registry.tests.cpp b/kernel/kapi/devices/interface_registry.tests.cpp index aea7776c..78cd7ecf 100644 --- a/kernel/kapi/devices/interface_registry.tests.cpp +++ b/kernel/kapi/devices/interface_registry.tests.cpp @@ -35,11 +35,6 @@ namespace , m_const_device{&const_device} {} - auto init() -> bool override - { - return true; - } - [[nodiscard]] constexpr auto get_value() const noexcept -> int override { return m_value; diff --git a/kernel/kernel/test_support/devices/block_device.cpp b/kernel/kernel/test_support/devices/block_device.cpp index 3052a26d..3fd84611 100644 --- a/kernel/kernel/test_support/devices/block_device.cpp +++ b/kernel/kernel/test_support/devices/block_device.cpp @@ -24,11 +24,6 @@ namespace kernel::tests::devices data.resize(initial_size.value, 0); } - auto block_device::init() -> bool - { - return true; - } - auto block_device::read_block(size_t block_index, void * buffer) const -> kstd::result { auto const offset = block_index * block_size(); diff --git a/kernel/kernel/test_support/devices/block_device.hpp b/kernel/kernel/test_support/devices/block_device.hpp index c3a05aef..dc4f685e 100644 --- a/kernel/kernel/test_support/devices/block_device.hpp +++ b/kernel/kernel/test_support/devices/block_device.hpp @@ -18,8 +18,6 @@ namespace kernel::tests::devices { block_device(kstd::string const & name, kstd::units::bytes block_size, kstd::units::bytes initial_size = {}); - auto init() -> bool override; - auto read_block(size_t block_index, void * buffer) const -> kstd::result override; auto write_block(size_t block_index, void const * buffer) -> kstd::result override; diff --git a/kernel/kernel/test_support/devices/character_device.cpp b/kernel/kernel/test_support/devices/character_device.cpp index a433fe2d..b72c2a3f 100644 --- a/kernel/kernel/test_support/devices/character_device.cpp +++ b/kernel/kernel/test_support/devices/character_device.cpp @@ -9,9 +9,4 @@ namespace kernel::tests::devices character_device::character_device(kstd::string const & name) : kapi::devices::device{name} {} - - auto character_device::init() -> bool - { - return true; - } } // namespace kernel::tests::devices \ No newline at end of file diff --git a/kernel/kernel/test_support/devices/character_device.hpp b/kernel/kernel/test_support/devices/character_device.hpp index fe4b9370..31a5aafc 100644 --- a/kernel/kernel/test_support/devices/character_device.hpp +++ b/kernel/kernel/test_support/devices/character_device.hpp @@ -12,8 +12,6 @@ namespace kernel::tests::devices struct character_device : kapi::devices::device { character_device(kstd::string const & name); - - auto init() -> bool override; }; } // namespace kernel::tests::devices 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 b0a29118..79a9eae1 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 #include @@ -93,7 +94,7 @@ namespace kernel::tests::filesystem for (std::size_t i = 0; i < module_count; ++i) { - m_module_names.push_back(std::format("test_mod{}", i)); + 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))}); } @@ -105,11 +106,13 @@ namespace kernel::tests::filesystem kapi::boot_modules::set_boot_module_registry(m_registry); - m_storage_controller = kstd::make_shared( - &kapi::boot_modules::get_boot_module_registry()); - if (!m_storage_controller->init()) + m_boot_module_bus = + kstd::make_shared(&kapi::boot_modules::get_boot_module_registry()); + 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{"Failed to initialize the test fixture's RAM disk controller."}; + throw std::runtime_error{"No RAM disk driver bound to any of the test fixture's boot modules."}; } } @@ -132,11 +135,13 @@ namespace kernel::tests::filesystem kapi::boot_modules::set_boot_module_registry(m_registry); - m_storage_controller = kstd::make_shared( - &kapi::boot_modules::get_boot_module_registry()); - if (!m_storage_controller->init()) + m_boot_module_bus = + kstd::make_shared(&kapi::boot_modules::get_boot_module_registry()); + 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{"Failed to initialize the test fixture's RAM disk controller."}; + throw std::runtime_error{"No RAM disk driver bound to any of the test fixture's boot modules."}; } } 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 a8df7090..48bddf31 100644 --- a/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.hpp +++ b/kernel/kernel/test_support/filesystem/storage_boot_module_fixture.hpp @@ -1,9 +1,8 @@ #ifndef TEACHOS_KERNEL_TEST_SUPPORT_FILESYSTEM_STORAGE_BOOT_MODULE_FIXTURE_HPP #define TEACHOS_KERNEL_TEST_SUPPORT_FILESYSTEM_STORAGE_BOOT_MODULE_FIXTURE_HPP -#include - #include +#include #include @@ -44,7 +43,7 @@ namespace kernel::tests::filesystem std::vector> m_module_data{}; std::vector m_mapped_images{}; - kstd::shared_ptr m_storage_controller{}; + 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; diff --git a/kernel/kernel/test_support/state_reset_listener.cpp b/kernel/kernel/test_support/state_reset_listener.cpp index a6dc6528..1f2cb769 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 @@ -10,9 +11,12 @@ #include #include #include +#include #include #include +#include + #include #include #include @@ -30,6 +34,8 @@ struct state_reset_listener : Catch::EventListenerBase kapi::cpu::init(); kapi::memory::init(); kapi::devices::init(); + + kapi::devices::driver_registry::get().add(kstd::make_shared()); } void testCaseEnded(Catch::TestCaseStats const &) override diff --git a/libs/kstd/kstd/libc/string.cpp b/libs/kstd/kstd/libc/string.cpp index 986f63a6..bd4a4048 100644 --- a/libs/kstd/kstd/libc/string.cpp +++ b/libs/kstd/kstd/libc/string.cpp @@ -74,13 +74,9 @@ namespace kstd::libc auto memchr(void const * buffer, int character, std::size_t size) noexcept -> void * { - auto as_char_ptr = reinterpret_cast(buffer); - auto found = kstd::char_traits::find(as_char_ptr, size, static_cast(character)); - - if (found == as_char_ptr + size) - { - return nullptr; - } + auto as_char_ptr = reinterpret_cast(buffer); + auto as_char = static_cast(character); + auto found = kstd::char_traits::find(as_char_ptr, size, as_char); return const_cast(static_cast(found)); } -- cgit v1.2.3