From a8366afaf39321ae2bbae70740f5ca65bee06769 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 17 Mar 2024 19:54:32 +0100 Subject: unrelated: Rework the ACPI interface. Signed-off-by: Amlal El Mahrouss --- Private/Builtins/ACPI/ACPIFactoryInterface.hxx | 52 +++++++++++++++ Private/Builtins/ACPI/ACPIManager.hxx | 44 ------------- Private/Builtins/README.TXT | 2 +- Private/HALKit/AMD64/CPUID.hxx | 2 +- Private/HALKit/AMD64/HalACPIFactoryInterface.cxx | 74 ++++++++++++++++++++++ Private/HALKit/AMD64/HalACPIManager.cpp | 74 ---------------------- .../HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp | 19 +++--- 7 files changed, 138 insertions(+), 129 deletions(-) create mode 100644 Private/Builtins/ACPI/ACPIFactoryInterface.hxx delete mode 100644 Private/Builtins/ACPI/ACPIManager.hxx create mode 100644 Private/HALKit/AMD64/HalACPIFactoryInterface.cxx delete mode 100644 Private/HALKit/AMD64/HalACPIManager.cpp diff --git a/Private/Builtins/ACPI/ACPIFactoryInterface.hxx b/Private/Builtins/ACPI/ACPIFactoryInterface.hxx new file mode 100644 index 00000000..21cd026e --- /dev/null +++ b/Private/Builtins/ACPI/ACPIFactoryInterface.hxx @@ -0,0 +1,52 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#ifndef __ACPI_MANAGER__ +#define __ACPI_MANAGER__ + +#include +#include +#include +#include + +namespace HCore { +class ACPIFactoryInterface final { + public: + explicit ACPIFactoryInterface(voidPtr rsdPtr); + ~ACPIFactoryInterface() = default; + + ACPIFactoryInterface &operator=(const ACPIFactoryInterface &) = default; + ACPIFactoryInterface(const ACPIFactoryInterface &) = default; + + public: + void Shutdown(); // shutdown + void Reboot(); // soft-reboot + + public: + /// @brief Descriptor find factory. + /// @param signature The signature of the descriptor table (MADT, ACPI...) + /// @return the blob inside an ErrorOr object. + ErrorOr Find(const char *signature); + + /// @brief Checksum factory. + /// @param checksum the data to checksum + /// @param len it's size + /// @return if it succeed + bool Checksum(const char *checksum, SSizeT len); // watch for collides! + + public: + ErrorOr operator[](const char *signature) { + return this->Find(signature); + } + + private: + VoidPtr m_Rsdp; // pointer to root descriptor. + SSizeT m_Entries; // number of entries, -1 tells that no invalid entries were + // found. +}; +} // namespace HCore + +#endif // !__ACPI_MANAGER__ diff --git a/Private/Builtins/ACPI/ACPIManager.hxx b/Private/Builtins/ACPI/ACPIManager.hxx deleted file mode 100644 index 1fa5e714..00000000 --- a/Private/Builtins/ACPI/ACPIManager.hxx +++ /dev/null @@ -1,44 +0,0 @@ -/* ------------------------------------------- - - Copyright Mahrouss Logic - -------------------------------------------- */ - -#ifndef __ACPI_MANAGER__ -#define __ACPI_MANAGER__ - -#include -#include -#include -#include - -namespace HCore { -class ACPIManager final { - public: - explicit ACPIManager(voidPtr rsdPtr); - ~ACPIManager() = default; - - ACPIManager &operator=(const ACPIManager &) = default; - ACPIManager(const ACPIManager &) = default; - - public: - void Shutdown(); // shutdown - void Reset(); // soft-reboot - - ErrorOr Find(const char *signature); - - bool Checksum(const char *checksum, SSizeT len); // watch for collides! - - public: - ErrorOr operator[](const char *signature) { - return this->Find(signature); - } - - private: - VoidPtr m_Rsdp; // pointer to root descriptor. - SSizeT m_Entries; // number of entries, -1 tells that no invalid entries were - // found. -}; -} // namespace HCore - -#endif // !__ACPI_MANAGER__ diff --git a/Private/Builtins/README.TXT b/Private/Builtins/README.TXT index ce39f472..2e341e5d 100644 --- a/Private/Builtins/README.TXT +++ b/Private/Builtins/README.TXT @@ -12,5 +12,5 @@ These are HCore builtins device drivers. Maintainers =========== -ACPIManager: Amlal EL Mahrouss +ACPIFactoryInterface: Amlal EL Mahrouss AHCI: Amlal EL Mahrouss \ No newline at end of file diff --git a/Private/HALKit/AMD64/CPUID.hxx b/Private/HALKit/AMD64/CPUID.hxx index 2eac5bcb..381e07d4 100644 --- a/Private/HALKit/AMD64/CPUID.hxx +++ b/Private/HALKit/AMD64/CPUID.hxx @@ -2,7 +2,7 @@ Copyright Mahrouss Logic - File: CPUID.hpp + File: CPUID.hxx Purpose: CPUID flags. Revision History: diff --git a/Private/HALKit/AMD64/HalACPIFactoryInterface.cxx b/Private/HALKit/AMD64/HalACPIFactoryInterface.cxx new file mode 100644 index 00000000..f3e120b6 --- /dev/null +++ b/Private/HALKit/AMD64/HalACPIFactoryInterface.cxx @@ -0,0 +1,74 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#include +#include +#include + +namespace HCore { +ACPIFactoryInterface::ACPIFactoryInterface(voidPtr rsdPtr) : m_Rsdp(rsdPtr), m_Entries(0) { + volatile RSDP *_rsdPtr = reinterpret_cast(this->m_Rsdp); + + MUST_PASS(_rsdPtr); + MUST_PASS(_rsdPtr->Revision >= 2); +} + +void ACPIFactoryInterface::Shutdown() {} +void ACPIFactoryInterface::Reboot() {} + +/// @brief Finds a descriptor table inside ACPI XSDT. +ErrorOr ACPIFactoryInterface::Find(const char *signature) { + MUST_PASS(m_Rsdp); + + if (!signature) return ErrorOr{-2}; + + if (*signature == 0) return ErrorOr{-3}; + + RSDP *rsdPtr = reinterpret_cast(this->m_Rsdp); + + auto xsdt = rsdPtr->XsdtAddress; + SizeT num = (rsdPtr->Length + sizeof(SDT)) / 8; + + for (Size index = 0; index < num; ++index) { + SDT *sdt = reinterpret_cast(xsdt + sizeof(SDT) + index * 8); + + if (!Checksum(sdt->Signature, 4)) ke_stop(RUNTIME_CHECK_ACPI); + + if (StringBuilder::Equals(const_cast(sdt->Signature), + signature)) + return ErrorOr(reinterpret_cast(sdt)); + } + + return ErrorOr{-1}; +} + +/*** + @brief check SDT header + @param checksum the header to checksum + @param len the length of it. +*/ +bool ACPIFactoryInterface::Checksum(const char *checksum, SSizeT len) { + if (len == 0) return -1; + + char chr = 0; + + for (int index = 0; index < len; ++index) { + chr += checksum[index]; + } + + return chr == 0; +} + +/// Custom to the virtual machine, you'll need to parse the MADT instead. + +void rt_shutdown_acpi_qemu_20(void) { HAL::Out16(0xb004, 0x2000); } + +void rt_shutdown_acpi_qemu_30_plus(void) { HAL::Out16(0x604, 0x2000); } + +void rt_shutdown_acpi_virtualbox(void) { HAL::Out16(0x4004, 0x3400); } + +/// You have to parse the MADT! +} // namespace HCore diff --git a/Private/HALKit/AMD64/HalACPIManager.cpp b/Private/HALKit/AMD64/HalACPIManager.cpp deleted file mode 100644 index b73dd996..00000000 --- a/Private/HALKit/AMD64/HalACPIManager.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* ------------------------------------------- - - Copyright Mahrouss Logic - -------------------------------------------- */ - -#include -#include -#include - -namespace HCore { -ACPIManager::ACPIManager(voidPtr rsdPtr) : m_Rsdp(rsdPtr), m_Entries(0) { - volatile RSDP *_rsdPtr = reinterpret_cast(this->m_Rsdp); - - MUST_PASS(_rsdPtr); - MUST_PASS(_rsdPtr->Revision >= 2); -} - -void ACPIManager::Shutdown() {} -void ACPIManager::Reset() {} - -/// @brief Finds a descriptor table inside ACPI XSDT. -ErrorOr ACPIManager::Find(const char *signature) { - MUST_PASS(m_Rsdp); - - if (!signature) return ErrorOr{-2}; - - if (*signature == 0) return ErrorOr{-3}; - - RSDP *rsdPtr = reinterpret_cast(this->m_Rsdp); - - auto xsdt = rsdPtr->XsdtAddress; - SizeT num = (rsdPtr->Length + sizeof(SDT)) / 8; - - for (Size index = 0; index < num; ++index) { - SDT *sdt = reinterpret_cast(xsdt + sizeof(SDT) + index * 8); - - if (!Checksum(sdt->Signature, 4)) ke_stop(RUNTIME_CHECK_ACPI); - - if (StringBuilder::Equals(const_cast(sdt->Signature), - signature)) - return ErrorOr(reinterpret_cast(sdt)); - } - - return ErrorOr{-1}; -} - -/*** - @brief check SDT header - @param checksum the header to checksum - @param len the length of it. -*/ -bool ACPIManager::Checksum(const char *checksum, SSizeT len) { - if (len == 0) return -1; - - char chr = 0; - - for (int index = 0; index < len; ++index) { - chr += checksum[index]; - } - - return chr == 0; -} - -/// Custom to the virtual machine, you'll need to parse the MADT instead. - -void rt_shutdown_acpi_qemu_20(void) { HAL::Out16(0xb004, 0x2000); } - -void rt_shutdown_acpi_qemu_30_plus(void) { HAL::Out16(0x604, 0x2000); } - -void rt_shutdown_acpi_virtualbox(void) { HAL::Out16(0x4004, 0x3400); } - -/// You have to parse the MADT! -} // namespace HCore diff --git a/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp b/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp index edb821b9..d9161d17 100644 --- a/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp +++ b/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include +#include #include /////////////////////////////////////////////////////////////////////////////////////// @@ -39,10 +39,11 @@ struct ProcessorInfoAMD64 final { } Selector; }; -static voidPtr kApicMadt = nullptr; -static const char* kApicSignature = "APIC"; +STATIC voidPtr kApicMadt = nullptr; +STATIC const char* kApicSignature = "APIC"; -struct Madt final { +/// @brief Multiple APIC descriptor table. +struct MadtType final { char fMag[4]; Int32 fLength; char fRev; @@ -90,10 +91,10 @@ struct MadtLocalApicAddressOverride final { /////////////////////////////////////////////////////////////////////////////////////// -static Madt kApicMadtList[256]; +STATIC MadtType kApicMadtList[256]; -Madt* system_find_core(Madt* madt) { - madt = madt + sizeof(Madt); +MadtType* system_find_core(MadtType* madt) { + madt = madt + sizeof(MadtType); if (rt_string_cmp(madt->fMag, kApicSignature, rt_string_len(kApicSignature)) == 0) @@ -105,13 +106,13 @@ Madt* system_find_core(Madt* madt) { /////////////////////////////////////////////////////////////////////////////////////// void hal_system_get_cores(voidPtr rsdPtr) { - auto acpi = ACPIManager(rsdPtr); + auto acpi = ACPIFactoryInterface(rsdPtr); kApicMadt = acpi.Find(kApicSignature).Leak().Leak(); MUST_PASS(kApicMadt); // MADT must exist. SizeT counter = 0UL; - Madt* offset = system_find_core((Madt*)kApicMadt); + MadtType* offset = system_find_core((MadtType*)kApicMadt); //! now find core addresses. while (offset != nullptr) { // calls rt_copy_memory in NewC++ -- cgit v1.2.3