From 5563deabd8f7ce3fc713ea23f8cf5bbac33b4024 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 23 Feb 2024 02:58:39 +0100 Subject: Kernel: add heap information (allocator) - Force use of itanium ABI even of MPCC. - Revision of handover has been done. (it is not assuming any starting address) Signed-off-by: Amlal El Mahrouss --- Private/Source/CxxAbi.cxx | 26 ++--------- Private/Source/KernelCheck.cxx | 83 +++++++++++++++++++++++++++++++++++ Private/Source/KernelHeap.cxx | 76 ++++++++++++++++++++++---------- Private/Source/KernelMain.cxx | 6 +-- Private/Source/NewFS+FileManager.cxx | 6 +-- Private/Source/PEFCodeManager.cxx | 2 +- Private/Source/PermissionSelector.cxx | 2 +- Private/Source/ProcessManager.cxx | 2 - Private/Source/RuntimeCheck.cxx | 83 ----------------------------------- Private/Source/ThreadLocalStorage.cxx | 6 +-- Private/Source/UserHeap.cxx | 7 --- 11 files changed, 146 insertions(+), 153 deletions(-) create mode 100644 Private/Source/KernelCheck.cxx delete mode 100644 Private/Source/RuntimeCheck.cxx (limited to 'Private/Source') diff --git a/Private/Source/CxxAbi.cxx b/Private/Source/CxxAbi.cxx index c062219b..41c8587b 100644 --- a/Private/Source/CxxAbi.cxx +++ b/Private/Source/CxxAbi.cxx @@ -9,16 +9,16 @@ #include #include -#include - -#ifdef __GNUC__ +#include void *__dso_handle; atexit_func_entry_t __atexit_funcs[DSO_MAX_OBJECTS]; uarch_t __atexit_func_count; -extern "C" void __cxa_pure_virtual() { HCore::kcout << "[C++] Placeholder\n"; } +extern "C" void __cxa_pure_virtual() { + HCore::kcout << "Krnl\\Abi: Placeholder method.\n"; +} extern "C" void ___chkstk_ms() {} @@ -67,21 +67,3 @@ extern "C" int __cxa_guard_release(__guard *g) { extern "C" void __cxa_guard_abort(__guard *g) { (void)g; } } // namespace cxxabiv1 - -#else - -namespace cxxkit { -extern "C" void __unwind(void (**finis)(void), int cnt) { - for (int i = 0; i < cnt; ++i) (finis[i])(); -} - -extern "C" void __init_local(void (**init)(void), int cnt) { - for (int i = 0; i < cnt; ++i) (init[i])(); -} - -extern "C" void __fini_local(void (**finis)(void), int cnt) { - for (int i = 0; i < cnt; ++i) (finis[i])(); -} -} // namespace cxxkit - -#endif diff --git a/Private/Source/KernelCheck.cxx b/Private/Source/KernelCheck.cxx new file mode 100644 index 00000000..4fc24918 --- /dev/null +++ b/Private/Source/KernelCheck.cxx @@ -0,0 +1,83 @@ +/* + * ======================================================== + * + * HCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include +#include +#include +#include + +extern "C" [[noreturn]] void ke_wait_for_debugger() { + while (true) { + HCore::HAL::rt_cli(); + HCore::HAL::rt_halt(); + } +} + +/* Each error code is attributed with an ID, which will prompt a string onto the + * screen. Wait for debugger... */ + +namespace HCore { +void ke_stop(const HCore::Int &id) { + kcout << "*** STOP *** \r\n"; + kcout << "*** HCoreKrnl.exe has trigerred a runtime stop. *** \r\n"; + + switch (id) { + case RUNTIME_CHECK_PROCESS: { + kcout << "*** CAUSE: RUNTIME_CHECK_PROCESS *** \r\n"; + break; + } + case RUNTIME_CHECK_ACPI: { + kcout << "*** CAUSE: RUNTIME_CHECK_ACPI *** \r\n"; + break; + } + case RUNTIME_CHECK_POINTER: { + kcout << "*** CAUSE: RUNTIME_CHECK_POINTER *** \r\n"; + break; + } + case RUNTIME_CHECK_BAD_BEHAVIOR: { + kcout << "*** CAUSE: RUNTIME_CHECK_BAD_BEHAVIOR *** \r\n"; + break; + } + case RUNTIME_CHECK_BOOTSTRAP: { + kcout << "*** CAUSE: RUNTIME_CHECK_BOOTSTRAP *** \r\n"; + break; + } + case RUNTIME_CHECK_HANDSHAKE: { + kcout << "*** CAUSE: RUNTIME_CHECK_HANDSHAKE *** \r\n"; + break; + } + case RUNTIME_CHECK_LD: { + kcout << "*** CAUSE: RUNTIME_CHECK_LD *** \r\n"; + break; + } + case RUNTIME_CHECK_INVALID_PRIVILEGE: { + kcout << "*** CAUSE: RUNTIME_CHECK_INVALID_PRIVILEGE *** \r\n"; + break; + } + }; + + DumpManager::Dump(); + +#ifdef __DEBUG__ + ke_wait_for_debugger(); +#endif // ifdef __DEBUG__ +} + +void ke_runtime_check(bool expr, const char *file, const char *line) { + if (!expr) { +#ifdef __DEBUG__ + kcout << "Krnl: File: " << file << "\n"; + kcout << "Krnl: Line: " << line << "\n"; + +#endif // __DEBUG__ + + HCore::ke_stop(RUNTIME_CHECK_FAILED); // Runtime Check failed + } +} +} // namespace HCore diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx index 10d3fdd2..fdc4dc4f 100644 --- a/Private/Source/KernelHeap.cxx +++ b/Private/Source/KernelHeap.cxx @@ -7,22 +7,34 @@ * ======================================================== */ +#include #include //! @file KernelHeap.cpp //! @brief Kernel allocator. -#define kMaxWrappers (4096 * 8) +#define kHeapMaxWrappers (4096 * 8) +#define kHeapMagic 0xAA55 namespace HCore { -static Ref kWrapperList[kMaxWrappers]; -static SizeT kWrapperCount = 0UL; -static Ref kLastWrapper; -static PageManager kPageManager; +STATIC Ref kWrapperList[kHeapMaxWrappers]; +STATIC SizeT kHeapCount = 0UL; +STATIC Ref kLastWrapper; +STATIC PageManager kPageManager; namespace Detail { +/// @brief +struct HeapInformationBlock final { + Int16 hMagic; + Boolean hPresent; + Int64 hSize; + Int32 hCRC32; + VoidPtr hPtr; +}; + STATIC voidPtr ke_find_heap(const SizeT &sz, const bool rw, const bool user) { - for (SizeT indexWrapper = 0; indexWrapper < kMaxWrappers; ++indexWrapper) { + for (SizeT indexWrapper = 0; indexWrapper < kHeapMaxWrappers; + ++indexWrapper) { if (!kWrapperList[indexWrapper]->Present()) { kWrapperList[indexWrapper] ->Reclaim(); /* very straight-forward as you can see. */ @@ -35,7 +47,7 @@ STATIC voidPtr ke_find_heap(const SizeT &sz, const bool rw, const bool user) { } } // namespace Detail -/// @brief Page allocation routine. +/// @brief Allocate pointer. /// @param sz size of pointer /// @param rw read write (true to enable it) /// @param user is it accesible by user processes? @@ -49,10 +61,20 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { kLastWrapper = wrapper; - kWrapperList[kWrapperCount] = wrapper; - ++kWrapperCount; + Detail::HeapInformationBlock *heapInfo = + reinterpret_cast( + wrapper->VirtualAddress()); + + heapInfo->hSize = sz; + heapInfo->hMagic = kHeapMagic; + heapInfo->hCRC32 = ke_calculate_crc32((Char *)wrapper->VirtualAddress(), sz); + heapInfo->hPtr = (VoidPtr)wrapper->VirtualAddress(); + + kWrapperList[kHeapCount] = wrapper; + ++kHeapCount; - return reinterpret_cast(wrapper->VirtualAddress()); + return reinterpret_cast(wrapper->VirtualAddress() + + sizeof(Detail::HeapInformationBlock)); } /// @brief Declare pointer as free. @@ -60,10 +82,15 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { /// @return Int32 ke_delete_ke_heap(voidPtr ptr) { if (ptr) { - const UIntPtr virtualAddress = reinterpret_cast(ptr); + Detail::HeapInformationBlock *virtualAddress = + reinterpret_cast(ptr) - + sizeof(Detail::HeapInformationBlock); - if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) { + if (kLastWrapper && + (UIntPtr)virtualAddress->hPtr == kLastWrapper->VirtualAddress()) { if (kPageManager.Free(kLastWrapper)) { + virtualAddress->hSize = 0UL; + virtualAddress->hPresent = false; kLastWrapper->NoExecute(false); return true; } @@ -73,13 +100,18 @@ Int32 ke_delete_ke_heap(voidPtr ptr) { Ref wrapper{nullptr}; - for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) { - if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) { + for (SizeT indexWrapper = 0; indexWrapper < kHeapCount; ++indexWrapper) { + if (kWrapperList[indexWrapper]->VirtualAddress() == + (UIntPtr)virtualAddress->hPtr) { wrapper = kWrapperList[indexWrapper]; // if page is no more, then mark it also as non executable. if (kPageManager.Free(wrapper)) { + virtualAddress->hSize = 0UL; + virtualAddress->hPresent = false; + wrapper->NoExecute(false); + return true; } @@ -98,14 +130,17 @@ Boolean kernel_valid_ptr(voidPtr ptr) { if (ptr) { const UIntPtr virtualAddress = reinterpret_cast(ptr); - if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) { + if (kLastWrapper && + virtualAddress == (kLastWrapper->VirtualAddress() + + sizeof(Detail::HeapInformationBlock))) { return true; } Ref wrapper; - for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) { - if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) { + for (SizeT indexWrapper = 0; indexWrapper < kHeapCount; ++indexWrapper) { + if ((kLastWrapper->VirtualAddress() + + sizeof(Detail::HeapInformationBlock)) == virtualAddress) { wrapper = kWrapperList[indexWrapper]; return true; } @@ -114,11 +149,4 @@ Boolean kernel_valid_ptr(voidPtr ptr) { return false; } - -/// @brief The Kernel heap initializer function. -/// @return -Void ke_init_ke_heap() noexcept { - kWrapperCount = 0UL; - Ref kLastWrapper = Ref(nullptr); -} } // namespace HCore diff --git a/Private/Source/KernelMain.cxx b/Private/Source/KernelMain.cxx index 171deff1..e293cbf2 100644 --- a/Private/Source/KernelMain.cxx +++ b/Private/Source/KernelMain.cxx @@ -24,16 +24,12 @@ EXTERN_C void RuntimeMain( kKernelPhysicalSize = HandoverHeader->f_VirtualSize; kKernelPhysicalStart = HandoverHeader->f_VirtualStart; - /// Init memory managers. - HCore::ke_init_ke_heap(); - HCore::ke_init_heap(); - /// Init the HAL. MUST_PASS(HCore::ke_init_hal()); /// Mount a New partition. // HCore::IFilesystemManager::Mount(new HCore::NewFilesystemManager()); - HCore::PEFLoader img("/System/HCoreShell.exe"); + HCore::PEFLoader img("C:/System/HCoreShell.exe"); /// Run the shell. if (!HCore::Utils::execute_from_image(img)) { diff --git a/Private/Source/NewFS+FileManager.cxx b/Private/Source/NewFS+FileManager.cxx index d1ea58f6..51959d0b 100644 --- a/Private/Source/NewFS+FileManager.cxx +++ b/Private/Source/NewFS+FileManager.cxx @@ -11,6 +11,7 @@ #ifdef __FSKIT_NEWFS__ +/// @brief NewFS File manager. /// BUGS: 0 namespace HCore { @@ -18,11 +19,6 @@ NewFilesystemManager::NewFilesystemManager() = default; NewFilesystemManager::~NewFilesystemManager() = default; -/** - * Unallocates a file from disk. - * @param node_name it's path. - * @return operation status boolean. - */ bool NewFilesystemManager::Remove(const char* node_name) { if (node_name == nullptr || *node_name == 0) return false; diff --git a/Private/Source/PEFCodeManager.cxx b/Private/Source/PEFCodeManager.cxx index 68c5b556..ee535057 100644 --- a/Private/Source/PEFCodeManager.cxx +++ b/Private/Source/PEFCodeManager.cxx @@ -13,9 +13,9 @@ #include #include #include +#include #include #include -#include #include #include "KernelKit/PEF.hpp" diff --git a/Private/Source/PermissionSelector.cxx b/Private/Source/PermissionSelector.cxx index 34760b8f..87680b06 100644 --- a/Private/Source/PermissionSelector.cxx +++ b/Private/Source/PermissionSelector.cxx @@ -11,7 +11,7 @@ */ #include -#include +#include /// bugs 0 diff --git a/Private/Source/ProcessManager.cxx b/Private/Source/ProcessManager.cxx index ccf037ae..46533f00 100644 --- a/Private/Source/ProcessManager.cxx +++ b/Private/Source/ProcessManager.cxx @@ -18,8 +18,6 @@ #include #include -#include "NewKit/RuntimeCheck.hpp" - ///! bugs = 0 /***********************************************************************************/ diff --git a/Private/Source/RuntimeCheck.cxx b/Private/Source/RuntimeCheck.cxx deleted file mode 100644 index 03660076..00000000 --- a/Private/Source/RuntimeCheck.cxx +++ /dev/null @@ -1,83 +0,0 @@ -/* - * ======================================================== - * - * HCore - * Copyright Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include -#include -#include -#include - -extern "C" [[noreturn]] void ke_wait_for_debugger() { - while (true) { - HCore::HAL::rt_cli(); - HCore::HAL::rt_halt(); - } -} - -/* Each error code is attributed with an ID, which will prompt a string onto the - * screen. Wait for debugger... */ - -namespace HCore { -void ke_stop(const HCore::Int &id) { - kcout << "*** STOP *** \r\n"; - kcout << "*** HCoreKrnl.exe has trigerred a runtime stop. *** \r\n"; - - switch (id) { - case RUNTIME_CHECK_PROCESS: { - kcout << "*** CAUSE: RUNTIME_CHECK_PROCESS *** \r\n"; - break; - } - case RUNTIME_CHECK_ACPI: { - kcout << "*** CAUSE: RUNTIME_CHECK_ACPI *** \r\n"; - break; - } - case RUNTIME_CHECK_POINTER: { - kcout << "*** CAUSE: RUNTIME_CHECK_POINTER *** \r\n"; - break; - } - case RUNTIME_CHECK_BAD_BEHAVIOR: { - kcout << "*** CAUSE: RUNTIME_CHECK_BAD_BEHAVIOR *** \r\n"; - break; - } - case RUNTIME_CHECK_BOOTSTRAP: { - kcout << "*** CAUSE: RUNTIME_CHECK_BOOTSTRAP *** \r\n"; - break; - } - case RUNTIME_CHECK_HANDSHAKE: { - kcout << "*** CAUSE: RUNTIME_CHECK_HANDSHAKE *** \r\n"; - break; - } - case RUNTIME_CHECK_LD: { - kcout << "*** CAUSE: RUNTIME_CHECK_LD *** \r\n"; - break; - } - case RUNTIME_CHECK_INVALID_PRIVILEGE: { - kcout << "*** CAUSE: RUNTIME_CHECK_INVALID_PRIVILEGE *** \r\n"; - break; - } - }; - - DumpManager::Dump(); - -#ifdef __DEBUG__ - ke_wait_for_debugger(); -#endif // ifdef __DEBUG__ -} - -void ke_runtime_check(bool expr, const char *file, const char *line) { - if (!expr) { -#ifdef __DEBUG__ - kcout << "Krnl: File: " << file << "\n"; - kcout << "Krnl: Line: " << line << "\n"; - -#endif // __DEBUG__ - - HCore::ke_stop(RUNTIME_CHECK_FAILED); // Runtime Check failed - } -} -} // namespace HCore diff --git a/Private/Source/ThreadLocalStorage.cxx b/Private/Source/ThreadLocalStorage.cxx index 8aeb0431..9a9c8477 100644 --- a/Private/Source/ThreadLocalStorage.cxx +++ b/Private/Source/ThreadLocalStorage.cxx @@ -29,7 +29,7 @@ Boolean hcore_tls_check(VoidPtr ptr) { const char* _ptr = (const char*)ptr; - kcout << "TLS: Checking for cookie...\n"; + kcout << "Krnl\\TLS: Checking for cookie...\n"; return _ptr[0] == kCookieMag0 && _ptr[1] == kCookieMag1 && _ptr[2] == kCookieMag2; @@ -42,9 +42,9 @@ Boolean hcore_tls_check(VoidPtr ptr) { */ Void hcore_tls_check_syscall_impl(ThreadInformationBlock ptr) noexcept { if (!hcore_tls_check(ptr.Cookie)) { - kcout << "TLS: Verification failure, Crashing...\n"; + kcout << "Krnl\\TLS: Verification failure, Crashing...\n"; ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } - kcout << "TLS: Verification succeeded! Keeping on...\n"; + kcout << "Krnl\\TLS: Verification succeeded! Keeping on...\n"; } diff --git a/Private/Source/UserHeap.cxx b/Private/Source/UserHeap.cxx index d3d60f6b..cc243904 100644 --- a/Private/Source/UserHeap.cxx +++ b/Private/Source/UserHeap.cxx @@ -182,11 +182,4 @@ Int32 ke_free_heap(VoidPtr ptr) { return -1; } - -/// @brief Init HeapManager, set Count to zero and IsEnabled to true. -/// @return -Void ke_init_heap() { - HeapManager::Count() = 0UL; - HeapManager::IsEnabled() = true; -} } // namespace HCore -- cgit v1.2.3