summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-03-25 04:27:18 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-03-25 04:27:18 +0100
commit38e97a3241531571a56c1dc6a67f9cfa871eb31c (patch)
treef46ef7d064f66a77bdcdfd8915dbf0de1db11b00
parent78317d09c36ff8f5b1a879f4a094e877178646ce (diff)
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 <amlal@nekernel.org>
-rw-r--r--dev/kernel/FirmwareKit/EPM.h8
-rw-r--r--dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm14
-rw-r--r--dev/kernel/HALKit/AMD64/HalBootHeader.asm (renamed from dev/kernel/HALKit/AMD64/HalBoot.asm)18
-rw-r--r--dev/kernel/KernelKit/LockDelegate.h2
-rw-r--r--dev/kernel/src/MemoryMgr.cc81
-rw-r--r--public/frameworks/DiskImage.fwrk/headers/DiskImage.h10
-rw-r--r--public/tools/diutil/CommandLine.cc8
-rwxr-xr-xsetup_amd64.sh4
8 files changed, 79 insertions, 66 deletions
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/HalBootHeader.asm
index 3ccdd50e..ddf5bb53 100644
--- a/dev/kernel/HALKit/AMD64/HalBoot.asm
+++ b/dev/kernel/HALKit/AMD64/HalBootHeader.asm
@@ -4,25 +4,29 @@
;; * 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]
-;; Global symbol of this unit
-[extern hal_init_platform]
-
%define kTypeKernel 100
%define kArchAmd64 122
%define kHandoverMagic 0xBADCC
+global _HandoverMagic
+global _HandoverType
+global _HandoverPad
+global _HandoverArch
+
section .ldr
-HandoverMagic:
+_HandoverMagic:
dq kHandoverMagic
-HandoverType:
+_HandoverType:
dw kTypeKernel
-HandoverPad:
+_HandoverPad:
dw 0
-HandoverArch:
+_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<Detail::HEAP_INFORMATION_BLOCK_PTR>(
- wrapper.VirtualAddress() + sizeof(Detail::HEAP_INFORMATION_BLOCK));
+ Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr =
+ reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>(
+ 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<UIntPtr>(heap_info_ptr) + sizeof(Detail::HEAP_INFORMATION_BLOCK);
+ heap_info_ptr->fOffset = reinterpret_cast<UIntPtr>(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<VoidPtr>(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<Detail::HEAP_INFORMATION_BLOCK_PTR>(
- (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK));
+ Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr =
+ reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>(
+ (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<Detail::HEAP_INFORMATION_BLOCK_PTR>(
- (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK));
+ Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr =
+ reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>(
+ (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<Detail::HEAP_INFORMATION_BLOCK_PTR>(
- (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK));
+ Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr =
+ reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>(
+ (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<Detail::HEAP_INFORMATION_BLOCK_PTR>(
- (UIntPtr)(heap_ptr) - sizeof(Detail::HEAP_INFORMATION_BLOCK));
+ Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr =
+ reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>(
+ (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<UIntPtr>(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<UIntPtr>(heap_info_ptr) - sizeof(Detail::MM_INFORMATION_BLOCK));
Ref<PTEWrapper> 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<Detail::HEAP_INFORMATION_BLOCK_PTR>(
- (UIntPtr)(heap_ptr) - sizeof(Detail::HEAP_INFORMATION_BLOCK));
+ Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr =
+ reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>(
+ (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<Detail::HEAP_INFORMATION_BLOCK_PTR>(
- (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK));
+ Detail::MM_INFORMATION_BLOCK_PTR heap_info_ptr =
+ reinterpret_cast<Detail::MM_INFORMATION_BLOCK_PTR>(
+ (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);
diff --git a/public/frameworks/DiskImage.fwrk/headers/DiskImage.h b/public/frameworks/DiskImage.fwrk/headers/DiskImage.h
index 3a13caa0..678c2e81 100644
--- a/public/frameworks/DiskImage.fwrk/headers/DiskImage.h
+++ b/public/frameworks/DiskImage.fwrk/headers/DiskImage.h
@@ -19,7 +19,7 @@
#define kDIFailureStatus (1)
#define kDIDiskNameLen (16)
-#define kDIOutNameLen (256)
+#define kDIOutNameLen (256)
/// @brief Disk Image file structure.
/// @param disk_name Disk partition name.
@@ -30,10 +30,10 @@
struct DI_DISK_IMAGE
{
Char disk_name[kDIDiskNameLen] = kDIDefaultDiskName;
- SInt32 sector_sz = kDISectorSz;
- SInt32 block_cnt = 0;
- SizeT disk_sz = kDIMinDiskSz;
- Char out_name[kDIOutNameLen] = kDIDefaultOutputName;
+ SInt32 sector_sz = kDISectorSz;
+ SInt32 block_cnt = 0;
+ SizeT disk_sz = kDIMinDiskSz;
+ Char out_name[kDIOutNameLen] = kDIDefaultOutputName;
};
/// @brief Format with an EPM partition.
diff --git a/public/tools/diutil/CommandLine.cc b/public/tools/diutil/CommandLine.cc
index 7b95665a..22c1616c 100644
--- a/public/tools/diutil/CommandLine.cc
+++ b/public/tools/diutil/CommandLine.cc
@@ -23,28 +23,28 @@ int main(int argc, char** argv)
{
const Char* arg_s = argv[arg];
- if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr)"--disk-output-name", MmStrLen("--disk-output-name") == 0))
+ if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr) "--disk-output-name", MmStrLen("--disk-output-name") == 0))
{
if ((arg + 1) < argc)
{
MmCopyMemory((VoidPtr)kOutDisk, argv[arg + 1], kDIDiskNameLen);
}
}
- else if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr)"--disk-output-size", MmStrLen("--disk-output-size") == 0))
+ else if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr) "--disk-output-size", MmStrLen("--disk-output-size") == 0))
{
if ((arg + 1) < argc)
{
kDiskSz = MathToNumber(argv[arg + 1], nullptr, 10);
}
}
- else if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr)"--disk-sector-size", MmStrLen("--disk-sector-size") == 0))
+ else if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr) "--disk-sector-size", MmStrLen("--disk-sector-size") == 0))
{
if ((arg + 1) < argc)
{
kDiskSectorSz = MathToNumber(argv[arg + 1], nullptr, 10);
}
}
- else if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr)"--disk-part-name", MmStrLen("--disk-part-name") == 0))
+ else if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr) "--disk-part-name", MmStrLen("--disk-part-name") == 0))
{
if ((arg + 1) < argc)
{
diff --git a/setup_amd64.sh b/setup_amd64.sh
index de69c1b2..b211b4bc 100755
--- a/setup_amd64.sh
+++ b/setup_amd64.sh
@@ -1,9 +1,13 @@
#!/bin/sh
+# LOG HISTORY:
+# 25/03/25: Add 'disk' build step.
+
cd dev/boot/modules/SysChk
btb amd64.json
cd ../../
make -f amd64-desktop.make efi
+make -f amd64-desktop.make disk
make -f amd64-desktop.make epm-img
cd ../../dev/kernel/HALKit/AMD64
./make_ap_blob.sh