diff options
| author | Lukas Oesch <lukas.oesch@ost.ch> | 2026-06-10 10:40:46 +0200 |
|---|---|---|
| committer | Lukas Oesch <lukas.oesch@ost.ch> | 2026-06-10 10:40:46 +0200 |
| commit | 33abd5cf264cb9e34121082105b0bc17b3cf7a36 (patch) | |
| tree | 36b15d53fea04f4f9d9af817100f7ad013bd9b5c /arch/x86_64/kapi/cpu.cpp | |
| parent | d01caf1c4aef3c89c68b9d1cc9fe56445f0860b5 (diff) | |
| parent | 7e27130c342b7299a1d2188a7192a7f17b5ac2ad (diff) | |
| download | kernel-33abd5cf264cb9e34121082105b0bc17b3cf7a36.tar.xz kernel-33abd5cf264cb9e34121082105b0bc17b3cf7a36.zip | |
Merge of BA-FS26 branch into develop
See merge request teachos/kernel!49
Diffstat (limited to 'arch/x86_64/kapi/cpu.cpp')
| -rw-r--r-- | arch/x86_64/kapi/cpu.cpp | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index 2a0f8f7..40dc228 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -1,12 +1,90 @@ -#include "kapi/cpu.hpp" +#include <kapi/cpu.hpp> + +#include <arch/cpu/initialization.hpp> +#include <arch/devices/local_apic.hpp> + +#include <kapi/acpi.hpp> +#include <kapi/devices.hpp> +#include <kapi/devices/cpu.hpp> +#include <kapi/memory.hpp> +#include <kapi/system.hpp> + +#include <acpi/acpi.hpp> + +#include <kstd/memory> +#include <kstd/print> + +#include <atomic> +#include <ranges> +#include <utility> namespace kapi::cpu { + namespace + { + constexpr auto candidate_flags = ::acpi::processor_local_apic_entry::flags::processor_enabled // + | ::acpi::processor_local_apic_entry::flags::online_capable; + } + + auto init() -> void + { + auto static constinit is_initialized = std::atomic_flag{}; + + if (is_initialized.test_and_set()) + { + system::panic("[x86_64] CPU has already been initialized."); + } + + arch::cpu::initialize_descriptors(); + arch::cpu::initialize_legacy_interrupts(); + } + auto halt() -> void { asm volatile("1: hlt\njmp 1b"); __builtin_unreachable(); } + auto discover_topology() -> bool + { + auto static const cpu_major = kapi::devices::allocate_major_number(); + auto static const core_major = kapi::devices::allocate_major_number(); + auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); + + auto madt = kapi::acpi::get_table<::acpi::table_signature_v<::acpi::madt>>(); + if (!madt) + { + kstd::println("[x86_64:PLT] Failed to find ACPI APIC table"); + return false; + } + + auto lapic_entries = *madt | std::views::filter([](auto const & entry) { + return entry.type() == ::acpi::madt_entry::type::processor_local_apic; + }) | std::views::transform([](auto const & entry) { + return static_cast<::acpi::processor_local_apic_entry const &>(entry); + }) | std::views::filter([](auto const & entry) { return static_cast<bool>(entry.flags() & candidate_flags); }); + + auto bsp_found = false; + auto core_count = 0uz; + auto local_apic_address = memory::physical_address{madt->local_interrupt_controller_address()}; + auto cpu_bus = kstd::make_unique<devices::cpu>(cpu_major, 0); + + for (auto const & apic : lapic_entries) + { + auto is_bsp = !bsp_found; + bsp_found = true; + auto core = kstd::make_unique<devices::cpu::core>(core_major, core_count, apic.processor_id(), is_bsp); + core->add_child(kstd::make_unique<arch::devices::local_apic>(interrupt_controller_major, core_count, apic.id(), + local_apic_address, is_bsp)); + cpu_bus->add_child(std::move(core)); + ++core_count; + } + + devices::get_root_bus().add_child(std::move(cpu_bus)); + + kstd::println("[x86_64:PLT] Found {} CPU cores", core_count); + return core_count > 0; + } + } // namespace kapi::cpu |
