From a0f82d57976648c5bfcf165b2e304d2a4c8fb0c7 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 19 Mar 2024 22:50:16 +0100 Subject: Kernel:Secret: Fix String class. Improve kernel APIs. --- .../Kits/System.Core/AMD64/HCoreAssemblyRoutines.s | 24 ++++++++ Public/Kits/System.Core/ARM64/.gitkeep | 0 Public/Kits/System.Core/Defs.hxx | 17 +++--- Public/Kits/System.Core/HCoreHeap.s | 23 -------- Public/Kits/System.Core/Heap.cxx | 42 +++++++++++++ Public/Kits/System.Core/Heap.hxx | 22 +------ Public/Kits/System.Core/HeapCoreImpl.cxx | 47 --------------- Public/Kits/System.Core/HeapImpl.cxx | 68 ++++++++++++---------- Public/Kits/System.Core/Makefile | 6 +- 9 files changed, 116 insertions(+), 133 deletions(-) create mode 100644 Public/Kits/System.Core/AMD64/HCoreAssemblyRoutines.s create mode 100644 Public/Kits/System.Core/ARM64/.gitkeep delete mode 100644 Public/Kits/System.Core/HCoreHeap.s create mode 100644 Public/Kits/System.Core/Heap.cxx delete mode 100644 Public/Kits/System.Core/HeapCoreImpl.cxx (limited to 'Public/Kits/System.Core') diff --git a/Public/Kits/System.Core/AMD64/HCoreAssemblyRoutines.s b/Public/Kits/System.Core/AMD64/HCoreAssemblyRoutines.s new file mode 100644 index 00000000..71984042 --- /dev/null +++ b/Public/Kits/System.Core/AMD64/HCoreAssemblyRoutines.s @@ -0,0 +1,24 @@ +/** =========================================== + (C) Mahrouss Logic + ===========================================*/ + +.section .text + +.globl HcGetProcessObject +.globl __assert_chk_fail + +/* @brief Process object getter */ +/* @throws: ApptError: appartement error. */ +HcGetProcessObject: + mov $0x10, %rcx /* sysGetProcessObject */ + int $0x21 + + /* rax gets saved and returned. */ + ret + +__assert_chk_fail: + mov $0x11, %rcx /* sysTerminateCurrentProcess */ + int $0x21 + + ret + diff --git a/Public/Kits/System.Core/ARM64/.gitkeep b/Public/Kits/System.Core/ARM64/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Public/Kits/System.Core/Defs.hxx b/Public/Kits/System.Core/Defs.hxx index 9a17d571..84bdc7bb 100644 --- a/Public/Kits/System.Core/Defs.hxx +++ b/Public/Kits/System.Core/Defs.hxx @@ -88,6 +88,7 @@ typedef bool BOOL; #define CA_STATIC static #define CA_INLINE inline +#define CA_CONST const #ifdef __cplusplus #define CA_CONSTEXPR constexpr @@ -110,19 +111,19 @@ enum HcProcessCall { #include -class Exception { +class SystemException { public: - explicit Exception() = default; - virtual ~Exception() = default; + explicit SystemException() = default; + virtual ~SystemException() = default; public: - HCORE_COPY_DEFAULT(Exception); + HCORE_COPY_DEFAULT(SystemException); public: - const char *Name(); - const char *Reason(); + virtual const char *Name() = 0; + virtual const char *Reason() = 0; private: const char *mReason{ - "System.Core: System Exception: Catastrophic failure!"}; -}; + "System.Core: SystemException: Catastrophic failure!"}; +}; \ No newline at end of file diff --git a/Public/Kits/System.Core/HCoreHeap.s b/Public/Kits/System.Core/HCoreHeap.s deleted file mode 100644 index 89d85680..00000000 --- a/Public/Kits/System.Core/HCoreHeap.s +++ /dev/null @@ -1,23 +0,0 @@ -/** =========================================== - (C) Mahrouss Logic - ===========================================*/ - -.section .text - -.globl HcGetProcessObject -.globl __assert_chk_fail - -/* Process Heap getter */ -HcGetProcessObject: - mov $0x10, %rcx /* sysGetProcessObject */ - int $0x21 - - /* rax gets saved and returned. */ - ret - -__assert_chk_fail: - mov $0x11, %rcx /* sysTerminateCurrentProcess */ - int $0x21 - - ret - diff --git a/Public/Kits/System.Core/Heap.cxx b/Public/Kits/System.Core/Heap.cxx new file mode 100644 index 00000000..4dfd34e4 --- /dev/null +++ b/Public/Kits/System.Core/Heap.cxx @@ -0,0 +1,42 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#include +#include + +using namespace System; + +/// @brief Shared instance of the heap. +/// @return +Heap* Heap::Shared() noexcept { + static Heap* heap = nullptr; + + if (!heap) { + heap = new Heap(); + } + + return heap; +} + +Heap::Heap() { } +Heap::~Heap() { delete this; } + +void Heap::Delete(HeapPtr me) noexcept { + CA_MUST_PASS(me); + HcFreeProcessHeap(kInstanceObject, me); +} + +SizeT Heap::Size(HeapPtr me) noexcept { + CA_MUST_PASS(me); + return HcProcessHeapSize(kInstanceObject, me); +} + +HeapPtr Heap::New(const SizeT& sz, const Int32 flags) { + SizeT _sz = sz; + if (!_sz) ++_sz; + + return HcAllocateProcessHeap(kInstanceObject, _sz, flags); +} \ No newline at end of file diff --git a/Public/Kits/System.Core/Heap.hxx b/Public/Kits/System.Core/Heap.hxx index 31e44c7e..1bfc00de 100644 --- a/Public/Kits/System.Core/Heap.hxx +++ b/Public/Kits/System.Core/Heap.hxx @@ -40,7 +40,7 @@ enum { class Heap final { private: - explicit Heap() = default; + explicit Heap(); public: ~Heap(); @@ -56,24 +56,4 @@ class Heap final { SizeT Size(HeapPtr me) noexcept; HeapPtr New(const SizeT &sz, const Int32 flags = kHeapNoFlags); }; - -class MemoryException final { - public: - explicit MemoryException() = default; - ~MemoryException() = default; - - public: - HCORE_COPY_DEFAULT(MemoryException); - - public: - const char *Name(); - const char *Reason(); - - private: - const char *mReason{ - "System.Core: Process Heap Exception: Catastrophic failure!"}; - - private: - friend Heap; -}; } // namespace System \ No newline at end of file diff --git a/Public/Kits/System.Core/HeapCoreImpl.cxx b/Public/Kits/System.Core/HeapCoreImpl.cxx deleted file mode 100644 index 2980a3de..00000000 --- a/Public/Kits/System.Core/HeapCoreImpl.cxx +++ /dev/null @@ -1,47 +0,0 @@ -/** =========================================== - (C) Mahrouss Logic - ===========================================*/ - -#include - -/// @brief Allocate from the user's heap. -/// @param refObj Process object. -/// @param sz size of object. -/// @param flags flags. -/// @return -CA_EXTERN_C PVOID HcAllocateProcessHeap(ObjectPtr refObj, QWORD sz, DWORD flags) -{ - CA_MUST_PASS(sz); - CA_MUST_PASS(flags); - - return (PVOID)refObj->Invoke(refObj, kProcessCallAllocPtr, sz, flags); -} - -/// @brief Free pointer from the user's heap. -/// @param refObj Process object. -/// @param ptr the pointer to free. -CA_EXTERN_C VOID HcFreeProcessHeap(ObjectPtr refObj, PVOID ptr) -{ - CA_MUST_PASS(ptr); - CA_UNREFERENCED_PARAMETER(refObj->Invoke(refObj, kProcessCallFreePtr, ptr)); -} - -/// @brief Get pointer size. -/// @param refObj Process object. -/// @param ptr the pointer to find. -/// @return the size. -CA_EXTERN_C QWORD HcProcessHeapSize(ObjectPtr refObj, PVOID ptr) -{ - CA_MUST_PASS(ptr); - return refObj->Invoke(refObj, kProcessCallSizePtr, ptr); -} - -/// @brief Check if the pointer exists. -/// @param refObj Process object. -/// @param ptr the pointer to check. -/// @return if it exists -CA_EXTERN_C BOOL HcProcessHeapExists(ObjectPtr refObj, PVOID ptr) -{ - CA_MUST_PASS(ptr); - return refObj->Invoke(refObj, kProcessCallCheckPtr, ptr); -} diff --git a/Public/Kits/System.Core/HeapImpl.cxx b/Public/Kits/System.Core/HeapImpl.cxx index f41c868f..2980a3de 100644 --- a/Public/Kits/System.Core/HeapImpl.cxx +++ b/Public/Kits/System.Core/HeapImpl.cxx @@ -1,41 +1,47 @@ -/* ------------------------------------------- - - Copyright Mahrouss Logic - -------------------------------------------- */ +/** =========================================== + (C) Mahrouss Logic + ===========================================*/ #include -#include -using namespace System; - -/// @brief Shared instance of the heap. +/// @brief Allocate from the user's heap. +/// @param refObj Process object. +/// @param sz size of object. +/// @param flags flags. /// @return -Heap* Heap::Shared() noexcept { - static Heap* heap = nullptr; - - if (!heap) { - heap = new Heap(); - } - - return heap; +CA_EXTERN_C PVOID HcAllocateProcessHeap(ObjectPtr refObj, QWORD sz, DWORD flags) +{ + CA_MUST_PASS(sz); + CA_MUST_PASS(flags); + + return (PVOID)refObj->Invoke(refObj, kProcessCallAllocPtr, sz, flags); } -Heap::~Heap() { delete this; } - -void Heap::Delete(HeapPtr me) noexcept { - CA_MUST_PASS(me); - HcFreeProcessHeap(kInstanceObject, me); +/// @brief Free pointer from the user's heap. +/// @param refObj Process object. +/// @param ptr the pointer to free. +CA_EXTERN_C VOID HcFreeProcessHeap(ObjectPtr refObj, PVOID ptr) +{ + CA_MUST_PASS(ptr); + CA_UNREFERENCED_PARAMETER(refObj->Invoke(refObj, kProcessCallFreePtr, ptr)); } -SizeT Heap::Size(HeapPtr me) noexcept { - CA_MUST_PASS(me); - return HcProcessHeapSize(kInstanceObject, me); +/// @brief Get pointer size. +/// @param refObj Process object. +/// @param ptr the pointer to find. +/// @return the size. +CA_EXTERN_C QWORD HcProcessHeapSize(ObjectPtr refObj, PVOID ptr) +{ + CA_MUST_PASS(ptr); + return refObj->Invoke(refObj, kProcessCallSizePtr, ptr); } -HeapPtr Heap::New(const SizeT& sz, const Int32 flags) { - SizeT _sz = sz; - if (!_sz) ++_sz; - - return HcAllocateProcessHeap(kInstanceObject, _sz, flags); -} \ No newline at end of file +/// @brief Check if the pointer exists. +/// @param refObj Process object. +/// @param ptr the pointer to check. +/// @return if it exists +CA_EXTERN_C BOOL HcProcessHeapExists(ObjectPtr refObj, PVOID ptr) +{ + CA_MUST_PASS(ptr); + return refObj->Invoke(refObj, kProcessCallCheckPtr, ptr); +} diff --git a/Public/Kits/System.Core/Makefile b/Public/Kits/System.Core/Makefile index 286c1120..fb0f3e87 100644 --- a/Public/Kits/System.Core/Makefile +++ b/Public/Kits/System.Core/Makefile @@ -7,9 +7,9 @@ CC=x86_64-w64-mingw32-g++ CCFLAGS=-shared -ffreestanding -nostdlib -fno-rtti -fno-exceptions -std=c++20 -Xlinker --subsystem=17 OUTPUT=System.Core.dll -.PHONY: build-core -build-core: - $(CC) -I../ -I$(HOME) -I../../../Private/ $(CCFLAGS) $(wildcard *.cxx) $(wildcard *.s) -o $(OUTPUT) +.PHONY: build-core-amd64 +build-core-amd64: + $(CC) -I../ -I$(HOME) -I../../../Private/ $(CCFLAGS) $(wildcard *.cxx) $(wildcard AMD64/*.s) -o $(OUTPUT) .PHONY: all all: build-core -- cgit v1.2.3