From 2a51866f3f39d4ebf0fd2d132cfad1e2962d1236 Mon Sep 17 00:00:00 2001 From: Amlal EL Mahrouss Date: Tue, 8 Oct 2024 19:17:37 +0200 Subject: IMP: A New set of features and APIs in zka-sci-cxx.dll - IStr object for a class like string object. - New SCI APIs, Disk management. Loader, I/O and new Mm functions. - Fixed and improved XPCOMReleaseClass. - Using IStr when dealing with XPCOM events. Signed-off-by: Amlal EL Mahrouss --- dev/sci/sci.json | 2 +- dev/sci/sci_base.hxx | 105 +++++++++++++++++++++++++++++++++++++++----- dev/sci/sci_lpc.hxx | 2 + dev/sci/xpcom_core.hxx | 21 ++++++--- dev/zka/NewKit/Defines.hxx | 3 +- dev/zka/src/Heap.cxx | 12 ++--- dev/zka/src/Network/IPC.cxx | 4 +- 7 files changed, 122 insertions(+), 27 deletions(-) (limited to 'dev') diff --git a/dev/sci/sci.json b/dev/sci/sci.json index 0b7c6bb7..0bc6cdcf 100644 --- a/dev/sci/sci.json +++ b/dev/sci/sci.json @@ -13,7 +13,7 @@ "-Wl,--subsystem=17" ], "cpp_macros": [ - "__SCI_IMPL__", + "__XPCOM_IMPL__", "cSCIVersion=0x0100", "cSCIVersionHighest=0x0100", "cSCIVersionLowest=0x0100" diff --git a/dev/sci/sci_base.hxx b/dev/sci/sci_base.hxx index e73da45b..70a0f7f4 100644 --- a/dev/sci/sci_base.hxx +++ b/dev/sci/sci_base.hxx @@ -2,8 +2,8 @@ Copyright ZKA Technologies. -File: SCIBase.hxx -Purpose: sci/M core header file (C++) +File: sci_base.hxx +Purpose: SCI core header file (C++ only). ------------------------------------------- */ @@ -39,7 +39,7 @@ typedef char Char; #include -#ifdef __SCI_IMPL__ +#ifdef __XPCOM_IMPL__ #include #else class IUnknown; // Refrenced from an IDB entry. @@ -92,25 +92,25 @@ typedef Object SocketObject; // ------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------ // -/// @note Part of ZKA loader API. +/// @note Dynamic Loader API. // ------------------------------------------------------------------------------------------ // /// @brief Get function which is part of the DLL. /// @param symbol the symbol to look for /// @param dll_handle the DLL handle. /// @return the proc pointer. -IMPORT_C Object LdrGetDLLProc(_Input const Char* symbol, _Input Object dll_handle); +IMPORT_C Object LdrGetDLLSymbolFromHandle(_Input const Char* symbol, _Input Object dll_handle); /// @brief Open DLL handle. /// @param path /// @param drv /// @return -IMPORT_C Object LdrOpenDLL(_Input const Char* path, _Input const Char* drive_letter); +IMPORT_C Object LdrOpenDLLHandle(_Input const Char* path, _Input const Char* drive_letter); /// @brief Close DLL handle /// @param dll_handle /// @return -IMPORT_C Void LdrCloseDLL(_Input Object dll_handle); +IMPORT_C Void LdrCloseDLLHandle(_Input Object* dll_handle); // ------------------------------------------------------------------------------------------ // // File API. @@ -127,12 +127,27 @@ IMPORT_C Object IoOpenFile(const Char* fs_path, const Char* drive_letter); /// @return void. IMPORT_C Void IoCloseFile(_Input Object file_desc); +/// @brief Write data to a file. +/// @param file_desc the file descriptor. +/// @param out_data the data to write. +/// @param sz_data the size of the data to write. +/// @return the number of bytes written. IMPORT_C UInt32 IoWriteFile(_Input Object file_desc, _Output VoidPtr out_data, SizeT sz_data); +/// @brief Read data from a file. +/// @param file_desc the file descriptor. +/// @param out_data the data to read. +/// @param sz_data the size of the data to read. IMPORT_C UInt32 IoReadFile(_Input Object file_desc, _Output VoidPtr* out_data, SizeT sz_data); +/// @brief Rewind the file pointer to the beginning of the file. +/// @param file_desc the file descriptor. +/// @return the number of bytes read. IMPORT_C UInt64 IoRewindFile(_Input Object file_desc); +/// @brief Tell the current position of the file pointer. +/// @param file_desc the file descriptor. +/// @return the current position of the file pointer. IMPORT_C UInt64 IoTellFile(_Input Object file_desc); IMPORT_C UInt64 IoSeekFile(_Input Object file_desc, UInt64 file_offset); @@ -146,7 +161,7 @@ IMPORT_C UInt64 IoSeekFile(_Input Object file_desc, UInt64 file_offset); /// @return > 0 error ocurred or already present, = 0 success. IMPORT_C UInt32 RtlTlsInstall(Void); -#ifndef __SCI_IMPL__ +#ifndef __XPCOM_IMPL__ // ------------------------------------------------------------------------ // XPCOM API. @@ -176,7 +191,7 @@ IMPORT_C SInt32 XPCOMCreateInstance(_Input UInt32 flags, _Output Object* handle_ /// @param handle_instance the XPCOM handle. IMPORT_C Void XPCOMDestroyInstance(_Input Object handle_instance); -#endif // !__SCI_IMPL__ +#endif // !__XPCOM_IMPL__ // ------------------------------------------------------------------------ // Memory Management API. @@ -196,9 +211,15 @@ IMPORT_C Void 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. +/// @brief Fill memory region with CRC32. IMPORT_C UInt32 MmFillCRC32Heap(_Input VoidPtr heap); +/// @brief Copy memory region. +IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len); + +/// @brief Fill memory region. +IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value); + // ------------------------------------------------------------------------ // Error handling API. // ------------------------------------------------------------------------ @@ -209,22 +230,86 @@ IMPORT_C SInt32 ErrGetLastError(Void); // Threading API. // ------------------------------------------------------------------------ +/// @brief Exit the current thread. +/// @param exit_code the exit code. IMPORT_C Void ThrExitCurrentThread(_Input SInt32 exit_code); + +/// @brief Exit the main thread. +/// @param exit_code the exit code. IMPORT_C Void ThrExitMainThread(_Input SInt32 exit_code); +/// @brief Exit a thread. +/// @param thread the thread to exit. +/// @param exit_code the exit code. IMPORT_C Void ThrExitThread(_Input ThreadObject thread, _Input SInt32 exit_code); typedef Void(*GenericThreadFn)(Void); +/// @brief Create a thread. +/// @param proc the thread procedure. +/// @return the thread object. IMPORT_C ThreadObject ThrCreateThread(GenericThreadFn proc); +/// @brief Yield the current thread. +/// @param thread the thread to yield. IMPORT_C Void ThrExitYieldThread(Void); +/// @brief Join a thread. +/// @param thread the thread to join. IMPORT_C Void ThrExitJoinThread(Void); + +/// @brief Detach a thread. +/// @param thread the thread to detach. IMPORT_C Void ThrExitDetachThread(Void); // ------------------------------------------------------------------------ // Drive Management API. // ------------------------------------------------------------------------ +/// @brief Get the default drive letter. +/// @param void. +/// @return the drive letter. +IMPORT_C Char* DrvGetDefaultDriveLetter(Void); + +/// @brief Get the drive letter from a path. +/// @param path the path. +/// @return the drive letter. +IMPORT_C Char* DrvGetDriveLetterFromPath(_Input const Char* path); + +/// @brief Get a mounted drive from a letter. +/// @param letter the letter (A..Z). +/// @return the drive object. +IMPORT_C Object DrvGetMountedDrive(_Input const Char letter); + +/// @brief Mount a drive. +/// @param path the path to mount. +/// @param letter the letter to mount. +IMPORT_C Void DrvMountDrive(_Input const Char* path, _Input const Char* letter); + +/// @brief Unmount a drive. +/// @param letter the letter to unmount. +IMPORT_C Void DrvUnmountDrive(_Input const Char letter); + +// ------------------------------------------------------------------------ +// Event handling API, use to listen to OS specific events. +// ------------------------------------------------------------------------ + +/// @brief Add an event listener. +/// @param event_name the event name. +/// @param listener the listener to add. +/// @return the event listener. +IMPORT_C Void EvtAddListener(_Input const Char* event_name, _Input Object listener); + +/// @brief Remove an event listener. +/// @param event_name the event name. +/// @param listener the listener to remove. +/// @return the event listener. +IMPORT_C Void EvtRemoveListener(_Input const Char* event_name, _Input Object listener); + +/// @brief Dispatch an event. +/// @param event_name the event name. +/// @param event_data the event data. +/// @return the event data. +IMPORT_C VoidPtr EvtDispatchEvent(_Input const Char* event_name, _Input VoidPtr event_data); + #endif // ifndef __SCI_BASE_HXX__ diff --git a/dev/sci/sci_lpc.hxx b/dev/sci/sci_lpc.hxx index 6b52050b..c8fb0b55 100644 --- a/dev/sci/sci_lpc.hxx +++ b/dev/sci/sci_lpc.hxx @@ -44,4 +44,6 @@ inline constexpr ErrObject kErrorSign = 60; inline constexpr ErrObject kErrorInvalidCreds = 61; inline constexpr ErrObject kErrorUnimplemented = 0; +/// @brief The last error reported by the system to the process. IMPORT_C ErrObject kLastError; + diff --git a/dev/sci/xpcom_core.hxx b/dev/sci/xpcom_core.hxx index 2b2754e6..ef82733e 100644 --- a/dev/sci/xpcom_core.hxx +++ b/dev/sci/xpcom_core.hxx @@ -20,6 +20,7 @@ Purpose: Base code of XPCOM. protocol IUnknown; // Refrenced from an IDB entry. protocol ICLSID; // From the IDB, the constructor of the object, e.g: TextUCLSID. object UUID; +object IStr; /// @brief Unknown XPCOM interface protocol clsid("d7c144b6-0792-44b8-b06b-02b227b547df") IUnknown @@ -45,6 +46,9 @@ public: template inline TCLS* XPCOMQueryInterface(UCLSID* uclsidOfCls, Args&&... args) { + if (uclsidOfCls == nullptr) + return nullptr; + uclsidOfCls->AddRef(); return uclsidOfCls->QueryInterfaceWithArgs(args...); } @@ -56,17 +60,20 @@ inline TCLS* XPCOMQueryInterface(UCLSID* uclsidOfCls, Args&&... args) template inline SInt32 XPCOMReleaseClass(TCLS** cls) { - if (!cls) - return -1; + if (!*cls) + return -kErrorInvalidData; - cls->RemoveRef(); - cls->Release(); + (*cls)->RemoveRef(); + (*cls)->Release(); - cls = nullptr; + *cls = nullptr; - return 0; + return kErrorSuccess; } +/// @brief Event listener interface. +/// @tparam FnSign the event listener function type. +/// @tparam ClsID the event listener class ID. template protocol IEventListener : public ClsID { @@ -78,7 +85,7 @@ protocol IEventListener : public ClsID IEventListener& operator=(const IEventListener&) = default; IEventListener(const IEventListener&) = default; - virtual IEventListener& operator-=(const Char* event_name) + virtual IEventListener& operator-=(const IStr* event_name) { this->RemoveEventListener(event_name); return *this; diff --git a/dev/zka/NewKit/Defines.hxx b/dev/zka/NewKit/Defines.hxx index 712b1f63..b50252a0 100644 --- a/dev/zka/NewKit/Defines.hxx +++ b/dev/zka/NewKit/Defines.hxx @@ -147,6 +147,7 @@ namespace Kernel }; /// \brief Scheduler interface, represents a scheduler object. + /// @note This is used to schedule tasks, such as threads, drivers, user threads, etc. class ISchedulerObject { public: @@ -176,7 +177,7 @@ namespace Kernel }; } // namespace Kernel -#define DEDUCE_ENDIAN(address, value) \ +#define cDeduceEndian(address, value) \ (((reinterpret_cast(address)[0]) == (value)) \ ? (Kernel::Endian::kEndianBig) \ : (Kernel::Endian::kEndianLittle)) diff --git a/dev/zka/src/Heap.cxx b/dev/zka/src/Heap.cxx index edab14a2..ddf20b9c 100644 --- a/dev/zka/src/Heap.cxx +++ b/dev/zka/src/Heap.cxx @@ -19,7 +19,7 @@ ------------------------------------------- */ //! @file Heap.cxx -//! @brief Kernel's heap allocator. +//! @brief Kernel's heap manager, serves as the main memory manager. #define kKernelHeapMagic (0xD4D7D5) #define kKernelAlignSz (__BIGGEST_ALIGNMENT__) @@ -33,7 +33,7 @@ namespace Kernel /// @brief Kernel heap information block. /// Located before the address bytes. - /// | HIB | ADDRESS | + /// | HIB | CLASS/STRUCT/DATA TYPES... | struct PACKED HEAP_INFORMATION_BLOCK final { ///! @brief 32-bit value which contains the magic number of the heap. @@ -65,7 +65,7 @@ namespace Kernel }; /// @brief Check for heap address validity. - /// @param heap_ptr The address_ptr + /// @param heap_ptr The address_ptr to check. /// @return Bool if the pointer is valid or not. auto mm_check_heap_address(VoidPtr heap_ptr) -> Bool { @@ -160,8 +160,8 @@ namespace Kernel } /// @brief Makes a page heap. - /// @param heap_ptr - /// @return + /// @param heap_ptr the pointer to make a page heap. + /// @return kErrorSuccess if successful, otherwise an error code. Int32 mm_make_ke_page(VoidPtr heap_ptr) { if (Detail::mm_check_heap_address(heap_ptr) == No) @@ -271,7 +271,7 @@ namespace Kernel { Detail::HEAP_INFORMATION_BLOCK_PTR heap_blk = reinterpret_cast( - (UIntPtr)(heap_ptr) - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); if (heap_ptr && heap_blk->fPresent && kKernelHeapMagic == heap_blk->fMagic) { diff --git a/dev/zka/src/Network/IPC.cxx b/dev/zka/src/Network/IPC.cxx index 42b0fcb9..f533b58e 100644 --- a/dev/zka/src/Network/IPC.cxx +++ b/dev/zka/src/Network/IPC.cxx @@ -14,7 +14,7 @@ using namespace Kernel; /// @brief The internal sanitize function. Bool ipc_int_sanitize_packet(IPC_MESSAGE_STRUCT* pckt) { - auto endian = DEDUCE_ENDIAN(pckt, ((Char*)pckt)[0]); + auto endian = cDeduceEndian(pckt, ((Char*)pckt)[0]); switch (endian) { @@ -92,7 +92,7 @@ namespace Kernel if (*pckt_in) { - auto endian = DEDUCE_ENDIAN((*pckt_in), ((Char*)(*pckt_in))[0]); + auto endian = cDeduceEndian((*pckt_in), ((Char*)(*pckt_in))[0]); (*pckt_in)->IpcHeaderMagic = cXPCOMHeaderMagic; -- cgit v1.2.3