From 2ac97283d813414973f83d177280aafa7fbaa66f Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 8 Apr 2025 13:38:35 +0200 Subject: kernel, storage, heap, abi: lots of improvements and tweaks. - Please read the commit details for in-depth insights. - Add stack smash prevention code. - Better prevention in BitMap Mgr. Signed-off-by: Amlal El Mahrouss --- dev/kernel/src/BitMapMgr.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'dev/kernel/src/BitMapMgr.cc') diff --git a/dev/kernel/src/BitMapMgr.cc b/dev/kernel/src/BitMapMgr.cc index 7f3f8500..51c4aed1 100644 --- a/dev/kernel/src/BitMapMgr.cc +++ b/dev/kernel/src/BitMapMgr.cc @@ -141,7 +141,15 @@ namespace Kernel return (VoidPtr)ptr_bit_set; } - base = reinterpret_cast(reinterpret_cast(base) + ((ptr_bit_set[kBitMapMagIdx] != kBitMapMagic) ? (size + pad) : ptr_bit_set[kBitMapSizeIdx])); + UIntPtr raw_base = reinterpret_cast(base); + UIntPtr offset = (ptr_bit_set[kBitMapMagIdx] != kBitMapMagic) + ? (size + pad) + : ptr_bit_set[kBitMapSizeIdx]; + + base = reinterpret_cast(raw_base + offset); + + if (base == nullptr) + return nullptr; } return nullptr; -- cgit v1.2.3 From 26932fb28c6d8724222fc80705976369f2f5dbb3 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 10 Apr 2025 08:07:00 +0200 Subject: dev, meta: minor tweaks and improvements. Signed-off-by: Amlal El Mahrouss --- dev/boot/modules/SysChk/SysChk.cc | 4 ++-- dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc | 20 ++++++++++++++++++-- dev/kernel/HALKit/AMD64/PCI/Device.cc | 6 +++--- dev/kernel/KernelKit/PCI/Device.h | 2 +- dev/kernel/src/BitMapMgr.cc | 8 ++++---- dev/kernel/src/DriveMgr.cc | 2 +- 6 files changed, 29 insertions(+), 13 deletions(-) (limited to 'dev/kernel/src/BitMapMgr.cc') diff --git a/dev/boot/modules/SysChk/SysChk.cc b/dev/boot/modules/SysChk/SysChk.cc index 471b89a4..ed14d2cd 100644 --- a/dev/boot/modules/SysChk/SysChk.cc +++ b/dev/boot/modules/SysChk/SysChk.cc @@ -35,10 +35,10 @@ EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) return kEfiOk; Boot::BDiskFormatFactory::BFileDescriptor desc{}; - + desc.fFileName[0] = '/'; desc.fFileName[1] = 0; - desc.fKind = kNeFSCatalogKindDir; + desc.fKind = kNeFSCatalogKindDir; partition_factory.Format(kMachineModel, &desc, sizeof(Boot::BDiskFormatFactory::BFileDescriptor)); diff --git a/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc b/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc index c4898dfc..040b6fd9 100644 --- a/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc +++ b/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc @@ -12,8 +12,11 @@ namespace Kernel::HAL { - /// @brief Go over the Page structure and find the address of *virtual_address* - + /***********************************************************************************/ + /// @brief Gets a physical address from a virtual address. + /// @param virt a valid virtual address. + /// @return Physical address. + /***********************************************************************************/ UIntPtr hal_get_phys_address(VoidPtr virt) { const UInt64 vaddr = (UInt64)virt; @@ -25,18 +28,21 @@ namespace Kernel::HAL // Level 4 auto pml4 = reinterpret_cast(cr3); UInt64 pml4e = pml4[(vaddr >> 39) & kMask9Bits]; + if (!(pml4e & 1)) return 0; // Level 3 auto pdpt = reinterpret_cast(pml4e & ~kPageOffsetMask); UInt64 pdpte = pdpt[(vaddr >> 30) & kMask9Bits]; + if (!(pdpte & 1)) return 0; // Level 2 auto pd = reinterpret_cast(pdpte & ~kPageOffsetMask); UInt64 pde = pd[(vaddr >> 21) & kMask9Bits]; + if (!(pde & 1)) return 0; @@ -49,6 +55,7 @@ namespace Kernel::HAL // Level 1 auto pt = reinterpret_cast(pde & ~kPageOffsetMask); UInt64 pte = pt[(vaddr >> 12) & kMask9Bits]; + if (!(pte & 1)) return 0; @@ -86,16 +93,19 @@ namespace Kernel::HAL auto pml4 = reinterpret_cast(cr3); UInt64 pml4e = pml4[(vaddr >> 39) & kMask9]; + if (!(pml4e & 1)) return 1; auto pdpt = reinterpret_cast(pml4e & ~kPageMask); UInt64 pdpte = pdpt[(vaddr >> 30) & kMask9]; + if (!(pdpte & 1)) return 1; auto pd = reinterpret_cast(pdpte & ~kPageMask); UInt64 pde = pd[(vaddr >> 21) & kMask9]; + if (!(pde & 1)) return 1; @@ -104,10 +114,16 @@ namespace Kernel::HAL // Set the new PTE pte = (reinterpret_cast(physical_address) & ~0xFFFULL) | 0x01ULL; // Present + + if (flags & ~kMMFlagsPresent) + pte &= ~(0x01ULL); // Not Present + if (flags & kMMFlagsWr) pte |= 1 << 1; // Writable + if (flags & kMMFlagsUser) pte |= 1 << 2; // User + if (flags & kMMFlagsNX) pte |= 1ULL << 63; // NX diff --git a/dev/kernel/HALKit/AMD64/PCI/Device.cc b/dev/kernel/HALKit/AMD64/PCI/Device.cc index 7ad19360..ced473ed 100644 --- a/dev/kernel/HALKit/AMD64/PCI/Device.cc +++ b/dev/kernel/HALKit/AMD64/PCI/Device.cc @@ -11,7 +11,7 @@ #define PCI_BAR_LOWMEM (0x02) #define PCI_BAR_64 (0x04) #define PCI_BAR_PREFETCH (0x08) -#define PCI_ENABLE_BIT (0x80000000) +#define PCI_ENABLE_BIT (0x80000000) static Kernel::UInt NE_PCIReadRaw(Kernel::UInt bar, Kernel::UShort bus, Kernel::UShort dev, Kernel::UShort fun) { @@ -77,7 +77,7 @@ namespace Kernel::PCI else if (sz == 2) { UInt temp = HAL::rt_in32((UShort)PciConfigKind::ConfigData); - + temp &= ~(0xFFFF << ((bar & 2) * 8)); temp |= (data & 0xFFFF) << ((bar & 2) * 8); @@ -89,7 +89,7 @@ namespace Kernel::PCI temp &= ~(0xFF << ((bar & 3) * 8)); temp |= (data & 0xFF) << ((bar & 3) * 8); - + HAL::rt_out32((UShort)PciConfigKind::ConfigAddress, temp); } } diff --git a/dev/kernel/KernelKit/PCI/Device.h b/dev/kernel/KernelKit/PCI/Device.h index 64dd9062..0c434b0b 100644 --- a/dev/kernel/KernelKit/PCI/Device.h +++ b/dev/kernel/KernelKit/PCI/Device.h @@ -27,7 +27,7 @@ namespace Kernel::PCI Device(UShort bus, UShort device, UShort function, UInt32 bar); Device& operator=(const Device&) = default; - Device(const Device&) = default; + Device(const Device&) = default; ~Device(); diff --git a/dev/kernel/src/BitMapMgr.cc b/dev/kernel/src/BitMapMgr.cc index 51c4aed1..4f27e654 100644 --- a/dev/kernel/src/BitMapMgr.cc +++ b/dev/kernel/src/BitMapMgr.cc @@ -142,10 +142,10 @@ namespace Kernel } UIntPtr raw_base = reinterpret_cast(base); - UIntPtr offset = (ptr_bit_set[kBitMapMagIdx] != kBitMapMagic) - ? (size + pad) - : ptr_bit_set[kBitMapSizeIdx]; - + UIntPtr offset = (ptr_bit_set[kBitMapMagIdx] != kBitMapMagic) + ? (size + pad) + : ptr_bit_set[kBitMapSizeIdx]; + base = reinterpret_cast(raw_base + offset); if (base == nullptr) diff --git a/dev/kernel/src/DriveMgr.cc b/dev/kernel/src/DriveMgr.cc index 36e0a7e4..0d24d4f8 100644 --- a/dev/kernel/src/DriveMgr.cc +++ b/dev/kernel/src/DriveMgr.cc @@ -110,7 +110,7 @@ namespace Kernel { return "ATA-DMA"; } -#elif defined( __AHCI__) +#elif defined(__AHCI__) const Char* io_drv_kind(Void) { return "AHCI"; -- cgit v1.2.3 From e50f871e6852beacb53986f930ed2d5dead84838 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 10 Apr 2025 09:41:33 +0200 Subject: kernel, boot: a lot of improvements regarding memory probing. Signed-off-by: Amlal El Mahrouss --- dev/boot/modules/BootNet/amd64.json | 2 +- dev/boot/modules/SysChk/SysChk.cc | 6 +-- dev/boot/modules/SysChk/amd64-ahci.json | 25 +++++++++++++ dev/boot/modules/SysChk/amd64-pio.json | 25 +++++++++++++ dev/boot/modules/SysChk/amd64.json | 24 ------------ dev/boot/src/HEL/AMD64/BootEFI.cc | 65 ++++++++++++--------------------- dev/kernel/amd64-ci.make | 2 +- dev/kernel/amd64-desktop.make | 2 +- dev/kernel/src/BitMapMgr.cc | 4 +- 9 files changed, 81 insertions(+), 74 deletions(-) create mode 100644 dev/boot/modules/SysChk/amd64-ahci.json create mode 100644 dev/boot/modules/SysChk/amd64-pio.json delete mode 100644 dev/boot/modules/SysChk/amd64.json (limited to 'dev/kernel/src/BitMapMgr.cc') diff --git a/dev/boot/modules/BootNet/amd64.json b/dev/boot/modules/BootNet/amd64.json index 86143e6a..da4a15f0 100644 --- a/dev/boot/modules/BootNet/amd64.json +++ b/dev/boot/modules/BootNet/amd64.json @@ -10,7 +10,7 @@ "-fPIC", "-fno-rtti", "-fno-exceptions", - "-Wl,--subsystem=17,--image-base,0x1000000,-e,BootNetModuleMain" + "-Wl,--subsystem=17,--image-base,0x10000000,-e,BootNetModuleMain" ], "cpp_macros": [ "__NEOSKRNL__", diff --git a/dev/boot/modules/SysChk/SysChk.cc b/dev/boot/modules/SysChk/SysChk.cc index ed14d2cd..3086cdc6 100644 --- a/dev/boot/modules/SysChk/SysChk.cc +++ b/dev/boot/modules/SysChk/SysChk.cc @@ -23,12 +23,12 @@ // Makes the compiler shut up. #ifndef kMachineModel -#define kMachineModel "NeKrnl" +#define kMachineModel "Ne" #endif // !kMachineModel EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) { -#ifdef __NE_AMD64__ +#ifdef __ATA_PIO__ Boot::BDiskFormatFactory partition_factory; if (partition_factory.IsPartitionValid()) @@ -40,7 +40,7 @@ EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) desc.fFileName[1] = 0; desc.fKind = kNeFSCatalogKindDir; - partition_factory.Format(kMachineModel, &desc, sizeof(Boot::BDiskFormatFactory::BFileDescriptor)); + partition_factory.Format(kMachineModel, &desc, 1); if (partition_factory.IsPartitionValid()) return kEfiOk; diff --git a/dev/boot/modules/SysChk/amd64-ahci.json b/dev/boot/modules/SysChk/amd64-ahci.json new file mode 100644 index 00000000..527e79f9 --- /dev/null +++ b/dev/boot/modules/SysChk/amd64-ahci.json @@ -0,0 +1,25 @@ +{ + "compiler_path": "x86_64-w64-mingw32-g++", + "compiler_std": "c++20", + "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], + "sources_path": ["*.cc", "*.S", "../../src/HEL/AMD64/*.cc", "../../src/HEL/AMD64/*.S", "../../src/*.cc"], + "output_name": "chk.efi", + "compiler_flags": [ + "-nostdlib", + "-std=c++20", + "-fPIC", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17,--image-base,0x10000000,-e,SysChkModuleMain" + ], + "cpp_macros": [ + "__NEOSKRNL__", + "__BOOTZ__", + "__BOOTZ_STANDALONE__", + "__NE_AMD64__", + "__AHCI__", + "kChkVersionHighest=0x0100", + "kChkVersionLowest=0x0100", + "kChkVersion=0x0100" + ] +} diff --git a/dev/boot/modules/SysChk/amd64-pio.json b/dev/boot/modules/SysChk/amd64-pio.json new file mode 100644 index 00000000..de3195ce --- /dev/null +++ b/dev/boot/modules/SysChk/amd64-pio.json @@ -0,0 +1,25 @@ +{ + "compiler_path": "x86_64-w64-mingw32-g++", + "compiler_std": "c++20", + "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], + "sources_path": ["*.cc", "*.S", "../../src/HEL/AMD64/*.cc", "../../src/HEL/AMD64/*.S", "../../src/*.cc"], + "output_name": "chk.efi", + "compiler_flags": [ + "-nostdlib", + "-std=c++20", + "-fPIC", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17,--image-base,0x10000000,-e,SysChkModuleMain" + ], + "cpp_macros": [ + "__NEOSKRNL__", + "__BOOTZ__", + "__BOOTZ_STANDALONE__", + "__NE_AMD64__", + "__ATA_PIO__", + "kChkVersionHighest=0x0100", + "kChkVersionLowest=0x0100", + "kChkVersion=0x0100" + ] +} diff --git a/dev/boot/modules/SysChk/amd64.json b/dev/boot/modules/SysChk/amd64.json deleted file mode 100644 index 3b5bab6f..00000000 --- a/dev/boot/modules/SysChk/amd64.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "compiler_path": "x86_64-w64-mingw32-g++", - "compiler_std": "c++20", - "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], - "sources_path": ["*.cc", "*.S", "../../src/HEL/AMD64/*.cc", "../../src/HEL/AMD64/*.S", "../../src/*.cc"], - "output_name": "chk.efi", - "compiler_flags": [ - "-nostdlib", - "-std=c++20", - "-fPIC", - "-fno-rtti", - "-fno-exceptions", - "-Wl,--subsystem=17,--image-base,0x1000000,-e,SysChkModuleMain" - ], - "cpp_macros": [ - "__NEOSKRNL__", - "__BOOTZ__", - "__BOOTZ_STANDALONE__", - "__NE_AMD64__", - "kChkVersionHighest=0x0100", - "kChkVersionLowest=0x0100", - "kChkVersion=0x0100" - ] -} diff --git a/dev/boot/src/HEL/AMD64/BootEFI.cc b/dev/boot/src/HEL/AMD64/BootEFI.cc index e07ad6d6..57651016 100644 --- a/dev/boot/src/HEL/AMD64/BootEFI.cc +++ b/dev/boot/src/HEL/AMD64/BootEFI.cc @@ -165,58 +165,39 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, // Fill handover header now. - // ---------------------------------------------------- // - // The following checks for an exisiting partition - // inside the disk, if it doesn't have one, - // format the disk. - // ---------------------------------------------------- // - Boot::BootTextWriter writer; - auto ret = BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); - - if (ret == kEfiFail) - { - writer.Write("BootZ: GetMemoryMap failed (x1)\r"); - Boot::Stop(); - } - - size_struct_ptr += sz_desc * 2; - BS->AllocatePool(EfiMemoryType::EfiBootServicesData, size_struct_ptr, reinterpret_cast(&struct_ptr)); + //-------------------------------------------------------------// + // Update handover file specific table and phyiscal start field. + //-------------------------------------------------------------// - ret = BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + handover_hdr->f_BitMapSize = gib_cast(4); /* Size of bitmap in bytes. */ + Int32 trials = 5 * 10000000; - if (ret == kEfiFail) + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) { - writer.Write("BootZ: GetMemoryMap failed (x2)\r"); - Boot::Stop(); - } + --trials; - //-----------------------------------------------------------// - // A simple loop which finds a usable memory region for us. - //-----------------------------------------------------------// + if (!trials) + { + writer.Write("BootZ: Unable to allocate sufficent memory, trying again with 2GB...\r"); - SizeT lookup_index = 0UL; - SizeT entry_count = size_struct_ptr / sz_desc; + trials = 3 * 10000000; - for (; lookup_index < entry_count; ++lookup_index) - { - if (struct_ptr[lookup_index].Kind == EfiMemoryType::EfiConventionalMemory) - break; - } + handover_hdr->f_BitMapSize = gib_cast(2); /* Size of bitmap in bytes. */ - if (lookup_index > entry_count) - { - writer.Write("BootZ: No usable entries.\r"); - Boot::Stop(); - } + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) + { + --trials; - //-------------------------------------------------------------// - // Update handover file specific table and phyiscal start field. - //-------------------------------------------------------------// - - handover_hdr->f_BitMapStart = (VoidPtr)(struct_ptr[lookup_index].VirtualStart); /* Start of bitmap. */ - handover_hdr->f_BitMapSize = struct_ptr[lookup_index].NumberOfPages * kib_cast(4); /* Size of bitmap in bytes. */ + if (!trials) + { + writer.Write("BootZ: Unable to allocate sufficent memory, aborting...\r"); + Boot::Stop(); + } + } + } + } handover_hdr->f_FirmwareCustomTables[0] = (VoidPtr)BS; handover_hdr->f_FirmwareCustomTables[1] = (VoidPtr)ST; diff --git a/dev/kernel/amd64-ci.make b/dev/kernel/amd64-ci.make index d74b5523..a03c8fdd 100644 --- a/dev/kernel/amd64-ci.make +++ b/dev/kernel/amd64-ci.make @@ -33,7 +33,7 @@ COPY = cp ASMFLAGS = -f win64 # Kernel subsystem is 17 and entrypoint is hal_init_platform -LDFLAGS = -e hal_init_platform --subsystem=17 --image-base 0x1000000 +LDFLAGS = -e hal_init_platform --subsystem=17 --image-base 0x4000000 LDOBJ = obj/*.obj # This file is the Kernel, responsible of task, memory, driver, sci, disk and device management. diff --git a/dev/kernel/amd64-desktop.make b/dev/kernel/amd64-desktop.make index d74b5523..a03c8fdd 100644 --- a/dev/kernel/amd64-desktop.make +++ b/dev/kernel/amd64-desktop.make @@ -33,7 +33,7 @@ COPY = cp ASMFLAGS = -f win64 # Kernel subsystem is 17 and entrypoint is hal_init_platform -LDFLAGS = -e hal_init_platform --subsystem=17 --image-base 0x1000000 +LDFLAGS = -e hal_init_platform --subsystem=17 --image-base 0x4000000 LDOBJ = obj/*.obj # This file is the Kernel, responsible of task, memory, driver, sci, disk and device management. diff --git a/dev/kernel/src/BitMapMgr.cc b/dev/kernel/src/BitMapMgr.cc index 4f27e654..63cadde3 100644 --- a/dev/kernel/src/BitMapMgr.cc +++ b/dev/kernel/src/BitMapMgr.cc @@ -95,9 +95,9 @@ namespace Kernel if (!size) return nullptr; - constexpr const UInt32 kStartOffset = 0x1000; + VoidPtr base = reinterpret_cast((UIntPtr)base_ptr); - VoidPtr base = reinterpret_cast(((UIntPtr)base_ptr) + kStartOffset); + MUST_PASS(base); static SizeT biggest = 0UL; -- cgit v1.2.3