diff options
| author | amlal <amlal@el-mahrouss-logic.com> | 2024-03-21 00:38:52 +0100 |
|---|---|---|
| committer | amlal <amlal@el-mahrouss-logic.com> | 2024-03-21 00:38:52 +0100 |
| commit | 333fed96b7ccd3ee4f5f097445408dde34d330db (patch) | |
| tree | a1c9894b26bd81c2e762b5487e04601b357682f2 | |
| parent | f48c5b2cda43241919d3ea1b263bef01e014c537 (diff) | |
Kernel: See below.
- Fix kernel heap, made it better.
- Fix System.Core, made it better.
Signed-off-by: amlal <amlal@el-mahrouss-logic.com>
| -rw-r--r-- | Private/ArchKit/ArchKit.hpp | 24 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalInterruptRouting.asm | 17 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalKernelMain.cxx | 3 | ||||
| -rw-r--r-- | Private/HALKit/RISCV/Hart.hxx | 2 | ||||
| -rw-r--r-- | Private/KernelKit/HError.hpp | 1 | ||||
| -rw-r--r-- | Private/Source/KernelCheck.cxx | 4 | ||||
| -rw-r--r-- | Private/Source/KernelHeap.cxx | 49 | ||||
| -rw-r--r-- | Public/Kits/System.Core/Defs.hxx | 71 | ||||
| -rw-r--r-- | Public/Kits/System.Core/File.hxx | 57 | ||||
| -rw-r--r-- | Public/Kits/System.Core/Heap.cxx | 5 | ||||
| -rw-r--r-- | Public/Kits/System.Core/Heap.hxx | 20 | ||||
| -rw-r--r-- | Public/Kits/System.Core/HeapRuntime.cxx (renamed from Public/Kits/System.Core/HeapImpl.cxx) | 35 | ||||
| -rw-r--r-- | Public/Kits/System.Core/System.Core.hxx | 2 |
13 files changed, 186 insertions, 104 deletions
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 <SSizeT ID> -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<RET>()"; - 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::Array<rt_syscall_proc, kSyscalls; EXTERN_C HCore::Void rt_wait_400ns(); -EXTERN_C HCore::Void ATTRIBUTE(interrupt) rt_syscall_handle(HCore::HAL::StackFramePtr stackFrame); EXTERN_C HCore::HAL::StackFramePtr rt_get_current_context(); EXTERN_C HCore::Void rt_do_context_switch(HCore::HAL::StackFramePtr stackFrame); diff --git a/Private/HALKit/AMD64/HalInterruptRouting.asm b/Private/HALKit/AMD64/HalInterruptRouting.asm index ae3d51c5..fe6fd2b5 100644 --- a/Private/HALKit/AMD64/HalInterruptRouting.asm +++ b/Private/HALKit/AMD64/HalInterruptRouting.asm @@ -111,16 +111,7 @@ __HCR_INT_33: push rax - mov rcx, kSystemCallLabelEnter - call ke_io_print - - pop rax - - push rax - - ;; Find and execute system call TODO - - mov rcx, kSystemCallLabelExit + mov rcx, kSystemCallLabel call ke_io_print pop rax @@ -228,9 +219,7 @@ kInterruptVectorTable: %assign i i+1 %endrep -kSystemCallLabelEnter: - db "HCoreKrnl.exe: SystemCall: Enter.", 0xa, 0xd, 0 -kSystemCallLabelExit: - db "HCoreKrnl.exe: SystemCall: Exit.", 0xa, 0xd, 0 +kSystemCallLabel: + db "HCoreKrnl.exe: SystemCall: Enter SCM.", 0xa, 0xd, 0 kMouseLabelExit: db "HCoreKrnl.exe: KernelMouse: Acknowledge Interrupt.", 0xa, 0xd, 0
\ No newline at end of file diff --git a/Private/HALKit/AMD64/HalKernelMain.cxx b/Private/HALKit/AMD64/HalKernelMain.cxx index 6c2ad263..abbf5feb 100644 --- a/Private/HALKit/AMD64/HalKernelMain.cxx +++ b/Private/HALKit/AMD64/HalKernelMain.cxx @@ -27,7 +27,7 @@ EXTERN_C void RuntimeMain( /// Setup kernel globals. kKernelVirtualSize = HandoverHeader->f_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 <KernelKit/DebugOutput.hpp> #include <KernelKit/KernelHeap.hpp> -#include <NewKit/Crc32.hpp> #include <NewKit/PageManager.hpp> +#include <KernelKit/HError.hpp> +#include <NewKit/Crc32.hpp> //! @file KernelHeap.cxx //! @brief Kernel allocator. @@ -16,14 +17,13 @@ namespace HCore { STATIC SizeT kHeapCount = 0UL; -STATIC Ref<PTEWrapper> 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<Detail::HeapInformationBlockPtr>( @@ -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<VoidPtr>(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<Detail::HeapInformationBlockPtr>(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<Detail::HeapInformationBlockPtr>(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<Detail::HeapInformationBlockPtr>(ptr) - - sizeof(Detail::HeapInformationBlock); + reinterpret_cast<Detail::HeapInformationBlockPtr>(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 <CompilerKit/CompilerKit.hxx> #include <NewKit/Defines.hpp> -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/HeapRuntime.cxx index 2980a3de..c2f1f0e9 100644 --- a/Public/Kits/System.Core/HeapImpl.cxx +++ b/Public/Kits/System.Core/HeapRuntime.cxx @@ -8,40 +8,37 @@ /// @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); +/// @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)); +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); +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); +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 <System.Core/File.hxx> /// Process & Threading API -#include <System.Core/Threading.hxx>
\ No newline at end of file +#include <System.Core/Threading.hxx> |
