From e757bb6a90c98f53995e4828d68eba26a2327540 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 1 Sep 2024 09:23:10 +0200 Subject: [ IMP ] Found out the issue of ring 3 switch. Signed-off-by: Amlal El Mahrouss --- dev/DDK/KernelDev.h | 2 +- dev/DDK/KernelStd.h | 2 +- dev/SCI/sci_base.hxx | 24 +++++--- dev/SCI/scm_core.hxx | 87 ----------------------------- dev/SCI/scm_xpcom.idl | 36 ------------ dev/SCI/xpcom_core.hxx | 85 ++++++++++++++++++++++++++++ dev/SCI/xpcom_sms.idl | 34 +++++++++++ dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm | 9 ++- dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm | 2 +- dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx | 18 ++++++ dev/ZKA/HALKit/AMD64/HalPageAlloc.hxx | 3 +- dev/ZKA/NetworkKit/IPC.hxx | 10 ++-- dev/ZKA/Sources/Heap.cxx | 9 ++- dev/ZKA/Sources/Network/IPC.cxx | 6 +- dev/ZKA/Sources/PageManager.cxx | 3 +- 15 files changed, 182 insertions(+), 148 deletions(-) delete mode 100644 dev/SCI/scm_core.hxx delete mode 100644 dev/SCI/scm_xpcom.idl create mode 100644 dev/SCI/xpcom_core.hxx create mode 100644 dev/SCI/xpcom_sms.idl (limited to 'dev') diff --git a/dev/DDK/KernelDev.h b/dev/DDK/KernelDev.h index ce7b1a2d..2e166ace 100644 --- a/dev/DDK/KernelDev.h +++ b/dev/DDK/KernelDev.h @@ -13,7 +13,7 @@ struct _KERNEL_DEVICE; /// @brief Kernel Device driver. -typedef struct _KERNEL_DEVICE +typedef struct _KERNEL_DEVICE DK_FINAL { char name[255]; // the device name. Could be /./DEVICE_NAME/ void* (*read)(void* arg, int len); // read from device. diff --git a/dev/DDK/KernelStd.h b/dev/DDK/KernelStd.h index d5350c8e..ebc8f47b 100644 --- a/dev/DDK/KernelStd.h +++ b/dev/DDK/KernelStd.h @@ -32,7 +32,7 @@ struct DDK_PROPERTY_RECORD DK_FINAL { char* p_name; void* p_object; - void* p_scm_object; + void* p_xpcom_object; }; /// \brief DDK status structure (__at_enable, __at_disable...) diff --git a/dev/SCI/sci_base.hxx b/dev/SCI/sci_base.hxx index 9a3038dc..e3bc39de 100644 --- a/dev/SCI/sci_base.hxx +++ b/dev/SCI/sci_base.hxx @@ -37,7 +37,7 @@ typedef char Char; #include #ifdef __SCI_IMPL__ -#include +#include #else class IUnknown; // Refrenced from an IDB entry. class UnknownUCLSID; // From the IDB, the constructor of the object, e.g: WordUCLSID. @@ -49,10 +49,10 @@ class UUID; /// @param uclsidOfCls /// @return template -TCLS* ScmQueryInterface(UCLSID uclsidOfCls, Args... args); +TCLS* XPCOMQueryInterface(UCLSID uclsidOfCls, Args... args); template -SInt32 ScmReleaseClass(TCLS** cls); +SInt32 XPCOMReleaseClass(TCLS** cls); /// @brief Release SCM class. /// @tparam TCLS @@ -169,23 +169,23 @@ IMPORT_C UInt32 TlsInstallIB(UInt0); /// @param uclsidOfCls UCLS factory class /// @return TCLS interface template -TCLS* ScmQueryInterface(_Input UCLSID* uclsidOfCls, _Input Args&&... args); +TCLS* XPCOMQueryInterface(_Input UCLSID* uclsidOfCls, _Input Args&&... args); /// @brief Release SCM object. /// @tparam TCLS the class type. /// @param cls the class to release. /// @return status code. template -SInt32 ScmReleaseClass(_Input TCLS* cls); +SInt32 XPCOMReleaseClass(_Input TCLS* cls); /// @brief Creates an SCM instance in the process. /// @param handle_instance the SCM handle. /// @param flags the SCM flags. -IMPORT_C SInt32 ScmCreateInstance(_Input UInt32 flags, _Output ZKAObject* handle_instance); +IMPORT_C SInt32 XPCOMCreateInstance(_Input UInt32 flags, _Output ZKAObject* handle_instance); /// @brief Destroys an SCM instance of the process. /// @param handle_instance the SCM handle. -IMPORT_C UInt0 ScmDestroyInstance(_Input ZKAObject handle_instance); +IMPORT_C UInt0 XPCOMDestroyInstance(_Input ZKAObject handle_instance); // ------------------------------------------------------------------------ // Memory Management API. @@ -195,12 +195,18 @@ IMPORT_C UInt0 ScmDestroyInstance(_Input ZKAObject handle_instance); /// @param len the length of it. /// @param flags the flags of it. /// @return heap pointer. -IMPORT_C VoidPtr RtlCreateHeap(_Input SizeT len, _Input UInt32 flags); +IMPORT_C VoidPtr MmCreateHeap(_Input SizeT len, _Input UInt32 flags); /// @brief Destroys the pointer /// @param heap the heap itself. /// @return void. -IMPORT_C UInt0 RtlDestroyHeap(_Input VoidPtr heap); +IMPORT_C UInt0 MmDestroyHeap(_Input VoidPtr heap); + +/// @brief Change protection flags of memory region. +IMPORT_C UInt32 MmChangeHeapFlags(_Input VoidPtr heap, _Input UInt32 flags); + +/// @brief Fill memory region with CRC32. +IMPORT_C UInt32 MmFillCRC32Heap(_Input VoidPtr heap); // ------------------------------------------------------------------------ // Error handling API. diff --git a/dev/SCI/scm_core.hxx b/dev/SCI/scm_core.hxx deleted file mode 100644 index db3b9c69..00000000 --- a/dev/SCI/scm_core.hxx +++ /dev/null @@ -1,87 +0,0 @@ -/* ------------------------------------------- - -Copyright ZKA Technologies. - -File: rt.internal.inl -Purpose: Base code of SCM. - -------------------------------------------- */ - -/// @internal - -#ifndef __NDK__ -#define object class -#define protocol class -#define clsid(X) - -#warning ! You may be using the clang version of the ZKA kit, please be cautious that some thing mayn't be present. ! -#endif // !__NDK__ - -// Interfaces are divided between classes. -// So that they aren't too big. - -protocol IUnknown; // Refrenced from an IDB entry. -protocol UnknownUCLSID; // From the IDB, the constructor of the object, e.g: TextUCLSID. -object UUID; - -protocol clsid("d7c144b6-0792-44b8-b06b-02b227b547df") IUnknown -{ -public: - explicit IUnknown() = default; - virtual ~IUnknown() = default; - - IUnknown& operator=(const IUnknown&) = default; - IUnknown(const IUnknown&) = default; - - virtual SInt32 Release() = 0; - virtual void RemoveRef() = 0; - virtual IUnknown* AddRef() = 0; - virtual VoidPtr QueryInterface(UUID* p_uuid) = 0; -}; - -/// @brief Allocate new SCM object. -/// @tparam TCLS the class type. -/// @tparam UCLSID UCLS factory class type. -/// @param uclsidOfCls UCLS factory class -/// @return TCLS interface -template -inline TCLS* ScmQueryInterface(UCLSID* uclsidOfCls, Args&&... args) -{ - uclsidOfCls->AddRef(); - return uclsidOfCls->QueryInterfaceWithArgs(args...); -} - -/// @brief Release SCM object. -/// @tparam TCLS the class type. -/// @param cls the class to release. -/// @return status code. -template -inline SInt32 ScmReleaseClass(TCLS** cls) -{ - if (!cls) - return -1; - - cls->RemoveRef(); - cls->Release(); - - cls = nullptr; - - return 0; -} - -template -protocol IEventListener : public ClsID -{ -public: - explicit IEventListener() = default; - virtual ~IEventListener() = default; - - IEventListener& operator=(const IEventListener&) = default; - IEventListener(const IEventListener&) = default; - - virtual IEventListener& operator +=(FnSign arg) - { - this->AddEventListener(arg); - return *this; - } -}; diff --git a/dev/SCI/scm_xpcom.idl b/dev/SCI/scm_xpcom.idl deleted file mode 100644 index 1fdbd6c9..00000000 --- a/dev/SCI/scm_xpcom.idl +++ /dev/null @@ -1,36 +0,0 @@ -/* ------------------------------------------- - -Copyright ZKA Technologies. - -File: rt.internal.inl -Purpose: Base code of SCM. - -------------------------------------------- */ - -/// @internal - -#ifndef __NDK__ -#define object class -#define protocol class -#define interface private -#define interface_method -#define CONST const -#define CHAR char -#define INT32 __INT32_TYPE__ -#define SIZE_T __SIZE_TYPE__ -#define _Output -#define _Input -#define clsid(X) - -#warning ! You may be using the clang version of the ZKA kit, please be cautious that some thing mayn't be present. ! -#endif // !__NDK__ - -protocol IPXCOMSocket; - -clsid("0943A614-0201-4107-8F8D-E909DF7F53C9") -protocol IPXCOMSocket -{ -interface: - interface_method INT32 SendMessage(_Input CONST CHAR* bytes, _Input SIZE_T bytes_size); - interface_method INT32 RecvMessage(_Output CONST CHAR** bytes_in, _Input SIZE_T bytes_size); -}; diff --git a/dev/SCI/xpcom_core.hxx b/dev/SCI/xpcom_core.hxx new file mode 100644 index 00000000..f48fd30b --- /dev/null +++ b/dev/SCI/xpcom_core.hxx @@ -0,0 +1,85 @@ +/* ------------------------------------------- + +Copyright ZKA Technologies. + +File: rt.internal.inl +Purpose: Base code of SCM. + +------------------------------------------- */ + +/// @internal + +#ifndef __NDK__ +#define object class +#define protocol class +#define clsid(X) + +#warning ! You may be using the clang version of the ZKA kit, please be cautious that some thing mayn't be present. ! +#endif // !__NDK__ + +protocol IUnknown; // Refrenced from an IDB entry. +protocol UnknownUCLSID; // From the IDB, the constructor of the object, e.g: TextUCLSID. +object UUID; + +/// @brief Unknown XPCOM interface +protocol clsid("d7c144b6-0792-44b8-b06b-02b227b547df") IUnknown +{ +public: + explicit IUnknown() = default; + virtual ~IUnknown() = default; + + IUnknown& operator=(const IUnknown&) = default; + IUnknown(const IUnknown&) = default; + + virtual SInt32 Release() = 0; + virtual void RemoveRef() = 0; + virtual IUnknown* AddRef() = 0; + virtual VoidPtr QueryInterface(UUID* p_uuid) = 0; +}; + +/// @brief Allocate new SCM object. +/// @tparam TCLS the class type. +/// @tparam UCLSID UCLS factory class type. +/// @param uclsidOfCls UCLS factory class +/// @return TCLS interface +template +inline TCLS* XPCOMQueryInterface(UCLSID* uclsidOfCls, Args&&... args) +{ + uclsidOfCls->AddRef(); + return uclsidOfCls->QueryInterfaceWithArgs(args...); +} + +/// @brief Release SCM object. +/// @tparam TCLS the class type. +/// @param cls the class to release. +/// @return status code. +template +inline SInt32 XPCOMReleaseClass(TCLS** cls) +{ + if (!cls) + return -1; + + cls->RemoveRef(); + cls->Release(); + + cls = nullptr; + + return 0; +} + +template +protocol IEventListener : public ClsID +{ +public: + explicit IEventListener() = default; + virtual ~IEventListener() = default; + + IEventListener& operator=(const IEventListener&) = default; + IEventListener(const IEventListener&) = default; + + virtual IEventListener& operator +=(FnSign arg) + { + this->AddEventListener(arg); + return *this; + } +}; diff --git a/dev/SCI/xpcom_sms.idl b/dev/SCI/xpcom_sms.idl new file mode 100644 index 00000000..8c95616b --- /dev/null +++ b/dev/SCI/xpcom_sms.idl @@ -0,0 +1,34 @@ +/* ------------------------------------------- + +Copyright ZKA Technologies. + +File: rt.internal.inl +Purpose: Base code of SCM. + +------------------------------------------- */ + +/// @internal + +#ifndef __NDK__ +#define object class +#define protocol class +#define interface private +#define interface_method +#define CONST const +#define CHAR char +#define INT32 __INT32_TYPE__ +#define SIZE_T __SIZE_TYPE__ +#define _Output +#define _Input +#define clsid(X) + +#warning ! You may be using the clang version of the ZKA kit, please be cautious that some thing mayn't be present. ! +#endif // !__NDK__ + +clsid("0943A614-0201-4107-8F8D-E909DF7F53C9") +protocol ISMS +{ +interface: + interface_method INT32 SendMessage(_Input CONST CHAR* bytes, _Input SIZE_T bytes_size); + interface_method INT32 RecvMessage(_Output CONST CHAR** bytes_in, _Input SIZE_T bytes_size); +}; diff --git a/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm b/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm index 847c609e..89cb4078 100644 --- a/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm +++ b/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm @@ -267,7 +267,7 @@ hal_switch_to_user_code: mov rbx, 0x28 mov es, rbx - mov rsp, [hal_user_code_stack_end] + mov rsp, hal_user_code_stack_end mov rcx, hal_user_code_start mov r11, 0x0202 @@ -275,9 +275,14 @@ hal_switch_to_user_code: o64 sysret hal_user_code_start: -L0: + hlt nop jmp $ +hal_user_code_end: + +section .data + +hal_user_code_sz: dq hal_user_code_end - hal_user_code_start section .bss diff --git a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm index f20ffcee..f5f36b27 100644 --- a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm +++ b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm @@ -50,7 +50,7 @@ mp_do_context_switch: ;; Swap registers, since it's the other way around. mov rcx, r12 ;; code ptr - mov rsp, [r11] ;; stack ptr + mov rsp, r11 ;; stack ptr mov r11, 0x0202 ;; rcx and rdx already set. diff --git a/dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx b/dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx index 4e1b500c..82eea45e 100644 --- a/dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx +++ b/dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx @@ -135,5 +135,23 @@ namespace Kernel // Now allocate the page. return hal_try_alloc_new_page(rw, user, size); } + + auto hal_free_page(VoidPtr page_ptr) -> Bool + { + if (!page_ptr) + return false; + + Detail::VIRTUAL_MEMORY_HEADER* result = reinterpret_cast((UIntPtr)page_ptr - sizeof(Detail::VIRTUAL_MEMORY_HEADER)); + + if (result->Magic != cVMHMagic) + return false; + + if (result->Present != true) + return true; + + result->Present = false; + + return true; + } } // namespace HAL } // namespace Kernel diff --git a/dev/ZKA/HALKit/AMD64/HalPageAlloc.hxx b/dev/ZKA/HALKit/AMD64/HalPageAlloc.hxx index 30cb7911..38666e7e 100644 --- a/dev/ZKA/HALKit/AMD64/HalPageAlloc.hxx +++ b/dev/ZKA/HALKit/AMD64/HalPageAlloc.hxx @@ -82,7 +82,8 @@ namespace Kernel::HAL PageTable64 ALIGN(kPTEAlign) Pte[kPTEMax]; }; - VoidPtr hal_alloc_page(Boolean rw, Boolean user, SizeT size); + auto hal_alloc_page(Boolean rw, Boolean user, SizeT size) -> VoidPtr; + auto hal_free_page(VoidPtr page_ptr) -> Bool; } // namespace Kernel::HAL namespace Kernel diff --git a/dev/ZKA/NetworkKit/IPC.hxx b/dev/ZKA/NetworkKit/IPC.hxx index 069a2c66..d71248cf 100644 --- a/dev/ZKA/NetworkKit/IPC.hxx +++ b/dev/ZKA/NetworkKit/IPC.hxx @@ -19,12 +19,12 @@ /// @brief IPC EP protocol. /// IA separator. -#define cIPCEPRemoteSeparator ":" +#define cXPCOMRemoteSeparator ":" /// Interchange address, consists of PID:TEAM. -#define cIPCEPRemoteInvalid "00:00" +#define cXPCOMRemoteInvalid "00:00" -#define cIPCEPHeaderMagic (0x4950434) +#define cXPCOMHeaderMagic (0x4950434) namespace Kernel { @@ -61,7 +61,7 @@ namespace Kernel eIPCEPMixedEndian = 2, }; - constexpr auto cIPCEPMsgSize = 6094U; + constexpr auto cXPCOMMsgSize = 6094U; /// @brief IPC connection header, message cannot be greater than 6K. typedef struct IPC_MESSAGE_STRUCT final @@ -74,7 +74,7 @@ namespace Kernel UInt32 IpcCRC32; UInt32 IpcMsg; UInt32 IpcMsgSz; - UInt8 IpcData[cIPCEPMsgSize]; + UInt8 IpcData[cXPCOMMsgSize]; } PACKED IPC_MESSAGE_STRUCT; /// @brief Sanitize packet function diff --git a/dev/ZKA/Sources/Heap.cxx b/dev/ZKA/Sources/Heap.cxx index a89ed584..0146bd55 100644 --- a/dev/ZKA/Sources/Heap.cxx +++ b/dev/ZKA/Sources/Heap.cxx @@ -34,6 +34,8 @@ namespace Kernel UInt32 fMagic; ///! @brief Boolean value which tells if the heap is allocated. Boolean fPresent; + /// @brief Is this valued owned by the user? + Boolean fUserOwned; ///! @brief 32-bit CRC checksum. UInt32 fCRC32; /// @brief 64-bit pointer size. @@ -108,6 +110,8 @@ namespace Kernel heap_info_ptr->fCRC32 = 0U; // dont fill it for now. heap_info_ptr->fTargetPtr = wrapper.VirtualAddress() + sizeof(Detail::HEAP_INFORMATION_BLOCK); heap_info_ptr->fPagePtr = 0UL; + heap_info_ptr->fUserOwned = user; + heap_info_ptr->fPresent = true; ++kHeapCount; @@ -176,7 +180,10 @@ namespace Kernel ke_calculate_crc32((Char*)heapInfoBlk->fTargetPtr, heapInfoBlk->fTargetPtrSize)) { - ke_stop(RUNTIME_CHECK_POINTER); + if (!heapInfoBlk->fUserOwned) + { + ke_stop(RUNTIME_CHECK_POINTER); + } } } diff --git a/dev/ZKA/Sources/Network/IPC.cxx b/dev/ZKA/Sources/Network/IPC.cxx index e5926c7f..e60742ca 100644 --- a/dev/ZKA/Sources/Network/IPC.cxx +++ b/dev/ZKA/Sources/Network/IPC.cxx @@ -41,12 +41,12 @@ Bool ipc_int_sanitize_packet(IPC_MESSAGE_STRUCT* pckt) } if (pckt->IpcFrom == pckt->IpcTo || - pckt->IpcPacketSize > cIPCEPMsgSize) + pckt->IpcPacketSize > cXPCOMMsgSize) { goto ipc_check_failed; } - return pckt->IpcPacketSize > 1 && pckt->IpcHeaderMagic == cIPCEPHeaderMagic; + return pckt->IpcPacketSize > 1 && pckt->IpcHeaderMagic == cXPCOMHeaderMagic; ipc_check_failed: ErrLocal() = kErrorIPC; @@ -92,7 +92,7 @@ namespace Kernel if (*pckt_in) { - (*pckt_in)->IpcHeaderMagic = cIPCEPHeaderMagic; + (*pckt_in)->IpcHeaderMagic = cXPCOMHeaderMagic; auto endian = DEDUCE_ENDIAN((*pckt_in), ((Char*)(*pckt_in))[0]); diff --git a/dev/ZKA/Sources/PageManager.cxx b/dev/ZKA/Sources/PageManager.cxx index 549415f3..d14130ff 100644 --- a/dev/ZKA/Sources/PageManager.cxx +++ b/dev/ZKA/Sources/PageManager.cxx @@ -82,8 +82,9 @@ namespace Kernel { if (wrapper) { - if (!Detail::page_disable(wrapper->VirtualAddress())) + if (!Kernel::HAL::hal_free_page((VoidPtr)wrapper->VirtualAddress())) return false; + return true; } -- cgit v1.2.3