aboutsummaryrefslogtreecommitdiff
path: root/kernel/src
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/src')
-rw-r--r--kernel/src/acpi/manager.cpp103
-rw-r--r--kernel/src/devices/cpu.cpp45
-rw-r--r--kernel/src/main.cpp10
3 files changed, 158 insertions, 0 deletions
diff --git a/kernel/src/acpi/manager.cpp b/kernel/src/acpi/manager.cpp
new file mode 100644
index 0000000..300b85e
--- /dev/null
+++ b/kernel/src/acpi/manager.cpp
@@ -0,0 +1,103 @@
+#include "kernel/acpi/manager.hpp"
+
+#include "kapi/acpi.hpp"
+#include "kapi/memory.hpp"
+#include "kapi/system.hpp"
+
+#include <kstd/memory>
+#include <kstd/print>
+
+#include <cstddef>
+#include <cstdint>
+#include <string_view>
+
+namespace kernel::acpi
+{
+
+ manager::manager(kapi::acpi::root_system_description_pointer const & sdp)
+ : m_sdp{&sdp}
+ {
+ if (m_sdp->signature() != "RSD PTR ")
+ {
+ kapi::system::panic("[OS:ACPI] Invalid RSDP signature!");
+ }
+
+ if (m_sdp->revision() >= 2)
+ {
+ auto const xsdp = static_cast<kapi::acpi::extended_system_description_pointer const *>(m_sdp);
+ if (!xsdp->validate())
+ {
+ kapi::system::panic("[OS:ACPI] Invalid XSDP signature!");
+ }
+ auto physical_extended_table_address = xsdp->table_address();
+ auto linear_extended_table_address = kapi::memory::hhdm_to_linear(physical_extended_table_address);
+ m_rsdt = static_cast<kapi::acpi::system_description_table_header const *>(linear_extended_table_address);
+ m_extended = true;
+ }
+ else
+ {
+ if (!m_sdp->validate())
+ {
+ kapi::system::panic("[OS:ACPI] Invalid RSDP checksum!");
+ }
+ auto physical_root_table_address = m_sdp->table_address();
+ auto linear_root_table_address = kapi::memory::hhdm_to_linear(physical_root_table_address);
+ m_rsdt = static_cast<kapi::acpi::system_description_table_header const *>(linear_root_table_address);
+ }
+ }
+
+ auto manager::load_tables() -> bool
+ {
+ if (!kapi::acpi::validate_checksum({reinterpret_cast<std::byte const *>(m_rsdt), m_rsdt->length().value}))
+ {
+ kapi::system::panic("[OS:ACPI] Invalid RSDT checksum!");
+ }
+
+ auto entry_size = m_extended ? sizeof(std::uint64_t) : sizeof(std::uint32_t);
+ auto entry_count = (m_rsdt->length().value - sizeof(kapi::acpi::system_description_table_header)) / entry_size;
+ auto entries_base =
+ reinterpret_cast<std::byte const *>(m_rsdt) + sizeof(kapi::acpi::system_description_table_header);
+
+ for (std::size_t i = 0; i < entry_count; ++i)
+ {
+ auto physical_table_address = kapi::memory::physical_address{};
+
+ if (m_extended)
+ {
+ auto entry = reinterpret_cast<std::uint64_t const *>(entries_base + (i * entry_size));
+ physical_table_address = kapi::memory::physical_address{*entry};
+ }
+ else
+ {
+ auto entry = reinterpret_cast<std::uint32_t const *>(entries_base + (i * entry_size));
+ physical_table_address = kapi::memory::physical_address{*entry};
+ }
+
+ auto linear_table_address = kapi::memory::hhdm_to_linear(physical_table_address);
+ auto table = static_cast<kapi::acpi::system_description_table_header const *>(linear_table_address);
+
+ if (!kapi::acpi::validate_checksum({reinterpret_cast<std::byte const *>(table), table->length().value}))
+ {
+ kstd::println(kstd::print_sink::stderr, "[OS:ACPI] Invalid table checksum!");
+ }
+ else
+ {
+ kstd::println("[OS:ACPI] Found '{}' ACPI table", table->signature());
+ m_tables.emplace(table->signature(), table);
+ }
+ }
+
+ return !m_tables.empty();
+ }
+
+ auto manager::get_table(std::string_view signature)
+ -> kstd::observer_ptr<kapi::acpi::system_description_table_header const>
+ {
+ if (m_tables.contains(signature))
+ {
+ return kstd::make_observer(m_tables.at(signature));
+ }
+ return nullptr;
+ }
+
+} // namespace kernel::acpi
diff --git a/kernel/src/devices/cpu.cpp b/kernel/src/devices/cpu.cpp
new file mode 100644
index 0000000..eb10d74
--- /dev/null
+++ b/kernel/src/devices/cpu.cpp
@@ -0,0 +1,45 @@
+#include "kernel/devices/cpu.hpp"
+
+#include "kapi/devices.hpp"
+#include "kapi/platform.hpp"
+
+#include <kstd/print>
+
+#include <cstddef>
+#include <cstdint>
+
+namespace kernel::devices
+{
+
+ cpu::core::core(std::size_t major_number, std::size_t minor_number, std::uint64_t hardware_id, bool is_bsp)
+ : kapi::devices::bus{major_number, minor_number, "cpu_core"}
+ , m_hardware_id{hardware_id}
+ , m_is_bsp{is_bsp}
+ {}
+
+ auto cpu::core::hardware_id() const -> std::uint64_t
+ {
+ return m_hardware_id;
+ }
+
+ auto cpu::core::is_bsp() const -> bool
+ {
+ return m_is_bsp;
+ }
+
+ cpu::cpu(std::size_t major_number)
+ : kapi::devices::bus{major_number, 0, "cpu"}
+ {}
+
+ auto cpu::probe() -> bool
+ {
+ if (!kapi::platform::discover_cpu_topology(*this))
+ {
+ kstd::println("[OS:DEV] Failed to discover CPU topology");
+ return false;
+ }
+
+ return true;
+ }
+
+} // namespace kernel::devices \ No newline at end of file
diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp
index 2eaa2d8..e704955 100644
--- a/kernel/src/main.cpp
+++ b/kernel/src/main.cpp
@@ -1,3 +1,4 @@
+#include "kapi/acpi.hpp"
#include "kapi/boot_modules.hpp"
#include "kapi/cio.hpp"
#include "kapi/cpu.hpp"
@@ -179,6 +180,15 @@ auto main() -> int
kstd::println("[OS] Memory subsystem initialized.");
kapi::system::memory_initialized();
+ auto acpi_root_pointer = kapi::acpi::get_root_pointer();
+ if (acpi_root_pointer && acpi_root_pointer->validate())
+ {
+ if (kapi::acpi::init(*acpi_root_pointer))
+ {
+ kstd::println("[OS] ACPI subsystem initialized.");
+ }
+ }
+
kapi::devices::init();
kstd::println("[OS] System root bus initialized.");