From 333fed96b7ccd3ee4f5f097445408dde34d330db Mon Sep 17 00:00:00 2001 From: amlal Date: Thu, 21 Mar 2024 00:38:52 +0100 Subject: Kernel: See below. - Fix kernel heap, made it better. - Fix System.Core, made it better. Signed-off-by: amlal --- Private/ArchKit/ArchKit.hpp | 24 ++-------- Private/HALKit/AMD64/HalInterruptRouting.asm | 17 ++----- Private/HALKit/AMD64/HalKernelMain.cxx | 3 +- Private/HALKit/RISCV/Hart.hxx | 2 +- Private/KernelKit/HError.hpp | 1 + Private/Source/KernelCheck.cxx | 4 +- Private/Source/KernelHeap.cxx | 49 ++++++++++--------- Public/Kits/System.Core/Defs.hxx | 71 +++++++++++++++++++++++++--- Public/Kits/System.Core/File.hxx | 57 ++++++++++++++++------ Public/Kits/System.Core/Heap.cxx | 5 +- Public/Kits/System.Core/Heap.hxx | 20 ++++++++ Public/Kits/System.Core/HeapImpl.cxx | 47 ------------------ Public/Kits/System.Core/HeapRuntime.cxx | 44 +++++++++++++++++ Public/Kits/System.Core/System.Core.hxx | 2 +- 14 files changed, 214 insertions(+), 132 deletions(-) delete mode 100644 Public/Kits/System.Core/HeapImpl.cxx create mode 100644 Public/Kits/System.Core/HeapRuntime.cxx diff --git a/Private/ArchKit/ArchKit.hpp b/Private/ArchKit/ArchKit.hpp index 17b70aa7..7f5624c1 100644 --- a/Private/ArchKit/ArchKit.hpp +++ b/Private/ArchKit/ArchKit.hpp @@ -20,25 +20,10 @@ #error Unknown architecture #endif -namespace HCore { -template -class SystemCall { - public: - explicit SystemCall() { kcout << "SystemCall::SystemCall"; } - - virtual ~SystemCall() { kcout << "SystemCall::~SystemCall"; } +#define kVirtualAddressStartOffset 0x100 - SystemCall &operator=(const SystemCall &) = default; - SystemCall(const SystemCall &) = default; - - // Should not be called alone! - virtual bool Exec() const { - kcout << "SystemCall->Exec()"; - return false; - } -}; - -constexpr static inline SSizeT syscall_hash(const char *seed, int mul) { +namespace HCore { +constexpr static inline SSizeT rt_hash_seed(const char *seed, int mul) { SSizeT hash = 0; for (SSizeT idx = 0; seed[idx] != 0; ++idx) { @@ -48,8 +33,6 @@ constexpr static inline SSizeT syscall_hash(const char *seed, int mul) { return hash; } - -bool ke_init_hal(); } // namespace HCore #define kKernelMaxSystemCalls (256) @@ -61,7 +44,6 @@ extern HCore::Arrayf_VirtualSize; - kKernelVirtualStart = HandoverHeader->f_VirtualStart; + kKernelVirtualStart = (HandoverHeader->f_VirtualStart + kVirtualAddressStartOffset); kKernelPhysicalSize = HandoverHeader->f_PhysicalSize; kKernelPhysicalStart = HandoverHeader->f_PhysicalStart; @@ -76,6 +76,7 @@ EXTERN_C void RuntimeMain( /// Mounts a NewFS block. HCore::FilesystemManagerInterface::Mount(new HCore::NewFilesystemManager()); + HCore::ke_delete_ke_heap(HCore::FilesystemManagerInterface::Unmount()); /// We already have an install of HCore. if (HandoverHeader->f_Bootloader == kInstalledMedia) { diff --git a/Private/HALKit/RISCV/Hart.hxx b/Private/HALKit/RISCV/Hart.hxx index f351d3e5..fbf7dca1 100644 --- a/Private/HALKit/RISCV/Hart.hxx +++ b/Private/HALKit/RISCV/Hart.hxx @@ -3,7 +3,7 @@ Copyright Mahrouss Logic File: Hart.hxx - Purpose: RISC-V hart abstraction. + Purpose: RISC-V hardware threads. Revision History: diff --git a/Private/KernelKit/HError.hpp b/Private/KernelKit/HError.hpp index 6fb55f5a..af498520 100644 --- a/Private/KernelKit/HError.hpp +++ b/Private/KernelKit/HError.hpp @@ -29,6 +29,7 @@ inline constexpr HError kErrorNoSuchDisk = 45; inline constexpr HError kErrorFileExists = 46; inline constexpr HError kErrorFormatFailed = 47; inline constexpr HError kErrorNetworkTimeout = 48; +inline constexpr HError kErrorInternal = 49; inline constexpr HError kErrorUnimplemented = 0; Boolean ke_bug_check(void) noexcept; diff --git a/Private/Source/KernelCheck.cxx b/Private/Source/KernelCheck.cxx index 30c76050..0fb81d47 100644 --- a/Private/Source/KernelCheck.cxx +++ b/Private/Source/KernelCheck.cxx @@ -32,12 +32,12 @@ void ke_stop(const HCore::Int &id) { } case RUNTIME_CHECK_ACPI: { kcout << "*** CAUSE: RUNTIME_CHECK_ACPI *** \r\n"; - kcout << "*** WHAT: ACPI DEFECT. *** \r\n"; + kcout << "*** WHAT: ACPI ERROR, UNSTABLE STATE. *** \r\n"; break; } case RUNTIME_CHECK_POINTER: { kcout << "*** CAUSE: RUNTIME_CHECK_POINTER *** \r\n"; - kcout << "*** WHAT: BAD POINTER. *** \r\n"; + kcout << "*** WHAT: HEAP ERROR, UNSTABLE STATE. *** \r\n"; break; } case RUNTIME_CHECK_BAD_BEHAVIOR: { diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx index 438df005..f5cb01da 100644 --- a/Private/Source/KernelHeap.cxx +++ b/Private/Source/KernelHeap.cxx @@ -6,8 +6,9 @@ #include #include -#include #include +#include +#include //! @file KernelHeap.cxx //! @brief Kernel allocator. @@ -16,14 +17,13 @@ namespace HCore { STATIC SizeT kHeapCount = 0UL; -STATIC Ref kHeapLastWrapper; STATIC PageManager kHeapPageManager; namespace Detail { /// @brief Kernel heap information block. /// Located before the address bytes. /// | HIB | ADDRESS | -struct HeapInformationBlock final { +struct PACKED HeapInformationBlock final { UInt16 hMagic; Boolean hPresent; Int32 hCRC32; @@ -43,9 +43,6 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { if (sz == 0) ++sz; auto wrapper = kHeapPageManager.Request(rw, user, false); - kHeapLastWrapper = wrapper; - - kcout << "HCoreKrnl.exe: Populating HIB...\r\n"; Detail::HeapInformationBlockPtr heapInfo = reinterpret_cast( @@ -58,8 +55,6 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { ++kHeapCount; - kcout << "HCoreKrnl.exe: Return address...\r\n"; - return reinterpret_cast(wrapper.VirtualAddress() + sizeof(Detail::HeapInformationBlock)); } @@ -68,23 +63,31 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { /// @param ptr the pointer. /// @return Int32 ke_delete_ke_heap(VoidPtr ptr) { - if (ptr) { - Detail::HeapInformationBlockPtr virtualAddress = - reinterpret_cast(ptr) - - sizeof(Detail::HeapInformationBlock); - - if (kHeapLastWrapper && virtualAddress->hMagic == kHeapMagic && - virtualAddress->hAddress == kHeapLastWrapper.Leak().VirtualAddress()) { - virtualAddress->hSizeAddress = 0UL; - virtualAddress->hPresent = false; + if (kHeapCount < 1) return -kErrorInternal; + + Detail::HeapInformationBlockPtr virtualAddress = + reinterpret_cast(ptr - + sizeof(Detail::HeapInformationBlock)); + + if (virtualAddress && virtualAddress->hMagic == kHeapMagic) { + if (virtualAddress->hCRC32 != 0) { + if (virtualAddress->hCRC32 != + ke_calculate_crc32((Char *)virtualAddress->hAddress, + virtualAddress->hSizeAddress)) + ke_stop(RUNTIME_CHECK_POINTER); + } - --kHeapCount; + virtualAddress->hSizeAddress = 0UL; + virtualAddress->hPresent = false; + virtualAddress->hAddress = 0; + virtualAddress->hCRC32 = 0; + virtualAddress->hMagic = 0; - return true; - } + --kHeapCount; + return 0; } - return -1; + return -kErrorInternal; } /// @brief Check if pointer is a valid kernel pointer. @@ -95,8 +98,8 @@ Boolean ke_is_valid_ptr(VoidPtr ptr) { if (ptr) { Detail::HeapInformationBlockPtr virtualAddress = - reinterpret_cast(ptr) - - sizeof(Detail::HeapInformationBlock); + reinterpret_cast(ptr - + sizeof(Detail::HeapInformationBlock)); if (virtualAddress->hPresent && virtualAddress->hMagic == kHeapMagic) { return true; diff --git a/Public/Kits/System.Core/Defs.hxx b/Public/Kits/System.Core/Defs.hxx index 84bdc7bb..e99abcc4 100644 --- a/Public/Kits/System.Core/Defs.hxx +++ b/Public/Kits/System.Core/Defs.hxx @@ -7,7 +7,7 @@ #pragma once #ifndef __cplusplus -#error This API is meant to be used with C++ +#error This API is meant to be used with C++. #endif #ifdef CA_MUST_PASS @@ -38,7 +38,7 @@ CA_EXTERN_C void __assert_chk_fail(void); #define CA_CDECL __attribute__((cdecl)) #define CA_MSCALL __attribute__((ms_abi)) -#define CA_PASCALL CA_STDCALL +#define CA_PASCAL CA_STDCALL typedef __UINT8_TYPE__ BYTE; typedef __UINT16_TYPE__ WORD; @@ -61,13 +61,16 @@ typedef __INT32_TYPE__ INT32; typedef __WCHAR_TYPE__ WCHAR; typedef WCHAR* PWCHAR; -typedef bool BOOL; +typedef CHAR BOOL; -#define TRUE true -#define FALSE false +#define TRUE 1 +#define FALSE 0 #define PTR * +#define FAR __far +#define NEAR __near + #define CA_UNREFERENCED_PARAMETER(e) ((VOID)e) #ifdef __x86_64__ @@ -123,7 +126,61 @@ class SystemException { virtual const char *Name() = 0; virtual const char *Reason() = 0; +}; + +/// @brief Object exception +/// Throws when the object isn't found. +class ObjectNotFoundException : public SystemException { + public: + explicit ObjectNotFoundException() = default; + virtual ~ObjectNotFoundException() = default; + + public: + HCORE_COPY_DEFAULT(ObjectNotFoundException); + + public: + const char *Name() override { return "ObjectNotFoundException"; } + const char *Reason() override { return mReason; } + + private: + const char *mReason{ + "System.Core: ObjectNotFoundException: Catastrophic failure!"}; +}; + +/// @brief pointer exception +/// Throws when the object isn't found. +class PointerException : public SystemException { + public: + explicit PointerException() = default; + virtual ~PointerException() = default; + + public: + HCORE_COPY_DEFAULT(PointerException); + + public: + const char *Name() override { return "PointerException"; } + const char *Reason() override { return mReason; } + private: const char *mReason{ - "System.Core: SystemException: Catastrophic failure!"}; -}; \ No newline at end of file + "System.Core: PointerException: Catastrophic failure!"}; +}; + +/// @brief pointer exception +/// Throws when the object isn't found. +class NullPointerException : public SystemException { + public: + explicit NullPointerException() = default; + virtual ~NullPointerException() = default; + + public: + HCORE_COPY_DEFAULT(NullPointerException); + + public: + const char *Name() override { return "NullPointerException"; } + const char *Reason() override { return mReason; } + + private: + const char *mReason{ + "System.Core: NullPointerException: Catastrophic failure!"}; +}; diff --git a/Public/Kits/System.Core/File.hxx b/Public/Kits/System.Core/File.hxx index 97377800..7ae70faf 100644 --- a/Public/Kits/System.Core/File.hxx +++ b/Public/Kits/System.Core/File.hxx @@ -4,39 +4,68 @@ ------------------------------------------- */ -#ifndef _SYSTEM_KIT_HCORE_FILE_HPP -#define _SYSTEM_KIT_HCORE_FILE_HPP +#ifndef __FILE_API__ +#define __FILE_API__ #include #include -using namespace HCore; - /// @brief SOM class, translated to C++ namespace System { class File final { public: - explicit File(const char *path); - ~File(); + explicit File(const char *path) { + mHandle = kInstanceObject->Invoke(kInstanceObject, kProcessCallOpenHandle, + 0, path); + } + + ~File() { + kInstanceObject->Invoke(kInstanceObject, kProcessCallCloseHandle, 0, + mHandle); + } public: HCORE_COPY_DEFAULT(File); public: - voidPtr Read(SizeT off, SizeT sz); - void Write(VoidPtr buf, SizeT off, SizeT sz); - void Seek(SizeT off); - voidPtr Read(SizeT sz); - void Write(VoidPtr buf, SizeT sz); - void Rewind(); + voidPtr Read(UIntPtr off, SizeT sz) { return (VoidPtr)kInstanceObject->Invoke(kInstanceObject, mHandle, 2, off, sz); } + voidPtr Read(SizeT sz) { return (VoidPtr)kInstanceObject->Invoke(kInstanceObject, mHandle, 3, sz); } + + void Write(VoidPtr buf, UIntPtr off, SizeT sz) { kInstanceObject->Invoke(kInstanceObject, mHandle, 4, buf, off, sz); } + void Write(VoidPtr buf, SizeT sz) { kInstanceObject->Invoke(kInstanceObject, mHandle, 5, buf, sz); } + + void Seek(UIntPtr off) { kInstanceObject->Invoke(kInstanceObject, mHandle, 5); } + void Rewind() { kInstanceObject->Invoke(kInstanceObject, mHandle, 6); } public: const char *MIME(); void MIME(const char *mime); + + private: + IntPtr mHandle; }; typedef File *FilePtr; -} // namespace System -#endif // ifndef _SYSTEM_KIT_HCORE_FILE_HPP +/// @brief file exception +/// Throws when the file isn't found or invalid. +class FileException : public SystemException { + public: + explicit FileException() = default; + virtual ~FileException() = default; + + public: + HCORE_COPY_DEFAULT(FileException); + + public: + const char *Name() override { return "FileException"; } + const char *Reason() override { return mReason; } + + private: + const char *mReason{"System.Core: FileException: Catastrophic failure!"}; +}; + +} // namespace System + +#endif // ifndef __FILE_API__ diff --git a/Public/Kits/System.Core/Heap.cxx b/Public/Kits/System.Core/Heap.cxx index 4dfd34e4..55850b1a 100644 --- a/Public/Kits/System.Core/Heap.cxx +++ b/Public/Kits/System.Core/Heap.cxx @@ -21,7 +21,10 @@ Heap* Heap::Shared() noexcept { return heap; } -Heap::Heap() { } +Heap::Heap() { + CA_MUST_PASS(HcProcessHeapExists(kInstanceObject, (VoidPtr)this)); +} + Heap::~Heap() { delete this; } void Heap::Delete(HeapPtr me) noexcept { diff --git a/Public/Kits/System.Core/Heap.hxx b/Public/Kits/System.Core/Heap.hxx index 1bfc00de..6bad93e0 100644 --- a/Public/Kits/System.Core/Heap.hxx +++ b/Public/Kits/System.Core/Heap.hxx @@ -56,4 +56,24 @@ class Heap final { SizeT Size(HeapPtr me) noexcept; HeapPtr New(const SizeT &sz, const Int32 flags = kHeapNoFlags); }; + + +/// @brief heap exception +/// Throws when the heap pointer isn't found or invalid. +class HeapException : public SystemException { + public: + explicit HeapException() = default; + virtual ~HeapException() = default; + + public: + HCORE_COPY_DEFAULT(HeapException); + + public: + const char *Name() override { return "HeapException"; } + const char *Reason() override { return mReason; } + + private: + const char *mReason{"System.Core: HeapException: Catastrophic failure!"}; +}; + } // namespace System \ No newline at end of file diff --git a/Public/Kits/System.Core/HeapImpl.cxx b/Public/Kits/System.Core/HeapImpl.cxx deleted file mode 100644 index 2980a3de..00000000 --- a/Public/Kits/System.Core/HeapImpl.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/HeapRuntime.cxx b/Public/Kits/System.Core/HeapRuntime.cxx new file mode 100644 index 00000000..c2f1f0e9 --- /dev/null +++ b/Public/Kits/System.Core/HeapRuntime.cxx @@ -0,0 +1,44 @@ +/** =========================================== + (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/System.Core.hxx b/Public/Kits/System.Core/System.Core.hxx index 07b7199c..96ab45be 100644 --- a/Public/Kits/System.Core/System.Core.hxx +++ b/Public/Kits/System.Core/System.Core.hxx @@ -11,4 +11,4 @@ #include /// Process & Threading API -#include \ No newline at end of file +#include -- cgit v1.2.3