diff options
| author | Amlal EL Mahrouss <amlalelmahrouss@icloud.com> | 2024-08-30 20:46:01 +0200 |
|---|---|---|
| committer | Amlal EL Mahrouss <amlalelmahrouss@icloud.com> | 2024-08-30 20:46:01 +0200 |
| commit | 43bac17a2986ac2ea86e9d70c61268fa7e90ca4e (patch) | |
| tree | d259814b8c388e87bb02860405ff668a92434c44 /dev/ZKA/Sources | |
| parent | 8a4f0e988a901e4fce5d32e3d5f9dbdc5f309863 (diff) | |
Fixed many issues with the kernel, and refactored it.
Signed-off-by: Amlal EL Mahrouss <amlalelmahrouss@icloud.com>
Diffstat (limited to 'dev/ZKA/Sources')
| -rw-r--r-- | dev/ZKA/Sources/CodeManager.cxx | 5 | ||||
| -rw-r--r-- | dev/ZKA/Sources/DLLMain.cxx | 14 | ||||
| -rw-r--r-- | dev/ZKA/Sources/Network/IPC.cxx | 10 | ||||
| -rw-r--r-- | dev/ZKA/Sources/PEFCodeManager.cxx | 6 | ||||
| -rw-r--r-- | dev/ZKA/Sources/ProcessHeap.cxx | 277 | ||||
| -rw-r--r-- | dev/ZKA/Sources/ProcessScheduler.cxx | 63 | ||||
| -rw-r--r-- | dev/ZKA/Sources/ProcessTeam.cxx | 2 |
7 files changed, 50 insertions, 327 deletions
diff --git a/dev/ZKA/Sources/CodeManager.cxx b/dev/ZKA/Sources/CodeManager.cxx index 47625ac1..3dc08ca6 100644 --- a/dev/ZKA/Sources/CodeManager.cxx +++ b/dev/ZKA/Sources/CodeManager.cxx @@ -21,9 +21,8 @@ namespace Kernel PROCESS_HEADER_BLOCK proc((VoidPtr)main); proc.Kind = PROCESS_HEADER_BLOCK::kExeKind; - rt_copy_memory((VoidPtr)processName, proc.Name, rt_string_len(proc.Name)); - proc.Image = (VoidPtr)main; + rt_copy_memory((VoidPtr)processName, proc.Name, rt_string_len(processName)); - return ProcessScheduler::The().Leak().Add(proc); + return ProcessScheduler::The().Add(proc) == kErrorSuccess; } } // namespace Kernel diff --git a/dev/ZKA/Sources/DLLMain.cxx b/dev/ZKA/Sources/DLLMain.cxx index 0cecc820..89ca0d3c 100644 --- a/dev/ZKA/Sources/DLLMain.cxx +++ b/dev/ZKA/Sources/DLLMain.cxx @@ -17,7 +17,6 @@ #include <KernelKit/PEF.hxx> #include <KernelKit/PEFCodeManager.hxx> #include <KernelKit/ProcessScheduler.hxx> -#include <KernelKit/ProcessHeap.hxx> #include <NewKit/Json.hxx> #include <NewKit/KernelCheck.hxx> #include <NewKit/String.hxx> @@ -189,6 +188,8 @@ EXTERN_C Kernel::Void ke_dll_entrypoint(Kernel::Void) Kernel::cProcessScheduler = nullptr; Kernel::ProcessHelper::StartScheduling(); + CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Starting ZKA System...", 30, 10, RGB(0, 0, 0)); + Kernel::PEFLoader sys_user_drv(kSysDrv); if (sys_user_drv.IsLoaded()) @@ -212,13 +213,14 @@ EXTERN_C Kernel::Void ke_dll_entrypoint(Kernel::Void) CG::CGDrawStringToWnd(cKernelWnd, kSysDrv, 20, 10 + (FONT_SIZE_X * Kernel::rt_string_len("newoskrnl.dll: Missing catalog: ")), RGB(0, 0, 0)); } - CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.dll: Starting ZKA...", 30, 10, RGB(0, 0, 0)); - - Kernel::ProcessHelper::StartScheduling(); - Kernel::execute_from_image([]() -> void { }, "ZKA Logger"); - Kernel::ProcessHelper::StartScheduling(); + while (Yes) + { + Kernel::ProcessHelper::StartScheduling(); + } + + Kernel::ke_stop(RUNTIME_CHECK_BOOTSTRAP); } diff --git a/dev/ZKA/Sources/Network/IPC.cxx b/dev/ZKA/Sources/Network/IPC.cxx index 2ec50892..92476d9c 100644 --- a/dev/ZKA/Sources/Network/IPC.cxx +++ b/dev/ZKA/Sources/Network/IPC.cxx @@ -63,7 +63,7 @@ namespace Kernel if (!pckt || !ipc_int_sanitize_packet(pckt)) { - ProcessScheduler::The().Leak().TheCurrent().Leak().Crash(); + ProcessScheduler::The().CurrentProcess().Leak().Crash(); return false; } @@ -82,7 +82,7 @@ namespace Kernel // crash process if the packet pointer of pointer is NULL. if (!pckt_in) { - ProcessScheduler::The().Leak().TheCurrent().Leak().Crash(); + ProcessScheduler::The().CurrentProcess().Leak().Crash(); return false; } @@ -98,12 +98,12 @@ namespace Kernel (*pckt_in)->IpcEndianess = static_cast<UInt8>(endian); (*pckt_in)->IpcPacketSize = sizeof(IPC_MESSAGE_STRUCT); - (*pckt_in)->IpcFrom.ProcessID = Kernel::ProcessScheduler::The().Leak().TheCurrent().Leak().ProcessId; - (*pckt_in)->IpcFrom.ProcessTeam = Kernel::ProcessScheduler::The().Leak().CurrentTeam().mTeamId; + (*pckt_in)->IpcFrom.ProcessID = Kernel::ProcessScheduler::The().CurrentProcess().Leak().ProcessId; + (*pckt_in)->IpcFrom.ProcessTeam = Kernel::ProcessScheduler::The().CurrentTeam().mTeamId; return true; } - ProcessScheduler::The().Leak().TheCurrent().Leak().Crash(); + ProcessScheduler::The().CurrentProcess().Leak().Crash(); return false; } } // namespace Kernel diff --git a/dev/ZKA/Sources/PEFCodeManager.cxx b/dev/ZKA/Sources/PEFCodeManager.cxx index 1b4adcfc..cecc4820 100644 --- a/dev/ZKA/Sources/PEFCodeManager.cxx +++ b/dev/ZKA/Sources/PEFCodeManager.cxx @@ -79,12 +79,12 @@ namespace Kernel return; } - kcout << "CodeManagerPEF: Warning: Executable format error!\r"; fBad = true; if (fCachedBlob) mm_delete_ke_heap(fCachedBlob); + kcout << "CodeManagerPEF: Warning: Executable format error!\r"; fCachedBlob = nullptr; } @@ -200,11 +200,9 @@ namespace Kernel return false; PROCESS_HEADER_BLOCK proc(errOrStart.Leak().Leak()); - Ref<PROCESS_HEADER_BLOCK> refProc = proc; - proc.Kind = procKind; - return ProcessScheduler::The().Leak().Add(refProc); + return ProcessScheduler::The().Add(proc); } } // namespace Utils diff --git a/dev/ZKA/Sources/ProcessHeap.cxx b/dev/ZKA/Sources/ProcessHeap.cxx deleted file mode 100644 index d501f31d..00000000 --- a/dev/ZKA/Sources/ProcessHeap.cxx +++ /dev/null @@ -1,277 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include <KernelKit/ProcessScheduler.hxx> -#include <KernelKit/ProcessHeap.hxx> -#include <NewKit/PageManager.hxx> - -#define cHeapHeaderPaddingSz (16U) - -/// @file ProcessHeap.cxx -/// @brief User Heap Manager, Process heap allocator. -/// @note if you want to look at the kernel allocator, please look for -/// KernelHeap.cxx -/// BUGS: 0 - -namespace Kernel -{ - /** - * @brief Process Heap Header - * @note Allocated per process, it denotes the user's heap. - */ - struct PROCESS_HEAP_HEADER final - { - UInt32 fPageMagic; - Int32 fPageFlags; - Boolean fPageFree; - UIntPtr fPageVirtStart; - SizeT fPageVirtSize; - UInt8 fPagePad[cHeapHeaderPaddingSz]; - }; - - /// @brief PROCESS_HEAP_HEADER as pointer type. - typedef PROCESS_HEAP_HEADER* PROCESS_HEAP_HEADER_PTR; - - /** - * @brief Process heap class, takes care of allocating the process pools. - * @note This rely on Virtual Memory! Consider adding good vmem support when - * @note porting to a new arch. - */ - class ProcessHeapHelper final - { - ProcessHeapHelper() = delete; - - public: - ~ProcessHeapHelper() = default; - - public: - STATIC SizeT& Count() noexcept - { - return s_NumPools; - } - - STATIC Ref<Pmm>& Leak() noexcept - { - return s_Pmm; - } - - STATIC Boolean& IsEnabled() noexcept - { - return s_PoolsAreEnabled; - } - - STATIC MutableArray<Ref<PTEWrapper>>& The() noexcept - { - return s_Pool; - } - - private: - STATIC Size s_NumPools; - STATIC Ref<Pmm> s_Pmm; - - private: - STATIC Boolean s_PoolsAreEnabled; - STATIC MutableArray<Ref<PTEWrapper>> s_Pool; - }; - - //! declare fields - - SizeT ProcessHeapHelper::s_NumPools = 0UL; - Ref<Pmm> ProcessHeapHelper::s_Pmm; - Boolean ProcessHeapHelper::s_PoolsAreEnabled = true; - MutableArray<Ref<PTEWrapper>> ProcessHeapHelper::s_Pool; - - STATIC VoidPtr sched_find_unused_heap(Int32 flags, SizeT len); - STATIC Void sched_free_heap_internal(VoidPtr vaddr); - STATIC VoidPtr sched_make_heap_internal(VoidPtr vaddr, Int32 flags, SizeT len); - STATIC Boolean sched_check_and_free_heap(const SizeT& index, VoidPtr ptr); - - /// @brief Find an unused heap header to allocate on. - /// @param flags the flags to use. - /// @return VoidPtr the heap pointer. - STATIC VoidPtr sched_find_unused_heap(Int32 flags, SizeT len) - { - SizeT index = 0UL; - - while (true) - { - /* ************************************ */ - /* allocate if it doesnt exist. */ - /* ************************************ */ - if (!ProcessHeapHelper::The()[index]) - { - ProcessHeapHelper::The().Add(Kernel::Ref<Kernel::PTEWrapper>()); - } - - if (ProcessHeapHelper::The()[index] && - !ProcessHeapHelper::The()[index].Leak().Leak().Present()) - { - ProcessHeapHelper::Leak().Leak().TogglePresent( - ProcessHeapHelper::The()[index].Leak().Leak(), true); - - ProcessHeapHelper::Leak().Leak().ToggleUser( - ProcessHeapHelper::The()[index].Leak().Leak(), true); - - kcout << "[sched_find_unused_heap] Done, trying to make a pool now...\r"; - - return sched_make_heap_internal( - (VoidPtr)ProcessHeapHelper::The()[index].Leak().Leak().VirtualAddress(), - flags, len); - } - - ++index; - } - - return nullptr; - } - - /// @brief Makes a new heap for the process to use. - /// @param virtual_address the virtual address of the process. - /// @param flags the flags. - /// @return - STATIC VoidPtr sched_make_heap_internal(VoidPtr virtual_address, Int32 flags, SizeT len_in_gb) - { - if (virtual_address) - { - PROCESS_HEAP_HEADER* process_heap_hdr = reinterpret_cast<PROCESS_HEAP_HEADER*>(virtual_address); - - if (!process_heap_hdr->fPageFree) - { - kcout - << "[sched_make_heap_internal] process_heap_hdr->fPageFree, HeapPtr already exists\n"; - return nullptr; - } - - process_heap_hdr->fPageFlags = flags; - process_heap_hdr->fPageMagic = kProcessHeapMag; - process_heap_hdr->fPageFree = false; - process_heap_hdr->fPageVirtStart = (UIntPtr)virtual_address + sizeof(PROCESS_HEAP_HEADER); - process_heap_hdr->fPageVirtSize = len_in_gb; - - kcout << "[sched_make_heap_internal] New allocation has been done, returning new chunk.\n"; - - return reinterpret_cast<VoidPtr>( - (reinterpret_cast<UIntPtr>(virtual_address) + sizeof(PROCESS_HEAP_HEADER))); - } - - kcout << "[sched_make_heap_internal] Address is invalid"; - return nullptr; - } - - /// @brief Internally makrs the heap as free. - /// This is done by setting the fPageFree bit to true - /// @param virtual_address - /// @return - STATIC Void sched_free_heap_internal(VoidPtr virtual_address) - { - PROCESS_HEAP_HEADER* process_heap_hdr = reinterpret_cast<PROCESS_HEAP_HEADER*>( - reinterpret_cast<UIntPtr>(virtual_address) - sizeof(PROCESS_HEAP_HEADER)); - - if (process_heap_hdr->fPageMagic == kProcessHeapMag) - { - if (!process_heap_hdr->fPageFree) - { - ProcessScheduler::The().Leak().TheCurrent().Leak().Crash(); - return; - } - - process_heap_hdr->fPageFree = true; - process_heap_hdr->fPageFlags = 0; - - kcout << "[sched_free_heap_internal] Successfully marked header as free!\r"; - } - } - - /** - * @brief Check for the ptr and frees it. - * - * @param index Where to look at. - * @param ptr The ptr to check. - * @return Boolean true if successful. - */ - STATIC Boolean sched_check_and_free_heap(const SizeT& index, VoidPtr ptr) - { - if (ProcessHeapHelper::The()[index]) - { - // ErrorOr<>::operator Boolean - /// if (address matches) - /// -> Free heap. - if (ProcessHeapHelper::The()[index].Leak().Leak().VirtualAddress() == - (UIntPtr)ptr) - { - ProcessHeapHelper::Leak().Leak().FreePage( - ProcessHeapHelper::The()[index].Leak().Leak()); - - --ProcessHeapHelper::Count(); - - sched_free_heap_internal(ptr); - ptr = nullptr; - - return true; - } - } - - return false; - } - - /// @brief Creates a new pool pointer. - /// @param flags the flags attached to it. - /// @return a pool pointer with selected permissions. - VoidPtr sched_new_heap(Int32 flags, SizeT page_size) - { - if (!ProcessHeapHelper::IsEnabled()) - return nullptr; - - if (VoidPtr ret = sched_find_unused_heap(flags, page_size)) - return ret; - - // this wasn't set to true - auto ref_page = ProcessHeapHelper::Leak().Leak().RequestPage( - ((flags & kProcessHeapUser)), (flags & kProcessHeapRw)); - - if (ref_page) - { - ///! reserve page. - ProcessHeapHelper::The()[ProcessHeapHelper::Count()].Leak() = ref_page; - auto& ref = ProcessHeapHelper::Count(); - - ++ref; // increment the number of addresses we have now. - - // finally make the pool address. - return sched_make_heap_internal( - reinterpret_cast<VoidPtr>(ref_page.Leak().VirtualAddress()), flags, page_size); - } - - return nullptr; - } - - /// @brief free a pool pointer. - /// @param ptr The pool pointer to free. - /// @return status code - Int32 sched_free_heap(VoidPtr ptr) - { - if (!ProcessHeapHelper::IsEnabled()) - return -1; - - if (ptr) - { - SizeT base = ProcessHeapHelper::Count(); - - if (sched_check_and_free_heap(base, ptr)) - return 0; - - for (SizeT index = 0; index < ProcessHeapHelper::The().Count(); ++index) - { - if (sched_check_and_free_heap(index, ptr)) - return 0; - - --base; - } - } - - return -1; - } -} // namespace Kernel diff --git a/dev/ZKA/Sources/ProcessScheduler.cxx b/dev/ZKA/Sources/ProcessScheduler.cxx index 579294fb..82c1cf57 100644 --- a/dev/ZKA/Sources/ProcessScheduler.cxx +++ b/dev/ZKA/Sources/ProcessScheduler.cxx @@ -211,71 +211,71 @@ namespace Kernel /// @brief Add process to list. /// @param process the process *Ref* class. /// @return the process index inside the team. - SizeT ProcessScheduler::Add(Ref<PROCESS_HEADER_BLOCK> process) + SizeT ProcessScheduler::Add(PROCESS_HEADER_BLOCK& process) { - if (!process.Leak().Image) + if (!process.Image) { return -kErrorInvalidData; } - if (mTeam.AsArray().Count() > kSchedProcessLimitPerTeam) - return -kErrorOutOfTeamSlot; - kcout << "ProcessScheduler: Adding process to team...\r"; // Create heap according to type of process. - if (process.Leak().Kind == PROCESS_HEADER_BLOCK::kExeKind) + if (process.Kind == PROCESS_HEADER_BLOCK::kExeKind) { - process.Leak().HeapPtr = mm_new_ke_heap(process.Leak().SizeMemory, true, true); + process.HeapPtr = mm_new_ke_heap(process.SizeMemory, true, true); } - else if (process.Leak().Kind == PROCESS_HEADER_BLOCK::kSharedObjectKind) + else if (process.Kind == PROCESS_HEADER_BLOCK::kSharedObjectKind) { - process.Leak().DLLPtr = rtl_init_shared_object(&process.Leak()); - process.Leak().HeapPtr = mm_new_ke_heap(process.Leak().SizeMemory, true, true); + process.DLLPtr = rtl_init_shared_object(&process); + process.HeapPtr = mm_new_ke_heap(process.SizeMemory, true, true); } else { // Something went wrong, do not continue, process may be incorrect. - process.Leak().Crash(); + process.Crash(); return -kErrorProcessFault; } - process.Leak().StackFrame = reinterpret_cast<HAL::StackFrame*>( + process.StackFrame = reinterpret_cast<HAL::StackFrame*>( mm_new_ke_heap(sizeof(HAL::StackFrame), Yes, Yes)); - MUST_PASS(process.Leak().StackFrame); + MUST_PASS(process.StackFrame); - if (process.Leak().Image) + if (process.Image) { - process.Leak().StackFrame->BP = reinterpret_cast<HAL::Reg>(process.Leak().Image); + process.StackFrame->BP = reinterpret_cast<HAL::Reg>(process.Image); } else { - if (process.Leak().Kind != PROCESS_HEADER_BLOCK::kSharedObjectKind) + if (process.Kind != PROCESS_HEADER_BLOCK::kSharedObjectKind) { - process.Leak().Crash(); + process.Crash(); return -kErrorProcessFault; } } - if (!process.Leak().StackFrame->SP) - process.Leak().StackFrame->SP = reinterpret_cast<HAL::Reg>(mm_new_ke_heap(sizeof(UInt8) * 8196, Yes, Yes)); + if (!process.StackFrame->SP) + process.StackFrame->SP = reinterpret_cast<HAL::Reg>(mm_new_ke_heap(sizeof(UInt8) * 8196, Yes, Yes)); + + process.Status = ProcessStatus::kStarting; + + process.ProcessId = (mTeam.mCurrentProcess); - process.Leak().Status = ProcessStatus::kStarting; + ++mTeam.mProcessAmount; - process.Leak().ProcessId = (mTeam.AsArray().Count() - 1); - process.Leak().HeapCursor = process.Leak().HeapPtr; + process.HeapCursor = process.HeapPtr; - mTeam.AsArray()[process.Leak().ProcessId] = process.Leak(); + mTeam.AsArray()[process.ProcessId] = process; kcout << "ProcessScheduler: Adding process to team [ OK ]...\r"; - return (mTeam.AsArray().Count() - 1); + return process.ProcessId; } /***********************************************************************************/ - Ref<ProcessScheduler> ProcessScheduler::The() + ProcessScheduler& ProcessScheduler::The() { MUST_PASS(cProcessScheduler); return *cProcessScheduler; @@ -287,7 +287,7 @@ namespace Kernel /// @param processSlot process slot inside team. /// @retval true process was removed. /// @retval false process doesn't exist in team. - Bool ProcessScheduler::Remove(SizeT processSlot) + Bool ProcessScheduler::Remove(ProcessID processSlot) { // check if process is within range. if (processSlot > mTeam.AsArray().Count()) @@ -299,6 +299,7 @@ namespace Kernel kcout << "ProcessScheduler: Removing process...\r"; + --mTeam.mProcessAmount; mTeam.AsArray()[processSlot].Status = ProcessStatus::kDead; return true; } @@ -349,7 +350,7 @@ namespace Kernel /// @brief Gets current running process. /// @return - Ref<PROCESS_HEADER_BLOCK>& ProcessScheduler::TheCurrent() + Ref<PROCESS_HEADER_BLOCK>& ProcessScheduler::CurrentProcess() { return mTeam.AsRef(); } @@ -359,7 +360,7 @@ namespace Kernel PID& ProcessHelper::TheCurrentPID() { kcout << "ProcessHelper::TheCurrentPID: Leaking ProcessId...\r"; - return cProcessScheduler->TheCurrent().Leak().ProcessId; + return cProcessScheduler->CurrentProcess().Leak().ProcessId; } /// @brief Check if process can be schedulded. @@ -377,16 +378,16 @@ namespace Kernel if (auto start = process.DLLPtr->Load<VoidPtr>(kPefStart, rt_string_len(kPefStart), kPefCode); start) { - process.Image = start; + process.Image = start; process.StackFrame->BP = reinterpret_cast<HAL::Reg>(start); } } - return process.PTime > 0; + return process.PTime < 0; } /** - * @brief Spin scheduler class. + * @brief Scheduler helper class. */ SizeT ProcessHelper::StartScheduling() diff --git a/dev/ZKA/Sources/ProcessTeam.cxx b/dev/ZKA/Sources/ProcessTeam.cxx index c6a26275..58a639f6 100644 --- a/dev/ZKA/Sources/ProcessTeam.cxx +++ b/dev/ZKA/Sources/ProcessTeam.cxx @@ -22,7 +22,7 @@ namespace Kernel /// @brief Get team ID. /// @return The team's ID. - UInt64& ProcessTeam::Id() noexcept + ProcessID& ProcessTeam::Id() noexcept { return mTeamId; } |
