From 98403fe342c8c2795de34e922958ee8d02c94e04 Mon Sep 17 00:00:00 2001 From: Amlal EL Mahrouss Date: Thu, 15 Aug 2024 10:38:58 +0200 Subject: [IMP] MHR-28: + Fixed VMH allocator, which was hanging because of a unitialized global field. + Working on fixing User save method for authorization purposes. + If .bss -> Zero memory region. Signed-off-by: Amlal EL Mahrouss --- CRTKit/__ndk_new_delete.hxx | 33 ++++++++++++++ .../HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx | 7 ++- Kernel/HALKit/AMD64/HalKernelMain.cxx | 19 ++++++-- Kernel/HALKit/AMD64/HalPageAlloc.cxx | 7 ++- Kernel/Sources/FS/NewFS.cxx | 6 ++- Kernel/Sources/Heap.cxx | 50 +++++++++++----------- Kernel/Sources/PageManager.cxx | 2 + Kernel/Sources/User.cxx | 9 +--- 8 files changed, 92 insertions(+), 41 deletions(-) diff --git a/CRTKit/__ndk_new_delete.hxx b/CRTKit/__ndk_new_delete.hxx index cde972cc..7b30c9ab 100644 --- a/CRTKit/__ndk_new_delete.hxx +++ b/CRTKit/__ndk_new_delete.hxx @@ -7,6 +7,7 @@ #pragma once #include +#include namespace stdx { @@ -26,3 +27,35 @@ namespace stdx delete ptr; } } // namespace stdx + +void* operator new(size_type len) +{ + if (!len) + ++len; + + return RtlCreateHeap(len, 0); +} + +void operator delete(void* ptr) +{ + if (!ptr) + return; + + RtlDestroyHeap(ptr); +} + +void* operator new[](size_type len) +{ + if (!len) + ++len; + + return RtlCreateHeap(len, 0); +} + +void operator delete[](void* ptr) +{ + if (!ptr) + return; + + RtlDestroyHeap(ptr); +} \ No newline at end of file diff --git a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx index 9de1b5b4..6018d20f 100644 --- a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx +++ b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx @@ -81,9 +81,12 @@ namespace Kernel::HAL struct MADT_TABLE final : public SDT { UInt32 Address; // Madt address - UInt8 Flags; // Madt flags + UInt32 Flags; // Madt flags - VoidPtr Records[]; // Records List + struct { + UInt8 Type; + UInt8 Len; + } Records[]; // Records List }; /////////////////////////////////////////////////////////////////////////////////////// diff --git a/Kernel/HALKit/AMD64/HalKernelMain.cxx b/Kernel/HALKit/AMD64/HalKernelMain.cxx index 1fff99e2..49ddfa6c 100644 --- a/Kernel/HALKit/AMD64/HalKernelMain.cxx +++ b/Kernel/HALKit/AMD64/HalKernelMain.cxx @@ -24,6 +24,8 @@ Kernel::Property cKernelVersion; Kernel::Property cAutoFormatDisk; +EXTERN Kernel::Boolean kAllocationInProgress; + EXTERN_C Kernel::VoidPtr kInterruptVectorTable[]; struct HEAP_ALLOC_INFO final @@ -84,7 +86,11 @@ EXTERN_C void hal_init_platform( } Kernel::Void hal_real_init(Kernel::Void) noexcept -{ // get page size. +{ + // reset kAllocationInProgress field to zero. + kAllocationInProgress = false; + + // get page size. kKernelVirtualSize = kHandoverHeader->f_VirtualSize; // get virtual address start (for the heap) @@ -210,15 +216,20 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept kSyscalls[cShutdownInterrupt].Leak().Leak()->fHooked = true; kSyscalls[cRebootInterrupt].Leak().Leak()->fHooked = true; - auto fs = new Kernel::NewFilesystemManager(); + Kernel::HAL::hal_system_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr); - Kernel::NewFilesystemManager::Mount(fs); + Kernel::kcout << "newoskrnl: Creating filesystem and such.\r"; + auto fs = new Kernel::NewFilesystemManager(); + + MUST_PASS(fs); MUST_PASS(fs->GetParser()); + Kernel::NewFilesystemManager::Mount(fs); + delete fs->GetParser()->CreateCatalog("\\Users\\", 0, kNewFSCatalogKindDir); - Kernel::kcout << "newoskrnl: Creating filesystem and " << kSuperUser << "..." << Kernel::endl; + Kernel::kcout << "newoskrnl: Created filesystem and now creating " << kSuperUser << "..." << Kernel::endl; cRoot = new Kernel::User(Kernel::RingKind::kRingSuperUser, kSuperUser); diff --git a/Kernel/HALKit/AMD64/HalPageAlloc.cxx b/Kernel/HALKit/AMD64/HalPageAlloc.cxx index b554e211..0e39a0f1 100644 --- a/Kernel/HALKit/AMD64/HalPageAlloc.cxx +++ b/Kernel/HALKit/AMD64/HalPageAlloc.cxx @@ -6,6 +6,8 @@ #include +#define cVMHMagic (0xDEEFD00D) + #ifdef __NEWOS_AMD64__ #include #elif defined(__NEWOS_ARM64__) @@ -19,7 +21,6 @@ Kernel::Boolean kAllocationInProgress = false; namespace Kernel { - constexpr auto cVMHMagic = 0xDEEFD00D; namespace HAL { @@ -99,12 +100,16 @@ namespace Kernel /// @return auto hal_alloc_page(Boolean rw, Boolean user, SizeT size) -> VoidPtr { + kcout << "Waiting now..."; + // Wait for a ongoing allocation to complete. while (kAllocationInProgress) { (void)0; } + kcout << ", done waiting, allocating...\r"; + if (size == 0) ++size; diff --git a/Kernel/Sources/FS/NewFS.cxx b/Kernel/Sources/FS/NewFS.cxx index 3382aa04..90f48eb2 100644 --- a/Kernel/Sources/FS/NewFS.cxx +++ b/Kernel/Sources/FS/NewFS.cxx @@ -1034,16 +1034,18 @@ namespace Kernel::Detail /***********************************************************************************/ Boolean fs_init_newfs(Void) noexcept { + kcout << "newoskrnl: Creating drives...\r"; + sMountpointInterface.A() = io_construct_main_drive(); sMountpointInterface.B() = io_construct_drive(); sMountpointInterface.C() = io_construct_drive(); sMountpointInterface.D() = io_construct_drive(); - kcout << "newoskrnl: Testing drive...\r"; + kcout << "newoskrnl: Testing main drive...\r"; sMountpointInterface.A().fVerify(&sMountpointInterface.A().fPacket); - kcout << "newoskrnl: Testing drive [ OK ]...\r"; + kcout << "newoskrnl: Testing main drive [ OK ]...\r"; return true; } diff --git a/Kernel/Sources/Heap.cxx b/Kernel/Sources/Heap.cxx index 5b004880..de0c8a5c 100644 --- a/Kernel/Sources/Heap.cxx +++ b/Kernel/Sources/Heap.cxx @@ -94,17 +94,19 @@ namespace Kernel if (szFix == 0) ++szFix; + kcout << "newoskrnl: allocating VMH page...\r"; + auto wrapper = kHeapPageManager.Request(rw, user, false, szFix); - Detail::HEAP_INFORMATION_BLOCK_PTR heapInfo = + Detail::HEAP_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast( wrapper.VirtualAddress()); - heapInfo->fTargetPtrSize = szFix; - heapInfo->fMagic = kKernelHeapMagic; - heapInfo->fCRC32 = 0; // dont fill it for now. - heapInfo->fTargetPtr = wrapper.VirtualAddress(); - heapInfo->fPagePtr = 0; + heap_info_ptr->fTargetPtrSize = szFix; + heap_info_ptr->fMagic = kKernelHeapMagic; + heap_info_ptr->fCRC32 = 0; // dont fill it for now. + heap_info_ptr->fTargetPtr = wrapper.VirtualAddress(); + heap_info_ptr->fPagePtr = 0; ++kHeapCount; @@ -115,22 +117,22 @@ namespace Kernel } /// @brief Makes a page heap. - /// @param heapPtr + /// @param heap_ptr /// @return - Int32 mm_make_ke_page(VoidPtr heapPtr) + Int32 mm_make_ke_page(VoidPtr heap_ptr) { if (kHeapCount < 1) return -kErrorInternal; - if (((IntPtr)heapPtr - sizeof(Detail::HEAP_INFORMATION_BLOCK)) <= 0) + if (((IntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)) <= 0) return -kErrorInternal; - if (((IntPtr)heapPtr - kBadPtr) < 0) + if (((IntPtr)heap_ptr - kBadPtr) < 0) return -kErrorInternal; Detail::mm_alloc_init_timeout(); Detail::HEAP_INFORMATION_BLOCK_PTR heapInfoBlk = reinterpret_cast( - (UIntPtr)heapPtr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); heapInfoBlk->fPagePtr = 1; @@ -140,22 +142,22 @@ namespace Kernel } /// @brief Declare pointer as free. - /// @param heapPtr the pointer. + /// @param heap_ptr the pointer. /// @return - Int32 mm_delete_ke_heap(VoidPtr heapPtr) + Int32 mm_delete_ke_heap(VoidPtr heap_ptr) { if (kHeapCount < 1) return -kErrorInternal; - if (((IntPtr)heapPtr - sizeof(Detail::HEAP_INFORMATION_BLOCK)) <= 0) + if (((IntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)) <= 0) return -kErrorInternal; - if (((IntPtr)heapPtr - kBadPtr) < 0) + if (((IntPtr)heap_ptr - kBadPtr) < 0) return -kErrorInternal; Detail::mm_alloc_init_timeout(); Detail::HEAP_INFORMATION_BLOCK_PTR heapInfoBlk = reinterpret_cast( - (UIntPtr)heapPtr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); if (heapInfoBlk && heapInfoBlk->fMagic == kKernelHeapMagic) { @@ -197,18 +199,18 @@ namespace Kernel } /// @brief Check if pointer is a valid kernel pointer. - /// @param heapPtr the pointer + /// @param heap_ptr the pointer /// @return if it exists. - Boolean mm_is_valid_heap(VoidPtr heapPtr) + Boolean mm_is_valid_heap(VoidPtr heap_ptr) { if (kHeapCount < 1) return false; - if (heapPtr) + if (heap_ptr) { Detail::HEAP_INFORMATION_BLOCK_PTR virtualAddress = reinterpret_cast( - (UIntPtr)heapPtr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); if (virtualAddress->fPresent && virtualAddress->fMagic == kKernelHeapMagic) { @@ -220,15 +222,15 @@ namespace Kernel } /// @brief Protect the heap with a CRC value. - /// @param heapPtr HIB pointer. + /// @param heap_ptr HIB pointer. /// @return if it valid: point has crc now., otherwise fail. - Boolean mm_protect_ke_heap(VoidPtr heapPtr) + Boolean mm_protect_ke_heap(VoidPtr heap_ptr) { - if (heapPtr) + if (heap_ptr) { Detail::HEAP_INFORMATION_BLOCK_PTR heapInfoBlk = reinterpret_cast( - (UIntPtr)heapPtr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); if (heapInfoBlk->fPresent && kKernelHeapMagic == heapInfoBlk->fMagic) { diff --git a/Kernel/Sources/PageManager.cxx b/Kernel/Sources/PageManager.cxx index 8e9e77cc..7e0ef67b 100644 --- a/Kernel/Sources/PageManager.cxx +++ b/Kernel/Sources/PageManager.cxx @@ -63,6 +63,8 @@ namespace Kernel /// @return PTEWrapper PageManager::Request(Boolean Rw, Boolean User, Boolean ExecDisable, SizeT Sz) { + kcout << "newoskrnl: Allocating VMH page from PageManager...\r"; + // Store PTE wrapper right after PTE. VoidPtr ptr = Kernel::HAL::hal_alloc_page(Rw, User, Sz); diff --git a/Kernel/Sources/User.cxx b/Kernel/Sources/User.cxx index 22dda57e..4109864a 100644 --- a/Kernel/Sources/User.cxx +++ b/Kernel/Sources/User.cxx @@ -75,12 +75,7 @@ namespace Kernel if (NewFilesystemManager::GetMounted()) { - auto node = NewFilesystemManager::GetMounted()->Open(kUsersFile, "wb"); - - if (!node) - { - NewFilesystemManager::GetMounted()->Create(kUsersFile); - } + auto node = NewFilesystemManager::GetMounted()->Create(kUsersFile); if (node) { @@ -89,12 +84,10 @@ namespace Kernel } delete token; - return true; } delete token; - return false; } -- cgit v1.2.3