diff options
| author | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-23 02:58:39 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-23 02:58:39 +0100 |
| commit | 5563deabd8f7ce3fc713ea23f8cf5bbac33b4024 (patch) | |
| tree | f182700a0360ecf7319415915638e44a5d0074dc /Private/Source | |
| parent | ab4eaababec7f870378ed64fbbf51b154b292a7b (diff) | |
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 <amlalelmahrouss@icloud.com>
Diffstat (limited to 'Private/Source')
| -rw-r--r-- | Private/Source/CxxAbi.cxx | 26 | ||||
| -rw-r--r-- | Private/Source/KernelCheck.cxx (renamed from Private/Source/RuntimeCheck.cxx) | 2 | ||||
| -rw-r--r-- | Private/Source/KernelHeap.cxx | 76 | ||||
| -rw-r--r-- | Private/Source/KernelMain.cxx | 6 | ||||
| -rw-r--r-- | Private/Source/NewFS+FileManager.cxx | 6 | ||||
| -rw-r--r-- | Private/Source/PEFCodeManager.cxx | 2 | ||||
| -rw-r--r-- | Private/Source/PermissionSelector.cxx | 2 | ||||
| -rw-r--r-- | Private/Source/ProcessManager.cxx | 2 | ||||
| -rw-r--r-- | Private/Source/ThreadLocalStorage.cxx | 6 | ||||
| -rw-r--r-- | Private/Source/UserHeap.cxx | 7 |
10 files changed, 64 insertions, 71 deletions
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 <KernelKit/DebugOutput.hpp> #include <NewKit/CxxAbi.hpp> -#include <NewKit/RuntimeCheck.hpp> - -#ifdef __GNUC__ +#include <NewKit/KernelCheck.hpp> 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/RuntimeCheck.cxx b/Private/Source/KernelCheck.cxx index 03660076..4fc24918 100644 --- a/Private/Source/RuntimeCheck.cxx +++ b/Private/Source/KernelCheck.cxx @@ -9,7 +9,7 @@ #include <ArchKit/ArchKit.hpp> #include <KernelKit/DebugOutput.hpp> -#include <NewKit/RuntimeCheck.hpp> +#include <NewKit/KernelCheck.hpp> #include <NewKit/String.hpp> extern "C" [[noreturn]] void ke_wait_for_debugger() { 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 <NewKit/Crc32.hpp> #include <NewKit/KernelHeap.hpp> //! @file KernelHeap.cpp //! @brief Kernel allocator. -#define kMaxWrappers (4096 * 8) +#define kHeapMaxWrappers (4096 * 8) +#define kHeapMagic 0xAA55 namespace HCore { -static Ref<PTEWrapper *> kWrapperList[kMaxWrappers]; -static SizeT kWrapperCount = 0UL; -static Ref<PTEWrapper *> kLastWrapper; -static PageManager kPageManager; +STATIC Ref<PTEWrapper *> kWrapperList[kHeapMaxWrappers]; +STATIC SizeT kHeapCount = 0UL; +STATIC Ref<PTEWrapper *> 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<Detail::HeapInformationBlock *>( + 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<voidPtr>(wrapper->VirtualAddress()); + return reinterpret_cast<voidPtr>(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<UIntPtr>(ptr); + Detail::HeapInformationBlock *virtualAddress = + reinterpret_cast<Detail::HeapInformationBlock *>(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<PTEWrapper *> 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<UIntPtr>(ptr); - if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) { + if (kLastWrapper && + virtualAddress == (kLastWrapper->VirtualAddress() + + sizeof(Detail::HeapInformationBlock))) { return true; } Ref<PTEWrapper *> 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<PTEWrapper *> kLastWrapper = Ref<PTEWrapper *>(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 <KernelKit/ProcessManager.hpp> #include <NewKit/Defines.hpp> #include <NewKit/ErrorID.hpp> +#include <NewKit/KernelCheck.hpp> #include <NewKit/KernelHeap.hpp> #include <NewKit/OwnPtr.hpp> -#include <NewKit/RuntimeCheck.hpp> #include <NewKit/String.hpp> #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 <KernelKit/PermissionSelector.hxx> -#include <NewKit/RuntimeCheck.hpp> +#include <NewKit/KernelCheck.hpp> /// 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 <NewKit/KernelHeap.hpp> #include <NewKit/String.hpp> -#include "NewKit/RuntimeCheck.hpp" - ///! bugs = 0 /***********************************************************************************/ 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 |
