aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/kapi/cpu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/kapi/cpu.cpp')
-rw-r--r--arch/x86_64/kapi/cpu.cpp80
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