From f50815110789a0f8f6e5ca66ffd49b26578791a9 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 6 Apr 2026 18:43:28 +0200 Subject: kernel: generalize CPU discovery --- arch/x86_64/kapi/platform.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 arch/x86_64/kapi/platform.cpp (limited to 'arch/x86_64/kapi/platform.cpp') diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp new file mode 100644 index 0000000..2e7c7e4 --- /dev/null +++ b/arch/x86_64/kapi/platform.cpp @@ -0,0 +1,70 @@ +#include "kapi/platform.hpp" + +#include "kapi/acpi.hpp" +#include "kapi/devices.hpp" +#include "kapi/memory.hpp" + +#include "arch/devices/local_apic.hpp" + +#include +#include + +#include +#include + +namespace kapi::platform +{ + + namespace + { + constexpr auto default_lapic_address = kapi::memory::physical_address{0xFEE0'0000}; + } + + auto discover_cpu_topology(kapi::devices::bus & bus) -> bool + { + auto madt = kapi::acpi::get_table("APIC"); + if (!madt) + { + kstd::println("[x86_64:PLT] Failed to find ACPI APIC table"); + return false; + } + + auto const * current = reinterpret_cast(madt.get()) + sizeof(kapi::acpi::madt_header); + auto const * end = reinterpret_cast(madt.get()) + madt->length(); + + auto bsp_found = false; + auto core_count = 0uz; + + while (current < end) + { + auto const * sub_table = reinterpret_cast(current); + if (sub_table->type() == 0) + { + auto const * local_apic = reinterpret_cast(sub_table); + if (local_apic->flags() & 0b11) + { + auto is_bsp = !bsp_found; + bsp_found = true; + + if (kapi::platform::cpu_detected(bus, local_apic->processor_id(), is_bsp)) + { + ++core_count; + } + } + } + + current += sub_table->length(); + } + + kstd::println("[x86_64:PLT] Found {} CPU cores", core_count); + + return core_count > 0; + } + + auto create_core_interrupt_controller(std::size_t major, std::size_t minor, std::uint64_t hardware_id) + -> kstd::unique_ptr + { + return kstd::make_unique(major, minor, hardware_id, default_lapic_address); + } + +} // namespace kapi::platform \ No newline at end of file -- cgit v1.2.3 From d5c2e101d62f6b4b69c45c127e7a729d246da566 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 6 Apr 2026 19:04:16 +0200 Subject: kapi/platform: invert discovery dependencies --- arch/x86_64/kapi/platform.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'arch/x86_64/kapi/platform.cpp') diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp index 2e7c7e4..380fc66 100644 --- a/arch/x86_64/kapi/platform.cpp +++ b/arch/x86_64/kapi/platform.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include namespace kapi::platform { @@ -22,6 +22,10 @@ namespace kapi::platform auto discover_cpu_topology(kapi::devices::bus & bus) -> bool { + auto static const core_major = kapi::devices::allocate_major_number(); + auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); + auto static core_index = 0uz; + auto madt = kapi::acpi::get_table("APIC"); if (!madt) { @@ -46,7 +50,10 @@ namespace kapi::platform auto is_bsp = !bsp_found; bsp_found = true; - if (kapi::platform::cpu_detected(bus, local_apic->processor_id(), is_bsp)) + auto lapic = kstd::make_unique(interrupt_controller_major, core_index, + local_apic->apic_id(), default_lapic_address); + if (kapi::platform::cpu_detected(bus, core_major, core_index, local_apic->processor_id(), is_bsp, + std::move(lapic))) { ++core_count; } @@ -61,10 +68,4 @@ namespace kapi::platform return core_count > 0; } - auto create_core_interrupt_controller(std::size_t major, std::size_t minor, std::uint64_t hardware_id) - -> kstd::unique_ptr - { - return kstd::make_unique(major, minor, hardware_id, default_lapic_address); - } - } // namespace kapi::platform \ No newline at end of file -- cgit v1.2.3