From 38e97a3241531571a56c1dc6a67f9cfa871eb31c Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 25 Mar 2025 04:27:18 +0100 Subject: refactor(kernel): Refactor MemoryMgr code and log freed address location. feat(setup_amd64): Add 'disk' build step. fix(hal/amd64): Fix warning regarding resb being used inside a non-bss area (using no-op instead). feat(kernelkit/lockdelegate): add new entry to lock delegate's enum (ensure validation). feat(meta): ran format.sh to format files according to .clang-format. Signed-off-by: Amlal El Mahrouss --- dev/kernel/FirmwareKit/EPM.h | 8 +-- .../AMD64/HalApplicationProcessorStartup.asm | 14 ++-- dev/kernel/HALKit/AMD64/HalBoot.asm | 28 -------- dev/kernel/HALKit/AMD64/HalBootHeader.asm | 32 +++++++++ dev/kernel/KernelKit/LockDelegate.h | 2 + dev/kernel/src/MemoryMgr.cc | 81 +++++++++++----------- 6 files changed, 87 insertions(+), 78 deletions(-) delete mode 100644 dev/kernel/HALKit/AMD64/HalBoot.asm create mode 100644 dev/kernel/HALKit/AMD64/HalBootHeader.asm (limited to 'dev/kernel') diff --git a/dev/kernel/FirmwareKit/EPM.h b/dev/kernel/FirmwareKit/EPM.h index 4728e2f5..b50a0943 100644 --- a/dev/kernel/FirmwareKit/EPM.h +++ b/dev/kernel/FirmwareKit/EPM.h @@ -101,10 +101,10 @@ struct PACKED EPM_PART_BLOCK enum { kEPMInvalid = 0x00, - kEPMGenericOS = 0xcf, // Generic OS - kEPMLinux = 0x8f, // Linux on EPM - kEPMBSD = 0x9f, // Berkeley Soft. Distribution - kEPMNeOS = 0x1f, // This OS. + kEPMGenericOS = 0xcf, /// @brief Generic OS + kEPMLinux = 0x8f, /// @brief Linux on EPM + kEPMBSD = 0x9f, /// @brief Berkeley Soft. Distribution + kEPMNeOS = 0x1f, /// @brief NeKernel. kEPMInvalidOS = 0xff, }; diff --git a/dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm b/dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm index c64e0a09..c1c6e1fd 100644 --- a/dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm +++ b/dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm @@ -4,6 +4,8 @@ ;; * NeKernel ;; * Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. ;; * +;; * 25/03/25: FIX: Fix warning regarding resb being used inside a non-bss area (using no-op instead). +;; * ;; * ======================================================== ;; */ @@ -61,17 +63,17 @@ hal_ap_64bit_entry: mov ss, ax mov rsp, [hal_ap_64bit_entry_stack_end] - push 0x33 + push 0x33 push qword [hal_ap_64bit_entry_loop] - o64 pushf - push rsp - push 0x33 + o64 pushf + push rsp + push 0x33 - o64 iret + o64 iret hal_ap_64bit_entry_loop: jmp $ hal_ap_64bit_entry_stack: - resb 8196*2 + times 8196*2 nop hal_ap_64bit_entry_stack_end: \ No newline at end of file diff --git a/dev/kernel/HALKit/AMD64/HalBoot.asm b/dev/kernel/HALKit/AMD64/HalBoot.asm deleted file mode 100644 index 3ccdd50e..00000000 --- a/dev/kernel/HALKit/AMD64/HalBoot.asm +++ /dev/null @@ -1,28 +0,0 @@ -;; /* -;; * ======================================================== -;; * -;; * NeKernel -;; * Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. -;; * -;; * ======================================================== -;; */ - -[bits 64] - -;; Global symbol of this unit -[extern hal_init_platform] - -%define kTypeKernel 100 -%define kArchAmd64 122 -%define kHandoverMagic 0xBADCC - -section .ldr - -HandoverMagic: - dq kHandoverMagic -HandoverType: - dw kTypeKernel -HandoverPad: - dw 0 -HandoverArch: - dw kArchAmd64 diff --git a/dev/kernel/HALKit/AMD64/HalBootHeader.asm b/dev/kernel/HALKit/AMD64/HalBootHeader.asm new file mode 100644 index 00000000..ddf5bb53 --- /dev/null +++ b/dev/kernel/HALKit/AMD64/HalBootHeader.asm @@ -0,0 +1,32 @@ +;; /* +;; * ======================================================== +;; * +;; * NeKernel +;; * Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. +;; * +;; * 25/03/25: Rename HalBoot.asm to HalBootHeader.asm, expose struct symbols; remove unused external symbol. +;; * +;; * ======================================================== +;; */ + +[bits 64] + +%define kTypeKernel 100 +%define kArchAmd64 122 +%define kHandoverMagic 0xBADCC + +global _HandoverMagic +global _HandoverType +global _HandoverPad +global _HandoverArch + +section .ldr + +_HandoverMagic: + dq kHandoverMagic +_HandoverType: + dw kTypeKernel +_HandoverPad: + dw 0 +_HandoverArch: + dw kArchAmd64 diff --git a/dev/kernel/KernelKit/LockDelegate.h b/dev/kernel/KernelKit/LockDelegate.h index a657caa0..28eff060 100644 --- a/dev/kernel/KernelKit/LockDelegate.h +++ b/dev/kernel/KernelKit/LockDelegate.h @@ -13,8 +13,10 @@ namespace NeOS { enum { + kLockInvalid, kLockDone = 200, kLockTimedOut, + kLockCount = 3, }; /// @brief Lock condition pointer. diff --git a/dev/kernel/src/MemoryMgr.cc b/dev/kernel/src/MemoryMgr.cc index 5e595b51..7a79b906 100644 --- a/dev/kernel/src/MemoryMgr.cc +++ b/dev/kernel/src/MemoryMgr.cc @@ -16,28 +16,29 @@ Revision History: 10/8/24: FIX: Fix useless long name, alongside a new WR (WriteRead) field. - 20/10/24: Fix mm_new_ and mm_delete_ APIs inside MemoryMgr.h header. (amlal) - 27/01/25: Reworked code as the memory manager. + 20/10/24: FIX: Fix mm_new_ and mm_delete_ APIs inside MemoryMgr.h header. (amlal) + 27/01/25: REFACTOR: Reworked code as the memory manager. + 25/03/25: REFACTOR: Refactor MemoryMgr code and log freed address location. ------------------------------------------- */ //! @file MemoryMgr.cc //! @brief Heap algorithm that serves as the main memory manager. -#define kKernelHeapMagic (0xD4D75) -#define kKernelHeapAlignSz (4) +#define kMemoryMgrMagic (0xD4D75) +#define kMemoryMgrAlignSz (4) namespace NeOS { /// @brief Implementation details. namespace Detail { - struct PACKED HEAP_INFORMATION_BLOCK; + struct PACKED MM_INFORMATION_BLOCK; /// @brief Kernel heap information block. /// Located before the address bytes. /// | HIB | CLASS/STRUCT/DATA TYPES... | - struct PACKED HEAP_INFORMATION_BLOCK final + struct PACKED MM_INFORMATION_BLOCK final { ///! @brief 32-bit value which contains the magic number of the heap. UInt32 fMagic : 24; @@ -67,7 +68,7 @@ namespace NeOS UIntPtr fOffset; /// @brief Padding bytes for header. - UInt8 fPadding[kKernelHeapAlignSz]; + UInt8 fPadding[kMemoryMgrAlignSz]; }; /// @brief Check for heap address validity. @@ -78,7 +79,7 @@ namespace NeOS if (!heap_ptr) return false; - auto base_heap = ((IntPtr)heap_ptr) - sizeof(Detail::HEAP_INFORMATION_BLOCK); + auto base_heap = ((IntPtr)heap_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK); /// Add that check in case we're having an integer underflow. /// @@ -90,7 +91,7 @@ namespace NeOS return true; } - typedef HEAP_INFORMATION_BLOCK* HEAP_INFORMATION_BLOCK_PTR; + typedef MM_INFORMATION_BLOCK* MM_INFORMATION_BLOCK_PTR; } // namespace Detail /// @brief Declare a new size for ptr_heap. @@ -122,25 +123,25 @@ namespace NeOS if (sz_fix == 0) return nullptr; - sz_fix += sizeof(Detail::HEAP_INFORMATION_BLOCK); + sz_fix += sizeof(Detail::MM_INFORMATION_BLOCK); PageMgr heap_mgr; auto wrapper = heap_mgr.Request(wr, user, No, sz_fix); - Detail::HEAP_INFORMATION_BLOCK_PTR heap_info_ptr = - reinterpret_cast( - wrapper.VirtualAddress() + sizeof(Detail::HEAP_INFORMATION_BLOCK)); + Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = + reinterpret_cast( + wrapper.VirtualAddress() + sizeof(Detail::MM_INFORMATION_BLOCK)); heap_info_ptr->fSize = sz_fix; - heap_info_ptr->fMagic = kKernelHeapMagic; + heap_info_ptr->fMagic = kMemoryMgrMagic; heap_info_ptr->fCRC32 = 0; // dont fill it for now. - heap_info_ptr->fOffset = reinterpret_cast(heap_info_ptr) + sizeof(Detail::HEAP_INFORMATION_BLOCK); + heap_info_ptr->fOffset = reinterpret_cast(heap_info_ptr) + sizeof(Detail::MM_INFORMATION_BLOCK); heap_info_ptr->fPage = No; heap_info_ptr->fWriteRead = wr; heap_info_ptr->fUser = user; heap_info_ptr->fPresent = Yes; - rt_set_memory(heap_info_ptr->fPadding, 0, kKernelHeapAlignSz); + rt_set_memory(heap_info_ptr->fPadding, 0, kMemoryMgrAlignSz); auto result = reinterpret_cast(heap_info_ptr->fOffset); @@ -157,9 +158,9 @@ namespace NeOS if (Detail::mm_check_heap_address(heap_ptr) == No) return kErrorHeapNotPresent; - Detail::HEAP_INFORMATION_BLOCK_PTR heap_info_ptr = - reinterpret_cast( - (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = + reinterpret_cast( + (UIntPtr)heap_ptr - sizeof(Detail::MM_INFORMATION_BLOCK)); if (!heap_info_ptr) return kErrorHeapNotPresent; @@ -179,9 +180,9 @@ namespace NeOS if (Detail::mm_check_heap_address(heap_ptr) == No) return kErrorHeapNotPresent; - Detail::HEAP_INFORMATION_BLOCK_PTR heap_info_ptr = - reinterpret_cast( - (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = + reinterpret_cast( + (UIntPtr)heap_ptr - sizeof(Detail::MM_INFORMATION_BLOCK)); if (!heap_info_ptr) return kErrorHeapNotPresent; @@ -195,9 +196,9 @@ namespace NeOS /// @param heap_ptr the pointer to get. _Output UInt64 mm_get_flags(VoidPtr heap_ptr) { - Detail::HEAP_INFORMATION_BLOCK_PTR heap_info_ptr = - reinterpret_cast( - (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = + reinterpret_cast( + (UIntPtr)heap_ptr - sizeof(Detail::MM_INFORMATION_BLOCK)); if (!heap_info_ptr) return kErrorHeapNotPresent; @@ -213,11 +214,11 @@ namespace NeOS if (Detail::mm_check_heap_address(heap_ptr) == No) return kErrorHeapNotPresent; - Detail::HEAP_INFORMATION_BLOCK_PTR heap_info_ptr = - reinterpret_cast( - (UIntPtr)(heap_ptr) - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = + reinterpret_cast( + (UIntPtr)(heap_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK)); - if (heap_info_ptr && heap_info_ptr->fMagic == kKernelHeapMagic) + if (heap_info_ptr && heap_info_ptr->fMagic == kMemoryMgrMagic) { if (!heap_info_ptr->fPresent) { @@ -232,14 +233,14 @@ namespace NeOS heap_info_ptr->fUser = No; heap_info_ptr->fMagic = 0; - PTEWrapper page_wrapper(No, No, No, reinterpret_cast(heap_info_ptr) - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + kout << "Address has been successfully freed: " << hex_number((UIntPtr)heap_info_ptr) << kendl; + + PTEWrapper page_wrapper(No, No, No, reinterpret_cast(heap_info_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK)); Ref pte_address{page_wrapper}; PageMgr heap_mgr; heap_mgr.Free(pte_address); - kout << "Address has been successfully freed." << kendl; - return kErrorSuccess; } @@ -253,11 +254,11 @@ namespace NeOS { if (heap_ptr && HAL::mm_is_bitmap(heap_ptr)) { - Detail::HEAP_INFORMATION_BLOCK_PTR heap_info_ptr = - reinterpret_cast( - (UIntPtr)(heap_ptr) - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = + reinterpret_cast( + (UIntPtr)(heap_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK)); - return (heap_info_ptr && heap_info_ptr->fPresent && heap_info_ptr->fMagic == kKernelHeapMagic); + return (heap_info_ptr && heap_info_ptr->fPresent && heap_info_ptr->fMagic == kMemoryMgrMagic); } return No; @@ -270,12 +271,12 @@ namespace NeOS { if (heap_ptr) { - Detail::HEAP_INFORMATION_BLOCK_PTR heap_info_ptr = - reinterpret_cast( - (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr = + reinterpret_cast( + (UIntPtr)heap_ptr - sizeof(Detail::MM_INFORMATION_BLOCK)); /// if valid, present and is heap header, then compute crc32 - if (heap_info_ptr && heap_info_ptr->fPresent && kKernelHeapMagic == heap_info_ptr->fMagic) + if (heap_info_ptr && heap_info_ptr->fPresent && kMemoryMgrMagic == heap_info_ptr->fMagic) { heap_info_ptr->fCRC32 = ke_calculate_crc32((Char*)heap_info_ptr->fOffset, heap_info_ptr->fSize); -- cgit v1.2.3