From fc0d38259fd6670966b916b1f28a11f3cb2a4c45 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 1 Jun 2024 09:29:31 +0200 Subject: MHR-23: SMP: Add hal_send_start_ipi and hal_send_end_ipi, as well as DMA utilities. Signed-off-by: Amlal El Mahrouss --- Kernel/HALKit/AMD64/Processor.hpp | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'Kernel/HALKit/AMD64/Processor.hpp') diff --git a/Kernel/HALKit/AMD64/Processor.hpp b/Kernel/HALKit/AMD64/Processor.hpp index 235e425d..608a502f 100644 --- a/Kernel/HALKit/AMD64/Processor.hpp +++ b/Kernel/HALKit/AMD64/Processor.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #ifdef kCPUBackendName #undef kCPUBackendName @@ -53,6 +54,68 @@ namespace NewOS namespace NewOS::HAL { + + enum + { + eFlagsUser, + eFlagsRw, + eFlagsExecDisable + }; + + /// @brief Map address to PDE. + /// @param pde a valid page directory. + /// @param phys_addr a valid phyiscal address. + /// @param virt_addr a valid virtual address. + /// @param flags the flags to put on the page. + inline Int32 ke_map_address(PDE* pde, UIntPtr phys_addr, UIntPtr virt_addr, UInt32 flags) + { + UInt16 pml4_index = (virt_addr >> 39) & 0x1FF; + + if (!pde->Pte[pml4_index].Present) + { + pde->Pte[pml4_index].Present = true; + kcout << "PM: It is present now.\r"; + } + else + { + kcout << "PM: It is already present.\r"; + kcout << "Address? " << hex_number(pde->Pte[pml4_index].PhysicalAddress) << endl; + kcout << "User? " << (pde->Pte[pml4_index].User ? "yes" : "no") << "\r"; + kcout << "RW? " << (pde->Pte[pml4_index].Rw ? "yes" : "no") << "\r"; + + return 1; + } + + pde->Pte[pml4_index].PhysicalAddress = phys_addr; + pde->Pte[pml4_index].Rw = flags & eFlagsRw; + pde->Pte[pml4_index].User = flags & eFlagsUser; + pde->Pte[pml4_index].ExecDisable = flags & eFlagsExecDisable; + + return 0; + } + + /// @brief Map address to PDE. + /// @param pde + /// @param phys_addr + /// @param virt_addr + /// @param flags + inline void ke_unmap_address(PDE* pde, UIntPtr phys_addr, UIntPtr virt_addr, UInt32 flags) + { + UInt16 pml4_index = (virt_addr >> 39) & 0x1FF; + UInt16 pdpt_index = (virt_addr >> 30) & 0x1FF; + UInt16 pd_index = (virt_addr >> 21) & 0x1FF; + UInt16 pt_index = (virt_addr >> 12) & 0x1FF; + + if (pde->Pte[pml4_index].Present) + { + pde->Pte[pml4_index].Present = false; + pde->Pte[pml4_index].PhysicalAddress = 0; + pde->Pte[pml4_index].Rw = 0; + pde->Pte[pml4_index].User = 0; + pde->Pte[pml4_index].ExecDisable = 0; + } + } + EXTERN_C UChar In8(UInt16 port); EXTERN_C UShort In16(UInt16 port); EXTERN_C UInt In32(UInt16 port); @@ -162,6 +225,8 @@ namespace NewOS::HAL }; Void hal_system_get_cores(VoidPtr rsdPtr); + Void hal_send_start_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress); + Void hal_send_end_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress); /// @brief Processor specific structures. namespace Detail -- cgit v1.2.3 From 5bc5e4de0d4dcfc52c192315ca5c59a066d678d8 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 1 Jun 2024 23:59:03 +0200 Subject: MHR-23: Fix ACPI and APIC probe. Signed-off-by: Amlal El Mahrouss --- Kernel/Builtins/ACPI/ACPI.hxx | 2 +- Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx | 18 +++++----- .../HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp | 42 ++++++---------------- Kernel/HALKit/AMD64/Processor.hpp | 30 ++++++++-------- SDK/Libraries/CoreSystem/Headers/File.h | 5 +++ SDK/Libraries/CoreSystem/Sources/File.c | 13 ++++++- 6 files changed, 54 insertions(+), 56 deletions(-) (limited to 'Kernel/HALKit/AMD64/Processor.hpp') diff --git a/Kernel/Builtins/ACPI/ACPI.hxx b/Kernel/Builtins/ACPI/ACPI.hxx index d9bfeaca..430a39f4 100644 --- a/Kernel/Builtins/ACPI/ACPI.hxx +++ b/Kernel/Builtins/ACPI/ACPI.hxx @@ -81,7 +81,7 @@ namespace NewOS UInt32 OemRev; UInt32 CreatorID; UInt32 CreatorRevision; - UInt64 AddressArr[]; + UInt32 AddressArr[]; }; } // namespace NewOS diff --git a/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx b/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx index f92554f8..adeeb10d 100644 --- a/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx +++ b/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx @@ -76,36 +76,38 @@ namespace NewOS } /// FIXME - RSDT* xsdt = (RSDT*)rsdPtr->XsdtAddress; + RSDT* xsdt = (RSDT*)(rsdPtr->RsdtAddress); - if (NewOS::HAL::ke_map_address((PDE*)hal_read_cr3(), rsdPtr->XsdtAddress, (UIntPtr)xsdt, NewOS::HAL::eFlagsRw)) - return ErrorOr{-5}; - - Int64 num = (xsdt->Length - sizeof(SDT)) / sizeof(UInt64); + Int64 num = (xsdt->Length - sizeof(SDT)) / sizeof(UInt32); if (num < 1) { + kcout << "ACPI: No entries." << endl; return ErrorOr{-6}; } this->fEntries = num; kcout << "ACPI: Number of entries: " << number(this->fEntries) << endl; + kcout << "ACPI: Revision: " << number(xsdt->Revision) << endl; + kcout << "ACPI: XSDT: " << xsdt->Signature << endl; kcout << "ACPI: Address of XSDT: " << hex_number((UIntPtr)xsdt) << endl; const short cAcpiSignatureLength = 4; for (Size index = 0; index < this->fEntries; ++index) { - SDT* sdt = (SDT*)(xsdt->AddressArr[index]); + SDT& sdt = *(SDT*)xsdt->AddressArr[index]; + + kcout << "ACPI: Revision: " << number(sdt.CreatorID) << endl; for (short signature_index = 0; signature_index < cAcpiSignatureLength; ++signature_index) { - if (sdt->Signature[signature_index] != signature[signature_index]) + if (sdt.Signature[signature_index] != signature[signature_index]) break; if (signature_index == (cAcpiSignatureLength - 1)) - return ErrorOr(reinterpret_cast(sdt)); + return ErrorOr(reinterpret_cast(&sdt)); } } diff --git a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp index 287b1882..5ab40532 100644 --- a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp +++ b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp @@ -137,37 +137,17 @@ namespace NewOS::HAL if (kApicMadt != nullptr) { - auto madt = (SDT*)kApicMadt; - - const UInt8* madt_end = (const UInt8*)madt + madt->Length; - const UInt8* entry_ptr = (const UInt8*)(madt + 1); - - while (entry_ptr < madt_end) - { - const MadtType::MadtAddress* entry_header = (const MadtType::MadtAddress*)entry_ptr; - - switch (entry_header->Flags) - { - case 0: { - const MadtProcessorLocalApic* local_apic = (const MadtProcessorLocalApic*)entry_ptr; - if (local_apic->Flags & 1) - { - // Processor is enabled - kcout << "Processor ID: %d, APIC ID: %d\n" - << number(local_apic->AcpiProcessorId) << number(local_apic->ApicId); - } - break; - } - default: - break; - } - - entry_ptr += entry_header->RecordLen; - } - - while (true) - { - } + MadtType* madt = (MadtType*)kApicMadt; + + constexpr auto cMaxProbableCores = 4; + + for (SizeT i = 0; i < cMaxProbableCores; ++i) + { + if (madt->MadtRecords[i].Flags == 0x01) // if local apic. + { + // then register as a core for scheduler. + } + } } else { diff --git a/Kernel/HALKit/AMD64/Processor.hpp b/Kernel/HALKit/AMD64/Processor.hpp index 608a502f..4916d845 100644 --- a/Kernel/HALKit/AMD64/Processor.hpp +++ b/Kernel/HALKit/AMD64/Processor.hpp @@ -74,24 +74,27 @@ namespace NewOS::HAL if (!pde->Pte[pml4_index].Present) { pde->Pte[pml4_index].Present = true; - kcout << "PM: It is present now.\r"; + kcout << "PM is present now.\r"; + + pde->Pte[pml4_index].PhysicalAddress = phys_addr; + pde->Pte[pml4_index].Rw = flags & eFlagsRw; + pde->Pte[pml4_index].User = flags & eFlagsUser; + pde->Pte[pml4_index].ExecDisable = flags & eFlagsExecDisable; + + return 0; } else { - kcout << "PM: It is already present.\r"; - kcout << "Address? " << hex_number(pde->Pte[pml4_index].PhysicalAddress) << endl; - kcout << "User? " << (pde->Pte[pml4_index].User ? "yes" : "no") << "\r"; - kcout << "RW? " << (pde->Pte[pml4_index].Rw ? "yes" : "no") << "\r"; + kcout << "PM is already present.\r"; - return 1; - } + kcout << "PhysicalAddress: " << hex_number(pde->Pte[pml4_index].PhysicalAddress) << endl; + kcout << "User: " << (pde->Pte[pml4_index].User ? "yes" : "no") << "\r"; + kcout << "RW: " << (pde->Pte[pml4_index].Rw ? "yes" : "no") << "\r"; - pde->Pte[pml4_index].PhysicalAddress = phys_addr; - pde->Pte[pml4_index].Rw = flags & eFlagsRw; - pde->Pte[pml4_index].User = flags & eFlagsUser; - pde->Pte[pml4_index].ExecDisable = flags & eFlagsExecDisable; + return 1; + } - return 0; + return 0; } /// @brief Map address to PDE. @@ -102,9 +105,6 @@ namespace NewOS::HAL inline void ke_unmap_address(PDE* pde, UIntPtr phys_addr, UIntPtr virt_addr, UInt32 flags) { UInt16 pml4_index = (virt_addr >> 39) & 0x1FF; - UInt16 pdpt_index = (virt_addr >> 30) & 0x1FF; - UInt16 pd_index = (virt_addr >> 21) & 0x1FF; - UInt16 pt_index = (virt_addr >> 12) & 0x1FF; if (pde->Pte[pml4_index].Present) { diff --git a/SDK/Libraries/CoreSystem/Headers/File.h b/SDK/Libraries/CoreSystem/Headers/File.h index 0013b074..cfd9ee30 100644 --- a/SDK/Libraries/CoreSystem/Headers/File.h +++ b/SDK/Libraries/CoreSystem/Headers/File.h @@ -49,4 +49,9 @@ CS_EXTERN_C FSForkRef CSGetFork(FSRef refCS, const CharacterTypeUTF8* forkName); /// @return if not return false, or true. CS_EXTERN_C BooleanType CSIsValidPath(const CharacterTypeUTF8* path); +/// @brief Flush file +/// @param refCS the file reference. +/// @return +CS_EXTERN_C VoidType CSFlushFile(FSRef refCS); + /// END OF FILE diff --git a/SDK/Libraries/CoreSystem/Sources/File.c b/SDK/Libraries/CoreSystem/Sources/File.c index 7547e7f2..6e0e810a 100644 --- a/SDK/Libraries/CoreSystem/Sources/File.c +++ b/SDK/Libraries/CoreSystem/Sources/File.c @@ -38,10 +38,21 @@ CS_EXTERN_C VoidType CSCloseFile(FSRef refCS) { CS_MUST_PASS(kSharedApplication); - kSharedApplication->Invoke(kSharedApplication, refCS, kFlushFile); + CSFlushFile(refCS); + kSharedApplication->Invoke(kSharedApplication, kCallCloseFile, refCS); } +/// @brief Flush file +/// @param refCS the file reference. +/// @return +CS_EXTERN_C VoidType CSFlushFile(FSRef refCS) +{ + CS_MUST_PASS(kSharedApplication); + + kSharedApplication->Invoke(kSharedApplication, refCS, kFlushFile); +} + /// @brief Check if filesystem path is valid. /// @param path /// @return -- cgit v1.2.3