aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arch/x86_64/CMakeLists.txt1
-rw-r--r--arch/x86_64/kapi/cpu.cpp54
-rw-r--r--arch/x86_64/kapi/platform.cpp62
-rw-r--r--kapi/include/kapi/cpu.hpp20
-rw-r--r--kapi/include/kapi/platform.hpp34
-rw-r--r--kernel/CMakeLists.txt1
-rw-r--r--kernel/kapi/cpu.cpp19
-rw-r--r--kernel/kapi/platform.cpp27
-rw-r--r--kernel/src/devices/cpu.cpp4
9 files changed, 95 insertions, 127 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt
index 87cb98c..62a2aad 100644
--- a/arch/x86_64/CMakeLists.txt
+++ b/arch/x86_64/CMakeLists.txt
@@ -19,7 +19,6 @@ target_sources("x86_64" PRIVATE
"kapi/devices.cpp"
"kapi/interrupts.cpp"
"kapi/memory.cpp"
- "kapi/platform.cpp"
"kapi/system.cpp"
# CPU Initialization
diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp
index 12edb0f..1382e08 100644
--- a/arch/x86_64/kapi/cpu.cpp
+++ b/arch/x86_64/kapi/cpu.cpp
@@ -1,14 +1,28 @@
#include "kapi/cpu.hpp"
+#include "kapi/acpi.hpp"
+#include "kapi/devices.hpp"
#include "kapi/system.hpp"
#include "arch/cpu/initialization.hpp"
+#include "arch/devices/local_apic.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::flags::processor_enabled //
+ | acpi::processor_local_apic::flags::online_capable;
+ }
+
auto init() -> void
{
auto static constinit is_initialized = std::atomic_flag{};
@@ -28,4 +42,44 @@ namespace kapi::cpu
__builtin_unreachable();
}
+ auto discover_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 madt = kapi::acpi::get_table<acpi::madt_table_signature>();
+ if (!madt)
+ {
+ kstd::println("[x86_64:PLT] Failed to find ACPI APIC table");
+ return false;
+ }
+
+ auto bsp_found = false;
+ auto core_count = 0uz;
+ auto local_apic_address = madt->local_interrupt_controller_address();
+
+ auto lapic_entries = *madt | std::views::filter([](auto const & entry) {
+ return entry.type() == acpi::multiple_apic_description_table_entry::types::processor_local_apic;
+ }) | std::views::transform([](auto const & entry) {
+ return static_cast<acpi::processor_local_apic const &>(entry);
+ }) | std::views::filter([](auto const & entry) {
+ return static_cast<bool>(entry.active_flags() & candidate_flags);
+ });
+
+ for (auto const & apic : lapic_entries)
+ {
+ auto is_bsp = !bsp_found;
+ bsp_found = true;
+ auto instance = kstd::make_unique<arch::devices::local_apic>(interrupt_controller_major, core_count,
+ apic.apic_id(), local_apic_address, is_bsp);
+ if (core_detected(bus, core_major, core_count, apic.processor_id(), is_bsp, std::move(instance)))
+ {
+ ++core_count;
+ }
+ }
+
+ kstd::println("[x86_64:PLT] Found {} CPU cores", core_count);
+ return core_count > 0;
+ }
+
} // namespace kapi::cpu
diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp
deleted file mode 100644
index fb27329..0000000
--- a/arch/x86_64/kapi/platform.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-#include "kapi/platform.hpp"
-
-#include "kapi/acpi.hpp"
-#include "kapi/devices.hpp"
-
-#include "arch/devices/local_apic.hpp"
-
-#include <kstd/memory>
-#include <kstd/print>
-
-#include <ranges>
-#include <utility>
-
-namespace kapi::platform
-{
- namespace
- {
- constexpr auto candidate_flags = acpi::processor_local_apic::flags::processor_enabled //
- | acpi::processor_local_apic::flags::online_capable;
- }
-
- 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 madt = kapi::acpi::get_table<acpi::madt_table_signature>();
- if (!madt)
- {
- kstd::println("[x86_64:PLT] Failed to find ACPI APIC table");
- return false;
- }
-
- auto bsp_found = false;
- auto core_count = 0uz;
- auto local_apic_address = madt->local_interrupt_controller_address();
-
- auto lapic_entries = *madt | std::views::filter([](auto const & entry) {
- return entry.type() == acpi::multiple_apic_description_table_entry::types::processor_local_apic;
- }) | std::views::transform([](auto const & entry) {
- return static_cast<acpi::processor_local_apic const &>(entry);
- }) | std::views::filter([](auto const & entry) {
- return static_cast<bool>(entry.active_flags() & candidate_flags);
- });
-
- for (auto const & apic : lapic_entries)
- {
- auto is_bsp = !bsp_found;
- bsp_found = true;
- auto instance = kstd::make_unique<arch::devices::local_apic>(interrupt_controller_major, core_count,
- apic.apic_id(), local_apic_address, is_bsp);
- if (kapi::platform::cpu_detected(bus, core_major, core_count, apic.processor_id(), is_bsp, std::move(instance)))
- {
- ++core_count;
- }
- }
-
- kstd::println("[x86_64:PLT] Found {} CPU cores", core_count);
- return core_count > 0;
- }
-
-} // namespace kapi::platform
diff --git a/kapi/include/kapi/cpu.hpp b/kapi/include/kapi/cpu.hpp
index d90365a..c643cd7 100644
--- a/kapi/include/kapi/cpu.hpp
+++ b/kapi/include/kapi/cpu.hpp
@@ -1,10 +1,13 @@
#ifndef TEACHOS_KAPI_CPU_HPP
#define TEACHOS_KAPI_CPU_HPP
+#include "kapi/devices.hpp"
#include "kapi/memory.hpp"
#include <kstd/format>
+#include <kstd/memory>
+#include <cstddef>
#include <cstdint>
namespace kapi::cpu
@@ -72,6 +75,17 @@ namespace kapi::cpu
//! @return Whether the exception was handled.
[[nodiscard]] auto dispatch(exception const & context) -> bool;
+ //! A hook that is called when the platform detects a CPU core during topology discovery.
+ //!
+ //! @param bus The bus the CPU was detected on.
+ //! @param major The major number of the CPU device.
+ //! @param minor The minor number of the CPU device.
+ //! @param hardware_id The hardware specific ID of the CPU core.
+ //! @param is_bsp Whether the reported CPU is the the bootstrap processor.
+ //! @param core_interrupt_controller The local interrupt controller of this CPU core.
+ auto core_detected(kapi::devices::bus & bus, std::size_t major, std::size_t minor, std::uint64_t hardware_id,
+ bool is_bsp, kstd::unique_ptr<devices::device> core_interrupt_controller) -> bool;
+
//! @}
//! @addtogroup kapi-cpu-platform-defined
@@ -88,6 +102,12 @@ namespace kapi::cpu
//! interrupts itself.
auto init() -> void;
+ //! Discover the CPU topology of the platform and attach it to the given CPU bus.
+ //!
+ //! @param bus The bus to attach the CPU topology to.
+ //! @return true iff. the CPU topology was discovered successfully, false otherwise.
+ auto discover_topology(kapi::devices::bus & bus) -> bool;
+
//! @}
} // namespace kapi::cpu
diff --git a/kapi/include/kapi/platform.hpp b/kapi/include/kapi/platform.hpp
deleted file mode 100644
index e1e267e..0000000
--- a/kapi/include/kapi/platform.hpp
+++ /dev/null
@@ -1,34 +0,0 @@
-#ifndef TEACHOS_KAPI_PLATFORM_HPP
-#define TEACHOS_KAPI_PLATFORM_HPP
-
-#include "kapi/devices.hpp"
-
-#include <kstd/memory>
-
-#include <cstddef>
-#include <cstdint>
-
-namespace kapi::platform
-{
-
- //! @addtogroup kapi-platform-kernel-defined
- //! @{
-
- auto cpu_detected(kapi::devices::bus & bus, std::size_t major, std::size_t minor, std::uint64_t hardware_id,
- bool is_bsp, kstd::unique_ptr<devices::device> core_interrupt_controller) -> bool;
- //! @}
-
- //! @addtogroup kapi-platform-platform-defined
- //! @{
-
- //! Discover the CPU topology of the platform and attach it to the given CPU bus.
- //!
- //! @param bus The bus to attach the CPU topology to.
- //! @return true iff. the CPU topology was discovered successfully, false otherwise.
- auto discover_cpu_topology(kapi::devices::bus & bus) -> bool;
-
- //! @}
-
-} // namespace kapi::platform
-
-#endif \ No newline at end of file
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt
index 8af6fe7..61bc6f1 100644
--- a/kernel/CMakeLists.txt
+++ b/kernel/CMakeLists.txt
@@ -12,7 +12,6 @@ add_library("kernel_objs" OBJECT
"kapi/devices/device.cpp"
"kapi/interrupts.cpp"
"kapi/memory.cpp"
- "kapi/platform.cpp"
"kapi/system.cpp"
# KSTD OS Implementation
diff --git a/kernel/kapi/cpu.cpp b/kernel/kapi/cpu.cpp
index 13de584..d632628 100644
--- a/kernel/kapi/cpu.cpp
+++ b/kernel/kapi/cpu.cpp
@@ -1,9 +1,17 @@
#include "kapi/cpu.hpp"
+#include "kapi/devices.hpp"
#include "kapi/system.hpp"
+#include "kernel/devices/cpu.hpp"
+
+#include <kstd/memory>
#include <kstd/print>
+#include <cstddef>
+#include <cstdint>
+#include <utility>
+
namespace kapi::cpu
{
@@ -32,4 +40,15 @@ namespace kapi::cpu
}
}
+ auto core_detected(kapi::devices::bus & bus, std::size_t major, std::size_t minor, std::uint64_t hardware_id,
+ bool is_bsp, kstd::unique_ptr<devices::device> core_interrupt_controller) -> bool
+ {
+ auto core = kstd::make_unique<kernel::devices::cpu::core>(major, minor, hardware_id, is_bsp);
+
+ core->add_child(std::move(core_interrupt_controller));
+ bus.add_child(std::move(core));
+
+ return true;
+ }
+
} // namespace kapi::cpu \ No newline at end of file
diff --git a/kernel/kapi/platform.cpp b/kernel/kapi/platform.cpp
deleted file mode 100644
index 7638cf9..0000000
--- a/kernel/kapi/platform.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-#include "kapi/platform.hpp"
-
-#include "kapi/devices.hpp"
-
-#include "kernel/devices/cpu.hpp"
-
-#include <kstd/memory>
-
-#include <cstddef>
-#include <cstdint>
-#include <utility>
-
-namespace kapi::platform
-{
-
- auto cpu_detected(kapi::devices::bus & bus, std::size_t major, std::size_t minor, std::uint64_t hardware_id,
- bool is_bsp, kstd::unique_ptr<devices::device> core_interrupt_controller) -> bool
- {
- auto core = kstd::make_unique<kernel::devices::cpu::core>(major, minor, hardware_id, is_bsp);
-
- core->add_child(std::move(core_interrupt_controller));
- bus.add_child(std::move(core));
-
- return true;
- }
-
-} // namespace kapi::platform \ No newline at end of file
diff --git a/kernel/src/devices/cpu.cpp b/kernel/src/devices/cpu.cpp
index eb10d74..85f4d47 100644
--- a/kernel/src/devices/cpu.cpp
+++ b/kernel/src/devices/cpu.cpp
@@ -1,7 +1,7 @@
#include "kernel/devices/cpu.hpp"
+#include "kapi/cpu.hpp"
#include "kapi/devices.hpp"
-#include "kapi/platform.hpp"
#include <kstd/print>
@@ -33,7 +33,7 @@ namespace kernel::devices
auto cpu::probe() -> bool
{
- if (!kapi::platform::discover_cpu_topology(*this))
+ if (!kapi::cpu::discover_topology(*this))
{
kstd::println("[OS:DEV] Failed to discover CPU topology");
return false;