From 1e5d5f717e26989933181dd3e51d7c1a94fa2d05 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sat, 7 Dec 2024 10:02:20 +0100 Subject: META: 10:00 AM BUMP. Signed-off-by: Amlal --- dev/Modules/ReadMe.md | 9 +- dev/SCIKit/SysCalls.h | 2 +- dev/ZBAKit/ReadMe.md | 2 +- dev/ZKAKit/FSKit/NeFS.h | 12 +- dev/ZKAKit/FirmwareKit/EFI/EFI.h | 18 +- dev/ZKAKit/HALKit/AMD64/HalCoreMPScheduler.cc | 240 ------------------------- dev/ZKAKit/HALKit/AMD64/HalCoreScheduler.cc | 240 +++++++++++++++++++++++++ dev/ZKAKit/HALKit/AMD64/ReadMe.md | 8 +- dev/ZKAKit/HALKit/AMD64/Storage/AHCI-DMA.cc | 68 ++++--- dev/ZKAKit/KernelKit/HardwareThreadScheduler.h | 10 +- dev/ZKAKit/KernelKit/Heap.h | 2 +- dev/ZKAKit/KernelKit/LPC.h | 7 +- dev/ZKAKit/KernelKit/LoaderInterface.h | 6 +- dev/ZKAKit/src/FS/NeFS.cc | 10 +- dev/ZKAKit/src/Heap.cc | 10 +- dev/ZKAKit/src/ThreadLocalStorage.cc | 4 +- 16 files changed, 336 insertions(+), 312 deletions(-) delete mode 100644 dev/ZKAKit/HALKit/AMD64/HalCoreMPScheduler.cc create mode 100644 dev/ZKAKit/HALKit/AMD64/HalCoreScheduler.cc (limited to 'dev') diff --git a/dev/Modules/ReadMe.md b/dev/Modules/ReadMe.md index a7179eec..fe5625b3 100644 --- a/dev/Modules/ReadMe.md +++ b/dev/Modules/ReadMe.md @@ -1,7 +1,12 @@ -# Kernel Modules. +# Kernel Modules -## About: +## Brief They are pluggable modules for builtin hardware support. +## To-Do + +- [X] AHCI kernel module. +- [X] MBCI kernel module. + ###### Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. All rights reserved. diff --git a/dev/SCIKit/SysCalls.h b/dev/SCIKit/SysCalls.h index 563c8d69..643045ac 100644 --- a/dev/SCIKit/SysCalls.h +++ b/dev/SCIKit/SysCalls.h @@ -11,7 +11,7 @@ Purpose: SCIKit Macros header. #include -/// @brief Here we define filesystem calls. +/// @brief In this file we define filesystem calls. SYSCALL_DECL(FCloseFile, const SCIObject handle); SYSCALL_DECL(FOpenFile, const Char* name, const Char* rest); diff --git a/dev/ZBAKit/ReadMe.md b/dev/ZBAKit/ReadMe.md index d7dc06ec..d2acc502 100644 --- a/dev/ZBAKit/ReadMe.md +++ b/dev/ZBAKit/ReadMe.md @@ -8,7 +8,7 @@ You need: Start by cloning the repo: ``` -git clone git@github.com:amlel-el-mahrouss/zka-dev.git +git clone git@github.com:ElMahroussLogic/zka-dev.git ``` And then execute: diff --git a/dev/ZKAKit/FSKit/NeFS.h b/dev/ZKAKit/FSKit/NeFS.h index bb7d4877..fe5d9bdc 100644 --- a/dev/ZKAKit/FSKit/NeFS.h +++ b/dev/ZKAKit/FSKit/NeFS.h @@ -239,15 +239,15 @@ namespace Kernel /// @param theFork the fork itself. /// @return the fork _Output NFS_FORK_STRUCT* CreateFork(_Input NFS_CATALOG_STRUCT* catalog, - _Input NFS_FORK_STRUCT& theFork); + _Input NFS_FORK_STRUCT& theFork); /// @brief Find fork inside New filesystem. /// @param catalog the catalog. /// @param name the fork name. /// @return the fork. _Output NFS_FORK_STRUCT* FindFork(_Input NFS_CATALOG_STRUCT* catalog, - _Input const Char* name, - Boolean dataOrRsrc); + _Input const Char* name, + Boolean dataOrRsrc); _Output Void RemoveFork(_Input NFS_FORK_STRUCT* fork); @@ -257,7 +257,7 @@ namespace Kernel _Output NFS_CATALOG_STRUCT* GetCatalog(_Input const Char* name); - _Output NFS_CATALOG_STRUCT* CreateCatalog(_Input const Char* name, + _Output NFS_CATALOG_STRUCT* CreateCatalog(_Input const Char* name, _Input const Int32& flags, _Input const Int32& kind); @@ -267,12 +267,12 @@ namespace Kernel _Input Bool isRsrcFork, _Input VoidPtr data, _Input SizeT sizeOfData, - _Input const Char* forkName); + _Input const Char* forkName); VoidPtr ReadCatalog(_Input _Output NFS_CATALOG_STRUCT* catalog, _Input Bool isRsrcFork, _Input SizeT dataSz, - _Input const Char* forkName); + _Input const Char* forkName); bool Seek(_Input _Output NFS_CATALOG_STRUCT* catalog, SizeT off); diff --git a/dev/ZKAKit/FirmwareKit/EFI/EFI.h b/dev/ZKAKit/FirmwareKit/EFI/EFI.h index 3ccdf775..0565b3eb 100644 --- a/dev/ZKAKit/FirmwareKit/EFI/EFI.h +++ b/dev/ZKAKit/FirmwareKit/EFI/EFI.h @@ -823,11 +823,11 @@ typedef struct _EfiProcessorInformation typedef EfiStatusType EFI_API (*EfiMpServicesGetNumberOfProcessors)( IN struct _EfiMpServicesProtocol* Self, - OUT UInt32* NumberOfProcessors, - OUT UInt32* NumberOfEnabledProcessors); + OUT UInt32* NumberOfProcessors, + OUT UInt32* NumberOfEnabledProcessors); typedef EfiStatusType EFI_API (*EfiMpServicesGetProcessorInfo)( - IN struct _EfiMpServicesProtocol* Self, + IN struct _EfiMpServicesProtocol* Self, IN UInt32* ProcessorNumber, OUT struct _EfiProcessorInformation* NumberOfEnabledProcessors); @@ -840,8 +840,8 @@ typedef EfiStatusType EFI_API (*EfiMpServicesStartupAllAPS)( IN Boolean SingleThread, IN VoidPtr WaitEvent OPTIONAL, // EFI_EVENT first, but unused here. IN UInt32 TimeoutInMicroSeconds, - IN Void* ProcedureArgument OPTIONAL, - OUT UInt32** FailedCpuList OPTIONAL); + IN Void* ProcedureArgument OPTIONAL, + OUT UInt32** FailedCpuList OPTIONAL); typedef EfiStatusType EFI_API (*EfiMpServicesSwitchBSP)( IN struct _EfiMpServicesProtocol* Self, @@ -854,18 +854,18 @@ typedef EfiStatusType EFI_API (*EfiMpServicesStartupThisAP)( IN UInt32 ProcessorNumber, IN VoidPtr WaitEvent OPTIONAL, IN UInt32 TimeoutInMicroseconds, - IN Void* ProcedureArgument OPTIONAL, - OUT Boolean* Finished OPTIONAL); + IN Void* ProcedureArgument OPTIONAL, + OUT Boolean* Finished OPTIONAL); typedef EfiStatusType EFI_API (*EfiMpServicesDisableThisAP)( IN struct _EfiMpServicesProtocol* Self, IN UInt32 ProcessorNumber, IN Boolean EnableAP, - IN UInt32* HealthFlag OPTIONAL); + IN UInt32* HealthFlag OPTIONAL); typedef EfiStatusType EFI_API (*EfiMpServicesWhoAmI)( IN struct _EfiMpServicesProtocol* Self, - OUT UInt32* ProcessorNumber); + OUT UInt32* ProcessorNumber); typedef struct _EfiMpServicesProtocol { diff --git a/dev/ZKAKit/HALKit/AMD64/HalCoreMPScheduler.cc b/dev/ZKAKit/HALKit/AMD64/HalCoreMPScheduler.cc deleted file mode 100644 index 9c1e91b4..00000000 --- a/dev/ZKAKit/HALKit/AMD64/HalCoreMPScheduler.cc +++ /dev/null @@ -1,240 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. - -------------------------------------------- */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// Needed for SMP. -#include - -#define kApicSignature "APIC" - -#define kAPIC_ICR_Low 0x300 -#define kAPIC_ICR_High 0x310 -#define kAPIC_SIPI_Vector 0x00500 -#define kAPIC_EIPI_Vector 0x00400 - -#define kAPIC_BASE_MSR 0x1B -#define kAPIC_BASE_MSR_BSP 0x100 -#define kAPIC_BASE_MSR_ENABLE 0x800 - -#define cSMPMax (32U) - -/// @note: _hal_switch_context is internal - -/////////////////////////////////////////////////////////////////////////////////////// - -//! NOTE: fGSI stands 'Field Global System Interrupt' - -/////////////////////////////////////////////////////////////////////////////////////// - -namespace Kernel::HAL -{ - struct MADT_TABLE; - - EXTERN_C Void _hal_spin_core(Void); - - STATIC struct MADT_TABLE* kMADTBlock = nullptr; - STATIC Bool kSMPAware = false; - STATIC Int64 kSMPCount = 0; - - STATIC Int32 cSMPInterrupt = 0; - STATIC UInt64 kAPICLocales[cSMPMax] = {0}; - STATIC VoidPtr kRawMADT = nullptr; - - /// @brief Multiple APIC Descriptor Table. - struct MADT_TABLE final : public SDT - { - UInt32 Address; // Madt address - UInt8 Flags; // Madt flags - - struct - { - UInt8 Type; - UInt8 Len; - - union { - struct - { - UInt8 IoID; - UInt8 Resv; - UInt32 IoAddress; - UInt32 GISBase; - } IOAPIC; - - struct - { - UInt8 Source; - UInt8 IRQSource; - UInt32 GSI; - UInt16 Flags; - } IOAPIC_NMI; - - struct - { - UInt8 ProcessorID; - UInt16 Flags; - UInt8 LINT; - } LAPIC; - - struct - { - UInt16 Reserved; - UInt64 Address; - } LAPIC_ADDRESS_OVERRIDE; - - struct - { - UInt16 Reserved; - UInt32 x2APICID; - UInt32 Flags; - UInt32 AcpiID; - } LAPIC_ADDRESS; - }; - } List[]; // Records List - }; - - /////////////////////////////////////////////////////////////////////////////////////// - - /***********************************************************************************/ - /// @brief Send IPI command to APIC. - /// @param apicId programmable interrupt controller id. - /// @param vector vector interrupt. - /// @param targetAddress target APIC adress. - /// @return - /***********************************************************************************/ - - Void hal_send_start_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress) - { - Kernel::ke_dma_write(targetAddress, kAPIC_ICR_High, (apicId << 24)); - Kernel::ke_dma_write(targetAddress, kAPIC_ICR_Low, kAPIC_SIPI_Vector | vector); - } - - /***********************************************************************************/ - /// @brief Send end IPI for CPU. - /// @param apicId - /// @param vector - /// @param targetAddress - /// @return - /***********************************************************************************/ - Void hal_send_end_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress) - { - Kernel::ke_dma_write(targetAddress, kAPIC_ICR_High, (apicId << 24)); - Kernel::ke_dma_write(targetAddress, kAPIC_ICR_Low, kAPIC_EIPI_Vector | vector); - } - - STATIC struct PROCESS_CONTROL_BLOCK final - { - HAL::StackFramePtr f_Frame; - UInt8* f_Stack; - VoidPtr f_Image; - } kProcessBlocks[kSchedProcessLimitPerTeam] = {0}; - - EXTERN_C HAL::StackFramePtr mp_get_current_context(Int64 pid) - { - const auto process_index = pid % kSchedProcessLimitPerTeam; - return kProcessBlocks[process_index].f_Frame; - } - - EXTERN_C Bool mp_register_process(VoidPtr image, UInt8* stack_ptr, HAL::StackFramePtr stack_frame, ProcessID pid) - { - MUST_PASS(image && stack_ptr && stack_frame); - - const auto process_index = pid % kSchedProcessLimitPerTeam; - - kProcessBlocks[process_index].f_Frame = stack_frame; - kProcessBlocks[process_index].f_Stack = stack_ptr; - kProcessBlocks[process_index].f_Image = image; - - if (!mp_is_smp()) - { - /// TODO: Switch from process_index in hash list. - } - - return Yes; - } - - /***********************************************************************************/ - /// @brief Is the current config SMP aware? - /// @return True if YES, False if not. - /***********************************************************************************/ - Bool mp_is_smp(Void) noexcept - { - return kSMPAware; - } - - /***********************************************************************************/ - /// @brief Fetch and enable SMP scheduler. - /// @param vendor_ptr SMP containing structure. - /***********************************************************************************/ - Void mp_get_cores(VoidPtr vendor_ptr) noexcept - { - if (!vendor_ptr) - return; - - auto hw_and_pow_int = PowerFactoryInterface(vendor_ptr); - kRawMADT = hw_and_pow_int.Find(kApicSignature).Leak().Leak(); - - kMADTBlock = reinterpret_cast(kRawMADT); - kSMPAware = NO; - - if (kMADTBlock) - { - SizeT index = 0; - - // reset values. - - cSMPInterrupt = 0; - kSMPCount = 0; - - kcout << "SMP: Probing Local APICs...\r"; - - UIntPtr madt_address = kMADTBlock->Address; - - while (Yes) - { - if (kMADTBlock->List[index].Type > 9 || - kSMPCount > cSMPMax) - break; - - switch (kMADTBlock->List[index].Type) - { - case 0x00: { - kAPICLocales[index] = kMADTBlock->List[index].LAPIC.ProcessorID; - kcout << "SMP: APIC ID: " << number(kAPICLocales[index]) << endl; - ++kSMPCount; - break; - } - case 0x05: { - madt_address = kMADTBlock->List[index].LAPIC_ADDRESS_OVERRIDE.Address; - kcout << "SMP: APIC address: " << number(madt_address) << endl; - break; - } - } - - ++index; - } - - kcout << "SMP: number of cores: " << number(kSMPCount) << endl; - - // Kernel is now SMP aware. - // That means that the scheduler is now available (on MP Kernels) - - kSMPAware = true; - - /// TODO: Notify Boot AP that it must start. - } - } -} // namespace Kernel::HAL - -/////////////////////////////////////////////////////////////////////////////////////// diff --git a/dev/ZKAKit/HALKit/AMD64/HalCoreScheduler.cc b/dev/ZKAKit/HALKit/AMD64/HalCoreScheduler.cc new file mode 100644 index 00000000..9c1e91b4 --- /dev/null +++ b/dev/ZKAKit/HALKit/AMD64/HalCoreScheduler.cc @@ -0,0 +1,240 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Needed for SMP. +#include + +#define kApicSignature "APIC" + +#define kAPIC_ICR_Low 0x300 +#define kAPIC_ICR_High 0x310 +#define kAPIC_SIPI_Vector 0x00500 +#define kAPIC_EIPI_Vector 0x00400 + +#define kAPIC_BASE_MSR 0x1B +#define kAPIC_BASE_MSR_BSP 0x100 +#define kAPIC_BASE_MSR_ENABLE 0x800 + +#define cSMPMax (32U) + +/// @note: _hal_switch_context is internal + +/////////////////////////////////////////////////////////////////////////////////////// + +//! NOTE: fGSI stands 'Field Global System Interrupt' + +/////////////////////////////////////////////////////////////////////////////////////// + +namespace Kernel::HAL +{ + struct MADT_TABLE; + + EXTERN_C Void _hal_spin_core(Void); + + STATIC struct MADT_TABLE* kMADTBlock = nullptr; + STATIC Bool kSMPAware = false; + STATIC Int64 kSMPCount = 0; + + STATIC Int32 cSMPInterrupt = 0; + STATIC UInt64 kAPICLocales[cSMPMax] = {0}; + STATIC VoidPtr kRawMADT = nullptr; + + /// @brief Multiple APIC Descriptor Table. + struct MADT_TABLE final : public SDT + { + UInt32 Address; // Madt address + UInt8 Flags; // Madt flags + + struct + { + UInt8 Type; + UInt8 Len; + + union { + struct + { + UInt8 IoID; + UInt8 Resv; + UInt32 IoAddress; + UInt32 GISBase; + } IOAPIC; + + struct + { + UInt8 Source; + UInt8 IRQSource; + UInt32 GSI; + UInt16 Flags; + } IOAPIC_NMI; + + struct + { + UInt8 ProcessorID; + UInt16 Flags; + UInt8 LINT; + } LAPIC; + + struct + { + UInt16 Reserved; + UInt64 Address; + } LAPIC_ADDRESS_OVERRIDE; + + struct + { + UInt16 Reserved; + UInt32 x2APICID; + UInt32 Flags; + UInt32 AcpiID; + } LAPIC_ADDRESS; + }; + } List[]; // Records List + }; + + /////////////////////////////////////////////////////////////////////////////////////// + + /***********************************************************************************/ + /// @brief Send IPI command to APIC. + /// @param apicId programmable interrupt controller id. + /// @param vector vector interrupt. + /// @param targetAddress target APIC adress. + /// @return + /***********************************************************************************/ + + Void hal_send_start_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress) + { + Kernel::ke_dma_write(targetAddress, kAPIC_ICR_High, (apicId << 24)); + Kernel::ke_dma_write(targetAddress, kAPIC_ICR_Low, kAPIC_SIPI_Vector | vector); + } + + /***********************************************************************************/ + /// @brief Send end IPI for CPU. + /// @param apicId + /// @param vector + /// @param targetAddress + /// @return + /***********************************************************************************/ + Void hal_send_end_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress) + { + Kernel::ke_dma_write(targetAddress, kAPIC_ICR_High, (apicId << 24)); + Kernel::ke_dma_write(targetAddress, kAPIC_ICR_Low, kAPIC_EIPI_Vector | vector); + } + + STATIC struct PROCESS_CONTROL_BLOCK final + { + HAL::StackFramePtr f_Frame; + UInt8* f_Stack; + VoidPtr f_Image; + } kProcessBlocks[kSchedProcessLimitPerTeam] = {0}; + + EXTERN_C HAL::StackFramePtr mp_get_current_context(Int64 pid) + { + const auto process_index = pid % kSchedProcessLimitPerTeam; + return kProcessBlocks[process_index].f_Frame; + } + + EXTERN_C Bool mp_register_process(VoidPtr image, UInt8* stack_ptr, HAL::StackFramePtr stack_frame, ProcessID pid) + { + MUST_PASS(image && stack_ptr && stack_frame); + + const auto process_index = pid % kSchedProcessLimitPerTeam; + + kProcessBlocks[process_index].f_Frame = stack_frame; + kProcessBlocks[process_index].f_Stack = stack_ptr; + kProcessBlocks[process_index].f_Image = image; + + if (!mp_is_smp()) + { + /// TODO: Switch from process_index in hash list. + } + + return Yes; + } + + /***********************************************************************************/ + /// @brief Is the current config SMP aware? + /// @return True if YES, False if not. + /***********************************************************************************/ + Bool mp_is_smp(Void) noexcept + { + return kSMPAware; + } + + /***********************************************************************************/ + /// @brief Fetch and enable SMP scheduler. + /// @param vendor_ptr SMP containing structure. + /***********************************************************************************/ + Void mp_get_cores(VoidPtr vendor_ptr) noexcept + { + if (!vendor_ptr) + return; + + auto hw_and_pow_int = PowerFactoryInterface(vendor_ptr); + kRawMADT = hw_and_pow_int.Find(kApicSignature).Leak().Leak(); + + kMADTBlock = reinterpret_cast(kRawMADT); + kSMPAware = NO; + + if (kMADTBlock) + { + SizeT index = 0; + + // reset values. + + cSMPInterrupt = 0; + kSMPCount = 0; + + kcout << "SMP: Probing Local APICs...\r"; + + UIntPtr madt_address = kMADTBlock->Address; + + while (Yes) + { + if (kMADTBlock->List[index].Type > 9 || + kSMPCount > cSMPMax) + break; + + switch (kMADTBlock->List[index].Type) + { + case 0x00: { + kAPICLocales[index] = kMADTBlock->List[index].LAPIC.ProcessorID; + kcout << "SMP: APIC ID: " << number(kAPICLocales[index]) << endl; + ++kSMPCount; + break; + } + case 0x05: { + madt_address = kMADTBlock->List[index].LAPIC_ADDRESS_OVERRIDE.Address; + kcout << "SMP: APIC address: " << number(madt_address) << endl; + break; + } + } + + ++index; + } + + kcout << "SMP: number of cores: " << number(kSMPCount) << endl; + + // Kernel is now SMP aware. + // That means that the scheduler is now available (on MP Kernels) + + kSMPAware = true; + + /// TODO: Notify Boot AP that it must start. + } + } +} // namespace Kernel::HAL + +/////////////////////////////////////////////////////////////////////////////////////// diff --git a/dev/ZKAKit/HALKit/AMD64/ReadMe.md b/dev/ZKAKit/HALKit/AMD64/ReadMe.md index 7364b8e6..300a7a75 100644 --- a/dev/ZKAKit/HALKit/AMD64/ReadMe.md +++ b/dev/ZKAKit/HALKit/AMD64/ReadMe.md @@ -1,4 +1,8 @@ # AMD64 Hardware Abstraction Layer -- Supported CPU: AMD64 CPU. -- Supported Firmware: EDK 2 w/ ZKA's own extensions for EPM. +## Brief + +- Supported CPU: AMD64 BASED CPUs. +- Supported Firmware: EDK 2. + +###### Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. All rights reserved. \ No newline at end of file diff --git a/dev/ZKAKit/HALKit/AMD64/Storage/AHCI-DMA.cc b/dev/ZKAKit/HALKit/AMD64/Storage/AHCI-DMA.cc index fd543d4c..106a06b1 100644 --- a/dev/ZKAKit/HALKit/AMD64/Storage/AHCI-DMA.cc +++ b/dev/ZKAKit/HALKit/AMD64/Storage/AHCI-DMA.cc @@ -15,6 +15,9 @@ * */ +#include +#include + #include #include #include @@ -23,17 +26,17 @@ #ifdef __AHCI__ -#define AHCI_START_ADDRESS mib_cast(4) // 4M - #define HBA_ERR_TFE (1 << 30) #define HBA_PxCMD_ST 0x0001 #define HBA_PxCMD_FRE 0x0010 #define HBA_PxCMD_FR 0x4000 #define HBA_PxCMD_CR 0x8000 -#define AHCI_LBA_MODE (1 << 6) +#define kAhciStartAddress mib_cast(4) + +#define kAhciLBAMode (1 << 6) -#define kMaxAhciPoll (100000U) +#define kAhciMaxPoll (100000U) #define kCmdOrCtrlCmd 1 #define kCmdOrCtrlCtrl 0 @@ -49,7 +52,7 @@ enum }; STATIC Kernel::PCI::Device kAhciDevice; -STATIC HbaPort* kAhciPort = nullptr; +STATIC HbaPort* kAhciPort = nullptr; STATIC Kernel::Lba kCurrentDiskSectorCount = 0UL; Kernel::Void drv_calculate_disk_geometry() @@ -127,29 +130,29 @@ Kernel::Boolean drv_std_init(Kernel::UInt16& PortsImplemented) // do in-between - kAhciPort->Clb = AHCI_START_ADDRESS + (ahci_index << 10); + kAhciPort->Clb = kAhciStartAddress + (ahci_index << 10); kAhciPort->Clbu = 0; - rt_set_memory((void*)(kAhciPort->Clb), 0, 1024); + rt_set_memory((Kernel::VoidPtr)((Kernel::UIntPtr)kAhciPort->Clb + kAhciPort->Clbu), 0, 1024); // FIS offset: 32K+256*ahci_index // FIS entry size = 256 bytes per port - kAhciPort->Fb = AHCI_START_ADDRESS + (32 << 10) + (ahci_index << 8); + kAhciPort->Fb = kAhciStartAddress + (32 << 10) + (ahci_index << 8); kAhciPort->Fbu = 0; - rt_set_memory((void*)(kAhciPort->Fb), 0, 256); + rt_set_memory((Kernel::VoidPtr)((Kernel::UIntPtr)kAhciPort->Fb + kAhciPort->Fbu), 0, 256); // Command table offset: 40K + 8K*ahci_index // Command table size = 256*32 = 8K per port - HbaCmdHeader* cmdheader = (HbaCmdHeader*)(kAhciPort->Clb); + HbaCmdHeader* cmd_header = (HbaCmdHeader*)((Kernel::UIntPtr)kAhciPort->Clb + kAhciPort->Clbu); - for (int i = 0; i < 32; i++) + for (SizeT i = 0; i < 32; i++) { - cmdheader[i].Prdtl = 8; // 8 prdt entries per command table - // 256 bytes per command table, 64+16+48+16*8 + cmd_header[i].Prdtl = 8; // 8 prdt entries per command table + // 256 bytes per command table, 64+16+48+16*8 // Command table offset: 40K + 8K*ahci_index + cmdheader_index*256 - cmdheader[i].Ctba = AHCI_START_ADDRESS + (40 << 10) + (ahci_index << 13) + (i << 8); - cmdheader[i].Ctbau = 0; + cmd_header[i].Ctba = kAhciStartAddress + (40 << 10) + (ahci_index << 13) + (i << 8); + cmd_header[i].Ctbau = 0; - rt_set_memory((void*)cmdheader[i].Ctba, 0, 256); + rt_set_memory((VoidPtr)((Kernel::UIntPtr)cmd_header[i].Ctba + cmd_header[i].Ctbau), 0, 256); } // when it's starting @@ -194,8 +197,9 @@ Kernel::Void drv_std_write(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::Siz Kernel::Void drv_std_read(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::SizeT sector_cnt, Kernel::SizeT size_buffer) { - kAhciPort->Is = -1; - int port = 0; + kAhciPort->Is = -1; + + Kernel::SizeT port = 0; Kernel::UInt32 slots = (kAhciPort->Sact | kAhciPort->Ci); for (; port < slots; ++port) @@ -206,19 +210,19 @@ Kernel::Void drv_std_read(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::Size slots >>= 1; } - HbaCmdHeader* cmd_hdr = (HbaCmdHeader*)(kAhciPort->Clb); + HbaCmdHeader* cmd_hdr = (HbaCmdHeader*)((Kernel::UIntPtr)kAhciPort->Clb + kAhciPort->Clbu); cmd_hdr += port; cmd_hdr->Cfl = sizeof(FisRegH2D) / sizeof(Kernel::UInt32); cmd_hdr->Write = NO; cmd_hdr->Prdtl = (Kernel::UInt16)((sector_cnt - 1) >> 4) + 1; - HbaCmdTbl* cmd_tbl = (HbaCmdTbl*)(cmd_hdr->Ctba); + HbaCmdTbl* cmd_tbl = (HbaCmdTbl*)((Kernel::UIntPtr)cmd_hdr->Ctba + cmd_hdr->Ctbau); Kernel::rt_set_memory(cmd_tbl, 0, (cmd_hdr->Prdtl - 1) * sizeof(HbaPrdtEntry)); - int i = 0; + Kernel::SizeT i = 0; - for (int i = 0; i < cmd_hdr->Prdtl - 1; i++) + for (Kernel::SizeT i = 0; i < cmd_hdr->Prdtl - 1; i++) { cmd_tbl->PrdtEntries[i].Dba = (Kernel::UInt32)(Kernel::UInt64)buffer; cmd_tbl->PrdtEntries[i].Dbau = (Kernel::UInt32)((Kernel::UInt64)(buffer) >> 32); @@ -240,7 +244,7 @@ Kernel::Void drv_std_read(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::Size cmd_fis->Lba0 = (Kernel::UInt8)(Kernel::UInt32)lba & 0xFF; cmd_fis->Lba1 = (Kernel::UInt8)((Kernel::UInt32)lba >> 8); cmd_fis->Lba2 = (Kernel::UInt8)((Kernel::UInt32)lba >> 16); - cmd_fis->Device = AHCI_LBA_MODE; // LBA mode + cmd_fis->Device = kAhciLBAMode; // LBA mode cmd_fis->Lba3 = (Kernel::UInt8)((Kernel::UInt32)lba >> 24); cmd_fis->Lba4 = (Kernel::UInt8)(lba >> 32); @@ -252,13 +256,13 @@ Kernel::Void drv_std_read(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::Size Kernel::UInt64 spin = 0UL; // The below loop waits until the port is no longer busy before issuing a new command - while ((kAhciPort->Tfd & (kAhciSRBsy | kAhciSRDrq)) && spin < kMaxAhciPoll) + while ((kAhciPort->Tfd & (kAhciSRBsy | kAhciSRDrq)) && spin < kAhciMaxPoll) { spin++; } if (spin == 1000000) { - kcout << "Port is hung\r"; + kcout << "AHCI: Port is hung.\r"; return; } @@ -273,7 +277,11 @@ Kernel::Void drv_std_read(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::Size break; if (kAhciPort->Is & HBA_ERR_TFE) // Task file error { - kcout << ("Read disk error\r"); + using namespace Kernel; + kcout << ("AHCI: Read disk error.\r"); + + err_global_get() = kErrorUnrecoverableDisk; + return; } } @@ -281,8 +289,14 @@ Kernel::Void drv_std_read(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::Size // Check again for the last time. if (kAhciPort->Is & HBA_ERR_TFE) // task file error status { - kcout << ("Read disk error\r"); + using namespace Kernel; + + kcout << ("AHCI: Read disk error.\r"); *buffer = 0; + + err_global_get() = kErrorUnrecoverableDisk; + + return; } } diff --git a/dev/ZKAKit/KernelKit/HardwareThreadScheduler.h b/dev/ZKAKit/KernelKit/HardwareThreadScheduler.h index 44ea1042..ed2e931f 100644 --- a/dev/ZKAKit/KernelKit/HardwareThreadScheduler.h +++ b/dev/ZKAKit/KernelKit/HardwareThreadScheduler.h @@ -26,10 +26,10 @@ namespace Kernel enum ThreadKind { kAPSystemReserved, // System reserved thread, well user can't use it - kAPStandard, // user thread, cannot be used by Kernel - kAPFallback, // fallback thread, cannot be used by user if not clear or - // used by Kernel. - kAPBoot, // The core we booted from, the mama. + kAPStandard, // user thread, cannot be used by Kernel + kAPFallback, // fallback thread, cannot be used by user if not clear or + // used by Kernel. + kAPBoot, // The core we booted from, the mama. kInvalidAP, kAPCount, }; @@ -134,7 +134,7 @@ namespace Kernel private: Array fThreadList; - ThreadID fCurrentThread{0}; + ThreadID fCurrentThread{0}; }; /// @brief wakes up thread. diff --git a/dev/ZKAKit/KernelKit/Heap.h b/dev/ZKAKit/KernelKit/Heap.h index 999cb22d..300d30d5 100644 --- a/dev/ZKAKit/KernelKit/Heap.h +++ b/dev/ZKAKit/KernelKit/Heap.h @@ -66,7 +66,7 @@ namespace Kernel { if (*cls) { - ErrGlobal() = Kernel::kErrorInvalidData; + err_global_get() = Kernel::kErrorInvalidData; return; } diff --git a/dev/ZKAKit/KernelKit/LPC.h b/dev/ZKAKit/KernelKit/LPC.h index 0f7d84b3..e26a5f39 100644 --- a/dev/ZKAKit/KernelKit/LPC.h +++ b/dev/ZKAKit/KernelKit/LPC.h @@ -15,9 +15,9 @@ #define err_local_fail() (Kernel::UserProcessScheduler::The().GetCurrentProcess().Leak().GetLocalCode() != Kernel::kErrorSuccess) #define err_local_get() (Kernel::UserProcessScheduler::The().GetCurrentProcess().Leak().GetLocalCode()) -#define ErrGlobalIsOk() (Kernel::kErrorLocalNumber == Kernel::kErrorSuccess) -#define ErrGlobalFailed() (Kernel::kErrorLocalNumber != Kernel::kErrorSuccess) -#define ErrGlobal() (Kernel::kErrorLocalNumber) +#define err_global_ok() (Kernel::kErrorLocalNumber == Kernel::kErrorSuccess) +#define err_global_fail() (Kernel::kErrorLocalNumber != Kernel::kErrorSuccess) +#define err_global_get() (Kernel::kErrorLocalNumber) namespace Kernel { @@ -56,6 +56,7 @@ namespace Kernel inline constexpr HError kErrorSign = 60; inline constexpr HError kErrorInvalidCreds = 61; inline constexpr HError kErrorCDTrayBroken = 62; + inline constexpr HError kErrorUnrecoverableDisk = 63; inline constexpr HError kErrorUnimplemented = 0; /// @brief Raises a bug check stop code. diff --git a/dev/ZKAKit/KernelKit/LoaderInterface.h b/dev/ZKAKit/KernelKit/LoaderInterface.h index f5a34495..9323aec7 100644 --- a/dev/ZKAKit/KernelKit/LoaderInterface.h +++ b/dev/ZKAKit/KernelKit/LoaderInterface.h @@ -25,9 +25,9 @@ namespace Kernel public: virtual _Output ErrorOr GetBlob() = 0; - virtual _Output const Char* AsString() = 0; - virtual _Output const Char* MIME() = 0; - virtual _Output const Char* Path() = 0; + virtual _Output const Char* AsString() = 0; + virtual _Output const Char* MIME() = 0; + virtual _Output const Char* Path() = 0; virtual _Output ErrorOr FindStart() = 0; virtual _Output VoidPtr FindSymbol(_Input const Char* name, _Input Int32 kind) = 0; }; diff --git a/dev/ZKAKit/src/FS/NeFS.cc b/dev/ZKAKit/src/FS/NeFS.cc index 08125add..11c43c11 100644 --- a/dev/ZKAKit/src/FS/NeFS.cc +++ b/dev/ZKAKit/src/FS/NeFS.cc @@ -61,7 +61,7 @@ STATIC MountpointInterface kDiskMountpoint; /// @return the fork /***********************************************************************************/ _Output NFS_FORK_STRUCT* NeFSParser::CreateFork(_Input NFS_CATALOG_STRUCT* catalog, - _Input NFS_FORK_STRUCT& the_fork) + _Input NFS_FORK_STRUCT& the_fork) { if (catalog && the_fork.ForkName[0] != 0 && the_fork.DataSize <= kNeFSForkDataSz) @@ -170,8 +170,8 @@ _Output NFS_FORK_STRUCT* NeFSParser::CreateFork(_Input NFS_CATALOG_STRUCT* catal /// @return the fork. /***********************************************************************************/ _Output NFS_FORK_STRUCT* NeFSParser::FindFork(_Input NFS_CATALOG_STRUCT* catalog, - _Input const Char* name, - Boolean isDataFork) + _Input const Char* name, + Boolean isDataFork) { auto drv = kDiskMountpoint.A(); NFS_FORK_STRUCT* the_fork = nullptr; @@ -236,7 +236,7 @@ _Output NFS_CATALOG_STRUCT* NeFSParser::CreateCatalog(_Input const Char* name) /// @param kind the catalog kind. /// @return catalog pointer. /***********************************************************************************/ -_Output NFS_CATALOG_STRUCT* NeFSParser::CreateCatalog(_Input const Char* name, +_Output NFS_CATALOG_STRUCT* NeFSParser::CreateCatalog(_Input const Char* name, _Input const Int32& flags, _Input const Int32& kind) { @@ -936,7 +936,7 @@ Boolean NeFSParser::RemoveCatalog(_Input const Char* catalogName) VoidPtr NeFSParser::ReadCatalog(_Input _Output NFS_CATALOG_STRUCT* catalog, _Input Bool is_rsrc_fork, _Input SizeT dataSz, - _Input const Char* forkName) + _Input const Char* forkName) { if (!catalog) { diff --git a/dev/ZKAKit/src/Heap.cc b/dev/ZKAKit/src/Heap.cc index 9c46f312..4a780521 100644 --- a/dev/ZKAKit/src/Heap.cc +++ b/dev/ZKAKit/src/Heap.cc @@ -20,7 +20,7 @@ ------------------------------------------- */ //! @file Heap.cc -//! @brief The Kernel's heap manager serves as the main memory manager. +//! @brief This serves as the main memory manager. #define kKernelHeapMagic (0xD4D7D5) #define kKernelHeapAlignSz (__BIGGEST_ALIGNMENT__) @@ -95,7 +95,7 @@ namespace Kernel /// @brief Declare a new size for ptr_heap. /// @param ptr_heap the pointer. /// @return Newly allocated heap header. - VoidPtr mm_realloc_heap(VoidPtr ptr_heap, SizeT new_sz) + _Output VoidPtr mm_realloc_heap(VoidPtr ptr_heap, SizeT new_sz) { if (Detail::mm_check_heap_address(ptr_heap) == No) return nullptr; @@ -168,7 +168,7 @@ namespace Kernel heap_info_ptr->fPage = true; - kcout << "Created Page address: " << hex_number(reinterpret_cast(heap_info_ptr)) << endl; + kcout << "Created page address: " << hex_number(reinterpret_cast(heap_info_ptr)) << endl; return kErrorSuccess; } @@ -284,7 +284,7 @@ namespace Kernel /// @brief Protect the heap with a CRC value. /// @param heap_ptr HIB pointer. /// @return if it valid: point has crc now., otherwise fail. - Boolean mm_protect_heap(VoidPtr heap_ptr) + _Output Boolean mm_protect_heap(VoidPtr heap_ptr) { if (heap_ptr) { @@ -292,7 +292,7 @@ namespace Kernel reinterpret_cast( (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); - if (heap_ptr && heap_info_ptr->fPresent && kKernelHeapMagic == heap_info_ptr->fMagic) + if (heap_info_ptr && heap_info_ptr->fPresent && kKernelHeapMagic == heap_info_ptr->fMagic) { heap_info_ptr->fCRC32 = ke_calculate_crc32((Char*)heap_info_ptr->fHeapPtr, heap_info_ptr->fHeapSize); diff --git a/dev/ZKAKit/src/ThreadLocalStorage.cc b/dev/ZKAKit/src/ThreadLocalStorage.cc index 5fdf56aa..86bb88ae 100644 --- a/dev/ZKAKit/src/ThreadLocalStorage.cc +++ b/dev/ZKAKit/src/ThreadLocalStorage.cc @@ -32,8 +32,8 @@ Boolean tls_check_tib(THREAD_INFORMATION_BLOCK* tib_ptr) !tib_ptr->Record) return false; - ICodec encoder; - const Char* tib_as_bytes = encoder.AsBytes(tib_ptr); + ICodec encoder; + const Char* tib_as_bytes = encoder.AsBytes(tib_ptr); kcout << "Checking for a valid cookie inside the TIB...\r"; -- cgit v1.2.3