From 1069f411d413e2185f6536b01b8993187056fcd8 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 4 Sep 2024 22:20:17 +0200 Subject: [ IMP ] BMP allocator needs more tweaking and fixes, to be usable. Signed-off-by: Amlal El Mahrouss --- dev/ZKA/Sources/CodeMgr.cxx | 2 +- dev/ZKA/Sources/DLLInterface.cxx | 15 --- dev/ZKA/Sources/DLLMain.cxx | 2 + dev/ZKA/Sources/Heap.cxx | 16 +-- dev/ZKA/Sources/IDLLObject.cxx | 15 +++ dev/ZKA/Sources/IPEFDLLObject.cxx | 100 ++++++++++++++++++ dev/ZKA/Sources/PEFDLLInterface.cxx | 100 ------------------ dev/ZKA/Sources/UserProcessScheduler.cxx | 169 +++++++++++++++---------------- 8 files changed, 206 insertions(+), 213 deletions(-) delete mode 100644 dev/ZKA/Sources/DLLInterface.cxx create mode 100644 dev/ZKA/Sources/IDLLObject.cxx create mode 100644 dev/ZKA/Sources/IPEFDLLObject.cxx delete mode 100644 dev/ZKA/Sources/PEFDLLInterface.cxx (limited to 'dev/ZKA/Sources') diff --git a/dev/ZKA/Sources/CodeMgr.cxx b/dev/ZKA/Sources/CodeMgr.cxx index df5b0917..c4ff2ee3 100644 --- a/dev/ZKA/Sources/CodeMgr.cxx +++ b/dev/ZKA/Sources/CodeMgr.cxx @@ -23,7 +23,7 @@ namespace Kernel proc.SetImageStart(reinterpret_cast(main)); proc.Kind = UserProcess::kExeKind; - proc.StackSize = kib_cast(8); + proc.StackSize = kib_cast(4); rt_copy_memory((VoidPtr)processName, proc.Name, rt_string_len(processName)); diff --git a/dev/ZKA/Sources/DLLInterface.cxx b/dev/ZKA/Sources/DLLInterface.cxx deleted file mode 100644 index c18f2f00..00000000 --- a/dev/ZKA/Sources/DLLInterface.cxx +++ /dev/null @@ -1,15 +0,0 @@ -/* - * ======================================================== - * - * newoskrnl - * Copyright ZKA Technologies., all rights reserved. - * - * ======================================================== - */ - -#include -#include - -#include - -using namespace Kernel; diff --git a/dev/ZKA/Sources/DLLMain.cxx b/dev/ZKA/Sources/DLLMain.cxx index 88887ec3..75fb24ff 100644 --- a/dev/ZKA/Sources/DLLMain.cxx +++ b/dev/ZKA/Sources/DLLMain.cxx @@ -189,6 +189,8 @@ EXTERN_C Kernel::Void ke_dll_entrypoint(Kernel::Void) CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.exe: Starting ZKA System...", 20, 10, RGB(0, 0, 0)); + Kernel::UserProcessHelper::Init(); + Kernel::sched_execute_thread(HangCPU, "HANG TEST"); while (Yes) diff --git a/dev/ZKA/Sources/Heap.cxx b/dev/ZKA/Sources/Heap.cxx index 615f5664..0b54cbcd 100644 --- a/dev/ZKA/Sources/Heap.cxx +++ b/dev/ZKA/Sources/Heap.cxx @@ -63,26 +63,26 @@ namespace Kernel Detail::HEAP_INFORMATION_BLOCK_PTR kLatestAllocation = nullptr; - /// @brief Declare a new size for allocatedPtr. - /// @param allocatedPtr the pointer. + /// @brief Declare a new size for ptr_heap. + /// @param ptr_heap the pointer. /// @return - voidPtr mm_realloc_ke_heap(voidPtr allocatedPtr, SizeT newSz) + voidPtr mm_realloc_ke_heap(voidPtr ptr_heap, SizeT new_sz) { - if (!allocatedPtr || newSz < 1) + if (!ptr_heap || new_sz < 1) return nullptr; Detail::HEAP_INFORMATION_BLOCK_PTR heap_blk = reinterpret_cast( - (UIntPtr)allocatedPtr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + (UIntPtr)ptr_heap - sizeof(Detail::HEAP_INFORMATION_BLOCK)); - heap_blk->fHeapSize = newSz; + heap_blk->fHeapSize = new_sz; if (heap_blk->fCRC32 > 0) { - MUST_PASS(mm_protect_ke_heap(allocatedPtr)); + MUST_PASS(mm_protect_ke_heap(ptr_heap)); } - return allocatedPtr; + return ptr_heap; } /// @brief Allocate chunk of memory. diff --git a/dev/ZKA/Sources/IDLLObject.cxx b/dev/ZKA/Sources/IDLLObject.cxx new file mode 100644 index 00000000..b6b6a348 --- /dev/null +++ b/dev/ZKA/Sources/IDLLObject.cxx @@ -0,0 +1,15 @@ +/* + * ======================================================== + * + * newoskrnl + * Copyright ZKA Technologies., all rights reserved. + * + * ======================================================== + */ + +#include +#include + +#include + +using namespace Kernel; diff --git a/dev/ZKA/Sources/IPEFDLLObject.cxx b/dev/ZKA/Sources/IPEFDLLObject.cxx new file mode 100644 index 00000000..913912a4 --- /dev/null +++ b/dev/ZKA/Sources/IPEFDLLObject.cxx @@ -0,0 +1,100 @@ +/* + * ======================================================== + * + * newoskrnl + * Copyright ZKA Technologies., all rights reserved. + * + * ======================================================== + */ + +#include +#include +#include +#include +#include +#include + +/* ------------------------------------------- + + Revision History: + + 01/02/24: Rework shared sharedObj ABI, except a rtl_init_shared_object and + rtl_fini_shared_object (amlel) 15/02/24: Breaking changes, changed the name of the + routines. (amlel) + + 07/28/24: Replace rt_library_free with rtl_fini_shared_object + + ------------------------------------------- */ + +using namespace Kernel; + +/***********************************************************************************/ +/// @file PEFSharedObjectRT.cxx +/// @brief PEF's shared object runtime. +/***********************************************************************************/ + +/***********************************************************************************/ +/** @brief Library initializer. */ +/***********************************************************************************/ + +EXTERN_C IDLL rtl_init_shared_object(UserProcess* header) +{ + IDLL sharedObj = tls_new_class(); + + if (!sharedObj) + { + header->Crash(); + + return nullptr; + } + + sharedObj->Mount(tls_new_class()); + + if (!sharedObj->Get()) + { + header->Crash(); + + return nullptr; + } + + sharedObj->Get()->fImageObject = + header->Image; + + if (!sharedObj->Get()->fImageObject) + { + header->Crash(); + + return nullptr; + } + + sharedObj->Get()->fImageEntrypointOffset = + sharedObj->Load(kPefStart, rt_string_len(kPefStart, 0), kPefCode); + + return sharedObj; +} + +/***********************************************************************************/ +/** @brief Frees the sharedObj. */ +/** @note Please check if the lib got freed! */ +/** @param lib The sharedObj to free. */ +/** @param successful Reports if successful or not. */ +/***********************************************************************************/ + +EXTERN_C Void rtl_fini_shared_object(UserProcess* header, IDLL lib, Bool* successful) +{ + MUST_PASS(successful); + + // sanity check (will also trigger a bug check if this fails) + if (lib == nullptr) + { + *successful = false; + header->Crash(); + } + + delete lib->Get(); + delete lib; + + lib = nullptr; + + *successful = true; +} diff --git a/dev/ZKA/Sources/PEFDLLInterface.cxx b/dev/ZKA/Sources/PEFDLLInterface.cxx deleted file mode 100644 index 70c68783..00000000 --- a/dev/ZKA/Sources/PEFDLLInterface.cxx +++ /dev/null @@ -1,100 +0,0 @@ -/* - * ======================================================== - * - * newoskrnl - * Copyright ZKA Technologies., all rights reserved. - * - * ======================================================== - */ - -#include -#include -#include -#include -#include -#include - -/* ------------------------------------------- - - Revision History: - - 01/02/24: Rework shared sharedObj ABI, except a rtl_init_shared_object and - rtl_fini_shared_object (amlel) 15/02/24: Breaking changes, changed the name of the - routines. (amlel) - - 07/28/24: Replace rt_library_free with rtl_fini_shared_object - - ------------------------------------------- */ - -using namespace Kernel; - -/***********************************************************************************/ -/// @file PEFSharedObjectRT.cxx -/// @brief PEF's shared object runtime. -/***********************************************************************************/ - -/***********************************************************************************/ -/** @brief Library initializer. */ -/***********************************************************************************/ - -EXTERN_C DLLInterfacePtr rtl_init_shared_object(UserProcess* header) -{ - DLLInterfacePtr sharedObj = tls_new_class(); - - if (!sharedObj) - { - header->Crash(); - - return nullptr; - } - - sharedObj->Mount(tls_new_class()); - - if (!sharedObj->Get()) - { - header->Crash(); - - return nullptr; - } - - sharedObj->Get()->fImageObject = - header->Image; - - if (!sharedObj->Get()->fImageObject) - { - header->Crash(); - - return nullptr; - } - - sharedObj->Get()->fImageEntrypointOffset = - sharedObj->Load(kPefStart, rt_string_len(kPefStart, 0), kPefCode); - - return sharedObj; -} - -/***********************************************************************************/ -/** @brief Frees the sharedObj. */ -/** @note Please check if the lib got freed! */ -/** @param lib The sharedObj to free. */ -/** @param successful Reports if successful or not. */ -/***********************************************************************************/ - -EXTERN_C Void rtl_fini_shared_object(UserProcess* header, DLLInterfacePtr lib, Bool* successful) -{ - MUST_PASS(successful); - - // sanity check (will also trigger a bug check if this fails) - if (lib == nullptr) - { - *successful = false; - header->Crash(); - } - - delete lib->Get(); - delete lib; - - lib = nullptr; - - *successful = true; -} diff --git a/dev/ZKA/Sources/UserProcessScheduler.cxx b/dev/ZKA/Sources/UserProcessScheduler.cxx index 48988e57..7c576226 100644 --- a/dev/ZKA/Sources/UserProcessScheduler.cxx +++ b/dev/ZKA/Sources/UserProcessScheduler.cxx @@ -6,11 +6,11 @@ /***********************************************************************************/ /// @file UserProcessScheduler.cxx -/// @brief User UserProcess scheduler. +/// @brief User Process scheduler. /***********************************************************************************/ #include -#include +#include #include #include #include @@ -19,7 +19,7 @@ ///! BUGS: 0 /***********************************************************************************/ -/* TODO: Document more the Kernel, sdk and kits. */ +/* TODO: Document the Kernel, SDK and kits. */ /***********************************************************************************/ namespace Kernel @@ -53,7 +53,7 @@ namespace Kernel if (this->Name == 0) return; - kcout << this->Name << ": crashed, ID = " << number(kErrorProcessFault) << endl; + kcout << this->Name << ": crashed, ID = " << number(kErrorProcessFault) << endl; this->Exit(kErrorProcessFault); } @@ -83,27 +83,45 @@ namespace Kernel /***********************************************************************************/ + /** @brief Add pointer to entry. */ VoidPtr UserProcess::New(const SizeT& sz) { - if (this->HeapCursor) +#ifdef __ZKA_AMD64__ + auto pd = hal_read_cr3(); + hal_write_cr3(this->MemoryPD); + + auto ptr = mm_new_ke_heap(sz, Yes, Yes); + + hal_write_cr3(reinterpret_cast(pd)); +#else + auto ptr = mm_new_ke_heap(sz, Yes, Yes); +#endif + + if (!this->MemoryEntryList) { - if (this->FreeMemory < 1) - { - ErrLocal() = kErrorHeapOutOfMemory; + this->MemoryEntryList = new UserProcess::PROCESS_MEMORY_ENTRY(); + this->MemoryEntryList->MemoryEntry = ptr; - /* We're going out of memory! crash... */ - this->Crash(); + this->MemoryEntryList->MemoryPrev = nullptr; + this->MemoryEntryList->MemoryNext = nullptr; - return nullptr; - } + return ptr; + } + else + { + auto entry = this->MemoryEntryList; - this->HeapCursor = reinterpret_cast((UIntPtr)this->HeapCursor + (sizeof(sz))); - VoidPtr cursor = this->HeapCursor; + while (entry->MemoryNext) + { + if (entry->MemoryNext) + entry = entry->MemoryNext; + } - ++this->UsedMemory; - --this->FreeMemory; + entry->MemoryNext = new UserProcess::PROCESS_MEMORY_ENTRY(); + entry->MemoryNext->MemoryEntry = ptr; - return cursor; + entry->MemoryNext->MemoryPrev = entry; + entry->MemoryNext->MemoryNext = nullptr; } return nullptr; @@ -111,39 +129,31 @@ namespace Kernel /***********************************************************************************/ - /* @brief checks if runtime pointer is in region. */ - bool rt_is_in_pool(VoidPtr pool_ptr, VoidPtr pool, const SizeT& pool_ptr_cur_sz, const SizeT& pool_ptr_used_sz) - { - if (pool == nullptr || - pool_ptr == nullptr) - return false; - - UIntPtr* uint_pool_ptr = (UIntPtr*)pool_ptr; - UIntPtr* uint_pool = (UIntPtr*)pool; - - return (UIntPtr)&uint_pool > (UIntPtr)&uint_pool_ptr && - pool_ptr_cur_sz > pool_ptr_used_sz; - } - - /* @brief free pointer from usage. */ + /** @brief Free pointer from usage. */ Boolean UserProcess::Delete(VoidPtr ptr, const SizeT& sz) { - if (sz < 1 || this->HeapCursor == this->HeapPtr) - return false; - - // also check for the amount of allocations we've done so far. - if (this->UsedMemory < 1) - return false; + auto entry = this->MemoryEntryList; - if (rt_is_in_pool(ptr, this->HeapCursor, this->UsedMemory, this->FreeMemory)) + while (entry->MemoryNext) { - this->HeapCursor = (VoidPtr)((UIntPtr)this->HeapCursor - (sizeof(sz))); - rt_zero_memory(ptr, sz); + if (entry->MemoryEntry == ptr) + { +#ifdef __ZKA_AMD64__ + auto pd = hal_read_cr3(); + hal_write_cr3(this->MemoryPD); - ++this->FreeMemory; - --this->UsedMemory; + bool ret = mm_delete_ke_heap(ptr); + hal_write_cr3(reinterpret_cast(pd)); - return true; + return ret; +#else + bool ret = mm_delete_ke_heap(ptr); + return ret; +#endif + } + + if (entry->MemoryNext) + entry = entry->MemoryNext; } return false; @@ -183,7 +193,7 @@ namespace Kernel void UserProcess::Exit(const Int32& exit_code) { this->Status = ProcessStatusKind::kDead; - + fLastExitCode = exit_code; cLastExitCode = exit_code; @@ -211,7 +221,8 @@ namespace Kernel if (this->StackReserve) delete[] this->StackReserve; - cProcessScheduler->Remove(this->ProcessId); + if (this->ProcessId > 0) + UserProcessScheduler::The().Remove(this->ProcessId); } /// @brief Add process to list. @@ -219,73 +230,50 @@ namespace Kernel /// @return the process index inside the team. SizeT UserProcessScheduler::Add(UserProcess& process) { - if (!process.Image) - { - return -kErrorInvalidData; - } +#ifdef __ZKA_AMD64__ + process.MemoryPD = reinterpret_cast(hal_read_cr3()); +#endif // __ZKA_AMD64__ - kcout << "UserProcessScheduler: Adding process to team...\r"; + process.StackFrame = (HAL::StackFrame*)process.New(sizeof(HAL::StackFrame)); - // Create heap according to type of process. - if (process.Kind == UserProcess::kExeKind) - { - process.HeapPtr = mm_new_ke_heap(process.SizeMemory, true, true); - } - else if (process.Kind == UserProcess::kDLLKind) - { - process.DLLPtr = rtl_init_shared_object(&process); - process.HeapPtr = mm_new_ke_heap(process.SizeMemory, true, true); - } - else + if (!process.StackFrame) { - // Something went wrong, do not continue, process may be incorrect. - process.Crash(); return -kErrorProcessFault; } - process.StackFrame = new HAL::StackFrame(); - - if (!process.StackFrame) + // Create heap according to type of process. + if (process.Kind == UserProcess::kDLLKind) { - process.Crash(); - return -kErrorProcessFault; + process.DLLPtr = rtl_init_shared_object(&process); } - + if (process.Image) { // get preferred stack size by app. const auto cMaxStackSize = process.StackSize; - - process.StackReserve = (UInt8*)mm_new_ke_heap(cMaxStackSize, Yes, Yes); + process.StackReserve = (UInt8*)process.New(sizeof(UInt8) * cMaxStackSize); // if stack pointer isn't valid. if (!process.StackReserve) { - process.StackReserve = (UInt8*)mm_new_ke_heap(kSchedMaxStackSz, Yes, Yes); - kcout << "newoskrnl.exe: Use fallback reserve size.\r"; + return -kErrorProcessFault; } } else { if (process.Kind != UserProcess::kDLLKind) { - process.Crash(); return -kErrorProcessFault; } } - process.Status = ProcessStatusKind::kStarting; - + process.Status = ProcessStatusKind::kStarting; process.ProcessId = mTeam.mProcessAmount; ++mTeam.mProcessAmount; - process.HeapCursor = process.HeapPtr; - mTeam.AsArray()[process.ProcessId] = process; - kcout << "UserProcessScheduler: Adding process to team [ OK ]...\r"; - return process.ProcessId; } @@ -293,11 +281,6 @@ namespace Kernel UserProcessScheduler& UserProcessScheduler::The() { - if (!cProcessScheduler) - { - cProcessScheduler = new UserProcessScheduler(); - } - MUST_PASS(cProcessScheduler); return *cProcessScheduler; } @@ -341,7 +324,7 @@ namespace Kernel if (UserProcessHelper::CanBeScheduled(process)) { // set the current process. - mTeam.AsRef() = process; + mTeam.AsRef() = mTeam.AsArray()[process.ProcessId]; process.PTime = static_cast(process.Affinity); @@ -349,7 +332,7 @@ namespace Kernel // tell helper to find a core to schedule on. if (!UserProcessHelper::Switch(process.Image, &process.StackReserve[process.StackSize], process.StackFrame, - process.ProcessId)) + process.ProcessId)) { process.Crash(); continue; @@ -393,6 +376,14 @@ namespace Kernel return cProcessScheduler->CurrentProcess().Leak().ProcessId; } + Void UserProcessHelper::Init() + { + if (!cProcessScheduler) + { + cProcessScheduler = new UserProcessScheduler(); + } + } + /// @brief Check if process can be schedulded. /// @param process the process reference. /// @retval true can be schedulded. @@ -408,7 +399,7 @@ namespace Kernel if (auto start = process.DLLPtr->Load(kPefStart, rt_string_len(kPefStart), kPefCode); start) { - process.Image = start; + process.Image = start; } } -- cgit v1.2.3