From 2b5f722401dc47c9066b026d5f2c83f9096da9b0 Mon Sep 17 00:00:00 2001 From: Epiczhul <76625941+Epiczhul@users.noreply.github.com> Date: Sat, 25 May 2024 18:10:53 +0200 Subject: [PATCH 1/6] Update ACPI.cs --- source/Cosmos.Core/ACPI.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/source/Cosmos.Core/ACPI.cs b/source/Cosmos.Core/ACPI.cs index 20e4ef9fd8..12d718570e 100644 --- a/source/Cosmos.Core/ACPI.cs +++ b/source/Cosmos.Core/ACPI.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Runtime.InteropServices; namespace Cosmos.Core @@ -480,5 +480,21 @@ private static byte facpbget(int number) return null; } } + + /// + /// Find the RSDT (Root System Description Table). + /// + /// Pointer to the RSDT if found, null otherwise. + public static uint* FindRSDT() + { + uint rsdpAddress = RSDPAddress(); + if (rsdpAddress == 0) + { + return null; + } + + RSDPtr* rsdp = (RSDPtr*)rsdpAddress; + return (uint*)rsdp->RsdtAddress; + } } -} \ No newline at end of file +} From 9d10100cfdbc7c99416ac4f8c446c0d475308866 Mon Sep 17 00:00:00 2001 From: Epiczhul <76625941+Epiczhul@users.noreply.github.com> Date: Sat, 25 May 2024 18:17:10 +0200 Subject: [PATCH 2/6] Update ACPI.cs --- source/Cosmos.Core/ACPI.cs | 209 ++++++------------------------------- 1 file changed, 30 insertions(+), 179 deletions(-) diff --git a/source/Cosmos.Core/ACPI.cs b/source/Cosmos.Core/ACPI.cs index 12d718570e..c4fc8814b9 100644 --- a/source/Cosmos.Core/ACPI.cs +++ b/source/Cosmos.Core/ACPI.cs @@ -12,156 +12,67 @@ public unsafe class ACPI /// RSD table struct. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] - public unsafe struct RSDPtr + public struct RSDPtr { - /// - /// Signature. - /// public fixed byte Signature[8]; - /// - /// CheckSum - /// public byte CheckSum; - /// - /// OemID - /// public fixed byte OemID[6]; - /// - /// Revision - /// public byte Revision; - /// - /// RSDT Address - /// - public int RsdtAddress; + public uint RsdtAddress; }; - // New Port I/O /// - /// IO port. + /// RSDT table struct. /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct RSDT + { + public fixed byte Signature[4]; + public uint Length; + public byte Revision; + public byte Checksum; + public fixed byte OemID[6]; + public fixed byte OemTableID[8]; + public uint OemRevision; + public uint CreatorID; + public uint CreatorRevision; + public fixed uint Entry[1]; // Placeholder for the start of the array of pointers to other tables + } + + // New Port I/O private static ushort smiIO, pm1aIO, pm1bIO; // ACPI variables - /// - /// SMI CMD. - /// private static int* SMI_CMD; - /// - /// ACPI ENABLE. - /// private static byte ACPI_ENABLE; - /// - /// ACPI DISABLE. - /// private static byte ACPI_DISABLE; - /// - /// PM1a CNT - /// private static int* PM1a_CNT; - /// - /// PM1b CNT - /// private static int* PM1b_CNT; - /// - /// SLP TYPa - /// private static short SLP_TYPa; - /// - /// SLP TYPb - /// private static short SLP_TYPb; - /// - /// SLP EN. - /// private static short SLP_EN; - /// - /// PM1 CNT LEN1 - /// private static byte PM1_CNT_LEN; - /// - /// Check ACPI header. - /// - /// - /// - /// - static int acpiCheckHeader(byte* ptr, string sig) - { - return Compare(sig, ptr); - } - - /// - /// FACP. - /// private static byte* Facp = null; - /// - /// FACP struct. - /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] struct FACP { - /// - /// Signature. - /// public fixed byte Signature[4]; - /// - /// Length. - /// public int Length; - - /// - /// Unused. - /// public fixed byte unneded1[40 - 8]; - /// - /// DSDT. - /// public int* DSDT; - /// - /// Unused. - /// public fixed byte unneded2[48 - 44]; - /// - /// SMI CMD. - /// public int* SMI_CMD; - /// - /// ACPI ENABLE. - /// public byte ACPI_ENABLE; - /// - /// ACPI DISABLE. - /// public byte ACPI_DISABLE; - /// - /// Unused. - /// public fixed byte unneded3[64 - 54]; - /// - /// PM1a CNT BLK. - /// public int* PM1a_CNT_BLK; - /// - /// PM1b CNT BLK. - /// public int* PM1b_CNT_BLK; - /// - /// Unused. - /// public fixed byte unneded4[89 - 72]; - /// - /// PM1 CNT LEN. - /// public byte PM1_CNT_LEN; }; - /// - /// Compare string to byte array. - /// - /// String. - /// Pointer to the head of the byte array. - /// 0 - identical, -1 different. static int Compare(string c1, byte* c2) { for (int i = 0; i < c1.Length; i++) @@ -171,11 +82,6 @@ static int Compare(string c1, byte* c2) return 0; } - /// - /// Check RSD checksum. - /// - /// Address to check. - /// True if RSDT table checksum is good. static bool Check_RSD(uint address) { byte sum = 0; @@ -189,11 +95,6 @@ static bool Check_RSD(uint address) return sum == 0; } - /// - /// Start the ACPI. - /// - /// Initialize the ACPI. (default = true) - /// Enable the ACPI. (default = true) public static void Start(bool initialize = true, bool enable = true) { if (initialize) @@ -207,10 +108,6 @@ public static void Start(bool initialize = true, bool enable = true) } } - /// - /// Shutdown the ACPI. - /// - /// Thrown on IO error. public static void Shutdown() { Console.Clear(); @@ -229,20 +126,11 @@ public static void Shutdown() CPU.Halt(); } - /// - /// Reboot ACPI. - /// Not implemented. - /// - /// Thrown always. public static void Reboot() { - throw new NotImplementedException("ACPI Reset not implemented yet."); //TODO + throw new NotImplementedException("ACPI Reset not implemented yet."); } - /// - /// Initialize the ACPI. - /// - /// true on success, false on failure. private static bool Init() { byte* ptr = (byte*)RSDPAddress(); @@ -339,26 +227,16 @@ private static bool Init() return false; } - /// - /// Enable ACPI. - /// public static void Enable() { smiIO = ACPI_ENABLE; } - /// - /// Disable ACPI. - /// public static void Disable() { smiIO = ACPI_DISABLE; } - /// - /// Get the RSDP address. - /// - /// uint value. private static unsafe uint RSDPAddress() { for (uint addr = 0xE0000; addr < 0x100000; addr += 4) @@ -386,11 +264,6 @@ private static unsafe uint RSDPAddress() return 0; } - /// - /// Check RSDT table - /// - /// A pointer to the RSDT - /// RSDT table address private static uint* acpiCheckRSDPtr(uint* ptr) { string sig = "RSD PTR "; @@ -424,18 +297,6 @@ private static unsafe uint RSDPAddress() return null; } - /// - /// Get data from the FACP table. - /// - /// Index number of the data to get. - /// - /// 0 - ACPI ENABLE - /// 1 - ACPI DISABLE - /// 2 - PM1 CNT LEN - /// other - 0 - /// - /// - /// byte value. private static byte facpbget(int number) { switch (number) @@ -451,19 +312,6 @@ private static byte facpbget(int number) } } - /// - /// Get pointer to the data on the FACP. - /// - /// Index number of the data to get. - /// - /// 0 - DSDT - /// 1 - SMI CMD - /// 2 - PM1a - /// 3 - PM1b - /// other - null - /// - /// - /// int pointer. private static int* facpget(int number) { switch (number) @@ -481,11 +329,7 @@ private static byte facpbget(int number) } } - /// - /// Find the RSDT (Root System Description Table). - /// - /// Pointer to the RSDT if found, null otherwise. - public static uint* FindRSDT() + public static RSDT* FindRSDT() { uint rsdpAddress = RSDPAddress(); if (rsdpAddress == 0) @@ -494,7 +338,14 @@ private static byte facpbget(int number) } RSDPtr* rsdp = (RSDPtr*)rsdpAddress; - return (uint*)rsdp->RsdtAddress; + uint rsdtAddress = rsdp->RsdtAddress; + + if (rsdtAddress == 0) + { + return null; + } + + return (RSDT*)rsdtAddress; } } } From 6980b3ba91713eebea31f7ac2ed0eb66e274531b Mon Sep 17 00:00:00 2001 From: Epiczhul <76625941+Epiczhul@users.noreply.github.com> Date: Sat, 25 May 2024 18:24:02 +0200 Subject: [PATCH 3/6] Update ACPI.cs --- source/Cosmos.Core/ACPI.cs | 222 ++++++++++++++++++++++++++++++++----- 1 file changed, 193 insertions(+), 29 deletions(-) diff --git a/source/Cosmos.Core/ACPI.cs b/source/Cosmos.Core/ACPI.cs index c4fc8814b9..d08c03a7e2 100644 --- a/source/Cosmos.Core/ACPI.cs +++ b/source/Cosmos.Core/ACPI.cs @@ -12,67 +12,156 @@ public unsafe class ACPI /// RSD table struct. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct RSDPtr + public unsafe struct RSDPtr { + /// + /// Signature. + /// public fixed byte Signature[8]; + /// + /// CheckSum + /// public byte CheckSum; + /// + /// OemID + /// public fixed byte OemID[6]; + /// + /// Revision + /// public byte Revision; - public uint RsdtAddress; + /// + /// RSDT Address + /// + public int RsdtAddress; }; + // New Port I/O /// - /// RSDT table struct. + /// IO port. /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct RSDT - { - public fixed byte Signature[4]; - public uint Length; - public byte Revision; - public byte Checksum; - public fixed byte OemID[6]; - public fixed byte OemTableID[8]; - public uint OemRevision; - public uint CreatorID; - public uint CreatorRevision; - public fixed uint Entry[1]; // Placeholder for the start of the array of pointers to other tables - } - - // New Port I/O private static ushort smiIO, pm1aIO, pm1bIO; // ACPI variables + /// + /// SMI CMD. + /// private static int* SMI_CMD; + /// + /// ACPI ENABLE. + /// private static byte ACPI_ENABLE; + /// + /// ACPI DISABLE. + /// private static byte ACPI_DISABLE; + /// + /// PM1a CNT + /// private static int* PM1a_CNT; + /// + /// PM1b CNT + /// private static int* PM1b_CNT; + /// + /// SLP TYPa + /// private static short SLP_TYPa; + /// + /// SLP TYPb + /// private static short SLP_TYPb; + /// + /// SLP EN. + /// private static short SLP_EN; + /// + /// PM1 CNT LEN1 + /// private static byte PM1_CNT_LEN; - private static byte* Facp = null; + /// + /// Check ACPI header. + /// + /// + /// + /// + static int acpiCheckHeader(byte* ptr, string sig) + { + return Compare(sig, ptr); + } + /// + /// FACP. + /// + private static byte* Facp = null; + /// + /// FACP struct. + /// [StructLayout(LayoutKind.Sequential, Pack = 1)] struct FACP { + /// + /// Signature. + /// public fixed byte Signature[4]; + /// + /// Length. + /// public int Length; + + /// + /// Unused. + /// public fixed byte unneded1[40 - 8]; + /// + /// DSDT. + /// public int* DSDT; + /// + /// Unused. + /// public fixed byte unneded2[48 - 44]; + /// + /// SMI CMD. + /// public int* SMI_CMD; + /// + /// ACPI ENABLE. + /// public byte ACPI_ENABLE; + /// + /// ACPI DISABLE. + /// public byte ACPI_DISABLE; + /// + /// Unused. + /// public fixed byte unneded3[64 - 54]; + /// + /// PM1a CNT BLK. + /// public int* PM1a_CNT_BLK; + /// + /// PM1b CNT BLK. + /// public int* PM1b_CNT_BLK; + /// + /// Unused. + /// public fixed byte unneded4[89 - 72]; + /// + /// PM1 CNT LEN. + /// public byte PM1_CNT_LEN; }; + /// + /// Compare string to byte array. + /// + /// String. + /// Pointer to the head of the byte array. + /// 0 - identical, -1 different. static int Compare(string c1, byte* c2) { for (int i = 0; i < c1.Length; i++) @@ -82,6 +171,11 @@ static int Compare(string c1, byte* c2) return 0; } + /// + /// Check RSD checksum. + /// + /// Address to check. + /// True if RSDT table checksum is good. static bool Check_RSD(uint address) { byte sum = 0; @@ -95,6 +189,11 @@ static bool Check_RSD(uint address) return sum == 0; } + /// + /// Start the ACPI. + /// + /// Initialize the ACPI. (default = true) + /// Enable the ACPI. (default = true) public static void Start(bool initialize = true, bool enable = true) { if (initialize) @@ -108,6 +207,10 @@ public static void Start(bool initialize = true, bool enable = true) } } + /// + /// Shutdown the ACPI. + /// + /// Thrown on IO error. public static void Shutdown() { Console.Clear(); @@ -126,11 +229,20 @@ public static void Shutdown() CPU.Halt(); } + /// + /// Reboot ACPI. + /// Not implemented. + /// + /// Thrown always. public static void Reboot() { - throw new NotImplementedException("ACPI Reset not implemented yet."); + throw new NotImplementedException("ACPI Reset not implemented yet."); //TODO } + /// + /// Initialize the ACPI. + /// + /// true on success, false on failure. private static bool Init() { byte* ptr = (byte*)RSDPAddress(); @@ -227,16 +339,26 @@ private static bool Init() return false; } + /// + /// Enable ACPI. + /// public static void Enable() { smiIO = ACPI_ENABLE; } + /// + /// Disable ACPI. + /// public static void Disable() { smiIO = ACPI_DISABLE; } + /// + /// Get the RSDP address. + /// + /// uint value. private static unsafe uint RSDPAddress() { for (uint addr = 0xE0000; addr < 0x100000; addr += 4) @@ -264,6 +386,11 @@ private static unsafe uint RSDPAddress() return 0; } + /// + /// Check RSDT table + /// + /// A pointer to the RSDT + /// RSDT table address private static uint* acpiCheckRSDPtr(uint* ptr) { string sig = "RSD PTR "; @@ -297,6 +424,18 @@ private static unsafe uint RSDPAddress() return null; } + /// + /// Get data from the FACP table. + /// + /// Index number of the data to get. + /// + /// 0 - ACPI ENABLE + /// 1 - ACPI DISABLE + /// 2 - PM1 CNT LEN + /// other - 0 + /// + /// + /// byte value. private static byte facpbget(int number) { switch (number) @@ -312,6 +451,19 @@ private static byte facpbget(int number) } } + /// + /// Get pointer to the data on the FACP. + /// + /// Index number of the data to get. + /// + /// 0 - DSDT + /// 1 - SMI CMD + /// 2 - PM1a + /// 3 - PM1b + /// other - null + /// + /// + /// int pointer. private static int* facpget(int number) { switch (number) @@ -329,23 +481,35 @@ private static byte facpbget(int number) } } - public static RSDT* FindRSDT() + /// + /// Get the address of the RSDT. + /// + /// Address of the RSDT or 0 if not found. + public static uint GetRSDTAddress() { - uint rsdpAddress = RSDPAddress(); - if (rsdpAddress == 0) + uint rsdpAddr = RSDPAddress(); + if (rsdpAddr == 0) { - return null; + return 0; } - RSDPtr* rsdp = (RSDPtr*)rsdpAddress; - uint rsdtAddress = rsdp->RsdtAddress; + var rsdp = (RSDPtr*)rsdpAddr; + return (uint)rsdp->RsdtAddress; + } - if (rsdtAddress == 0) + /// + /// Get the contents of the RSDT. + /// + /// Pointer to the RSDT contents or null if not found. + public static byte* GetRSDTContents() + { + uint rsdtAddr = GetRSDTAddress(); + if (rsdtAddr == 0) { return null; } - return (RSDT*)rsdtAddress; + return (byte*)rsdtAddr; } } } From 1d9f6cef8f804f4c198ca812f2e51d1b7ed18188 Mon Sep 17 00:00:00 2001 From: Epiczhul <76625941+Epiczhul@users.noreply.github.com> Date: Sat, 25 May 2024 18:40:52 +0200 Subject: [PATCH 4/6] Update ACPI.cs --- source/Cosmos.Core/ACPI.cs | 240 ++++++++++++++++++------------------- 1 file changed, 119 insertions(+), 121 deletions(-) diff --git a/source/Cosmos.Core/ACPI.cs b/source/Cosmos.Core/ACPI.cs index d08c03a7e2..9bc87ad5f8 100644 --- a/source/Cosmos.Core/ACPI.cs +++ b/source/Cosmos.Core/ACPI.cs @@ -36,52 +36,62 @@ public unsafe struct RSDPtr public int RsdtAddress; }; - // New Port I/O + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct DescriptionHeader + { + public fixed byte Signature[4]; + public uint Length; + public byte Revision; + public byte Checksum; + public fixed byte OEMID[6]; + public fixed byte OEMTableID[8]; + public uint OEMRevision; + public uint CreatorID; + public uint CreatorRevision; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct APICHeader + { + public byte Type; + public byte Length; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct LocalAPIC + { + public byte Type; + public byte Length; + public byte ProcessorID; + public byte APICID; + public uint Flags; + } + /// - /// IO port. + /// New Port I/O /// private static ushort smiIO, pm1aIO, pm1bIO; - // ACPI variables /// - /// SMI CMD. + /// ACPI variables /// private static int* SMI_CMD; - /// - /// ACPI ENABLE. - /// private static byte ACPI_ENABLE; - /// - /// ACPI DISABLE. - /// private static byte ACPI_DISABLE; - /// - /// PM1a CNT - /// private static int* PM1a_CNT; - /// - /// PM1b CNT - /// private static int* PM1b_CNT; - /// - /// SLP TYPa - /// private static short SLP_TYPa; - /// - /// SLP TYPb - /// private static short SLP_TYPb; - /// - /// SLP EN. - /// private static short SLP_EN; + private static byte PM1_CNT_LEN; + /// - /// PM1 CNT LEN1 + /// FACP /// - private static byte PM1_CNT_LEN; + private static byte* Facp = null; /// - /// Check ACPI header. + /// Check ACPI header /// /// /// @@ -92,76 +102,11 @@ static int acpiCheckHeader(byte* ptr, string sig) } /// - /// FACP. - /// - private static byte* Facp = null; - /// - /// FACP struct. + /// Compare string to byte array /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - struct FACP - { - /// - /// Signature. - /// - public fixed byte Signature[4]; - /// - /// Length. - /// - public int Length; - - /// - /// Unused. - /// - public fixed byte unneded1[40 - 8]; - /// - /// DSDT. - /// - public int* DSDT; - /// - /// Unused. - /// - public fixed byte unneded2[48 - 44]; - /// - /// SMI CMD. - /// - public int* SMI_CMD; - /// - /// ACPI ENABLE. - /// - public byte ACPI_ENABLE; - /// - /// ACPI DISABLE. - /// - public byte ACPI_DISABLE; - /// - /// Unused. - /// - public fixed byte unneded3[64 - 54]; - /// - /// PM1a CNT BLK. - /// - public int* PM1a_CNT_BLK; - /// - /// PM1b CNT BLK. - /// - public int* PM1b_CNT_BLK; - /// - /// Unused. - /// - public fixed byte unneded4[89 - 72]; - /// - /// PM1 CNT LEN. - /// - public byte PM1_CNT_LEN; - }; - - /// - /// Compare string to byte array. - /// - /// String. - /// Pointer to the head of the byte array. - /// 0 - identical, -1 different. + /// String + /// Pointer to the head of the byte array + /// 0 - identical, -1 different static int Compare(string c1, byte* c2) { for (int i = 0; i < c1.Length; i++) @@ -172,10 +117,10 @@ static int Compare(string c1, byte* c2) } /// - /// Check RSD checksum. + /// Check RSD checksum /// - /// Address to check. - /// True if RSDT table checksum is good. + /// Address to check + /// True if RSDT table checksum is good static bool Check_RSD(uint address) { byte sum = 0; @@ -190,7 +135,7 @@ static bool Check_RSD(uint address) } /// - /// Start the ACPI. + /// Start the ACPI /// /// Initialize the ACPI. (default = true) /// Enable the ACPI. (default = true) @@ -208,9 +153,9 @@ public static void Start(bool initialize = true, bool enable = true) } /// - /// Shutdown the ACPI. + /// Shutdown the ACPI /// - /// Thrown on IO error. + /// Thrown on IO error public static void Shutdown() { Console.Clear(); @@ -230,19 +175,19 @@ public static void Shutdown() } /// - /// Reboot ACPI. + /// Reboot ACPI /// Not implemented. /// - /// Thrown always. + /// Thrown always public static void Reboot() { throw new NotImplementedException("ACPI Reset not implemented yet."); //TODO } /// - /// Initialize the ACPI. + /// Initialize the ACPI /// - /// true on success, false on failure. + /// true on success, false on failure private static bool Init() { byte* ptr = (byte*)RSDPAddress(); @@ -340,7 +285,7 @@ private static bool Init() } /// - /// Enable ACPI. + /// Enable ACPI /// public static void Enable() { @@ -348,7 +293,7 @@ public static void Enable() } /// - /// Disable ACPI. + /// Disable ACPI /// public static void Disable() { @@ -356,9 +301,9 @@ public static void Disable() } /// - /// Get the RSDP address. + /// Get the RSDP address /// - /// uint value. + /// uint value private static unsafe uint RSDPAddress() { for (uint addr = 0xE0000; addr < 0x100000; addr += 4) @@ -425,9 +370,9 @@ private static unsafe uint RSDPAddress() } /// - /// Get data from the FACP table. + /// Get data from the FACP table /// - /// Index number of the data to get. + /// Index number of the data to get /// /// 0 - ACPI ENABLE /// 1 - ACPI DISABLE @@ -435,7 +380,7 @@ private static unsafe uint RSDPAddress() /// other - 0 /// /// - /// byte value. + /// byte value private static byte facpbget(int number) { switch (number) @@ -452,9 +397,9 @@ private static byte facpbget(int number) } /// - /// Get pointer to the data on the FACP. + /// Get pointer to the data on the FACP /// - /// Index number of the data to get. + /// Index number of the data to get /// /// 0 - DSDT /// 1 - SMI CMD @@ -463,7 +408,7 @@ private static byte facpbget(int number) /// other - null /// /// - /// int pointer. + /// int pointer private static int* facpget(int number) { switch (number) @@ -482,9 +427,9 @@ private static byte facpbget(int number) } /// - /// Get the address of the RSDT. + /// Get the address of the RSDT /// - /// Address of the RSDT or 0 if not found. + /// Address of the RSDT or 0 if not found public static uint GetRSDTAddress() { uint rsdpAddr = RSDPAddress(); @@ -498,9 +443,9 @@ public static uint GetRSDTAddress() } /// - /// Get the contents of the RSDT. + /// Get the contents of the RSDT /// - /// Pointer to the RSDT contents or null if not found. + /// Pointer to the RSDT contents or null if not found public static byte* GetRSDTContents() { uint rsdtAddr = GetRSDTAddress(); @@ -511,5 +456,58 @@ public static uint GetRSDTAddress() return (byte*)rsdtAddr; } + + /// + /// Count the number of local APIC entries in the APIC table + /// + /// Number of local APIC entries + public static int CountLocalAPICEntries() + { + byte* rsdtContents = GetRSDTContents(); + if (rsdtContents == null) + { + return 0; + } + + var rsdtHeader = (DescriptionHeader*)rsdtContents; + int entriesCount = (rsdtHeader->Length - sizeof(DescriptionHeader)) / 4; + uint* tableEntries = (uint*)(rsdtContents + sizeof(DescriptionHeader)); + + for (int i = 0; i < entriesCount; i++) + { + byte* tablePtr = (byte*)tableEntries[i]; + if (Compare("APIC", tablePtr) == 0) + { + return CountAPICEntries(tablePtr); + } + } + + return 0; + } + + /// + /// Count the number of APIC entries + /// + /// Pointer to the APIC table + /// Number of APIC entries + private static int CountAPICEntries(byte* apicTable) + { + var apicHeader = (DescriptionHeader*)apicTable; + int length = (int)apicHeader->Length; + int count = 0; + byte* ptr = apicTable + sizeof(DescriptionHeader); + + while (ptr < apicTable + length) + { + var header = (APICHeader*)ptr; + if (header->Type == 0) // Type 0 indicates a Processor Local APIC + { + count++; + } + ptr += header->Length; + } + + return count; + } } } From f33c25e69fca9b86ceac75a67cb6f8ad6a52185c Mon Sep 17 00:00:00 2001 From: Epiczhul <76625941+Epiczhul@users.noreply.github.com> Date: Sat, 25 May 2024 18:48:59 +0200 Subject: [PATCH 5/6] Update ACPI.cs --- source/Cosmos.Core/ACPI.cs | 236 ++++++------------------------------- 1 file changed, 37 insertions(+), 199 deletions(-) diff --git a/source/Cosmos.Core/ACPI.cs b/source/Cosmos.Core/ACPI.cs index 9bc87ad5f8..c4fc8814b9 100644 --- a/source/Cosmos.Core/ACPI.cs +++ b/source/Cosmos.Core/ACPI.cs @@ -12,69 +12,37 @@ public unsafe class ACPI /// RSD table struct. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] - public unsafe struct RSDPtr + public struct RSDPtr { - /// - /// Signature. - /// public fixed byte Signature[8]; - /// - /// CheckSum - /// public byte CheckSum; - /// - /// OemID - /// public fixed byte OemID[6]; - /// - /// Revision - /// public byte Revision; - /// - /// RSDT Address - /// - public int RsdtAddress; + public uint RsdtAddress; }; + /// + /// RSDT table struct. + /// [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct DescriptionHeader + public struct RSDT { public fixed byte Signature[4]; public uint Length; public byte Revision; public byte Checksum; - public fixed byte OEMID[6]; - public fixed byte OEMTableID[8]; - public uint OEMRevision; + public fixed byte OemID[6]; + public fixed byte OemTableID[8]; + public uint OemRevision; public uint CreatorID; public uint CreatorRevision; + public fixed uint Entry[1]; // Placeholder for the start of the array of pointers to other tables } - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct APICHeader - { - public byte Type; - public byte Length; - } - - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct LocalAPIC - { - public byte Type; - public byte Length; - public byte ProcessorID; - public byte APICID; - public uint Flags; - } - - /// - /// New Port I/O - /// + // New Port I/O private static ushort smiIO, pm1aIO, pm1bIO; - /// - /// ACPI variables - /// + // ACPI variables private static int* SMI_CMD; private static byte ACPI_ENABLE; private static byte ACPI_DISABLE; @@ -85,28 +53,26 @@ public struct LocalAPIC private static short SLP_EN; private static byte PM1_CNT_LEN; - /// - /// FACP - /// private static byte* Facp = null; - /// - /// Check ACPI header - /// - /// - /// - /// - static int acpiCheckHeader(byte* ptr, string sig) + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct FACP { - return Compare(sig, ptr); - } + public fixed byte Signature[4]; + public int Length; + public fixed byte unneded1[40 - 8]; + public int* DSDT; + public fixed byte unneded2[48 - 44]; + public int* SMI_CMD; + public byte ACPI_ENABLE; + public byte ACPI_DISABLE; + public fixed byte unneded3[64 - 54]; + public int* PM1a_CNT_BLK; + public int* PM1b_CNT_BLK; + public fixed byte unneded4[89 - 72]; + public byte PM1_CNT_LEN; + }; - /// - /// Compare string to byte array - /// - /// String - /// Pointer to the head of the byte array - /// 0 - identical, -1 different static int Compare(string c1, byte* c2) { for (int i = 0; i < c1.Length; i++) @@ -116,11 +82,6 @@ static int Compare(string c1, byte* c2) return 0; } - /// - /// Check RSD checksum - /// - /// Address to check - /// True if RSDT table checksum is good static bool Check_RSD(uint address) { byte sum = 0; @@ -134,11 +95,6 @@ static bool Check_RSD(uint address) return sum == 0; } - /// - /// Start the ACPI - /// - /// Initialize the ACPI. (default = true) - /// Enable the ACPI. (default = true) public static void Start(bool initialize = true, bool enable = true) { if (initialize) @@ -152,10 +108,6 @@ public static void Start(bool initialize = true, bool enable = true) } } - /// - /// Shutdown the ACPI - /// - /// Thrown on IO error public static void Shutdown() { Console.Clear(); @@ -174,20 +126,11 @@ public static void Shutdown() CPU.Halt(); } - /// - /// Reboot ACPI - /// Not implemented. - /// - /// Thrown always public static void Reboot() { - throw new NotImplementedException("ACPI Reset not implemented yet."); //TODO + throw new NotImplementedException("ACPI Reset not implemented yet."); } - /// - /// Initialize the ACPI - /// - /// true on success, false on failure private static bool Init() { byte* ptr = (byte*)RSDPAddress(); @@ -284,26 +227,16 @@ private static bool Init() return false; } - /// - /// Enable ACPI - /// public static void Enable() { smiIO = ACPI_ENABLE; } - /// - /// Disable ACPI - /// public static void Disable() { smiIO = ACPI_DISABLE; } - /// - /// Get the RSDP address - /// - /// uint value private static unsafe uint RSDPAddress() { for (uint addr = 0xE0000; addr < 0x100000; addr += 4) @@ -331,11 +264,6 @@ private static unsafe uint RSDPAddress() return 0; } - /// - /// Check RSDT table - /// - /// A pointer to the RSDT - /// RSDT table address private static uint* acpiCheckRSDPtr(uint* ptr) { string sig = "RSD PTR "; @@ -369,18 +297,6 @@ private static unsafe uint RSDPAddress() return null; } - /// - /// Get data from the FACP table - /// - /// Index number of the data to get - /// - /// 0 - ACPI ENABLE - /// 1 - ACPI DISABLE - /// 2 - PM1 CNT LEN - /// other - 0 - /// - /// - /// byte value private static byte facpbget(int number) { switch (number) @@ -396,19 +312,6 @@ private static byte facpbget(int number) } } - /// - /// Get pointer to the data on the FACP - /// - /// Index number of the data to get - /// - /// 0 - DSDT - /// 1 - SMI CMD - /// 2 - PM1a - /// 3 - PM1b - /// other - null - /// - /// - /// int pointer private static int* facpget(int number) { switch (number) @@ -426,88 +329,23 @@ private static byte facpbget(int number) } } - /// - /// Get the address of the RSDT - /// - /// Address of the RSDT or 0 if not found - public static uint GetRSDTAddress() - { - uint rsdpAddr = RSDPAddress(); - if (rsdpAddr == 0) - { - return 0; - } - - var rsdp = (RSDPtr*)rsdpAddr; - return (uint)rsdp->RsdtAddress; - } - - /// - /// Get the contents of the RSDT - /// - /// Pointer to the RSDT contents or null if not found - public static byte* GetRSDTContents() + public static RSDT* FindRSDT() { - uint rsdtAddr = GetRSDTAddress(); - if (rsdtAddr == 0) + uint rsdpAddress = RSDPAddress(); + if (rsdpAddress == 0) { return null; } - return (byte*)rsdtAddr; - } - - /// - /// Count the number of local APIC entries in the APIC table - /// - /// Number of local APIC entries - public static int CountLocalAPICEntries() - { - byte* rsdtContents = GetRSDTContents(); - if (rsdtContents == null) - { - return 0; - } - - var rsdtHeader = (DescriptionHeader*)rsdtContents; - int entriesCount = (rsdtHeader->Length - sizeof(DescriptionHeader)) / 4; - uint* tableEntries = (uint*)(rsdtContents + sizeof(DescriptionHeader)); - - for (int i = 0; i < entriesCount; i++) - { - byte* tablePtr = (byte*)tableEntries[i]; - if (Compare("APIC", tablePtr) == 0) - { - return CountAPICEntries(tablePtr); - } - } - - return 0; - } - - /// - /// Count the number of APIC entries - /// - /// Pointer to the APIC table - /// Number of APIC entries - private static int CountAPICEntries(byte* apicTable) - { - var apicHeader = (DescriptionHeader*)apicTable; - int length = (int)apicHeader->Length; - int count = 0; - byte* ptr = apicTable + sizeof(DescriptionHeader); + RSDPtr* rsdp = (RSDPtr*)rsdpAddress; + uint rsdtAddress = rsdp->RsdtAddress; - while (ptr < apicTable + length) + if (rsdtAddress == 0) { - var header = (APICHeader*)ptr; - if (header->Type == 0) // Type 0 indicates a Processor Local APIC - { - count++; - } - ptr += header->Length; + return null; } - return count; + return (RSDT*)rsdtAddress; } } } From 7cb4a9b30ae1fc1d183956645d8b83c23b830e77 Mon Sep 17 00:00:00 2001 From: Epiczhul <76625941+Epiczhul@users.noreply.github.com> Date: Sat, 25 May 2024 18:56:05 +0200 Subject: [PATCH 6/6] Update ACPI.cs --- source/Cosmos.Core/ACPI.cs | 215 ++++++++++++++++++++++++++++++------- 1 file changed, 174 insertions(+), 41 deletions(-) diff --git a/source/Cosmos.Core/ACPI.cs b/source/Cosmos.Core/ACPI.cs index c4fc8814b9..7021685e3e 100644 --- a/source/Cosmos.Core/ACPI.cs +++ b/source/Cosmos.Core/ACPI.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Runtime.InteropServices; namespace Cosmos.Core @@ -12,67 +12,156 @@ public unsafe class ACPI /// RSD table struct. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct RSDPtr + public unsafe struct RSDPtr { + /// + /// Signature. + /// public fixed byte Signature[8]; + /// + /// CheckSum + /// public byte CheckSum; + /// + /// OemID + /// public fixed byte OemID[6]; + /// + /// Revision + /// public byte Revision; - public uint RsdtAddress; + /// + /// RSDT Address + /// + public int RsdtAddress; }; + // New Port I/O /// - /// RSDT table struct. + /// IO port. /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct RSDT - { - public fixed byte Signature[4]; - public uint Length; - public byte Revision; - public byte Checksum; - public fixed byte OemID[6]; - public fixed byte OemTableID[8]; - public uint OemRevision; - public uint CreatorID; - public uint CreatorRevision; - public fixed uint Entry[1]; // Placeholder for the start of the array of pointers to other tables - } - - // New Port I/O private static ushort smiIO, pm1aIO, pm1bIO; // ACPI variables + /// + /// SMI CMD. + /// private static int* SMI_CMD; + /// + /// ACPI ENABLE. + /// private static byte ACPI_ENABLE; + /// + /// ACPI DISABLE. + /// private static byte ACPI_DISABLE; + /// + /// PM1a CNT + /// private static int* PM1a_CNT; + /// + /// PM1b CNT + /// private static int* PM1b_CNT; + /// + /// SLP TYPa + /// private static short SLP_TYPa; + /// + /// SLP TYPb + /// private static short SLP_TYPb; + /// + /// SLP EN. + /// private static short SLP_EN; + /// + /// PM1 CNT LEN1 + /// private static byte PM1_CNT_LEN; - private static byte* Facp = null; + /// + /// Check ACPI header. + /// + /// + /// + /// + static int acpiCheckHeader(byte* ptr, string sig) + { + return Compare(sig, ptr); + } + /// + /// FACP. + /// + private static byte* Facp = null; + /// + /// FACP struct. + /// [StructLayout(LayoutKind.Sequential, Pack = 1)] struct FACP { + /// + /// Signature. + /// public fixed byte Signature[4]; + /// + /// Length. + /// public int Length; + + /// + /// Unused. + /// public fixed byte unneded1[40 - 8]; + /// + /// DSDT. + /// public int* DSDT; + /// + /// Unused. + /// public fixed byte unneded2[48 - 44]; + /// + /// SMI CMD. + /// public int* SMI_CMD; + /// + /// ACPI ENABLE. + /// public byte ACPI_ENABLE; + /// + /// ACPI DISABLE. + /// public byte ACPI_DISABLE; + /// + /// Unused. + /// public fixed byte unneded3[64 - 54]; + /// + /// PM1a CNT BLK. + /// public int* PM1a_CNT_BLK; + /// + /// PM1b CNT BLK. + /// public int* PM1b_CNT_BLK; + /// + /// Unused. + /// public fixed byte unneded4[89 - 72]; + /// + /// PM1 CNT LEN. + /// public byte PM1_CNT_LEN; }; + /// + /// Compare string to byte array. + /// + /// String. + /// Pointer to the head of the byte array. + /// 0 - identical, -1 different. static int Compare(string c1, byte* c2) { for (int i = 0; i < c1.Length; i++) @@ -82,6 +171,11 @@ static int Compare(string c1, byte* c2) return 0; } + /// + /// Check RSD checksum. + /// + /// Address to check. + /// True if RSDT table checksum is good. static bool Check_RSD(uint address) { byte sum = 0; @@ -95,6 +189,11 @@ static bool Check_RSD(uint address) return sum == 0; } + /// + /// Start the ACPI. + /// + /// Initialize the ACPI. (default = true) + /// Enable the ACPI. (default = true) public static void Start(bool initialize = true, bool enable = true) { if (initialize) @@ -108,6 +207,10 @@ public static void Start(bool initialize = true, bool enable = true) } } + /// + /// Shutdown the ACPI. + /// + /// Thrown on IO error. public static void Shutdown() { Console.Clear(); @@ -126,11 +229,20 @@ public static void Shutdown() CPU.Halt(); } + /// + /// Reboot ACPI. + /// Not implemented. + /// + /// Thrown always. public static void Reboot() { - throw new NotImplementedException("ACPI Reset not implemented yet."); + throw new NotImplementedException("ACPI Reset not implemented yet."); //TODO } + /// + /// Initialize the ACPI. + /// + /// true on success, false on failure. private static bool Init() { byte* ptr = (byte*)RSDPAddress(); @@ -227,16 +339,26 @@ private static bool Init() return false; } + /// + /// Enable ACPI. + /// public static void Enable() { smiIO = ACPI_ENABLE; } + /// + /// Disable ACPI. + /// public static void Disable() { smiIO = ACPI_DISABLE; } + /// + /// Get the RSDP address. + /// + /// uint value. private static unsafe uint RSDPAddress() { for (uint addr = 0xE0000; addr < 0x100000; addr += 4) @@ -264,6 +386,11 @@ private static unsafe uint RSDPAddress() return 0; } + /// + /// Check RSDT table + /// + /// A pointer to the RSDT + /// RSDT table address private static uint* acpiCheckRSDPtr(uint* ptr) { string sig = "RSD PTR "; @@ -297,6 +424,18 @@ private static unsafe uint RSDPAddress() return null; } + /// + /// Get data from the FACP table. + /// + /// Index number of the data to get. + /// + /// 0 - ACPI ENABLE + /// 1 - ACPI DISABLE + /// 2 - PM1 CNT LEN + /// other - 0 + /// + /// + /// byte value. private static byte facpbget(int number) { switch (number) @@ -312,6 +451,19 @@ private static byte facpbget(int number) } } + /// + /// Get pointer to the data on the FACP. + /// + /// Index number of the data to get. + /// + /// 0 - DSDT + /// 1 - SMI CMD + /// 2 - PM1a + /// 3 - PM1b + /// other - null + /// + /// + /// int pointer. private static int* facpget(int number) { switch (number) @@ -328,24 +480,5 @@ private static byte facpbget(int number) return null; } } - - public static RSDT* FindRSDT() - { - uint rsdpAddress = RSDPAddress(); - if (rsdpAddress == 0) - { - return null; - } - - RSDPtr* rsdp = (RSDPtr*)rsdpAddress; - uint rsdtAddress = rsdp->RsdtAddress; - - if (rsdtAddress == 0) - { - return null; - } - - return (RSDT*)rsdtAddress; - } } }