From 5ce5c28e0f0f034cb7c704f08e59089ef303ff7e Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 1 Aug 2026 00:33:56 +0200 Subject: kapi: add automatic driver registration --- arch/x86_64/CMakeLists.txt | 1 - arch/x86_64/arch/drivers/cpu/lapic.cpp | 20 +++++++++++++++++++- arch/x86_64/arch/drivers/init.cpp | 29 ----------------------------- arch/x86_64/arch/drivers/init.hpp | 11 ----------- arch/x86_64/arch/drivers/pit.cpp | 18 ++++++++++++++++++ arch/x86_64/kapi/devices.cpp | 26 ++++++++++++++++++++++++-- arch/x86_64/scripts/kernel.ld | 11 +++++++++++ 7 files changed, 72 insertions(+), 44 deletions(-) delete mode 100644 arch/x86_64/arch/drivers/init.cpp delete mode 100644 arch/x86_64/arch/drivers/init.hpp (limited to 'arch') diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt index 19022b5f..fc665c15 100644 --- a/arch/x86_64/CMakeLists.txt +++ b/arch/x86_64/CMakeLists.txt @@ -50,7 +50,6 @@ target_sources("x86_64" PRIVATE "arch/devices/cpu/lapic.cpp" # Drivers - "arch/drivers/init.cpp" "arch/drivers/cpu/lapic.cpp" "arch/drivers/pit.cpp" diff --git a/arch/x86_64/arch/drivers/cpu/lapic.cpp b/arch/x86_64/arch/drivers/cpu/lapic.cpp index 2f74abc4..f8aa8e90 100644 --- a/arch/x86_64/arch/drivers/cpu/lapic.cpp +++ b/arch/x86_64/arch/drivers/cpu/lapic.cpp @@ -4,9 +4,11 @@ #include #include +#include #include #include +#include #include #include #include @@ -31,6 +33,22 @@ namespace arch::drivers::cpu constexpr auto offset_of_version = 0u; constexpr auto offset_of_max_lvt_entry = 16u; constexpr auto offset_of_eoi_suppression = 24u; + + struct descriptor final : kapi::devices::driver_descriptor + { + [[nodiscard]] auto make_instance() const -> kstd::shared_ptr override + { + return kstd::make_shared(); + } + + [[nodiscard]] auto name() const noexcept -> std::string_view override + { + return "intel_lapic"; + } + }; + + [[gnu::used]] + constexpr auto registration = kapi::devices::platform_driver{}; } // namespace enum struct lapic::registers : std::ptrdiff_t @@ -164,4 +182,4 @@ namespace arch::drivers::cpu return kapi::devices::driver::query_facet(facet); } -} // namespace arch::drivers +} // namespace arch::drivers::cpu diff --git a/arch/x86_64/arch/drivers/init.cpp b/arch/x86_64/arch/drivers/init.cpp deleted file mode 100644 index 19f45829..00000000 --- a/arch/x86_64/arch/drivers/init.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include - -#include -#include - -#include - -#include - -#include - -namespace arch::drivers -{ - - namespace - { - //! The interrupt frequency of the Programmable Interrupt Timer. - constexpr auto pit_frequency_in_hz = std::uint32_t{100u}; - } // namespace - - auto init_legacy_drivers() -> void - { - auto & driver_registry = kapi::devices::driver_registry::get(); - - driver_registry.add(kstd::make_shared(pit_frequency_in_hz)); - driver_registry.add(kstd::make_shared()); - } - -} // namespace arch::drivers diff --git a/arch/x86_64/arch/drivers/init.hpp b/arch/x86_64/arch/drivers/init.hpp deleted file mode 100644 index 797edb0f..00000000 --- a/arch/x86_64/arch/drivers/init.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef TEACHOS_ARCH_X86_64_DRIVERS_INIT_HPP -#define TEACHOS_ARCH_X86_64_DRIVERS_INIT_HPP - -namespace arch::drivers -{ - - auto init_legacy_drivers() -> void; - -} - -#endif diff --git a/arch/x86_64/arch/drivers/pit.cpp b/arch/x86_64/arch/drivers/pit.cpp index 49b3f228..e6e98796 100644 --- a/arch/x86_64/arch/drivers/pit.cpp +++ b/arch/x86_64/arch/drivers/pit.cpp @@ -29,6 +29,24 @@ namespace arch::drivers constexpr auto command_offset = 3u; constexpr auto pit_names = std::array{"pit"sv}; + constexpr auto pit_frequency_in_hz = std::uint32_t{100u}; + + struct descriptor final : kapi::devices::driver_descriptor + { + [[nodiscard]] auto make_instance() const -> kstd::shared_ptr override + { + return kstd::make_shared(pit_frequency_in_hz); + } + + [[nodiscard]] auto name() const noexcept -> std::string_view override + { + return "i8253_pit"sv; + } + }; + + [[gnu::used]] + constexpr auto registration = kapi::devices::platform_driver{}; + } // namespace pit::pit(std::uint32_t frequency_in_hz) diff --git a/arch/x86_64/kapi/devices.cpp b/arch/x86_64/kapi/devices.cpp index d9a0b2b9..888b6e20 100644 --- a/arch/x86_64/kapi/devices.cpp +++ b/arch/x86_64/kapi/devices.cpp @@ -2,11 +2,15 @@ #include #include -#include +#include #include #include +#include + +#include +#include namespace kapi::devices { @@ -16,9 +20,27 @@ namespace kapi::devices auto static constinit cpu_bus = kstd::shared_ptr{}; } + extern "C" + { + // We need to suppress clang-tidy linting warnings here, since these symbols are generated by the linker and we + // cannot choose their names, unless we wanted to extend the linker script needlessly. + // NOLINTBEGIN(readability-identifier-naming) + extern kstd::observer_ptr const __start_platform_drivers; + extern kstd::observer_ptr const __stop_platform_drivers; + // NOLINTEND(readability-identifier-naming) + } + auto init_platform_drivers() -> void { - arch::drivers::init_legacy_drivers(); + auto descriptors = std::span{&__start_platform_drivers, &__stop_platform_drivers} | // + std::views::filter([](auto p) { return p != nullptr; }); + + for (auto driver : descriptors) + { + auto instance = driver->make_instance(); + kstd::println("[x86_64:DRV] registering driver '{}' ({})", instance->name(), driver->name()); + kapi::devices::driver_registry::get().add(driver->make_instance()); + } } auto init_platform_devices() -> void diff --git a/arch/x86_64/scripts/kernel.ld b/arch/x86_64/scripts/kernel.ld index dbb0f8f0..f621c3f1 100644 --- a/arch/x86_64/scripts/kernel.ld +++ b/arch/x86_64/scripts/kernel.ld @@ -75,9 +75,20 @@ SECTIONS . = ALIGN(8); + /* Filesystem driver factories */ PROVIDE(__start_fs_types = .); KEEP(*(fs_types)); PROVIDE(__stop_fs_types = .); + + /* Kernel driver factories */ + PROVIDE(__start_kernel_drivers = .); + KEEP(*(kernel_drivers)); + PROVIDE(__stop_kernel_drivers = .); + + /* Platform driver factories */ + PROVIDE(__start_platform_drivers = .); + KEEP(*(platform_drivers)); + PROVIDE(__stop_platform_drivers = .); } :kernel_rodata .kernel_data ALIGN(4K) : AT (ADDR (.kernel_data) - TEACHOS_VMA) -- cgit v1.2.3