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 +++++++++++++++------------- 7 files changed, 38 insertions(+), 62 deletions(-) (limited to 'Private') 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; -- cgit v1.2.3