diff options
Diffstat (limited to 'dev/kernel/src')
49 files changed, 540 insertions, 645 deletions
diff --git a/dev/kernel/src/ACPIFactoryInterface.cc b/dev/kernel/src/ACPIFactoryInterface.cc index a76caff2..8cc11cad 100644 --- a/dev/kernel/src/ACPIFactoryInterface.cc +++ b/dev/kernel/src/ACPIFactoryInterface.cc @@ -5,22 +5,23 @@ ------------------------------------------- */ #include <ArchKit/ArchKit.h> -#include <KernelKit/MemoryMgr.h> -#include <NewKit/KString.h> +#include <KernelKit/HeapMgr.h> +#include <NeKit/KString.h> #include <modules/ACPI/ACPIFactoryInterface.h> namespace Kernel { +constexpr STATIC const auto kMinACPIVer = 1; + /// @brief Finds a descriptor table inside ACPI XSDT. ErrorOr<voidPtr> ACPIFactoryInterface::Find(const Char* signature) { MUST_PASS(this->fRsdp); - if (!signature) return ErrorOr<voidPtr>{-1}; - - if (*signature == 0) return ErrorOr<voidPtr>{-1}; + if (!signature) return ErrorOr<voidPtr>{-kErrorInvalidData}; + if (*signature == 0) return ErrorOr<voidPtr>{-kErrorInvalidData}; RSDP* rsp_ptr = reinterpret_cast<RSDP*>(this->fRsdp); - if (rsp_ptr->Revision <= 1) return ErrorOr<voidPtr>{-1}; + if (rsp_ptr->Revision < kMinACPIVer) return ErrorOr<voidPtr>{-kErrorInvalidData}; RSDT* xsdt = reinterpret_cast<RSDT*>(rsp_ptr->RsdtAddress); diff --git a/dev/kernel/src/Array.cc b/dev/kernel/src/Array.cc index 7cb1b156..36a8e744 100644 --- a/dev/kernel/src/Array.cc +++ b/dev/kernel/src/Array.cc @@ -4,4 +4,4 @@ ------------------------------------------- */ -#include <NewKit/Array.h> +#include <NeKit/Array.h> diff --git a/dev/kernel/src/ArrayList.cc b/dev/kernel/src/ArrayList.cc index 269dc47e..39291935 100644 --- a/dev/kernel/src/ArrayList.cc +++ b/dev/kernel/src/ArrayList.cc @@ -4,4 +4,4 @@ ------------------------------------------- */ -#include <NewKit/ArrayList.h> +#include <NeKit/ArrayList.h> diff --git a/dev/kernel/src/Atom.cc b/dev/kernel/src/Atom.cc index 8968a2c2..6e84d7d5 100644 --- a/dev/kernel/src/Atom.cc +++ b/dev/kernel/src/Atom.cc @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include <NewKit/Atom.h> +#include <NeKit/Atom.h> // @file Atom.cpp // @brief Atomic primitives diff --git a/dev/kernel/src/BitMapMgr.cc b/dev/kernel/src/BitMapMgr.cc index 7cbcf376..df678d41 100644 --- a/dev/kernel/src/BitMapMgr.cc +++ b/dev/kernel/src/BitMapMgr.cc @@ -11,7 +11,7 @@ #endif #include <ArchKit/ArchKit.h> -#include <NewKit/KernelPanic.h> +#include <NeKit/KernelPanic.h> #define kBitMapMagic (0x10210U) @@ -22,6 +22,8 @@ namespace Kernel { namespace HAL { namespace Detail { + STATIC SizeT kBitMapCursor = 0UL; + /***********************************************************************************/ /// \brief Proxy Interface to manage a bitmap allocator. /***********************************************************************************/ @@ -47,6 +49,8 @@ namespace HAL { UIntPtr* ptr_bit_set = reinterpret_cast<UIntPtr*>(page_ptr); + kBitMapCursor += ptr_bit_set[kBitMapSizeIdx]; + ptr_bit_set[kBitMapMagIdx] = kBitMapMagic; ptr_bit_set[kBitMapUsedIdx] = No; @@ -59,7 +63,6 @@ namespace HAL { UInt32 flags = kMMFlagsPresent; if (wr) flags |= kMMFlagsWr; - if (user) flags |= kMMFlagsUser; return flags; @@ -77,11 +80,18 @@ namespace HAL { auto FindBitMap(VoidPtr base_ptr, SizeT size, Bool wr, Bool user, SizeT pad) -> VoidPtr { if (!size) return nullptr; + if (kBitMapCursor > kKernelBitMpSize) { + err_global_get() = kErrorOutOfBitMapMemory; + + (Void)(kout << "Bitmap limit reached, can't allocate more bitmaps." << kendl); + return nullptr; + } + VoidPtr base = reinterpret_cast<VoidPtr>((UIntPtr) base_ptr); MUST_PASS(base); - static SizeT biggest = 0UL; + STATIC SizeT biggest = 0UL; while (YES) { UIntPtr* ptr_bit_set = reinterpret_cast<UIntPtr*>(base); @@ -99,6 +109,8 @@ namespace HAL { if (biggest < (size + pad)) biggest = size + pad; + kBitMapCursor += size + pad; + return (VoidPtr) ptr_bit_set; } } else if (ptr_bit_set[kBitMapMagIdx] != kBitMapMagic) { @@ -113,6 +125,8 @@ namespace HAL { if (biggest < (size + pad)) biggest = (size + pad); + kBitMapCursor += size + pad; + return (VoidPtr) ptr_bit_set; } @@ -136,7 +150,6 @@ namespace HAL { return; } -#ifdef __NE_VERBOSE_BITMAP__ (Void)(kout << "Magic: " << hex_number(ptr_bit_set[kBitMapMagIdx]) << kendl); (Void)(kout << "Is Allocated? " << (ptr_bit_set[kBitMapUsedIdx] ? "YES" : "NO") << kendl); (Void)(kout << "Size of BitMap (B): " << number(ptr_bit_set[kBitMapSizeIdx]) << kendl); @@ -149,7 +162,6 @@ namespace HAL { (Void)(kout << "Size of BitMap (TIB): " << number(TIB(ptr_bit_set[kBitMapSizeIdx])) << kendl); (Void)(kout << "BitMap Address: " << hex_number((UIntPtr) ptr_bit_set) << kendl); -#endif } }; } // namespace Detail @@ -169,10 +181,16 @@ namespace HAL { VoidPtr ptr_new = nullptr; Detail::IBitMapProxy bitmp; - NE_UNUSED(is_page); + if (is_page) return nullptr; ptr_new = bitmp.FindBitMap(kKernelBitMpStart, size, wr, user, pad); - return (UIntPtr*) ptr_new; + + if (!ptr_new) { + ke_panic(RUNTIME_CHECK_VIRTUAL_OUT_OF_MEM, "Out of memory bitmap"); + return nullptr; + } + + return ptr_new; } /***********************************************************************************/ diff --git a/dev/kernel/src/CodeMgr.cc b/dev/kernel/src/CodeMgr.cc index c4ac011c..6146682a 100644 --- a/dev/kernel/src/CodeMgr.cc +++ b/dev/kernel/src/CodeMgr.cc @@ -6,7 +6,7 @@ #include <KernelKit/CodeMgr.h> #include <KernelKit/ProcessScheduler.h> -#include <NewKit/Utils.h> +#include <NeKit/Utils.h> namespace Kernel { /***********************************************************************************/ diff --git a/dev/kernel/src/Crc32.cc b/dev/kernel/src/Crc32.cc index 332faa33..fddcb095 100644 --- a/dev/kernel/src/Crc32.cc +++ b/dev/kernel/src/Crc32.cc @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include <NewKit/Crc32.h> +#include <NeKit/Crc32.h> // @file CRC32.cpp // @brief Check sequence implementation. diff --git a/dev/kernel/src/CxxAbi-AMD64.cc b/dev/kernel/src/CxxAbi-AMD64.cc index 452f23cd..5b0f7c3c 100644 --- a/dev/kernel/src/CxxAbi-AMD64.cc +++ b/dev/kernel/src/CxxAbi-AMD64.cc @@ -8,7 +8,8 @@ #include <KernelKit/DebugOutput.h> #include <KernelKit/KPC.h> -#include <NewKit/CxxAbi.h> +#include <KernelKit/UserProcessScheduler.h> +#include <NeKit/CxxAbi.h> atexit_func_entry_t __atexit_funcs[kAtExitMacDestructors]; @@ -23,10 +24,7 @@ EXTERN_C Kernel::Void __cxa_pure_virtual(void* self) { (Kernel::Void)(Kernel::kout << ", has unimplemented virtual functions.\r"); } -EXTERN_C void ___chkstk_ms(void) { - (Kernel::Void)(Kernel::kout << "Stack smashing detected!\r"); - dbg_break_point(); -} +EXTERN_C void ___chkstk_ms(void) {} EXTERN_C int atexit(void (*f)()) { if (__atexit_func_count >= kAtExitMacDestructors) return 1; diff --git a/dev/kernel/src/CxxAbi-ARM64.cc b/dev/kernel/src/CxxAbi-ARM64.cc index 1605692b..02f3dbcf 100644 --- a/dev/kernel/src/CxxAbi-ARM64.cc +++ b/dev/kernel/src/CxxAbi-ARM64.cc @@ -8,7 +8,7 @@ #include <KernelKit/DebugOutput.h> #include <KernelKit/KPC.h> -#include <NewKit/CxxAbi.h> +#include <NeKit/CxxAbi.h> atexit_func_entry_t __atexit_funcs[kAtExitMacDestructors]; diff --git a/dev/kernel/src/Defines.cc b/dev/kernel/src/Defines.cc index 7f6e571d..a06b4dd2 100644 --- a/dev/kernel/src/Defines.cc +++ b/dev/kernel/src/Defines.cc @@ -4,4 +4,4 @@ ------------------------------------------- */ -#include <NewKit/Defines.h> +#include <NeKit/Defines.h> diff --git a/dev/kernel/src/DeviceMgr.cc b/dev/kernel/src/DeviceMgr.cc index c137552e..b12470a9 100644 --- a/dev/kernel/src/DeviceMgr.cc +++ b/dev/kernel/src/DeviceMgr.cc @@ -5,3 +5,5 @@ ------------------------------------------- */ #include <KernelKit/DeviceMgr.h> + +namespace Kernel {} // namespace Kernel diff --git a/dev/kernel/src/DriveMgr.cc b/dev/kernel/src/DriveMgr.cc index 449640f9..b7c181c8 100644 --- a/dev/kernel/src/DriveMgr.cc +++ b/dev/kernel/src/DriveMgr.cc @@ -8,7 +8,7 @@ #include <FirmwareKit/VEPM.h> #include <KernelKit/DebugOutput.h> #include <KernelKit/DriveMgr.h> -#include <NewKit/Utils.h> +#include <NeKit/Utils.h> #include <modules/AHCI/AHCI.h> #include <modules/ATA/ATA.h> #include <modules/NVME/NVME.h> @@ -146,7 +146,7 @@ DriveTrait io_construct_blank_drive() noexcept { trait.fInit = io_drv_unimplemented; trait.fProtocol = io_drv_kind; - kout << "Construct: " << trait.fName << "\r"; + kout << "DriveMgr: Construct: " << trait.fName << "\r"; return trait; } @@ -170,7 +170,7 @@ namespace Detail { trait.fPacket.fPacketReadOnly = NO; trait.fKind = kMassStorageDrive | kEPMDrive; - kout << "Disk is EPM formatted.\r"; + kout << "DriveMgr: Disk is EPM formatted.\r"; trait.fSectorSz = block_struct.SectorSz; trait.fLbaEnd = block_struct.LbaEnd; @@ -191,13 +191,13 @@ namespace Detail { trait.fPacket.fPacketReadOnly = NO; trait.fKind = kMassStorageDrive | kGPTDrive; - kout << "Disk is GPT formatted.\r"; + kout << "DriveMgr: Disk is GPT formatted.\r"; trait.fSectorSz = gpt_struct.SizeOfEntries; trait.fLbaEnd = gpt_struct.LastGPTEntry; trait.fLbaStart = gpt_struct.FirstGPTEntry; } else { - kout << "Disk is unformatted.\r"; + kout << "DriveMgr: Disk is unformatted.\r"; trait.fPacket.fPacketReadOnly = YES; trait.fKind = kMassStorageDrive | kUnformattedDrive | kReadOnlyDrive; @@ -233,7 +233,7 @@ DriveTrait io_construct_main_drive() noexcept { trait.fInit = io_drv_init; trait.fProtocol = io_drv_kind; - kout << "Detecting partition scheme of: " << trait.fName << ".\r"; + kout << "DriveMgr: Detecting partition scheme of: " << trait.fName << ".\r"; Detail::io_detect_drive(trait); diff --git a/dev/kernel/src/ErrorOr.cc b/dev/kernel/src/ErrorOr.cc index 69668b2f..a872164c 100644 --- a/dev/kernel/src/ErrorOr.cc +++ b/dev/kernel/src/ErrorOr.cc @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include <NewKit/ErrorOr.h> +#include <NeKit/ErrorOr.h> /***********************************************************************************/ /// @file ErrorOr.cc /// diff --git a/dev/kernel/src/FS/Ext2+FileMgr.cc b/dev/kernel/src/FS/Ext2+FileMgr.cc index a55d917a..810e7e44 100644 --- a/dev/kernel/src/FS/Ext2+FileMgr.cc +++ b/dev/kernel/src/FS/Ext2+FileMgr.cc @@ -8,7 +8,7 @@ #ifdef __FSKIT_INCLUDES_EXT2__ #include <KernelKit/FileMgr.h> -#include <KernelKit/MemoryMgr.h> +#include <KernelKit/HeapMgr.h> #endif // ifdef __FSKIT_INCLUDES_EXT2__ #endif // ifndef __NE_MINIMAL_OS__ diff --git a/dev/kernel/src/FS/Ext2+FileSystemParser.cc b/dev/kernel/src/FS/Ext2+FileSystemParser.cc index b6d04f46..80449ed9 100644 --- a/dev/kernel/src/FS/Ext2+FileSystemParser.cc +++ b/dev/kernel/src/FS/Ext2+FileSystemParser.cc @@ -10,11 +10,11 @@ #include <FirmwareKit/EPM.h> #include <KernelKit/KPC.h> #include <KernelKit/ProcessScheduler.h> -#include <KernelKit/User.h> -#include <NewKit/Crc32.h> -#include <NewKit/KString.h> -#include <NewKit/KernelPanic.h> -#include <NewKit/Utils.h> +#include <KernelKit/UserMgr.h> +#include <NeKit/Crc32.h> +#include <NeKit/KString.h> +#include <NeKit/KernelPanic.h> +#include <NeKit/Utils.h> #include <modules/AHCI/AHCI.h> #include <modules/ATA/ATA.h> diff --git a/dev/kernel/src/FS/HeFS+FileMgr.cc b/dev/kernel/src/FS/HeFS+FileMgr.cc index e0b92a8d..e4985a3b 100644 --- a/dev/kernel/src/FS/HeFS+FileMgr.cc +++ b/dev/kernel/src/FS/HeFS+FileMgr.cc @@ -8,7 +8,7 @@ #ifdef __FSKIT_INCLUDES_HEFS__ #include <KernelKit/FileMgr.h> -#include <KernelKit/MemoryMgr.h> +#include <KernelKit/HeapMgr.h> #endif // ifdef __FSKIT_INCLUDES_HEFS__ #endif // ifndef __NE_MINIMAL_OS__ diff --git a/dev/kernel/src/FS/HeFS+FileSystemParser.cc b/dev/kernel/src/FS/HeFS+FileSystemParser.cc index a285593e..893f43ef 100644 --- a/dev/kernel/src/FS/HeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/HeFS+FileSystemParser.cc @@ -11,11 +11,11 @@ #include <FirmwareKit/GPT.h> #include <KernelKit/KPC.h> #include <KernelKit/ProcessScheduler.h> -#include <KernelKit/User.h> -#include <NewKit/Crc32.h> -#include <NewKit/KString.h> -#include <NewKit/KernelPanic.h> -#include <NewKit/Utils.h> +#include <KernelKit/UserMgr.h> +#include <NeKit/Crc32.h> +#include <NeKit/KString.h> +#include <NeKit/KernelPanic.h> +#include <NeKit/Utils.h> #include <modules/AHCI/AHCI.h> #include <modules/ATA/ATA.h> @@ -36,14 +36,14 @@ namespace Detail { /***********************************************************************************/ /// @brief Get the index node of a file or directory. - /// @param root The root node of the filesystem. + /// @param boot The boot node of the filesystem. /// @param mnt The mnt to read from. /// @param dir_name The name of the directory. /// @param file_name The name of the file. /// @param kind The kind of the file (regular, directory, block, character, FIFO, socket, symbolic /// link, unknown). /***********************************************************************************/ - STATIC ATTRIBUTE(unused) _Output HEFS_INDEX_NODE* hefsi_fetch_in(HEFS_BOOT_NODE* root, + STATIC ATTRIBUTE(unused) _Output HEFS_INDEX_NODE* hefsi_fetch_in(HEFS_BOOT_NODE* boot, DriveTrait* mnt, const Utf8Char* dir_name, const Utf8Char* file_name, @@ -51,31 +51,31 @@ namespace Detail { /***********************************************************************************/ /// @brief Allocate a new index node-> - /// @param root The root node of the filesystem. + /// @param boot The boot node of the filesystem. /// @param mnt The mnt to read/write from. /// @param dir_name The name of the parent directory. /// @return Status, see err_global_get(). /***********************************************************************************/ STATIC ATTRIBUTE(unused) _Output BOOL - hefsi_update_in_status(HEFS_BOOT_NODE* root, DriveTrait* mnt, const Utf8Char* dir_name, + hefsi_update_in_status(HEFS_BOOT_NODE* boot, DriveTrait* mnt, const Utf8Char* dir_name, HEFS_INDEX_NODE* node, const BOOL create_or_delete); /***********************************************************************************/ /// @brief Balance RB-Tree of the filesystem. - /// @param root The root node of the filesystem. + /// @param boot The boot node of the filesystem. /// @param mnt The mnt to read/write from. /// @return Status, see err_global_get(). /***********************************************************************************/ - STATIC ATTRIBUTE(unused) _Output BOOL hefsi_balance_ind(HEFS_BOOT_NODE* root, DriveTrait* mnt); + STATIC ATTRIBUTE(unused) _Output BOOL hefsi_balance_ind(HEFS_BOOT_NODE* boot, DriveTrait* mnt); /// @brief Alllocate IND from boot node. - /// @param root The root node of the filesystem. + /// @param boot The boot node of the filesystem. /// @param mnt The mnt to read from. /// @param dir_name The name of the directory. /// @param dir_name The parent of the directory. /// @param flags Directory flags. /// @param delete_or_create Delete or create directory. - STATIC _Output BOOL hefsi_update_ind_status(HEFS_BOOT_NODE* root, DriveTrait* mnt, + STATIC _Output BOOL hefsi_update_ind_status(HEFS_BOOT_NODE* boot, DriveTrait* mnt, const Utf8Char* dir_name, UInt16 flags, const BOOL delete_or_create); @@ -251,24 +251,24 @@ namespace Detail { } /// @brief Alllocate IND from boot node. - /// @param root The root node of the filesystem. + /// @param boot The boot node of the filesystem. /// @param mnt The mnt to read from. /// @param dir_name The name of the directory. /// @param dir_name The parent of the directory. /// @param flags Directory flags. /// @param delete_or_create Delete or create directory. - STATIC _Output BOOL hefsi_update_ind_status(HEFS_BOOT_NODE* root, DriveTrait* mnt, + STATIC _Output BOOL hefsi_update_ind_status(HEFS_BOOT_NODE* boot, DriveTrait* mnt, const Utf8Char* dir_name, UInt16 flags, const BOOL delete_or_create) { if (mnt) { HEFS_INDEX_NODE_DIRECTORY* tmpdir = - (HEFS_INDEX_NODE_DIRECTORY*) mm_new_heap(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); + (HEFS_INDEX_NODE_DIRECTORY*) mm_alloc_ptr(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); - auto start = root->fStartIND; + auto start = boot->fStartIND; auto prev_location = start; auto parent_location = 0UL; - MUST_PASS(root->fStartIND > mnt->fLbaStart); + MUST_PASS(boot->fStartIND > mnt->fLbaStart); while (YES) { auto prev_start = start; @@ -294,7 +294,7 @@ namespace Detail { if (expr) { HEFS_INDEX_NODE_DIRECTORY* dirent = - (HEFS_INDEX_NODE_DIRECTORY*) mm_new_heap(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); + (HEFS_INDEX_NODE_DIRECTORY*) mm_alloc_ptr(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); rt_set_memory(dirent, 0, sizeof(HEFS_INDEX_NODE_DIRECTORY)); @@ -323,7 +323,7 @@ namespace Detail { if (tmpend->fChecksum != ke_calculate_crc32((Char*) tmpend, sizeof(HEFS_INDEX_NODE_DIRECTORY))) - ke_panic(RUNTIME_CHECK_FILESYSTEM, "Bad CRC32"); + ke_panic(RUNTIME_CHECK_FILESYSTEM, "Bad CRC32 value, aborting."); if (delete_or_create) --tmpend->fEntryCount; @@ -349,7 +349,7 @@ namespace Detail { break; } - hefsi_traverse_tree(tmpend, mnt, root->fStartIND, child_first); + hefsi_traverse_tree(tmpend, mnt, boot->fStartIND, child_first); } } @@ -364,11 +364,11 @@ namespace Detail { } if (dirent->fPrev == 0) { - dirent->fPrev = root->fStartIND; + dirent->fPrev = boot->fStartIND; } if (dirent->fParent == 0) { - dirent->fParent = root->fStartIND; + dirent->fParent = boot->fStartIND; } if (tmpdir->fChild == 0) { @@ -389,13 +389,13 @@ namespace Detail { } child += sizeof(HEFS_INDEX_NODE_DIRECTORY); - if (child > root->fEndIND) break; + if (child > boot->fEndIND) break; } dirent->fColor = kHeFSRed; dirent->fChild = child; - if (child > root->fEndIND) dirent->fChild = root->fStartIND; + if (child > boot->fEndIND) dirent->fChild = boot->fStartIND; } for (SizeT index = 0UL; index < kHeFSSliceCount; ++index) { @@ -412,19 +412,19 @@ namespace Detail { err_global_get() = kErrorSuccess; - mm_delete_heap(dirent); - mm_delete_heap(tmpdir); + mm_free_ptr(dirent); + mm_free_ptr(tmpdir); if (!delete_or_create) - ++root->fINDCount; + ++boot->fINDCount; else - --root->fINDCount; + --boot->fINDCount; - root->fChecksum = ke_calculate_crc32((Char*) root, sizeof(HEFS_BOOT_NODE)); + boot->fChecksum = ke_calculate_crc32((Char*) boot, sizeof(HEFS_BOOT_NODE)); mnt->fPacket.fPacketLba = mnt->fLbaStart; mnt->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); - mnt->fPacket.fPacketContent = root; + mnt->fPacket.fPacketContent = boot; mnt->fOutput(mnt->fPacket); @@ -433,12 +433,12 @@ namespace Detail { prev_location = start; - hefsi_traverse_tree(tmpdir, mnt, root->fStartIND, start); - if (start > root->fEndIND || start == 0) break; + hefsi_traverse_tree(tmpdir, mnt, boot->fStartIND, start); + if (start > boot->fEndIND || start == 0) break; } err_global_get() = kErrorDisk; - mm_delete_heap(tmpdir); + mm_free_ptr(tmpdir); return NO; } @@ -448,26 +448,26 @@ namespace Detail { } /// @brief Get the index node of a file or directory. - /// @param root The root node of the filesystem. + /// @param boot The boot node of the filesystem. /// @param mnt The mnt to read from. /// @param dir_name The name of the directory. /// @param file_name The name of the file. /// @param kind The kind of the file (regular, directory, block, character, FIFO, socket, symbolic /// link, unknown). - STATIC ATTRIBUTE(unused) _Output HEFS_INDEX_NODE* hefsi_fetch_in(HEFS_BOOT_NODE* root, + STATIC ATTRIBUTE(unused) _Output HEFS_INDEX_NODE* hefsi_fetch_in(HEFS_BOOT_NODE* boot, DriveTrait* mnt, const Utf8Char* dir_name, const Utf8Char* file_name, UInt8 kind) { if (mnt) { - if (root->fStartIND > root->fEndIND) return nullptr; - if (root->fStartIN > root->fEndIN) return nullptr; + if (boot->fStartIND > boot->fEndIND) return nullptr; + if (boot->fStartIN > boot->fEndIN) return nullptr; - auto start = root->fStartIND; + auto start = boot->fStartIND; HEFS_INDEX_NODE* node = new HEFS_INDEX_NODE(); HEFS_INDEX_NODE_DIRECTORY* dir = - (HEFS_INDEX_NODE_DIRECTORY*) mm_new_heap(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); + (HEFS_INDEX_NODE_DIRECTORY*) mm_alloc_ptr(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); while (YES) { if (err_global_get() == kErrorDiskIsCorrupted) { @@ -511,8 +511,8 @@ namespace Detail { } } - hefsi_traverse_tree(dir, mnt, root->fStartIND, start); - if (start == root->fStartIND || start == root->fStartIND) break; + hefsi_traverse_tree(dir, mnt, boot->fStartIND, start); + if (start == boot->fStartIND || start == boot->fStartIND) break; } delete node; @@ -530,20 +530,20 @@ namespace Detail { } STATIC ATTRIBUTE(unused) _Output BOOL - hefsi_update_in_status(HEFS_BOOT_NODE* root, DriveTrait* mnt, const Utf8Char* dir_name, + hefsi_update_in_status(HEFS_BOOT_NODE* boot, DriveTrait* mnt, const Utf8Char* dir_name, HEFS_INDEX_NODE* node, BOOL delete_or_create) { - if (!root || !mnt) return NO; + if (!boot || !mnt) return NO; - auto start = root->fStartIND; + auto start = boot->fStartIND; - if (start > root->fEndIND) return NO; - if (root->fStartIN > root->fEndIN) return NO; + if (start > boot->fEndIND) return NO; + if (boot->fStartIN > boot->fEndIN) return NO; ; - if (root->fStartBlock > root->fEndBlock) return NO; + if (boot->fStartBlock > boot->fEndBlock) return NO; if (mnt) { HEFS_INDEX_NODE_DIRECTORY* dir = - (HEFS_INDEX_NODE_DIRECTORY*) mm_new_heap(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); + (HEFS_INDEX_NODE_DIRECTORY*) mm_alloc_ptr(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); auto hash_file = node->fHashPath; @@ -560,7 +560,7 @@ namespace Detail { if (hefsi_hash_64(dir_name) == dir->fHashPath) { for (SizeT inode_index = 0UL; inode_index < kHeFSSliceCount; ++inode_index) { if (dir->fINSlices[inode_index] == 0 && !delete_or_create) { - dir->fINSlices[inode_index] = root->fStartIN; + dir->fINSlices[inode_index] = boot->fStartIN; ++dir->fEntryCount; @@ -574,8 +574,8 @@ namespace Detail { auto lba = dir->fINSlices[inode_index]; - node->fOffsetSliceLow = (UInt32) (root->fStartBlock); - node->fOffsetSliceHigh = (UInt32) (root->fStartBlock >> 32); + node->fOffsetSliceLow = (UInt32) (boot->fStartBlock); + node->fOffsetSliceHigh = (UInt32) (boot->fStartBlock >> 32); node->fChecksum = ke_calculate_crc32((Char*) node, sizeof(HEFS_INDEX_NODE)); @@ -585,18 +585,18 @@ namespace Detail { mnt->fOutput(mnt->fPacket); - root->fStartIN += sizeof(HEFS_INDEX_NODE); - root->fStartBlock += kHeFSBlockLen; + boot->fStartIN += sizeof(HEFS_INDEX_NODE); + boot->fStartBlock += kHeFSBlockLen; - root->fChecksum = ke_calculate_crc32((Char*) root, sizeof(HEFS_BOOT_NODE)); + boot->fChecksum = ke_calculate_crc32((Char*) boot, sizeof(HEFS_BOOT_NODE)); mnt->fPacket.fPacketLba = mnt->fLbaStart; mnt->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); - mnt->fPacket.fPacketContent = root; + mnt->fPacket.fPacketContent = boot; mnt->fOutput(mnt->fPacket); - mm_delete_heap(dir); + mm_free_ptr(dir); return YES; } else if (dir->fINSlices[inode_index] != 0 && delete_or_create) { @@ -617,14 +617,14 @@ namespace Detail { node->fOffsetSliceLow = 0; node->fOffsetSliceHigh = 0; - root->fStartIN -= sizeof(HEFS_INDEX_NODE); - root->fStartBlock -= kHeFSBlockLen; + boot->fStartIN -= sizeof(HEFS_INDEX_NODE); + boot->fStartBlock -= kHeFSBlockLen; - root->fChecksum = ke_calculate_crc32((Char*) root, sizeof(HEFS_BOOT_NODE)); + boot->fChecksum = ke_calculate_crc32((Char*) boot, sizeof(HEFS_BOOT_NODE)); mnt->fPacket.fPacketLba = mnt->fLbaStart; mnt->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); - mnt->fPacket.fPacketContent = root; + mnt->fPacket.fPacketContent = boot; mnt->fOutput(mnt->fPacket); @@ -646,18 +646,18 @@ namespace Detail { mnt->fOutput(mnt->fPacket); - mm_delete_heap(dir); + mm_free_ptr(dir); return YES; } } } - hefsi_traverse_tree(dir, mnt, root->fStartIND, start); - if (start > root->fEndIND || start == 0) break; + hefsi_traverse_tree(dir, mnt, boot->fStartIND, start); + if (start > boot->fEndIND || start == 0) break; } - mm_delete_heap(dir); + mm_free_ptr(dir); err_global_get() = kErrorFileNotFound; return NO; } @@ -667,18 +667,18 @@ namespace Detail { } /// @brief Balance RB-Tree of the filesystem. - /// @param root The root node of the filesystem. + /// @param boot The boot node of the filesystem. /// @param mnt The mnt to read/write from. /// @return Status, see err_global_get(). - STATIC ATTRIBUTE(unused) _Output BOOL hefsi_balance_ind(HEFS_BOOT_NODE* root, DriveTrait* mnt) { + STATIC ATTRIBUTE(unused) _Output BOOL hefsi_balance_ind(HEFS_BOOT_NODE* boot, DriveTrait* mnt) { if (mnt) { HEFS_INDEX_NODE_DIRECTORY* dir = (HEFS_INDEX_NODE_DIRECTORY*) RTL_ALLOCA(sizeof(HEFS_INDEX_NODE_DIRECTORY)); - auto start = root->fStartIND; + auto start = boot->fStartIND; while (YES) { - if (start == 0UL || start > root->fEndIND) break; + if (start == 0UL || start > boot->fEndIND) break; mnt->fPacket.fPacketLba = start; mnt->fPacket.fPacketSize = sizeof(HEFS_INDEX_NODE_DIRECTORY); @@ -692,7 +692,7 @@ namespace Detail { return NO; } - if (start == root->fStartIND) { + if (start == boot->fStartIND) { dir->fColor = kHeFSBlack; mnt->fPacket.fPacketLba = start; @@ -725,7 +725,7 @@ namespace Detail { mnt->fOutput(mnt->fPacket); } - hefsi_traverse_tree(dir, mnt, root->fStartIND, start); + hefsi_traverse_tree(dir, mnt, boot->fStartIND, start); } err_global_get() = kErrorSuccess; @@ -742,7 +742,7 @@ namespace Detail { /// real-time. /// @note This is certainly take longer to format a disk with it, but worth-it in the long run. -namespace Kernel::HeFS { +namespace Kernel { /// @brief Make a EPM+HeFS mnt out of the disk. /// @param mnt The mnt to write on. /// @return If it was sucessful, see err_local_get(). @@ -758,16 +758,15 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c } if (drv_std_get_size() < kHeFSMinimumDiskSize) { - kout << "HeFS requires at least 128 GiB." << kendl; - err_global_get() = kErrorDisk; - return NO; + (Void)(kout << "HeFS recommends at least 128 GiB of free space." << kendl); + (Void)(kout << "The OS will still try to format a HeFS disk here." << kendl); } - HEFS_BOOT_NODE* root = (HEFS_BOOT_NODE*) RTL_ALLOCA(sizeof(HEFS_BOOT_NODE)); + HEFS_BOOT_NODE* boot = (HEFS_BOOT_NODE*) RTL_ALLOCA(sizeof(HEFS_BOOT_NODE)); mnt->fPacket.fPacketLba = mnt->fLbaStart; mnt->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); - mnt->fPacket.fPacketContent = root; + mnt->fPacket.fPacketContent = boot; mnt->fInput(mnt->fPacket); @@ -779,9 +778,9 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c // Check if the disk is already formatted. - if (KStringBuilder::Equals(root->fMagic, kHeFSMagic) && root->fVersion == kHeFSVersion) { - if (ke_calculate_crc32((Char*) root, sizeof(HEFS_BOOT_NODE)) != root->fChecksum && - root->fChecksum > 0) { + if (KStringBuilder::Equals(boot->fMagic, kHeFSMagic) && boot->fVersion == kHeFSVersion) { + if (ke_calculate_crc32((Char*) boot, sizeof(HEFS_BOOT_NODE)) != boot->fChecksum && + boot->fChecksum > 0) { err_global_get() = kErrorDiskIsCorrupted; return NO; } @@ -790,8 +789,8 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c return YES; } - if (ke_calculate_crc32((Char*) root, sizeof(HEFS_BOOT_NODE)) != root->fChecksum && - root->fChecksum > 0) { + if (ke_calculate_crc32((Char*) boot, sizeof(HEFS_BOOT_NODE)) != boot->fChecksum && + boot->fChecksum > 0) { err_global_get() = kErrorDiskIsCorrupted; return NO; } @@ -799,8 +798,8 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c rt_copy_memory((VoidPtr) "fs/hefs-packet", mnt->fPacket.fPacketMime, rt_string_len("fs/hefs-packet")); - urt_copy_memory((VoidPtr) vol_name, root->fVolName, urt_string_len(vol_name) + 1); - rt_copy_memory((VoidPtr) kHeFSMagic, root->fMagic, kHeFSMagicLen - 1); + urt_copy_memory((VoidPtr) vol_name, boot->fVolName, urt_string_len(vol_name) + 1); + rt_copy_memory((VoidPtr) kHeFSMagic, boot->fMagic, kHeFSMagicLen - 1); if (mnt->fLbaStart > mnt->fLbaEnd) { err_global_get() = kErrorDiskIsCorrupted; @@ -808,66 +807,66 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c return NO; } - root->fBadSectors = 0; + boot->fBadSectors = 0; - root->fSectorCount = drv_std_get_sector_count(); - root->fSectorSize = mnt->fSectorSz; + boot->fSectorCount = drv_std_get_sector_count(); + boot->fSectorSize = mnt->fSectorSz; - MUST_PASS(root->fSectorSize); + MUST_PASS(boot->fSectorSize); /// @note all HeFS strucutres are equal to 512, so here it's fine, unless fSectoSize is 2048. - const SizeT max_lba = (drv_std_get_size()) / root->fSectorSize; + const SizeT max_lba = (drv_std_get_size()) / boot->fSectorSize; const SizeT dir_max = max_lba / 300; // 5% for directory inodes const SizeT inode_max = max_lba / 400; // 5% for inodes - root->fStartIND = mnt->fLbaStart + kHeFSINDStartOffset; - root->fEndIND = root->fStartIND + dir_max; + boot->fStartIND = mnt->fLbaStart + kHeFSINDStartOffset; + boot->fEndIND = boot->fStartIND + dir_max; - root->fStartIN = root->fEndIND; - root->fEndIN = root->fStartIN + inode_max; + boot->fStartIN = boot->fEndIND; + boot->fEndIN = boot->fStartIN + inode_max; - root->fStartBlock = root->fEndIN; - root->fEndBlock = drv_std_get_size(); + boot->fStartBlock = boot->fEndIN; + boot->fEndBlock = drv_std_get_size(); - root->fINDCount = 0; + boot->fINDCount = 0; - root->fDiskSize = drv_std_get_size(); - root->fDiskStatus = kHeFSStatusUnlocked; + boot->fDiskSize = drv_std_get_size(); + boot->fDiskStatus = kHeFSStatusUnlocked; - root->fDiskFlags = flags; + boot->fDiskFlags = flags; if (mnt->fKind & kMassStorageDrive) { - root->fDiskKind = kHeFSMassStorageDevice; + boot->fDiskKind = kHeFSMassStorageDevice; } else if (mnt->fKind & kHeFSOpticalDrive) { - root->fDiskKind = kHeFSOpticalDrive; + boot->fDiskKind = kHeFSOpticalDrive; } else { - root->fDiskKind = kHeFSUnknown; + boot->fDiskKind = kHeFSUnknown; } - root->fVersion = kHeFSVersion; + boot->fVersion = kHeFSVersion; - root->fVID = kHeFSInvalidVID; + boot->fVID = kHeFSInvalidVID; - root->fChecksum = ke_calculate_crc32((Char*) root, sizeof(HEFS_BOOT_NODE)); + boot->fChecksum = ke_calculate_crc32((Char*) boot, sizeof(HEFS_BOOT_NODE)); mnt->fPacket.fPacketLba = mnt->fLbaStart; mnt->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); - mnt->fPacket.fPacketContent = root; + mnt->fPacket.fPacketContent = boot; mnt->fOutput(mnt->fPacket); (Void)(kout << "Protocol: " << mnt->fProtocol() << kendl); - (Void)(kout8 << u8"Volume Name: " << root->fVolName << kendl8); - (Void)(kout << "Start IND: " << hex_number(root->fStartIND) << kendl); - (Void)(kout << "End IND: " << hex_number(root->fEndIND) << kendl); - (Void)(kout << "Start IN: " << hex_number(root->fStartIN) << kendl); - (Void)(kout << "End IN: " << hex_number(root->fEndIN) << kendl); - (Void)(kout << "Start Block: " << hex_number(root->fStartBlock) << kendl); - (Void)(kout << "End Block: " << hex_number(root->fEndBlock) << kendl); - (Void)(kout << "Number of IND: " << hex_number(root->fINDCount) << kendl); - (Void)(kout << "Sector Size: " << hex_number(root->fSectorSize) << kendl); - (Void)(kout << "Drive Kind: " << Detail::hefs_drive_kind_to_string(root->fDiskKind) << kendl); + (Void)(kout8 << u8"Volume Name: " << boot->fVolName << kendl8); + (Void)(kout << "Start IND: " << hex_number(boot->fStartIND) << kendl); + (Void)(kout << "End IND: " << hex_number(boot->fEndIND) << kendl); + (Void)(kout << "Start IN: " << hex_number(boot->fStartIN) << kendl); + (Void)(kout << "End IN: " << hex_number(boot->fEndIN) << kendl); + (Void)(kout << "Start Block: " << hex_number(boot->fStartBlock) << kendl); + (Void)(kout << "End Block: " << hex_number(boot->fEndBlock) << kendl); + (Void)(kout << "Number of IND: " << hex_number(boot->fINDCount) << kendl); + (Void)(kout << "Sector Size: " << hex_number(boot->fSectorSize) << kendl); + (Void)(kout << "Drive Kind: " << Detail::hefs_drive_kind_to_string(boot->fDiskKind) << kendl); if (!mnt->fPacket.fPacketGood) { err_global_get() = kErrorDiskIsCorrupted; @@ -902,23 +901,23 @@ _Output Bool HeFileSystemParser::INodeDirectoryCtlManip(_Input DriveTrait* mnt, return NO; } - HEFS_BOOT_NODE* root = (HEFS_BOOT_NODE*) mm_new_heap(sizeof(HEFS_BOOT_NODE), Yes, No); + HEFS_BOOT_NODE* boot = (HEFS_BOOT_NODE*) mm_alloc_ptr(sizeof(HEFS_BOOT_NODE), Yes, No); rt_copy_memory((VoidPtr) "fs/hefs-packet", mnt->fPacket.fPacketMime, rt_string_len("fs/hefs-packet")); mnt->fPacket.fPacketLba = mnt->fLbaStart; mnt->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); - mnt->fPacket.fPacketContent = root; + mnt->fPacket.fPacketContent = boot; mnt->fInput(mnt->fPacket); - if (!KStringBuilder::Equals(root->fMagic, kHeFSMagic) || root->fVersion != kHeFSVersion) { + if (!KStringBuilder::Equals(boot->fMagic, kHeFSMagic) || boot->fVersion != kHeFSVersion) { err_global_get() = kErrorDisk; return YES; } - if (!KStringBuilder::Equals(root->fMagic, kHeFSMagic) || root->fVersion != kHeFSVersion) { + if (!KStringBuilder::Equals(boot->fMagic, kHeFSMagic) || boot->fVersion != kHeFSVersion) { err_global_get() = kErrorDiskIsCorrupted; kout << "Invalid Boot Node, this can't continue!\r"; @@ -934,15 +933,15 @@ _Output Bool HeFileSystemParser::INodeDirectoryCtlManip(_Input DriveTrait* mnt, return NO; } - if (Detail::hefsi_update_ind_status(root, mnt, dir, flags, delete_or_create)) { + if (Detail::hefsi_update_ind_status(boot, mnt, dir, flags, delete_or_create)) { // todo: make it smarter for high-throughput. - Detail::hefsi_balance_ind(root, mnt); + Detail::hefsi_balance_ind(boot, mnt); - mm_delete_heap((VoidPtr) root); + mm_free_ptr((VoidPtr) boot); return YES; } - mm_delete_heap((VoidPtr) root); + mm_free_ptr((VoidPtr) boot); return NO; } @@ -983,9 +982,9 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc return NO; } - HEFS_BOOT_NODE* root = (HEFS_BOOT_NODE*) mm_new_heap(sizeof(HEFS_BOOT_NODE), Yes, No); + HEFS_BOOT_NODE* boot = (HEFS_BOOT_NODE*) mm_alloc_ptr(sizeof(HEFS_BOOT_NODE), Yes, No); - if (!root) { + if (!boot) { err_global_get() = kErrorInvalidData; return NO; } @@ -995,18 +994,18 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc mnt->fPacket.fPacketLba = mnt->fLbaStart; mnt->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); - mnt->fPacket.fPacketContent = root; + mnt->fPacket.fPacketContent = boot; mnt->fInput(mnt->fPacket); - if (!KStringBuilder::Equals(root->fMagic, kHeFSMagic) || root->fVersion != kHeFSVersion) { + if (!KStringBuilder::Equals(boot->fMagic, kHeFSMagic) || boot->fVersion != kHeFSVersion) { (Void)(kout << "Invalid Boot Node, HeFS partition is invalid." << kendl); - mm_delete_heap((VoidPtr) root); + mm_free_ptr((VoidPtr) boot); err_global_get() = kErrorDisk; return NO; } - auto start = Detail::hefsi_fetch_in(root, mnt, dir, name, kind); + auto start = Detail::hefsi_fetch_in(boot, mnt, dir, name, kind); if (start) { (Void)(kout << hex_number(start->fHashPath) << kendl); @@ -1021,7 +1020,7 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc mnt->fInput(mnt->fPacket); } else { if (start->fFlags & kHeFSFlagsReadOnly) { - mm_delete_heap((VoidPtr) root); + mm_free_ptr((VoidPtr) boot); delete start; kout << "Error: File is read-only\r"; @@ -1034,7 +1033,7 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc } } - mm_delete_heap((VoidPtr) root); + mm_free_ptr((VoidPtr) boot); delete start; return YES; } @@ -1058,7 +1057,7 @@ _Output Bool HeFileSystemParser::INodeCtlManip(_Input DriveTrait* mnt, _Input co return NO; } - HEFS_INDEX_NODE* node = (HEFS_INDEX_NODE*) mm_new_heap(sizeof(HEFS_INDEX_NODE), Yes, No); + HEFS_INDEX_NODE* node = (HEFS_INDEX_NODE*) mm_alloc_ptr(sizeof(HEFS_INDEX_NODE), Yes, No); if (!node) { err_global_get() = kErrorInvalidData; @@ -1067,10 +1066,10 @@ _Output Bool HeFileSystemParser::INodeCtlManip(_Input DriveTrait* mnt, _Input co rt_set_memory(node, 0, sizeof(HEFS_INDEX_NODE)); - HEFS_BOOT_NODE* root = (HEFS_BOOT_NODE*) RTL_ALLOCA(sizeof(HEFS_BOOT_NODE)); + HEFS_BOOT_NODE* boot = (HEFS_BOOT_NODE*) RTL_ALLOCA(sizeof(HEFS_BOOT_NODE)); - if (!root) { - mm_delete_heap((VoidPtr) node); + if (!boot) { + mm_free_ptr((VoidPtr) node); err_global_get() = kErrorInvalidData; return NO; @@ -1081,11 +1080,11 @@ _Output Bool HeFileSystemParser::INodeCtlManip(_Input DriveTrait* mnt, _Input co mnt->fPacket.fPacketLba = mnt->fLbaStart; mnt->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); - mnt->fPacket.fPacketContent = root; + mnt->fPacket.fPacketContent = boot; mnt->fInput(mnt->fPacket); - if (!KStringBuilder::Equals(root->fMagic, kHeFSMagic) || root->fVersion != kHeFSVersion) { + if (!KStringBuilder::Equals(boot->fMagic, kHeFSMagic) || boot->fVersion != kHeFSVersion) { err_global_get() = kErrorDisk; return YES; } @@ -1112,8 +1111,8 @@ _Output Bool HeFileSystemParser::INodeCtlManip(_Input DriveTrait* mnt, _Input co } node->fAccessed = 0; - node->fCreated = delete_or_create ? 0UL : 1UL; - node->fDeleted = delete_or_create ? 1UL : 0UL; + node->fCreated = delete_or_create ? NO : YES; + node->fDeleted = delete_or_create ? 1UL : NO; node->fModified = 0; node->fSize = 0; node->fKind = kind; @@ -1123,16 +1122,16 @@ _Output Bool HeFileSystemParser::INodeCtlManip(_Input DriveTrait* mnt, _Input co node->fUID = 0; node->fHashPath = Detail::hefsi_hash_64(name); - if (Detail::hefsi_update_in_status(root, mnt, dir, node, delete_or_create)) { - mm_delete_heap((VoidPtr) node); + if (Detail::hefsi_update_in_status(boot, mnt, dir, node, delete_or_create)) { + mm_free_ptr((VoidPtr) node); - Detail::hefsi_balance_ind(root, mnt); + Detail::hefsi_balance_ind(boot, mnt); err_global_get() = kErrorSuccess; return YES; } - mm_delete_heap((VoidPtr) node); + mm_free_ptr((VoidPtr) node); err_global_get() = kErrorDirectoryNotFound; return NO; @@ -1142,7 +1141,7 @@ STATIC DriveTrait kMountPoint; /// @brief Initialize the HeFS filesystem. /// @return To check its status, see err_local_get(). -Boolean fs_init_hefs(Void) { +Boolean HeFS::fs_init_hefs(Void) noexcept { kout << "Creating HeFS disk...\r"; kMountPoint = io_construct_main_drive(); @@ -1152,10 +1151,8 @@ Boolean fs_init_hefs(Void) { HeFileSystemParser parser; - parser.Format(&kMountPoint, kHeFSEncodingFlagsUTF8, kHeFSDefaultVolumeName); - - return YES; + return parser.Format(&kMountPoint, kHeFSEncodingFlagsUTF8, kHeFSDefaultVolumeName); } -} // namespace Kernel::HeFS +} // namespace Kernel #endif // ifdef __FSKIT_INCLUDES_HEFS__ diff --git a/dev/kernel/src/FS/NeFS+FileMgr.cc b/dev/kernel/src/FS/NeFS+FileMgr.cc index c92d6727..978a43a8 100644 --- a/dev/kernel/src/FS/NeFS+FileMgr.cc +++ b/dev/kernel/src/FS/NeFS+FileMgr.cc @@ -8,7 +8,7 @@ #ifdef __FSKIT_INCLUDES_NEFS__ #include <KernelKit/FileMgr.h> -#include <KernelKit/MemoryMgr.h> +#include <KernelKit/HeapMgr.h> /// @brief NeFS File System Manager. /// BUGS: 0 diff --git a/dev/kernel/src/FS/NeFS+FileSystemParser.cc b/dev/kernel/src/FS/NeFS+FileSystemParser.cc index 0b818bbb..dd0a1d9a 100644 --- a/dev/kernel/src/FS/NeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/NeFS+FileSystemParser.cc @@ -10,13 +10,14 @@ #include <FirmwareKit/EPM.h> #include <KernelKit/DriveMgr.h> +#include <KernelKit/IFS.h> #include <KernelKit/KPC.h> #include <KernelKit/ProcessScheduler.h> -#include <KernelKit/User.h> -#include <NewKit/Crc32.h> -#include <NewKit/KString.h> -#include <NewKit/KernelPanic.h> -#include <NewKit/Utils.h> +#include <KernelKit/UserMgr.h> +#include <NeKit/Crc32.h> +#include <NeKit/KString.h> +#include <NeKit/KernelPanic.h> +#include <NeKit/Utils.h> #include <modules/AHCI/AHCI.h> #include <modules/ATA/ATA.h> @@ -240,7 +241,7 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char return nullptr; } - Char* parent_name = (Char*) mm_new_heap(sizeof(Char) * rt_string_len(name), Yes, No); + Char* parent_name = (Char*) mm_alloc_ptr(sizeof(Char) * rt_string_len(name), Yes, No); /// Locate parent catalog, to then allocate right after it. @@ -269,7 +270,7 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char NEFS_CATALOG_STRUCT* catalog = this->FindCatalog(parent_name, out_lba); - mm_delete_heap(parent_name); + mm_free_ptr(parent_name); auto& drive = kMountpoint.A(); @@ -444,6 +445,8 @@ bool NeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input const I NEFS_ROOT_PARTITION_BLOCK* part_block = (NEFS_ROOT_PARTITION_BLOCK*) fs_buf; + if (rt_string_cmp(kNeFSIdent, part_block->Ident, kNeFSIdentLen) == 0) return true; + const auto kNeFSUntitledHD = part_name; rt_copy_memory((VoidPtr) kNeFSIdent, (VoidPtr) part_block->Ident, kNeFSIdentLen); @@ -463,6 +466,7 @@ bool NeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input const I part_block->FreeSectors = sectorCount / sizeof(NEFS_CATALOG_STRUCT) - 1; part_block->SectorCount = sectorCount; part_block->DiskSize = diskSize; + part_block->SectorSize = drive->fSectorSz; part_block->FreeCatalog = sectorCount / sizeof(NEFS_CATALOG_STRUCT) - 1; drive->fPacket.fPacketContent = fs_buf; @@ -479,26 +483,6 @@ bool NeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input const I (Void)(kout << "Free sectors: " << hex_number(part_block->FreeSectors) << kendl); (Void)(kout << "Sector size: " << hex_number(part_block->SectorSize) << kendl); - NEFS_CATALOG_STRUCT root{}; - - rt_set_memory(&root, 0, sizeof(NEFS_CATALOG_STRUCT)); - - root.PrevSibling = part_block->StartCatalog; - root.NextSibling = 0UL; - - root.Kind = kNeFSCatalogKindDir; - root.Flags |= kNeFSFlagCreated; - root.CatalogFlags |= kNeFSStatusUnlocked; - - root.Name[0] = '/'; - root.Name[1] = 0; - - drive->fPacket.fPacketLba = part_block->StartCatalog; - drive->fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); - drive->fPacket.fPacketContent = &root; - - drive->fOutput(drive->fPacket); - return true; } @@ -895,7 +879,7 @@ namespace Kernel::NeFS { /// @brief Construct NeFS drives. /***********************************************************************************/ Boolean fs_init_nefs(Void) noexcept { - kout << "Creating main disk...\r"; + kout << "Creating HeFS disk...\r"; kMountpoint.A() = io_construct_main_drive(); @@ -903,9 +887,8 @@ Boolean fs_init_nefs(Void) noexcept { ke_panic(RUNTIME_CHECK_FILESYSTEM, "Main disk cannot be mounted."); NeFileSystemParser parser; - parser.Format(&kMountpoint.A(), 0, kNeFSVolumeName); - return YES; + return parser.Format(&kMountpoint.A(), 0, kNeFSVolumeName); } } // namespace Kernel::NeFS diff --git a/dev/kernel/src/FileMgr.cc b/dev/kernel/src/FileMgr.cc index 06d19e5b..e000965f 100644 --- a/dev/kernel/src/FileMgr.cc +++ b/dev/kernel/src/FileMgr.cc @@ -5,7 +5,7 @@ ------------------------------------------- */ #include <KernelKit/FileMgr.h> -#include <NewKit/Utils.h> +#include <NeKit/Utils.h> /***********************************************************************************/ /// @file FileMgr.cc diff --git a/dev/kernel/src/GUIDWizard.cc b/dev/kernel/src/GUIDWizard.cc index 48ee1ec6..46915ace 100644 --- a/dev/kernel/src/GUIDWizard.cc +++ b/dev/kernel/src/GUIDWizard.cc @@ -10,7 +10,7 @@ ------------------------------------------- */ #include <CFKit/GUIDWizard.h> -#include <NewKit/Ref.h> +#include <NeKit/Ref.h> // begin of ascii 'readable' characters. (A, C, C, 1, 2) #define kUUIDAsciiBegin 47 diff --git a/dev/kernel/src/HardwareThreadScheduler.cc b/dev/kernel/src/HardwareThreadScheduler.cc index c49c3081..78bad9d6 100644 --- a/dev/kernel/src/HardwareThreadScheduler.cc +++ b/dev/kernel/src/HardwareThreadScheduler.cc @@ -21,7 +21,7 @@ namespace Kernel { /***********************************************************************************/ EXTERN_C Bool hal_check_stack(HAL::StackFramePtr frame); -EXTERN_C Bool mp_register_process(HAL::StackFramePtr frame, ProcessID pid); +EXTERN_C Bool mp_register_task(HAL::StackFramePtr frame, ProcessID pid); STATIC HardwareThreadScheduler kHardwareThreadScheduler; @@ -41,14 +41,14 @@ HardwareThread::~HardwareThread() = default; /***********************************************************************************/ //! @brief returns the id of the thread. /***********************************************************************************/ -const ThreadID& HardwareThread::ID() noexcept { +ThreadID& HardwareThread::ID() noexcept { return fID; } /***********************************************************************************/ //! @brief returns the kind of thread we have. /***********************************************************************************/ -const ThreadKind& HardwareThread::Kind() noexcept { +ThreadKind& HardwareThread::Kind() noexcept { return fKind; } @@ -57,19 +57,6 @@ const ThreadKind& HardwareThread::Kind() noexcept { //! @return whether the thread is busy or not. /***********************************************************************************/ Bool HardwareThread::IsBusy() noexcept { - STATIC Int64 busy_timer = 0U; - constexpr Int64 kTimeoutMax = - 0x1000000; // an arbitrary value used to tell if the timeout hasn't been reached yet. - - if (fBusy && (busy_timer > kTimeoutMax)) { - busy_timer = 0U; - fBusy = No; - - return No; - } - - ++busy_timer; - return fBusy; } @@ -104,21 +91,17 @@ Void HardwareThread::Wake(const bool wakeup) noexcept { /// @retval true stack was changed, code is running. /// @retval false stack is invalid, previous code is running. /***********************************************************************************/ -Bool HardwareThread::Switch(VoidPtr image_ptr, Ptr8 stack_ptr, HAL::StackFramePtr frame, - const ThreadID& pid) { - if (this->IsBusy()) return NO; - - this->fStack = frame; - this->fPID = pid; - - this->fStack->BP = reinterpret_cast<UIntPtr>(image_ptr); - this->fStack->SP = reinterpret_cast<UIntPtr>(stack_ptr); - - Bool ret = mp_register_process(fStack, this->fPID); +Bool HardwareThread::Switch(HAL::StackFramePtr frame) { + if (!frame) { + return NO; + } - if (ret) this->Busy(YES); + if (!hal_check_stack(frame)) { + return NO; + } - return ret; + this->fStack = frame; + return mp_register_task(fStack, this->fID); } /***********************************************************************************/ @@ -162,12 +145,8 @@ HAL::StackFramePtr HardwareThreadScheduler::Leak() noexcept { */ /***********************************************************************************/ Ref<HardwareThread*> HardwareThreadScheduler::operator[](SizeT idx) { - if (idx == 0) { - if (fThreadList[idx].Kind() != kAPSystemReserved) { - fThreadList[idx].fKind = kAPBoot; - } - } else if (idx >= kMaxAPInsideSched) { - static HardwareThread* kFakeThread = nullptr; + if (idx >= kMaxAPInsideSched) { + HardwareThread* kFakeThread = nullptr; return {kFakeThread}; } diff --git a/dev/kernel/src/MemoryMgr.cc b/dev/kernel/src/HeapMgr.cc index 9b7bea43..eb86f378 100644 --- a/dev/kernel/src/MemoryMgr.cc +++ b/dev/kernel/src/HeapMgr.cc @@ -6,27 +6,27 @@ #include <ArchKit/ArchKit.h> #include <KernelKit/DebugOutput.h> +#include <KernelKit/HeapMgr.h> #include <KernelKit/KPC.h> -#include <KernelKit/MemoryMgr.h> -#include <NewKit/Crc32.h> -#include <NewKit/PageMgr.h> -#include <NewKit/Utils.h> +#include <NeKit/Crc32.h> +#include <NeKit/PageMgr.h> +#include <NeKit/Utils.h> /* ------------------------------------------- Revision History: 10/8/24: FIX: Fix useless long name, alongside a new WR (WriteRead) field. - 20/10/24: FIX: Fix mm_new_ and mm_delete_ APIs inside MemoryMgr.h header. (amlal) + 20/10/24: FIX: Fix mm_new_ and mm_delete_ APIs inside HeapMgr.h header. (amlal) 27/01/25: REFACTOR: Reworked code as the memory manager. - 25/03/25: REFACTOR: Refactor MemoryMgr code and log freed address location. + 25/03/25: REFACTOR: Refactor HeapMgr code and log freed address location. ------------------------------------------- */ -//! @file MemoryMgr.cc -//! @brief Heap algorithm that serves as the main memory manager. +//! @file HeapMgr.cc +//! @brief Heap system that serves as the main memory manager. -#define kMemoryMgrMagic (0xD4D75) -#define kMemoryMgrAlignSz (4U) +#define kHeapMgrMagic (0xD4D75) +#define kHeapMgrAlignSz (4U) namespace Kernel { /// @brief Implementation details. @@ -68,20 +68,20 @@ namespace Detail { UInt32 fPad; /// @brief Padding bytes for header. - UInt8 fPadding[kMemoryMgrAlignSz]; + UInt8 fPadding[kHeapMgrAlignSz]; }; /// @brief Check for heap address validity. /// @param heap_ptr The address_ptr to check. /// @return Bool if the pointer is valid or not. - _Output auto mm_check_heap_address(VoidPtr heap_ptr) -> Bool { + _Output auto mm_check_ptr_address(VoidPtr heap_ptr) -> Bool { if (!heap_ptr) return false; - IntPtr base_heap = ((IntPtr) heap_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK); + IntPtr base_ptr = ((IntPtr) heap_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK); /// Add that check in case we're having an integer underflow. /// - if (base_heap < 0) { + if (base_ptr < 0) { return false; } @@ -91,43 +91,29 @@ namespace Detail { typedef MM_INFORMATION_BLOCK* MM_INFORMATION_BLOCK_PTR; } // namespace Detail -/// @brief Declare a new size for ptr_heap. -/// @param ptr_heap the pointer. -/// @return Newly allocated heap header. -_Output auto mm_realloc_heap(VoidPtr ptr_heap, SizeT new_sz) -> VoidPtr { - if (Detail::mm_check_heap_address(ptr_heap) == No) return nullptr; - - if (!ptr_heap || new_sz < 1) return nullptr; - - kout << "This function is not implemented by the kernel yet.\r"; - - ke_panic(RUNTIME_CHECK_INVALID); - - return nullptr; -} +STATIC PageMgr kPageMgr; /// @brief Allocate chunk of memory. /// @param sz Size of pointer /// @param wr Read Write bit. /// @param user User enable bit. /// @return The newly allocated pointer. -_Output VoidPtr mm_new_heap(SizeT sz, Bool wr, Bool user, SizeT pad_amount) { +_Output VoidPtr mm_alloc_ptr(SizeT sz, Bool wr, Bool user, SizeT pad_amount) { auto sz_fix = sz; if (sz_fix == 0) return nullptr; sz_fix += sizeof(Detail::MM_INFORMATION_BLOCK); - PageMgr page_mgr; - auto wrapper = page_mgr.Request(wr, user, No, sz_fix, pad_amount); + auto wrapper = kPageMgr.Request(wr, user, No, sz_fix, pad_amount); Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>(wrapper.VirtualAddress() + sizeof(Detail::MM_INFORMATION_BLOCK)); heap_info_ptr->fSize = sz_fix; - heap_info_ptr->fMagic = kMemoryMgrMagic; - heap_info_ptr->fCRC32 = 0; // dont fill it for now. + heap_info_ptr->fMagic = kHeapMgrMagic; + heap_info_ptr->fCRC32 = 0U; // dont fill it for now. heap_info_ptr->fOffset = reinterpret_cast<UIntPtr>(heap_info_ptr) + sizeof(Detail::MM_INFORMATION_BLOCK); heap_info_ptr->fPage = No; @@ -136,12 +122,12 @@ _Output VoidPtr mm_new_heap(SizeT sz, Bool wr, Bool user, SizeT pad_amount) { heap_info_ptr->fPresent = Yes; heap_info_ptr->fPad = pad_amount; - rt_set_memory(heap_info_ptr->fPadding, 0, kMemoryMgrAlignSz); + rt_set_memory(heap_info_ptr->fPadding, 0, kHeapMgrAlignSz); auto result = reinterpret_cast<VoidPtr>(heap_info_ptr->fOffset); - (Void)(kout << "Registered heap address: " << hex_number(reinterpret_cast<UIntPtr>(heap_info_ptr)) - << kendl); + (Void)(kout << "HeapMgr: Registered heap address: " + << hex_number(reinterpret_cast<UIntPtr>(heap_info_ptr)) << kendl); return result; } @@ -150,7 +136,7 @@ _Output VoidPtr mm_new_heap(SizeT sz, Bool wr, Bool user, SizeT pad_amount) { /// @param heap_ptr the pointer to make a page heap. /// @return kErrorSuccess if successful, otherwise an error code. _Output Int32 mm_make_page(VoidPtr heap_ptr) { - if (Detail::mm_check_heap_address(heap_ptr) == No) return kErrorHeapNotPresent; + if (Detail::mm_check_ptr_address(heap_ptr) == No) return kErrorHeapNotPresent; Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>((UIntPtr) heap_ptr - @@ -160,8 +146,8 @@ _Output Int32 mm_make_page(VoidPtr heap_ptr) { heap_info_ptr->fPage = true; - (Void)(kout << "Registered page address: " << hex_number(reinterpret_cast<UIntPtr>(heap_info_ptr)) - << kendl); + (Void)(kout << "HeapMgr: Registered page from heap address: " + << hex_number(reinterpret_cast<UIntPtr>(heap_info_ptr)) << kendl); return kErrorSuccess; } @@ -169,8 +155,8 @@ _Output Int32 mm_make_page(VoidPtr heap_ptr) { /// @brief Overwrites and set the flags of a heap header. /// @param heap_ptr the pointer to update. /// @param flags the flags to set. -_Output Int32 mm_make_flags(VoidPtr heap_ptr, UInt64 flags) { - if (Detail::mm_check_heap_address(heap_ptr) == No) return kErrorHeapNotPresent; +_Output Int32 mm_set_ptr_flags(VoidPtr heap_ptr, UInt64 flags) { + if (Detail::mm_check_ptr_address(heap_ptr) == No) return kErrorHeapNotPresent; Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>((UIntPtr) heap_ptr - @@ -185,7 +171,7 @@ _Output Int32 mm_make_flags(VoidPtr heap_ptr, UInt64 flags) { /// @brief Gets the flags of a heap header. /// @param heap_ptr the pointer to get. -_Output UInt64 mm_get_flags(VoidPtr heap_ptr) { +_Output UInt64 mm_get_ptr_flags(VoidPtr heap_ptr) { Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>((UIntPtr) heap_ptr - sizeof(Detail::MM_INFORMATION_BLOCK)); @@ -198,14 +184,14 @@ _Output UInt64 mm_get_flags(VoidPtr heap_ptr) { /// @brief Declare pointer as free. /// @param heap_ptr the pointer. /// @return -_Output Int32 mm_delete_heap(VoidPtr heap_ptr) { - if (Detail::mm_check_heap_address(heap_ptr) == No) return kErrorHeapNotPresent; +_Output Int32 mm_free_ptr(VoidPtr heap_ptr) { + if (Detail::mm_check_ptr_address(heap_ptr) == No) return kErrorHeapNotPresent; Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>((UIntPtr) (heap_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK)); - if (heap_info_ptr && heap_info_ptr->fMagic == kMemoryMgrMagic) { + if (heap_info_ptr && heap_info_ptr->fMagic == kHeapMgrMagic) { if (!heap_info_ptr->fPresent) { return kErrorHeapNotPresent; } @@ -219,16 +205,16 @@ _Output Int32 mm_delete_heap(VoidPtr heap_ptr) { heap_info_ptr->fMagic = 0; heap_info_ptr->fPad = 0; - (Void)(kout << "Freed heap address: " << hex_number(reinterpret_cast<UIntPtr>(heap_info_ptr)) - << kendl); + (Void)(kout << "HeapMgr: Freed heap address: " + << hex_number(reinterpret_cast<UIntPtr>(heap_info_ptr)) << kendl); PTEWrapper page_wrapper( No, No, No, reinterpret_cast<UIntPtr>(heap_info_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK)); + Ref<PTEWrapper> pte_address{page_wrapper}; - PageMgr page_mgr; - page_mgr.Free(pte_address); + kPageMgr.Free(pte_address); return kErrorSuccess; } @@ -239,13 +225,13 @@ _Output Int32 mm_delete_heap(VoidPtr heap_ptr) { /// @brief Check if pointer is a valid Kernel pointer. /// @param heap_ptr the pointer /// @return if it exists. -_Output Boolean mm_is_valid_heap(VoidPtr heap_ptr) { +_Output Boolean mm_is_valid_ptr(VoidPtr heap_ptr) { if (heap_ptr && HAL::mm_is_bitmap(heap_ptr)) { Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>((UIntPtr) (heap_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK)); - return (heap_info_ptr && heap_info_ptr->fPresent && heap_info_ptr->fMagic == kMemoryMgrMagic); + return (heap_info_ptr && heap_info_ptr->fPresent && heap_info_ptr->fMagic == kHeapMgrMagic); } return No; @@ -254,14 +240,14 @@ _Output Boolean mm_is_valid_heap(VoidPtr heap_ptr) { /// @brief Protect the heap with a CRC value. /// @param heap_ptr HIB pointer. /// @return if it valid: point has crc now., otherwise fail. -_Output Boolean mm_protect_heap(VoidPtr heap_ptr) { +_Output Boolean mm_protect_ptr(VoidPtr heap_ptr) { if (heap_ptr) { Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>((UIntPtr) heap_ptr - sizeof(Detail::MM_INFORMATION_BLOCK)); /// if valid, present and is heap header, then compute crc32 - if (heap_info_ptr && heap_info_ptr->fPresent && kMemoryMgrMagic == heap_info_ptr->fMagic) { + if (heap_info_ptr && heap_info_ptr->fPresent && kHeapMgrMagic == heap_info_ptr->fMagic) { heap_info_ptr->fCRC32 = ke_calculate_crc32((Char*) heap_info_ptr->fOffset, heap_info_ptr->fSize); diff --git a/dev/kernel/src/DriveMgr+IO.cc b/dev/kernel/src/IFS.cc index 9137c91e..ba2ec8c0 100644 --- a/dev/kernel/src/DriveMgr+IO.cc +++ b/dev/kernel/src/IFS.cc @@ -9,9 +9,9 @@ /************************************************************* * - * File: DriveMgr+IO.cc + * File: IFS.cc * Purpose: Filesystem to mountpoint interface. - * Date: 3/26/24 + * Date: 05/26/2025 * * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. * @@ -23,13 +23,13 @@ #define fsi_ifs_read(DRV, TRAITS, MP) (MP->DRV()).fInput(TRAITS) namespace Kernel { -/// @brief Read from newfs disk. +/// @brief Read from fs disk. /// @param Mnt mounted interface. /// @param DrvTrait drive info /// @param DrvIndex drive index. /// @return Int32 fs_ifs_read(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvIndex) { - if (!Mnt) return 1; + if (!Mnt) return kErrorDisk; DrvTrait.fPacket.fPacketGood = false; @@ -52,16 +52,16 @@ Int32 fs_ifs_read(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvIndex } } - return DrvTrait.fPacket.fPacketGood; + return DrvTrait.fPacket.fPacketGood ? kErrorSuccess : kErrorDisk; } -/// @brief Write to newfs disk. +/// @brief Write to fs disk. /// @param Mnt mounted interface. /// @param DrvTrait drive info /// @param DrvIndex drive index. /// @return Int32 fs_ifs_write(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvIndex) { - if (!Mnt) return 1; + if (!Mnt) return kErrorDisk; DrvTrait.fPacket.fPacketGood = false; @@ -84,6 +84,6 @@ Int32 fs_ifs_write(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvInde } } - return DrvTrait.fPacket.fPacketGood; + return DrvTrait.fPacket.fPacketGood ? kErrorSuccess : kErrorDisk; } } // namespace Kernel
\ No newline at end of file diff --git a/dev/kernel/src/IPEFDylibObject.cc b/dev/kernel/src/IPEFDylibObject.cc index f9cd758c..d1d4eeca 100644 --- a/dev/kernel/src/IPEFDylibObject.cc +++ b/dev/kernel/src/IPEFDylibObject.cc @@ -12,7 +12,7 @@ #include <KernelKit/PEF.h> #include <KernelKit/ProcessScheduler.h> #include <KernelKit/ThreadLocalStorage.h> -#include <NewKit/Defines.h> +#include <NeKit/Defines.h> /* ------------------------------------------- @@ -49,7 +49,7 @@ EXTERN_C IDylibRef rtl_init_dylib_pef(USER_PROCESS& process) { return nullptr; } - dll_obj->Mount(new IPEFDylibObject::DLL_TRAITS()); + dll_obj->Mount(new IPEFDylibObject::DylibTraits()); if (!dll_obj->Get()) { tls_delete_class(dll_obj); diff --git a/dev/kernel/src/IndexableProperty.cc b/dev/kernel/src/IndexableProperty.cc index a89ea92d..1d2a1ce8 100644 --- a/dev/kernel/src/IndexableProperty.cc +++ b/dev/kernel/src/IndexableProperty.cc @@ -6,8 +6,8 @@ #include <CompilerKit/CompilerKit.h> #include <FSKit/IndexableProperty.h> -#include <NewKit/MutableArray.h> -#include <NewKit/Utils.h> +#include <NeKit/MutableArray.h> +#include <NeKit/Utils.h> /// @brief File indexer API for fast path access. /// BUGS: 0 diff --git a/dev/kernel/src/Json.cc b/dev/kernel/src/Json.cc index 9264b2b8..68ab55fc 100644 --- a/dev/kernel/src/Json.cc +++ b/dev/kernel/src/Json.cc @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include <NewKit/Json.h> +#include <NeKit/Json.h> /// @brief Undefined object, is null in length. RTL_INIT_OBJECT(Kernel::Json::kNull, Kernel::Json); diff --git a/dev/kernel/src/KPC.cc b/dev/kernel/src/KPC.cc index 8937d19a..1693fbd3 100644 --- a/dev/kernel/src/KPC.cc +++ b/dev/kernel/src/KPC.cc @@ -4,9 +4,9 @@ ------------------------------------------- */ +#include <KernelKit/HeapMgr.h> #include <KernelKit/KPC.h> -#include <KernelKit/MemoryMgr.h> -#include <NewKit/KernelPanic.h> +#include <NeKit/KernelPanic.h> namespace Kernel { STATIC Bool kRaiseOnBugCheck = false; @@ -19,7 +19,7 @@ Boolean err_bug_check_raise(Void) noexcept { if (ptr == nullptr) goto bug_check_fail; - if (!mm_is_valid_heap(ptr)) goto bug_check_fail; + if (!mm_is_valid_ptr(ptr)) goto bug_check_fail; delete[] ptr; diff --git a/dev/kernel/src/KString.cc b/dev/kernel/src/KString.cc index 479eb2fc..9f332cdf 100644 --- a/dev/kernel/src/KString.cc +++ b/dev/kernel/src/KString.cc @@ -4,8 +4,8 @@ ------------------------------------------- */ -#include <NewKit/KString.h> -#include <NewKit/Utils.h> +#include <NeKit/KString.h> +#include <NeKit/Utils.h> /// @file KString.cc /// @brief Kernel String manipulation file. diff --git a/dev/kernel/src/MutableArray.cc b/dev/kernel/src/MutableArray.cc index 4b07f9ae..b7aaee1c 100644 --- a/dev/kernel/src/MutableArray.cc +++ b/dev/kernel/src/MutableArray.cc @@ -4,4 +4,4 @@ ------------------------------------------- */ -#include <NewKit/MutableArray.h> +#include <NeKit/MutableArray.h> diff --git a/dev/kernel/src/Network/IPAddr.cc b/dev/kernel/src/Network/IPAddr.cc index 4437df22..beb69470 100644 --- a/dev/kernel/src/Network/IPAddr.cc +++ b/dev/kernel/src/Network/IPAddr.cc @@ -4,8 +4,8 @@ ------------------------------------------- */ +#include <NeKit/Utils.h> #include <NetworkKit/IP.h> -#include <NewKit/Utils.h> namespace Kernel { Char* RawIPAddress::Address() { diff --git a/dev/kernel/src/Network/IPCMsg.cc b/dev/kernel/src/Network/IPCMsg.cc index e89e7c1b..cff19cfa 100644 --- a/dev/kernel/src/Network/IPCMsg.cc +++ b/dev/kernel/src/Network/IPCMsg.cc @@ -101,7 +101,6 @@ Bool ipc_construct_packet(_Output IPC_MSG** pckt_in) { Bool IPC_MSG::Pass(IPC_MSG* src, IPC_MSG* target) noexcept { if (src && target && (target != src)) { if (src->IpcMsgSz > target->IpcMsgSz) return No; - if (target->IpcMsgSz > src->IpcMsgSz) return No; rt_copy_memory(src->IpcData, target->IpcData, src->IpcMsgSz); diff --git a/dev/kernel/src/Network/NetworkDevice.cc b/dev/kernel/src/Network/NetworkDevice.cc index 6f77a244..7f93fa1b 100644 --- a/dev/kernel/src/Network/NetworkDevice.cc +++ b/dev/kernel/src/Network/NetworkDevice.cc @@ -4,14 +4,14 @@ ------------------------------------------- */ +#include <NeKit/Utils.h> #include <NetworkKit/NetworkDevice.h> -#include <NewKit/Utils.h> namespace Kernel { /// \brief Getter for fNetworkName. /// \return Network device name. const Char* NetworkDevice::Name() const { - return this->fNetworkName; + return "/devices/net{}"; } /// \brief Setter for fNetworkName. @@ -20,9 +20,9 @@ Boolean NetworkDevice::Name(const Char* name) { if (*name == 0) return NO; - if (rt_string_len(name) > cNetworkNameLen) return NO; + if (rt_string_len(name) > rt_string_len(this->Name())) return NO; - rt_copy_memory((VoidPtr) name, (VoidPtr) this->fNetworkName, rt_string_len(name)); + rt_copy_memory((VoidPtr) name, (VoidPtr) this->Name(), rt_string_len(this->Name())); return YES; } diff --git a/dev/kernel/src/New+Delete.cc b/dev/kernel/src/New+Delete.cc index 96e7ab64..da705f26 100644 --- a/dev/kernel/src/New+Delete.cc +++ b/dev/kernel/src/New+Delete.cc @@ -4,31 +4,31 @@ ------------------------------------------- */ -#include <KernelKit/MemoryMgr.h> -#include <NewKit/New.h> +#include <KernelKit/HeapMgr.h> +#include <NeKit/New.h> void* operator new[](size_t sz) { if (sz == 0) ++sz; - return Kernel::mm_new_heap(sz, true, false); + return Kernel::mm_alloc_ptr(sz, true, false); } void* operator new(size_t sz) { if (sz == 0) ++sz; - return Kernel::mm_new_heap(sz, true, false); + return Kernel::mm_alloc_ptr(sz, true, false); } void operator delete[](void* ptr) { if (ptr == nullptr) return; - Kernel::mm_delete_heap(ptr); + Kernel::mm_free_ptr(ptr); } void operator delete(void* ptr) { if (ptr == nullptr) return; - Kernel::mm_delete_heap(ptr); + Kernel::mm_free_ptr(ptr); } void operator delete(void* ptr, size_t sz) { @@ -36,7 +36,7 @@ void operator delete(void* ptr, size_t sz) { NE_UNUSED(sz); - Kernel::mm_delete_heap(ptr); + Kernel::mm_free_ptr(ptr); } void operator delete[](void* ptr, size_t sz) { @@ -44,5 +44,5 @@ void operator delete[](void* ptr, size_t sz) { NE_UNUSED(sz); - Kernel::mm_delete_heap(ptr); + Kernel::mm_free_ptr(ptr); } diff --git a/dev/kernel/src/OwnPtr.cc b/dev/kernel/src/OwnPtr.cc index 8fd2b985..c716c2f4 100644 --- a/dev/kernel/src/OwnPtr.cc +++ b/dev/kernel/src/OwnPtr.cc @@ -4,4 +4,4 @@ ------------------------------------------- */ -#include <NewKit/OwnPtr.h> +#include <NeKit/OwnPtr.h> diff --git a/dev/kernel/src/PEFCodeMgr.cc b/dev/kernel/src/PEFCodeMgr.cc index 09a262d4..ed72473f 100644 --- a/dev/kernel/src/PEFCodeMgr.cc +++ b/dev/kernel/src/PEFCodeMgr.cc @@ -5,13 +5,13 @@ ------------------------------------------- */ #include <KernelKit/DebugOutput.h> -#include <KernelKit/MemoryMgr.h> +#include <KernelKit/HeapMgr.h> #include <KernelKit/PEFCodeMgr.h> #include <KernelKit/ProcessScheduler.h> -#include <NewKit/Defines.h> -#include <NewKit/KString.h> -#include <NewKit/KernelPanic.h> -#include <NewKit/OwnPtr.h> +#include <NeKit/Defines.h> +#include <NeKit/KString.h> +#include <NeKit/KernelPanic.h> +#include <NeKit/OwnPtr.h> /// @brief PEF stack size symbol. #define kPefStackSizeSymbol "__PEFSizeOfReserveStack" @@ -78,7 +78,7 @@ PEFLoader::PEFLoader(const Char* path) : fCachedBlob(nullptr), fFatBinary(false) fBad = true; - if (fCachedBlob) mm_delete_heap(fCachedBlob); + if (fCachedBlob) mm_free_ptr(fCachedBlob); kout << "PEFLoader: Warning: Executable format error!\r"; @@ -89,7 +89,7 @@ PEFLoader::PEFLoader(const Char* path) : fCachedBlob(nullptr), fFatBinary(false) /// @brief PEF destructor. /***********************************************************************************/ PEFLoader::~PEFLoader() { - if (fCachedBlob) mm_delete_heap(fCachedBlob); + if (fCachedBlob) mm_free_ptr(fCachedBlob); fFile.Delete(); } @@ -147,7 +147,7 @@ ErrorOr<VoidPtr> PEFLoader::FindSymbol(const Char* name, Int32 kind) { if (container_header->Kind == kind) { if (container_header->Cpu != Detail::ldr_get_platform()) { if (!this->fFatBinary) { - mm_delete_heap(blob); + mm_free_ptr(blob); return ErrorOr<VoidPtr>{kErrorInvalidData}; } } @@ -156,16 +156,16 @@ ErrorOr<VoidPtr> PEFLoader::FindSymbol(const Char* name, Int32 kind) { rt_copy_memory((VoidPtr) ((Char*) blob + sizeof(PEFCommandHeader)), container_blob_value, container_header->Size); - mm_delete_heap(blob); + mm_free_ptr(blob); kout << "PEFLoader: Information: Loaded stub: " << container_header->Name << "!\r"; auto ret = HAL::mm_map_page((VoidPtr) container_header->VMAddress, - (VoidPtr) HAL::mm_get_phys_address(container_blob_value), + (VoidPtr) HAL::mm_get_page_addr(container_blob_value), HAL::kMMFlagsPresent | HAL::kMMFlagsUser); if (ret != kErrorSuccess) { - mm_delete_heap(container_blob_value); + mm_free_ptr(container_blob_value); return ErrorOr<VoidPtr>{kErrorInvalidData}; } @@ -174,7 +174,7 @@ ErrorOr<VoidPtr> PEFLoader::FindSymbol(const Char* name, Int32 kind) { } } - mm_delete_heap(blob); + mm_free_ptr(blob); return ErrorOr<VoidPtr>{kErrorInvalidData}; } @@ -236,7 +236,7 @@ namespace Utils { UserProcessScheduler::The().Spawn(reinterpret_cast<const Char*>(symname.Leak().Leak()), errOrStart.Leak().Leak(), exec.GetBlob().Leak().Leak()); - mm_delete_heap(symname.Leak().Leak()); + mm_free_ptr(symname.Leak().Leak()); if (id != kSchedInvalidPID) { auto stacksym = exec.FindSymbol(kPefStackSizeSymbol, kPefData); @@ -249,11 +249,11 @@ namespace Utils { *(volatile UIntPtr*) stacksym.Leak().Leak() = kSchedMaxStackSz; } - UserProcessScheduler::The().CurrentTeam().AsArray()[id].Kind = process_kind; - UserProcessScheduler::The().CurrentTeam().AsArray()[id].StackSize = + UserProcessScheduler::The().TheCurrentTeam().AsArray()[id].Kind = process_kind; + UserProcessScheduler::The().TheCurrentTeam().AsArray()[id].StackSize = *(UIntPtr*) stacksym.Leak().Leak(); - mm_delete_heap(stacksym.Leak().Leak()); + mm_free_ptr(stacksym.Leak().Leak()); } return id; diff --git a/dev/kernel/src/PRDT.cc b/dev/kernel/src/PRDT.cc index f7380d2d..0c84fd29 100644 --- a/dev/kernel/src/PRDT.cc +++ b/dev/kernel/src/PRDT.cc @@ -5,7 +5,7 @@ ------------------------------------------- */ #include <KernelKit/DebugOutput.h> -#include <NewKit/KString.h> +#include <NeKit/KString.h> #include <StorageKit/PRDT.h> namespace Kernel { diff --git a/dev/kernel/src/PageMgr.cc b/dev/kernel/src/PageMgr.cc index f60fa57a..e78f4908 100644 --- a/dev/kernel/src/PageMgr.cc +++ b/dev/kernel/src/PageMgr.cc @@ -5,7 +5,7 @@ ------------------------------------------- */ #include <KernelKit/DebugOutput.h> -#include <NewKit/PageMgr.h> +#include <NeKit/PageMgr.h> #ifdef __NE_AMD64__ #include <HALKit/AMD64/Paging.h> diff --git a/dev/kernel/src/Pmm.cc b/dev/kernel/src/Pmm.cc index 35ea4195..b9fba20e 100644 --- a/dev/kernel/src/Pmm.cc +++ b/dev/kernel/src/Pmm.cc @@ -5,7 +5,7 @@ ------------------------------------------- */ #include <KernelKit/DebugOutput.h> -#include <NewKit/Pmm.h> +#include <NeKit/Pmm.h> #if defined(__NE_ARM64__) #include <HALKit/ARM64/Processor.h> diff --git a/dev/kernel/src/Ref.cc b/dev/kernel/src/Ref.cc index c185952b..db584d9c 100644 --- a/dev/kernel/src/Ref.cc +++ b/dev/kernel/src/Ref.cc @@ -4,4 +4,4 @@ ------------------------------------------- */ -#include <NewKit/Ref.h> +#include <NeKit/Ref.h> diff --git a/dev/kernel/src/Stream.cc b/dev/kernel/src/Stream.cc index 8fd4dab3..e05f6e6d 100644 --- a/dev/kernel/src/Stream.cc +++ b/dev/kernel/src/Stream.cc @@ -9,4 +9,4 @@ ------------------------------------------- */ -#include <NewKit/Stream.h> +#include <NeKit/Stream.h> diff --git a/dev/kernel/src/Swap/DiskSwap.cc b/dev/kernel/src/Swap/DiskSwap.cc index 118be0f6..8578450c 100644 --- a/dev/kernel/src/Swap/DiskSwap.cc +++ b/dev/kernel/src/Swap/DiskSwap.cc @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal El Mahrouss Labs, all rights reserved. + Copyright (C) 2024-2025 Amlal El Mahrouss , all rights reserved. ------------------------------------------- */ diff --git a/dev/kernel/src/ThreadLocalStorage.cc b/dev/kernel/src/ThreadLocalStorage.cc index e248e67c..ec315ddf 100644 --- a/dev/kernel/src/ThreadLocalStorage.cc +++ b/dev/kernel/src/ThreadLocalStorage.cc @@ -10,7 +10,7 @@ #include <CFKit/Property.h> #include <KernelKit/ProcessScheduler.h> #include <KernelKit/ThreadLocalStorage.h> -#include <NewKit/KString.h> +#include <NeKit/KString.h> /***********************************************************************************/ /// @bugs: 0 diff --git a/dev/kernel/src/User.cc b/dev/kernel/src/UserMgr.cc index 3e6aeeba..c41b445b 100644 --- a/dev/kernel/src/User.cc +++ b/dev/kernel/src/UserMgr.cc @@ -11,10 +11,10 @@ */ #include <KernelKit/FileMgr.h> +#include <KernelKit/HeapMgr.h> #include <KernelKit/KPC.h> -#include <KernelKit/MemoryMgr.h> -#include <KernelKit/User.h> -#include <NewKit/KernelPanic.h> +#include <KernelKit/UserMgr.h> +#include <NeKit/KernelPanic.h> #define kStdUserType (0xEE) #define kSuperUserType (0xEF) @@ -29,20 +29,23 @@ namespace Detail { /// \param password password to hash. /// \return the hashed password //////////////////////////////////////////////////////////// - Int32 user_standard_token_generator(Char* password, const Char* in_password, User* user, - SizeT length) { + STATIC UInt64 user_fnv_generator(const Char* password, User* user) { if (!password || !user) return 1; if (*password == 0) return 1; - kout << "user_standard_token_generator: Hashing user password...\r"; + kout << "user_fnv_generator: Hashing user password...\r"; - for (SizeT i_pass = 0UL; i_pass < length; ++i_pass) { - Char cur_chr = in_password[i_pass]; + const UInt64 FNV_OFFSET_BASIS = 0xcbf29ce484222325ULL; + const UInt64 FNV_PRIME = 0x100000001b3ULL; - password[i_pass] = cur_chr | (user->IsStdUser() ? kStdUserType : kSuperUserType); + UInt64 hash = FNV_OFFSET_BASIS; + + while (*password) { + hash ^= (Utf8Char) (*password++); + hash *= FNV_PRIME; } - kout << "user_standard_token_generator: Hashed user password.\r"; + kout << "user_fnv_generator: Hashed user password.\r"; return 0; } @@ -68,70 +71,26 @@ User::User(const UserRingKind& ring_kind, const Char* user_name) : mUserRing(rin //////////////////////////////////////////////////////////// User::~User() = default; -Bool User::Save(const UserPublicKey password_to_fill) noexcept { - if (!password_to_fill || *password_to_fill == 0) return No; - - SizeT len = rt_string_len(password_to_fill); - - UserPublicKey password = new UserPublicKeyType[len]; - - MUST_PASS(password); - - rt_set_memory(password, 0, len); - - // fill data first, generate hash. - // return false on error. - - rt_copy_memory((VoidPtr) password_to_fill, password, len); - - if (!Detail::user_standard_token_generator(password, password_to_fill, this, len)) { - delete[] password; - password = nullptr; - - return No; - } - - // then store password. - - rt_copy_memory(password, this->mUserKey, rt_string_len(password_to_fill)); +Bool User::Save(const UserPublicKey password) noexcept { + if (!password || *password == 0) return No; - delete[] password; - password = nullptr; + this->mUserFNV = Detail::user_fnv_generator(password, this); kout << "User::Save: Saved password successfully...\r"; return Yes; } -Bool User::Matches(const UserPublicKey password_to_fill) noexcept { - if (!password_to_fill || *password_to_fill) return No; - - SizeT len = rt_string_len(password_to_fill); - - Char* password = new Char[len]; - MUST_PASS(password); - - // fill data first, generate hash. - // return false on error. - - rt_copy_memory((VoidPtr) password_to_fill, password, len); - - if (!Detail::user_standard_token_generator(password, password_to_fill, this, len)) { - delete[] password; - password = nullptr; - - return No; - } - - kout << "User::Matches: Validating hashed passwords...\r"; +Bool User::Login(const UserPublicKey password) noexcept { + if (!password || !*password) return No; // now check if the password matches. - if (rt_string_cmp(password, this->mUserKey, rt_string_len(this->mUserKey)) == 0) { - kout << "User::Matches: Password matches.\r"; + if (this->mUserFNV == Detail::user_fnv_generator(password, this)) { + kout << "User::Login: Password matches.\r"; return Yes; } - kout << "User::Matches: Password doesn't match.\r"; + kout << "User::Login: Password doesn't match.\r"; return No; } diff --git a/dev/kernel/src/UserProcessScheduler.cc b/dev/kernel/src/UserProcessScheduler.cc index 2082642c..4bbd085a 100644 --- a/dev/kernel/src/UserProcessScheduler.cc +++ b/dev/kernel/src/UserProcessScheduler.cc @@ -14,13 +14,13 @@ /***********************************************************************************/ #include <ArchKit/ArchKit.h> -#include <NewKit/KString.h> - #include <KernelKit/HardwareThreadScheduler.h> +#include <KernelKit/HeapMgr.h> #include <KernelKit/IPEFDylibObject.h> #include <KernelKit/KPC.h> -#include <KernelKit/MemoryMgr.h> #include <KernelKit/ProcessScheduler.h> +#include <NeKit/KString.h> +#include <SignalKit/Signals.h> ///! BUGS: 0 @@ -31,14 +31,10 @@ namespace Kernel { STATIC UInt32 kLastExitCode = 0U; -STATIC BOOL kCurrentlySwitching = No; - /***********************************************************************************/ /// @brief Scheduler itself. /***********************************************************************************/ -STATIC UserProcessScheduler kScheduler; - USER_PROCESS::USER_PROCESS() = default; USER_PROCESS::~USER_PROCESS() = default; @@ -101,16 +97,15 @@ Void USER_PROCESS::Wake(Bool should_wakeup) { /** @param tree The tree to calibrate */ /***********************************************************************************/ -STATIC USER_PROCESS::USER_HEAP_TREE* sched_try_go_upper_heap_tree( - USER_PROCESS::USER_HEAP_TREE* tree) { +STATIC PROCESS_HEAP_TREE<VoidPtr>* sched_try_go_upper_ptr_tree(PROCESS_HEAP_TREE<VoidPtr>* tree) { if (!tree) { return nullptr; } - tree = tree->MemoryParent; + tree = tree->Parent; if (tree) { - auto tree_tmp = tree->MemoryNext; + auto tree_tmp = tree->Next; if (!tree_tmp) { return tree; @@ -130,76 +125,75 @@ ErrorOr<VoidPtr> USER_PROCESS::New(SizeT sz, SizeT pad_amount) { if (this->UsedMemory > kSchedMaxMemoryLimit) return ErrorOr<VoidPtr>(-kErrorHeapOutOfMemory); #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - auto vm_register = kKernelCR3; + auto vm_register = kKernelVM; + hal_write_cr3(this->VMRegister); - auto ptr = mm_new_heap(sz, Yes, Yes, pad_amount); + auto ptr = mm_alloc_ptr(sz, Yes, Yes, pad_amount); hal_write_cr3(vm_register); #else - auto ptr = mm_new_heap(sz, Yes, Yes, pad_amount); + auto ptr = mm_alloc_ptr(sz, Yes, Yes, pad_amount); #endif if (!this->HeapTree) { - this->HeapTree = new USER_HEAP_TREE(); + this->HeapTree = new PROCESS_HEAP_TREE<VoidPtr>(); - this->HeapTree->MemoryEntryPad = pad_amount; - this->HeapTree->MemoryEntrySize = sz; + this->HeapTree->EntryPad = pad_amount; + this->HeapTree->EntrySize = sz; - this->HeapTree->MemoryEntry = ptr; + this->HeapTree->Entry = ptr; - this->HeapTree->MemoryColor = USER_HEAP_TREE::kBlackMemory; + this->HeapTree->Color = kBlackTreeKind; - this->HeapTree->MemoryPrev = nullptr; - this->HeapTree->MemoryNext = nullptr; - this->HeapTree->MemoryParent = nullptr; - this->HeapTree->MemoryChild = nullptr; + this->HeapTree->Prev = nullptr; + this->HeapTree->Next = nullptr; + this->HeapTree->Parent = nullptr; + this->HeapTree->Child = nullptr; } else { - USER_HEAP_TREE* entry = this->HeapTree; - USER_HEAP_TREE* prev_entry = entry; + PROCESS_HEAP_TREE<VoidPtr>* entry = this->HeapTree; + PROCESS_HEAP_TREE<VoidPtr>* prev_entry = entry; BOOL is_parent = NO; while (entry) { - if (entry->MemoryEntrySize < 1) break; + if (entry->EntrySize < 1) break; prev_entry = entry; - if (entry->MemoryColor == USER_HEAP_TREE::kBlackMemory) break; + if (entry->Color == kBlackTreeKind) break; - if (entry->MemoryChild && entry->MemoryChild->MemoryEntrySize > 0 && - entry->MemoryChild->MemoryEntrySize == sz) { - entry = entry->MemoryChild; + if (entry->Child && entry->Child->EntrySize > 0 && entry->Child->EntrySize == sz) { + entry = entry->Child; is_parent = YES; - } else if (entry->MemoryNext && entry->MemoryChild->MemoryEntrySize > 0 && - entry->MemoryNext->MemoryEntrySize == sz) { + } else if (entry->Next && entry->Child->EntrySize > 0 && entry->Next->EntrySize == sz) { is_parent = NO; - entry = entry->MemoryNext; + entry = entry->Next; } else { - entry = sched_try_go_upper_heap_tree(entry); - if (entry && entry->MemoryColor == USER_HEAP_TREE::kBlackMemory) break; + entry = sched_try_go_upper_ptr_tree(entry); + if (entry && entry->Color == kBlackTreeKind) break; } } - auto new_entry = new USER_HEAP_TREE(); + auto new_entry = new PROCESS_HEAP_TREE<VoidPtr>(); - new_entry->MemoryEntry = ptr; - new_entry->MemoryEntrySize = sz; - new_entry->MemoryEntryPad = pad_amount; - new_entry->MemoryParent = entry; - new_entry->MemoryChild = nullptr; - new_entry->MemoryNext = nullptr; - new_entry->MemoryPrev = nullptr; + new_entry->Entry = ptr; + new_entry->EntrySize = sz; + new_entry->EntryPad = pad_amount; + new_entry->Parent = entry; + new_entry->Child = nullptr; + new_entry->Next = nullptr; + new_entry->Prev = nullptr; - new_entry->MemoryColor = USER_HEAP_TREE::kBlackMemory; - prev_entry->MemoryColor = USER_HEAP_TREE::kRedMemory; + new_entry->Color = kBlackTreeKind; + prev_entry->Color = kRedTreeKind; if (is_parent) { - prev_entry->MemoryChild = new_entry; - new_entry->MemoryParent = prev_entry; + prev_entry->Child = new_entry; + new_entry->Parent = prev_entry; } else { - prev_entry->MemoryNext = new_entry; - new_entry->MemoryPrev = prev_entry; + prev_entry->Next = new_entry; + new_entry->Prev = prev_entry; } } @@ -243,20 +237,20 @@ const AffinityKind& USER_PROCESS::GetAffinity() noexcept { /** @brief Free heap tree. */ /***********************************************************************************/ -STATIC Void sched_free_heap_tree(USER_PROCESS::USER_HEAP_TREE* memory_heap_list) { +STATIC Void sched_free_ptr_tree(PROCESS_HEAP_TREE<VoidPtr>* memory_ptr_list) { // Deleting memory lists. Make sure to free all of them. - while (memory_heap_list) { - if (memory_heap_list->MemoryEntry) { - MUST_PASS(mm_delete_heap(memory_heap_list->MemoryEntry)); + while (memory_ptr_list) { + if (memory_ptr_list->Entry) { + MUST_PASS(mm_free_ptr(memory_ptr_list->Entry)); } - auto next = memory_heap_list->MemoryNext; + auto next = memory_ptr_list->Next; - mm_delete_heap(memory_heap_list); + mm_free_ptr(memory_ptr_list); - if (memory_heap_list->MemoryChild) sched_free_heap_tree(memory_heap_list->MemoryChild); + if (memory_ptr_list->Child) sched_free_ptr_tree(memory_ptr_list->Child); - memory_heap_list = next; + memory_ptr_list = next; } } @@ -273,14 +267,16 @@ Void USER_PROCESS::Exit(const Int32& exit_code) { kLastExitCode = exit_code; - auto memory_heap_list = this->HeapTree; + --this->ParentTeam->mProcessCount; + + auto memory_ptr_list = this->HeapTree; #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - auto pd = kKernelCR3; + auto pd = kKernelVM; hal_write_cr3(this->VMRegister); #endif - sched_free_heap_tree(memory_heap_list); + sched_free_ptr_tree(memory_ptr_list); #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ hal_write_cr3(pd); @@ -292,24 +288,19 @@ Void USER_PROCESS::Exit(const Int32& exit_code) { #endif //! Delete image if not done already. - if (this->Image.fCode && mm_is_valid_heap(this->Image.fCode)) mm_delete_heap(this->Image.fCode); + if (this->Image.fCode && mm_is_valid_ptr(this->Image.fCode)) mm_free_ptr(this->Image.fCode); //! Delete blob too. - if (this->Image.fBlob && mm_is_valid_heap(this->Image.fBlob)) mm_delete_heap(this->Image.fBlob); + if (this->Image.fBlob && mm_is_valid_ptr(this->Image.fBlob)) mm_free_ptr(this->Image.fBlob); //! Delete stack frame. - if (this->StackFrame && mm_is_valid_heap(this->StackFrame)) - mm_delete_heap((VoidPtr) this->StackFrame); - - //! Delete stack reserve. - if (this->StackReserve && mm_is_valid_heap(this->StackReserve)) - mm_delete_heap(reinterpret_cast<VoidPtr>(this->StackReserve)); + if (this->StackFrame && mm_is_valid_ptr(this->StackFrame)) + mm_free_ptr((VoidPtr) this->StackFrame); //! Avoid use after free. - this->Image.fBlob = nullptr; - this->Image.fCode = nullptr; - this->StackFrame = nullptr; - this->StackReserve = nullptr; + this->Image.fBlob = nullptr; + this->Image.fCode = nullptr; + this->StackFrame = nullptr; if (this->Kind == kExecutableDylibKind) { Bool success = false; @@ -330,6 +321,34 @@ Void USER_PROCESS::Exit(const Int32& exit_code) { } /***********************************************************************************/ +/// @brief Add dylib to process. +/***********************************************************************************/ + +Bool USER_PROCESS::SpawnDylib() { + // React according to process kind. + switch (this->Kind) { + case USER_PROCESS::kExecutableDylibKind: { + this->DylibDelegate = rtl_init_dylib_pef(*this); + + if (!this->DylibDelegate) { + this->Crash(); + return NO; + } + + return YES; + } + case USER_PROCESS::kExecutableKind: { + return NO; + } + default: { + (Void)(kout << "Unknown process kind: " << hex_number(this->Kind) << kendl); + this->Crash(); + return NO; + } + } +} + +/***********************************************************************************/ /// @brief Add process to team. /// @param process the process *Ref* class. /// @return the process index inside the team. @@ -366,18 +385,9 @@ ProcessID UserProcessScheduler::Spawn(const Char* name, VoidPtr code, VoidPtr im rt_copy_memory(reinterpret_cast<VoidPtr>(const_cast<Char*>(name)), process.Name, len); #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - process.VMRegister = new PDE(); - - if (!process.VMRegister) { - process.Crash(); - return -kErrorProcessFault; - } - - UInt32 flags = HAL::kMMFlagsPresent; - flags |= HAL::kMMFlagsWr; - flags |= HAL::kMMFlagsUser; - - HAL::mm_map_page((VoidPtr) process.VMRegister, process.VMRegister, flags); + process.VMRegister = kKernelVM; +#else + process.VMRegister = 0UL; #endif // ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ process.StackFrame = new HAL::StackFrame(); @@ -389,52 +399,27 @@ ProcessID UserProcessScheduler::Spawn(const Char* name, VoidPtr code, VoidPtr im rt_set_memory(process.StackFrame, 0, sizeof(HAL::StackFrame)); -#ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - flags = HAL::kMMFlagsPresent; - flags |= HAL::kMMFlagsWr; - flags |= HAL::kMMFlagsUser; + process.StackFrame->IP = reinterpret_cast<UIntPtr>(code); + process.StackFrame->SP = reinterpret_cast<UIntPtr>(&process.StackReserve[0] + process.StackSize); - HAL::mm_map_page((VoidPtr) process.StackFrame, process.StackFrame, flags); +#ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ + HAL::mm_map_page((VoidPtr) process.StackFrame->IP, + (VoidPtr) HAL::mm_get_page_addr((VoidPtr) process.StackFrame->IP), + HAL::kMMFlagsUser | HAL::kMMFlagsPresent); + HAL::mm_map_page((VoidPtr) process.StackFrame->SP, + (VoidPtr) HAL::mm_get_page_addr((VoidPtr) process.StackFrame->SP), + HAL::kMMFlagsUser | HAL::kMMFlagsPresent); #endif // ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - // React according to process kind. - switch (process.Kind) { - case USER_PROCESS::kExecutableDylibKind: { - process.DylibDelegate = rtl_init_dylib_pef(process); - MUST_PASS(process.DylibDelegate); - break; - } - case USER_PROCESS::kExecutableKind: { - break; - } - default: { - (Void)(kout << "Unknown process kind: " << hex_number(process.Kind) << kendl); - break; - } - } - - process.StackReserve = new UInt8[process.StackSize]; - - if (!process.StackReserve) { - process.Crash(); - return -kErrorProcessFault; - } + process.StackSize = kSchedMaxStackSz; rt_set_memory(process.StackReserve, 0, process.StackSize); -#ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - flags = HAL::kMMFlagsPresent; - flags |= HAL::kMMFlagsWr; - flags |= HAL::kMMFlagsUser; - - HAL::mm_map_page((VoidPtr) process.StackReserve, process.StackReserve, flags); -#endif // ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ - process.ParentTeam = &mTeam; process.ProcessId = pid; - process.Status = ProcessStatusKind::kStarting; - process.PTime = (UIntPtr) AffinityKind::kStandard; + process.Status = ProcessStatusKind::kRunning; + process.PTime = 0; process.RTime = 0; (Void)(kout << "PID: " << number(process.ProcessId) << kendl); @@ -448,6 +433,7 @@ ProcessID UserProcessScheduler::Spawn(const Char* name, VoidPtr code, VoidPtr im /***********************************************************************************/ UserProcessScheduler& UserProcessScheduler::The() { + STATIC UserProcessScheduler kScheduler; return kScheduler; } @@ -501,10 +487,6 @@ SizeT UserProcessScheduler::Run() noexcept { return 0UL; } - if (kCurrentlySwitching) return 0UL; - - kCurrentlySwitching = Yes; - SizeT process_index = 0UL; //! we store this guy to tell the scheduler how many //! things we have scheduled. @@ -513,65 +495,42 @@ SizeT UserProcessScheduler::Run() noexcept { //! Check if the process needs to be run. if (UserProcessHelper::CanBeScheduled(process)) { - if (process.StackSize > kSchedMaxStackSz) { - kout << "The process: " << process.Name << ", has not a valid stack size! Crashing it...\r"; - process.Crash(); - continue; - } + kout << process.Name << " will be run...\r"; - // Set current process header. - this->CurrentProcess() = process; + //! Increase the usage time of the process. + if (process.UTime < process.PTime) { + ++process.UTime; + } - process.PTime = static_cast<Int32>(process.Affinity); + this->TheCurrentProcess() = process; - // tell helper to find a core to schedule on. - BOOL ret = UserProcessHelper::Switch(process.Image.Leak().Leak().Leak(), - &process.StackReserve[process.StackSize - 1], - process.StackFrame, process.ProcessId); + if (UserProcessHelper::Switch(process.StackFrame, process.ProcessId)) { + process.PTime = static_cast<Int32>(process.Affinity); - if (!ret) { - kout << "The process: " << process.Name << ", is not valid! Crashing it...\r"; - process.Crash(); - } - } else { - if (process.ProcessId == this->CurrentProcess().Leak().ProcessId) { - if (process.PTime < process.RTime) { - if (process.RTime < (Int32) AffinityKind::kRealTime) - process.PTime = (Int32) AffinityKind::kVeryLowUsage; - else if (process.RTime < (Int32) AffinityKind::kVeryHigh) - process.PTime = (Int32) AffinityKind::kLowUsage; + if (process.PTime < process.RTime && AffinityKind::kRealTime != process.Affinity) { + if (process.RTime < (Int32) AffinityKind::kVeryHigh) + process.RTime = (Int32) AffinityKind::kLowUsage / 2; else if (process.RTime < (Int32) AffinityKind::kHigh) - process.PTime = (Int32) AffinityKind::kStandard; + process.RTime = (Int32) AffinityKind::kStandard / 3; else if (process.RTime < (Int32) AffinityKind::kStandard) - process.PTime = (Int32) AffinityKind::kHigh; - - process.RTime = static_cast<Int32>(process.Affinity); + process.RTime = (Int32) AffinityKind::kHigh / 4; - BOOL ret = UserProcessHelper::Switch(process.Image.Leak().Leak().Leak(), - &process.StackReserve[process.StackSize - 1], - process.StackFrame, process.ProcessId); - - if (!ret) { - kout << "The process: " << process.Name << ", is not valid! Crashing it...\r"; - process.Crash(); - } - } else { - ++process.RTime; + process.PTime -= process.RTime; + process.RTime = 0UL; } } - + } else { + ++process.RTime; --process.PTime; } } - kCurrentlySwitching = No; - return process_index; } /// @brief Gets the current scheduled team. /// @return -UserProcessTeam& UserProcessScheduler::CurrentTeam() { +UserProcessTeam& UserProcessScheduler::TheCurrentTeam() { return mTeam; } @@ -585,26 +544,24 @@ UserProcessTeam& UserProcessScheduler::CurrentTeam() { BOOL UserProcessScheduler::SwitchTeam(UserProcessTeam& team) { if (team.AsArray().Count() < 1) return No; - if (kCurrentlySwitching) return No; - - kScheduler.mTeam = team; + this->mTeam = team; return Yes; } /// @brief Gets current running process. /// @return -Ref<USER_PROCESS>& UserProcessScheduler::CurrentProcess() { +Ref<USER_PROCESS>& UserProcessScheduler::TheCurrentProcess() { return mTeam.AsRef(); } /// @brief Current proccess id getter. /// @return USER_PROCESS ID integer. ErrorOr<PID> UserProcessHelper::TheCurrentPID() { - if (!kScheduler.CurrentProcess()) return ErrorOr<PID>{-kErrorProcessFault}; + if (!UserProcessScheduler::The().TheCurrentProcess()) return ErrorOr<PID>{-kErrorProcessFault}; kout << "UserProcessHelper::TheCurrentPID: Leaking ProcessId...\r"; - return ErrorOr<PID>{kScheduler.CurrentProcess().Leak().ProcessId}; + return ErrorOr<PID>{UserProcessScheduler::The().TheCurrentProcess().Leak().ProcessId}; } /// @brief Check if process can be schedulded. @@ -612,32 +569,28 @@ ErrorOr<PID> UserProcessHelper::TheCurrentPID() { /// @retval true can be schedulded. /// @retval false cannot be schedulded. Bool UserProcessHelper::CanBeScheduled(const USER_PROCESS& process) { - if (process.Status == ProcessStatusKind::kKilled || - process.Status == ProcessStatusKind::kFinished || - process.Status == ProcessStatusKind::kStarting || - process.Status == ProcessStatusKind::kFrozen) - return No; - - if (process.Status == ProcessStatusKind::kInvalid) return No; - - if (!process.Image.HasCode()) return No; - + if (process.Status != ProcessStatusKind::kRunning) return No; + if (process.StackSize > kSchedMaxStackSz) return No; if (!process.Name[0]) return No; // real time processes shouldn't wait that much. if (process.Affinity == AffinityKind::kRealTime) return Yes; + if (process.Signal.SignalID == SIGTRAP) { + return No; + } + return process.PTime < 1; } /***********************************************************************************/ /** - * @brief Start scheduling current AP. + * @brief Start scheduling the current team. */ /***********************************************************************************/ SizeT UserProcessHelper::StartScheduling() { - return kScheduler.Run(); + return UserProcessScheduler::The().Run(); } /***********************************************************************************/ @@ -648,41 +601,35 @@ SizeT UserProcessHelper::StartScheduling() { */ /***********************************************************************************/ -Bool UserProcessHelper::Switch(VoidPtr image, UInt8* stack, HAL::StackFramePtr frame_ptr, - PID new_pid) { +Bool UserProcessHelper::Switch(HAL::StackFramePtr frame_ptr, PID new_pid) { + (Void)(kout << "IP: " << hex_number(frame_ptr->IP) << kendl); + for (SizeT index = 0UL; index < HardwareThreadScheduler::The().Capacity(); ++index) { + if (!HardwareThreadScheduler::The()[index].Leak()) continue; + if (HardwareThreadScheduler::The()[index].Leak()->Kind() == kAPInvalid || HardwareThreadScheduler::The()[index].Leak()->Kind() == kAPBoot) continue; - // A fallback is a special core for real-time tasks which needs immediate execution. - if (HardwareThreadScheduler::The()[index].Leak()->Kind() == kAPRealTime) { - if (UserProcessScheduler::The().CurrentTeam().AsArray()[new_pid].Affinity != - AffinityKind::kRealTime) - continue; - - if (HardwareThreadScheduler::The()[index].Leak()->Switch(image, stack, frame_ptr, new_pid)) { - HardwareThreadScheduler::The()[index].Leak()->fPTime = - UserProcessScheduler::The().CurrentTeam().AsArray()[new_pid].PTime; + (Void)(kout << "AP_" << hex_number(index) << kendl); - UserProcessHelper::TheCurrentPID().Leak().Leak() = UserProcessHelper::TheCurrentPID(); - - return YES; - } + if (HardwareThreadScheduler::The()[index].Leak()->IsBusy()) { + (Void)(kout << "AP_" << hex_number(index)); + kout << " is busy\r"; continue; } - if (UserProcessScheduler::The().CurrentTeam().AsArray()[new_pid].Affinity == - AffinityKind::kRealTime) - continue; + (Void)(kout << "AP_" << hex_number(index)); + kout << " is now trying to run a new task!\r"; //////////////////////////////////////////////////////////// /// Prepare task switch. /// //////////////////////////////////////////////////////////// - Bool ret = - HardwareThreadScheduler::The()[index].Leak()->Switch(image, stack, frame_ptr, new_pid); + HardwareThreadScheduler::The()[index].Leak()->Busy(YES); + + Bool ret = HardwareThreadScheduler::The()[index].Leak()->Switch(frame_ptr); //////////////////////////////////////////////////////////// /// Rollback on fail. /// @@ -693,26 +640,40 @@ Bool UserProcessHelper::Switch(VoidPtr image, UInt8* stack, HAL::StackFramePtr f UserProcessHelper::TheCurrentPID().Leak().Leak() = new_pid; HardwareThreadScheduler::The()[index].Leak()->fPTime = - UserProcessScheduler::The().CurrentTeam().AsArray()[new_pid].PTime; - HardwareThreadScheduler::The()[index].Leak()->Wake(YES); + UserProcessScheduler::The().TheCurrentTeam().AsArray()[new_pid].PTime; + + (Void)(kout << "AP_" << hex_number(index)); + kout << " is now running a new task!\r"; - return Yes; + return YES; } - return No; + kout << "Couldn't find a suitable core for the current process!\r"; + + return NO; } //////////////////////////////////////////////////////////// /// @brief this checks if any process is on the team. //////////////////////////////////////////////////////////// UserProcessScheduler::operator bool() { - return mTeam.AsArray().Count() > 0; + for (auto process_index = 0UL; process_index < mTeam.AsArray().Count(); ++process_index) { + auto& process = mTeam.AsArray()[process_index]; + if (UserProcessHelper::CanBeScheduled(process)) return true; + } + + return false; } //////////////////////////////////////////////////////////// /// @brief this checks if no process is on the team. //////////////////////////////////////////////////////////// Bool UserProcessScheduler::operator!() { - return mTeam.AsArray().Count() == 0; + for (auto process_index = 0UL; process_index < mTeam.AsArray().Count(); ++process_index) { + auto& process = mTeam.AsArray()[process_index]; + if (UserProcessHelper::CanBeScheduled(process)) return false; + } + + return true; } } // namespace Kernel diff --git a/dev/kernel/src/UserProcessTeam.cc b/dev/kernel/src/UserProcessTeam.cc index 7acbcf8d..8ef9a013 100644 --- a/dev/kernel/src/UserProcessTeam.cc +++ b/dev/kernel/src/UserProcessTeam.cc @@ -14,9 +14,11 @@ namespace Kernel { UserProcessTeam::UserProcessTeam() { for (SizeT i = 0U; i < this->mProcessList.Count(); ++i) { - this->mProcessList[i] = USER_PROCESS(); - this->mProcessList[i].PTime = 0; - this->mProcessList[i].Status = ProcessStatusKind::kKilled; + this->mProcessList[i] = USER_PROCESS(); + this->mProcessList[i].PTime = 0; + this->mProcessList[i].RTime = 0; + this->mProcessList[i].Status = ProcessStatusKind::kKilled; + this->mProcessList[i].ParentTeam = this; } this->mProcessCount = 0UL; diff --git a/dev/kernel/src/UtfUtils.cc b/dev/kernel/src/UtfUtils.cc index 11d7471a..ae17aed7 100644 --- a/dev/kernel/src/UtfUtils.cc +++ b/dev/kernel/src/UtfUtils.cc @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include <NewKit/Utils.h> +#include <NeKit/Utils.h> namespace Kernel { Size urt_string_len(const Utf8Char* str) { @@ -25,7 +25,17 @@ Void urt_set_memory(const voidPtr src, UInt32 dst, Size len) { } } -Int urt_copy_memory(const voidPtr src, voidPtr dst, Size len) { +Int32 rt_string_cmp(const Utf8Char* src, const Utf8Char* cmp, Size size) { + Int32 counter = 0; + + for (Size index = 0; index < size; ++index) { + if (src[index] != cmp[index]) ++counter; + } + + return counter; +} + +Int urt_copy_memory(const VoidPtr src, VoidPtr dst, Size len) { Utf8Char* srcChr = reinterpret_cast<Utf8Char*>(src); Utf8Char* dstChar = reinterpret_cast<Utf8Char*>(dst); diff --git a/dev/kernel/src/Utils.cc b/dev/kernel/src/Utils.cc index 4f47849b..d7cc08af 100644 --- a/dev/kernel/src/Utils.cc +++ b/dev/kernel/src/Utils.cc @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include <NewKit/Utils.h> +#include <NeKit/Utils.h> namespace Kernel { Int32 rt_string_cmp(const Char* src, const Char* cmp, Size size) { diff --git a/dev/kernel/src/Variant.cc b/dev/kernel/src/Variant.cc index 5dd39926..51e5f5b8 100644 --- a/dev/kernel/src/Variant.cc +++ b/dev/kernel/src/Variant.cc @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include <NewKit/Variant.h> +#include <NeKit/Variant.h> namespace Kernel { const Char* Variant::ToString() { |
