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/HALKit/AMD64/HalBMPMgr.cxx | 99 ++++++++++++++++ dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx | 99 ---------------- dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm | 34 ++---- dev/ZKA/HALKit/POWER/HalHardware.cxx | 19 ---- dev/ZKA/KernelKit/CodeMgr.hxx | 2 +- dev/ZKA/KernelKit/DLLInterface.hxx | 51 --------- dev/ZKA/KernelKit/IDLLObject.hxx | 45 ++++++++ dev/ZKA/KernelKit/IPEFDLLObject.hxx | 105 +++++++++++++++++ dev/ZKA/KernelKit/PEFDLLInterface.hxx | 105 ----------------- dev/ZKA/KernelKit/UserProcessScheduler.hxx | 36 +++--- 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 +++++++++++++--------------- 18 files changed, 490 insertions(+), 524 deletions(-) create mode 100644 dev/ZKA/HALKit/AMD64/HalBMPMgr.cxx delete mode 100644 dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx delete mode 100644 dev/ZKA/HALKit/POWER/HalHardware.cxx delete mode 100644 dev/ZKA/KernelKit/DLLInterface.hxx create mode 100644 dev/ZKA/KernelKit/IDLLObject.hxx create mode 100644 dev/ZKA/KernelKit/IPEFDLLObject.hxx delete mode 100644 dev/ZKA/KernelKit/PEFDLLInterface.hxx 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 diff --git a/dev/ZKA/HALKit/AMD64/HalBMPMgr.cxx b/dev/ZKA/HALKit/AMD64/HalBMPMgr.cxx new file mode 100644 index 00000000..7bafd075 --- /dev/null +++ b/dev/ZKA/HALKit/AMD64/HalBMPMgr.cxx @@ -0,0 +1,99 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include + +#define cVMHMagic ((Kernel::UIntPtr)0x10210) + +#ifdef __ZKA_AMD64__ +#include +#elif defined(__ZKA_ARM64__) +#include +#endif + +#include +#include + +namespace Kernel +{ + namespace HAL + { + namespace Detail + { + struct AllocatorTraits final + { + /// @brief Iterate over availables pages for a free one. + /// @return The new address which was found. + VoidPtr FindBitMap(VoidPtr base_ptr, SizeT size, Bool rw, Bool user) noexcept + { + while (base_ptr && size) + { + UIntPtr* ptr_bit_set = reinterpret_cast(base_ptr); + + if (ptr_bit_set[0] != cVMHMagic) + { + ptr_bit_set[0] = cVMHMagic; + ptr_bit_set[1] = size; + + kcout << "BBP: STATUS\r"; + kcout << "BBP: MAG: " << hex_number(ptr_bit_set[0]) << endl; + kcout << "BBP: ADDRESS: " << hex_number((UIntPtr)ptr_bit_set) << endl; + kcout << "BBP: SIZE: " << hex_number(ptr_bit_set[1]) << endl; + + if (rw) + mm_update_pte(base_ptr, eFlagsRw); + + if (user) + mm_update_pte(base_ptr, eFlagsUser); + + return (VoidPtr)(&ptr_bit_set[2]); + } + + base_ptr = reinterpret_cast(reinterpret_cast(base_ptr) + 1 + ptr_bit_set[1]); + } + + return nullptr; + } + }; + } // namespace Detail + + /// @brief Allocate a new page to be used by the OS. + /// @param rw read/write bit. + /// @param user user bit. + /// @return + auto mm_alloc_bitmap(Boolean rw, Boolean user, SizeT size) -> VoidPtr + { + VoidPtr ptr_new = nullptr; + Detail::AllocatorTraits traits; + + ptr_new = traits.FindBitMap(kKernelVirtualStart, size, rw, user); + + return &((UIntPtr*)ptr_new)[1]; + } + + auto mm_free_bitmap(VoidPtr page_ptr) -> Bool + { + if (!page_ptr) + return false; + + UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr) - 3; + + if (!ptr_bit_set[0] || + ptr_bit_set[0] != cVMHMagic) + return false; + + kcout << "BBP: FREE STATUS\r"; + kcout << "BBP: MAG: " << hex_number(ptr_bit_set[0]) << endl; + kcout << "BBP: ADDRESSS: " << hex_number((UIntPtr)ptr_bit_set) << endl; + kcout << "BBP: SIZE: " << hex_number(ptr_bit_set[1]) << endl; + + ptr_bit_set[0] = 0UL; + ptr_bit_set[1] = 0UL; + + return true; + } + } // namespace HAL +} // namespace Kernel diff --git a/dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx b/dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx deleted file mode 100644 index 7bafd075..00000000 --- a/dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx +++ /dev/null @@ -1,99 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include - -#define cVMHMagic ((Kernel::UIntPtr)0x10210) - -#ifdef __ZKA_AMD64__ -#include -#elif defined(__ZKA_ARM64__) -#include -#endif - -#include -#include - -namespace Kernel -{ - namespace HAL - { - namespace Detail - { - struct AllocatorTraits final - { - /// @brief Iterate over availables pages for a free one. - /// @return The new address which was found. - VoidPtr FindBitMap(VoidPtr base_ptr, SizeT size, Bool rw, Bool user) noexcept - { - while (base_ptr && size) - { - UIntPtr* ptr_bit_set = reinterpret_cast(base_ptr); - - if (ptr_bit_set[0] != cVMHMagic) - { - ptr_bit_set[0] = cVMHMagic; - ptr_bit_set[1] = size; - - kcout << "BBP: STATUS\r"; - kcout << "BBP: MAG: " << hex_number(ptr_bit_set[0]) << endl; - kcout << "BBP: ADDRESS: " << hex_number((UIntPtr)ptr_bit_set) << endl; - kcout << "BBP: SIZE: " << hex_number(ptr_bit_set[1]) << endl; - - if (rw) - mm_update_pte(base_ptr, eFlagsRw); - - if (user) - mm_update_pte(base_ptr, eFlagsUser); - - return (VoidPtr)(&ptr_bit_set[2]); - } - - base_ptr = reinterpret_cast(reinterpret_cast(base_ptr) + 1 + ptr_bit_set[1]); - } - - return nullptr; - } - }; - } // namespace Detail - - /// @brief Allocate a new page to be used by the OS. - /// @param rw read/write bit. - /// @param user user bit. - /// @return - auto mm_alloc_bitmap(Boolean rw, Boolean user, SizeT size) -> VoidPtr - { - VoidPtr ptr_new = nullptr; - Detail::AllocatorTraits traits; - - ptr_new = traits.FindBitMap(kKernelVirtualStart, size, rw, user); - - return &((UIntPtr*)ptr_new)[1]; - } - - auto mm_free_bitmap(VoidPtr page_ptr) -> Bool - { - if (!page_ptr) - return false; - - UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr) - 3; - - if (!ptr_bit_set[0] || - ptr_bit_set[0] != cVMHMagic) - return false; - - kcout << "BBP: FREE STATUS\r"; - kcout << "BBP: MAG: " << hex_number(ptr_bit_set[0]) << endl; - kcout << "BBP: ADDRESSS: " << hex_number((UIntPtr)ptr_bit_set) << endl; - kcout << "BBP: SIZE: " << hex_number(ptr_bit_set[1]) << endl; - - ptr_bit_set[0] = 0UL; - ptr_bit_set[1] = 0UL; - - return true; - } - } // namespace HAL -} // namespace Kernel diff --git a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm index 1f31c880..c26a346f 100644 --- a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm +++ b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm @@ -21,8 +21,6 @@ section .text ;; rcx: code ptr. ;; rdx: stack ptr. mp_do_context_switch: - swapgs - mov fs, rcx mov gs, rdx @@ -36,27 +34,12 @@ mp_do_context_switch: mov r11, gs mov r12, fs - ; Enable SCE that enables sysret and syscall - mov rcx, 0xc0000082 - wrmsr - mov rcx, 0xc0000080 - rdmsr - or eax, 1 - wrmsr - mov rcx, 0xc0000081 - rdmsr - mov edx, 0x00180008 - wrmsr - mov r11, 0x202 mov fs, [r8 + (8 * 4)] mov gs, [r8 + (8 * 9)] mov r8, [r8] - swapgs - sti - o64 sysret ;; @brief Gets the current stack frame. @@ -68,8 +51,6 @@ extern hal_system_call_enter global mp_system_call_handler mp_system_call_handler: - swapgs - push r8 push r9 push r10 @@ -80,9 +61,6 @@ mp_system_call_handler: pop r9 pop r8 - swapgs - sti - o64 sysret mp_do_context_switch_pre: @@ -96,4 +74,16 @@ mp_do_context_switch_pre: mov rcx, 0xc0000082 wrmsr + ; Enable SCE that enables sysret and syscall + mov rcx, 0xc0000082 + wrmsr + mov rcx, 0xc0000080 + rdmsr + or eax, 1 + wrmsr + mov rcx, 0xc0000081 + rdmsr + mov edx, 0x00180008 + wrmsr + ret diff --git a/dev/ZKA/HALKit/POWER/HalHardware.cxx b/dev/ZKA/HALKit/POWER/HalHardware.cxx deleted file mode 100644 index eb335d70..00000000 --- a/dev/ZKA/HALKit/POWER/HalHardware.cxx +++ /dev/null @@ -1,19 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include - -namespace Kernel -{ - namespace HAL - { - UIntPtr mm_alloc_bitmap(bool rw, bool user) - { - return 0; - } - } // namespace HAL -} // namespace Kernel diff --git a/dev/ZKA/KernelKit/CodeMgr.hxx b/dev/ZKA/KernelKit/CodeMgr.hxx index 8c4f090f..c8f9cca9 100644 --- a/dev/ZKA/KernelKit/CodeMgr.hxx +++ b/dev/ZKA/KernelKit/CodeMgr.hxx @@ -16,7 +16,7 @@ #include #include -#include +#include namespace Kernel { diff --git a/dev/ZKA/KernelKit/DLLInterface.hxx b/dev/ZKA/KernelKit/DLLInterface.hxx deleted file mode 100644 index 13f36b82..00000000 --- a/dev/ZKA/KernelKit/DLLInterface.hxx +++ /dev/null @@ -1,51 +0,0 @@ -/* - * ======================================================== - * - * Kernel - * Copyright ZKA Technologies., all rights reserved. - * - * ======================================================== - */ - -#pragma once - -#include - -namespace Kernel -{ - class DLLInterface - { - public: - explicit DLLInterface() = default; - virtual ~DLLInterface() = default; - - struct DLL_TRAITS final - { - VoidPtr fImageObject{nullptr}; - VoidPtr fImageEntrypointOffset{nullptr}; - - Bool IsValid() - { - return fImageObject && fImageEntrypointOffset; - } - }; - - ZKA_COPY_DEFAULT(DLLInterface); - - virtual DLL_TRAITS** GetAddressOf() = 0; - virtual DLL_TRAITS* Get() = 0; - - virtual Void Mount(DLL_TRAITS* to_mount) = 0; - virtual Void Unmount() = 0; - - - template - SymbolType Load(const Char* symbol_name, SizeT len, Int32 kind) - { - return nullptr; - } - }; - - /// @brief Pure implementation, missing method/function handler. - EXTERN_C void __zka_pure_call(void); -} // namespace Kernel diff --git a/dev/ZKA/KernelKit/IDLLObject.hxx b/dev/ZKA/KernelKit/IDLLObject.hxx new file mode 100644 index 00000000..39580307 --- /dev/null +++ b/dev/ZKA/KernelKit/IDLLObject.hxx @@ -0,0 +1,45 @@ +/* + * ======================================================== + * + * Kernel + * Copyright ZKA Technologies., all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include + +namespace Kernel +{ + class IDLLObject + { + public: + explicit IDLLObject() = default; + virtual ~IDLLObject() = default; + + struct DLL_TRAITS final + { + VoidPtr fImageObject{nullptr}; + VoidPtr fImageEntrypointOffset{nullptr}; + + Bool IsValid() + { + return fImageObject && fImageEntrypointOffset; + } + }; + + ZKA_COPY_DEFAULT(IDLLObject); + + virtual DLL_TRAITS** GetAddressOf() = 0; + virtual DLL_TRAITS* Get() = 0; + + virtual Void Mount(DLL_TRAITS* to_mount) = 0; + virtual Void Unmount() = 0; + + }; + + /// @brief Pure implementation, missing method/function handler. + EXTERN_C void __zka_pure_call(void); +} // namespace Kernel diff --git a/dev/ZKA/KernelKit/IPEFDLLObject.hxx b/dev/ZKA/KernelKit/IPEFDLLObject.hxx new file mode 100644 index 00000000..013173d1 --- /dev/null +++ b/dev/ZKA/KernelKit/IPEFDLLObject.hxx @@ -0,0 +1,105 @@ +/* + * ======================================================== + * + * Kernel + * Copyright ZKA Technologies., all rights reserved. + * + * ======================================================== + */ + +#ifndef __KERNELKIT_SHARED_OBJECT_HXX__ +#define __KERNELKIT_SHARED_OBJECT_HXX__ + +#include +#include +#include +#include + +namespace Kernel +{ + /** + * @brief Shared Library class + * Load library from this class + */ + class IPEFDLLObject final : public IDLLObject + { + public: + explicit IPEFDLLObject() = default; + ~IPEFDLLObject() = default; + + public: + ZKA_COPY_DEFAULT(IPEFDLLObject); + + private: + DLL_TRAITS* fMounted{nullptr}; + + public: + DLL_TRAITS** GetAddressOf() + { + return &fMounted; + } + + DLL_TRAITS* Get() + { + return fMounted; + } + + public: + void Mount(DLL_TRAITS* to_mount) + { + if (!to_mount || !to_mount->fImageObject) + return; + + fMounted = to_mount; + + if (fLoader && to_mount) + { + delete fLoader; + fLoader = nullptr; + } + + if (!fLoader) + { + fLoader = new PEFLoader(fMounted->fImageObject); + } + } + + void Unmount() + { + if (fMounted) + fMounted = nullptr; + }; + + template + SymbolType Load(const Char* symbol_name, SizeT len, Int32 kind) + { + if (symbol_name == nullptr || *symbol_name == 0) + return nullptr; + if (len > kPathLen || len < 1) + return nullptr; + + auto ret = + reinterpret_cast(fLoader->FindSymbol(symbol_name, kind)); + + if (!ret) + { + if (kind == kPefCode) + return (VoidPtr)&__zka_pure_call; + + return nullptr; + } + + return ret; + } + + private: + PEFLoader* fLoader{nullptr}; + }; + + typedef IPEFDLLObject* IDLL; + + EXTERN_C IDLL rtl_init_shared_object(UserProcess* header); + EXTERN_C Void rtl_fini_shared_object(UserProcess* header, IDLL lib, Bool* successful); +} // namespace Kernel + +#endif /* ifndef __KERNELKIT_SHARED_OBJECT_HXX__ */ diff --git a/dev/ZKA/KernelKit/PEFDLLInterface.hxx b/dev/ZKA/KernelKit/PEFDLLInterface.hxx deleted file mode 100644 index d664bfd8..00000000 --- a/dev/ZKA/KernelKit/PEFDLLInterface.hxx +++ /dev/null @@ -1,105 +0,0 @@ -/* - * ======================================================== - * - * Kernel - * Copyright ZKA Technologies., all rights reserved. - * - * ======================================================== - */ - -#ifndef __KERNELKIT_SHARED_OBJECT_HXX__ -#define __KERNELKIT_SHARED_OBJECT_HXX__ - -#include -#include -#include -#include - -namespace Kernel -{ - /** - * @brief Shared Library class - * Load library from this class - */ - class PEFDLLInterface final : public DLLInterface - { - public: - explicit PEFDLLInterface() = default; - ~PEFDLLInterface() = default; - - public: - ZKA_COPY_DEFAULT(PEFDLLInterface); - - private: - DLL_TRAITS* fMounted{nullptr}; - - public: - DLL_TRAITS** GetAddressOf() - { - return &fMounted; - } - - DLL_TRAITS* Get() - { - return fMounted; - } - - public: - void Mount(DLL_TRAITS* to_mount) - { - if (!to_mount || !to_mount->fImageObject) - return; - - fMounted = to_mount; - - if (fLoader && to_mount) - { - delete fLoader; - fLoader = nullptr; - } - - if (!fLoader) - { - fLoader = new PEFLoader(fMounted->fImageObject); - } - } - - void Unmount() - { - if (fMounted) - fMounted = nullptr; - }; - - template - SymbolType Load(const Char* symbol_name, SizeT len, Int32 kind) - { - if (symbol_name == nullptr || *symbol_name == 0) - return nullptr; - if (len > kPathLen || len < 1) - return nullptr; - - auto ret = - reinterpret_cast(fLoader->FindSymbol(symbol_name, kind)); - - if (!ret) - { - if (kind == kPefCode) - return (VoidPtr)&__zka_pure_call; - - return nullptr; - } - - return ret; - } - - private: - PEFLoader* fLoader{nullptr}; - }; - - typedef DLLInterface* DLLInterfacePtr; - - EXTERN_C DLLInterfacePtr rtl_init_shared_object(UserProcess* header); - EXTERN_C Void rtl_fini_shared_object(UserProcess* header, DLLInterfacePtr lib, Bool* successful); -} // namespace Kernel - -#endif /* ifndef __KERNELKIT_SHARED_OBJECT_HXX__ */ diff --git a/dev/ZKA/KernelKit/UserProcessScheduler.hxx b/dev/ZKA/KernelKit/UserProcessScheduler.hxx index 78d75af6..d5172fe9 100644 --- a/dev/ZKA/KernelKit/UserProcessScheduler.hxx +++ b/dev/ZKA/KernelKit/UserProcessScheduler.hxx @@ -10,12 +10,10 @@ #include #include #include -#include #include #define kSchedMinMicroTime (AffinityKind::kStandard) #define kSchedInvalidPID (-1) -#define kSchedMaxStackSz (4096) /* Max stack sz */ #define kSchedProcessLimitPerTeam (16U) //////////////////////////////////////////////////// @@ -29,7 +27,7 @@ namespace Kernel //! @note Forward declarations. class UserProcess; - class PEFDLLInterface; + class IPEFDLLObject; class UserProcessTeam; class UserProcessScheduler; class UserProcessHelper; @@ -148,24 +146,33 @@ namespace Kernel User* Owner{nullptr}; HAL::StackFramePtr StackFrame{nullptr}; AffinityKind Affinity{AffinityKind::kStandard}; - ProcessStatusKind Status{ProcessStatusKind::kDead}; + ProcessStatusKind Status{ProcessStatusKind::kDead}; UInt8* StackReserve{nullptr}; // Memory, images pointers. - HeapPtrKind HeapCursor{nullptr}; ImagePtr Image{nullptr}; - HeapPtrKind HeapPtr{nullptr}; SizeT StackSize{mib_cast(8)}; - // shared library handle, reserved for kDLLKind types of executables only. - DLLInterface* DLLPtr{nullptr}; - UserProcess* Parent{nullptr}; + //! @brief Shared library handle, reserved for kDLLKind types of executables only. + IPEFDLLObject* DLLPtr{nullptr}; + + /// @brief Parent process, reserved for threads only. + UserProcess* Parent{nullptr}; // Memory usage. - SizeT UsedMemory{0}; - SizeT FreeMemory{0}; - SizeT SizeMemory{mib_cast(64)}; + SizeT MemoryCursor{0}; + SizeT MemoryLimit{gib_cast(128)}; + + struct PROCESS_MEMORY_ENTRY + { + VoidPtr MemoryEntry; + + struct PROCESS_MEMORY_ENTRY *MemoryPrev; + struct PROCESS_MEMORY_ENTRY *MemoryNext; + } * MemoryEntryList{nullptr}; + + SizeT MemoryPD{0}; enum { @@ -213,9 +220,9 @@ namespace Kernel //! @return Int32 local error code. Int32& GetLocalCode() noexcept; - const User* GetOwner() noexcept; + const User* GetOwner() noexcept; const ProcessStatusKind& GetStatus() noexcept; - const AffinityKind& GetAffinity() noexcept; + const AffinityKind& GetAffinity() noexcept; private: UInt32 fLastExitCode{0}; @@ -291,6 +298,7 @@ namespace Kernel STATIC bool CanBeScheduled(UserProcess& process); STATIC PID& TheCurrentPID(); STATIC SizeT StartScheduling(); + STATIC Void Init(); }; const UInt32& sched_get_exit_code(void) noexcept; 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