From 9b2686756dee0e55b7860e7ea18d4bf92a365065 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 13 Feb 2024 20:29:28 +0100 Subject: HCR-18: First commit, bringing HCoreKrnl.exe into memory. Signed-off-by: Amlal El Mahrouss --- Private/EFIKit/Api.hxx | 7 ++- Private/EFIKit/EFI.hxx | 9 +++- Private/EFIKit/Handover.hxx | 8 +-- Private/KernelKit/MSDOS.hpp | 2 +- Private/NewBoot/Source/RuntimeMain.cxx | 89 +++++++++++++++++++++++++++++++--- Private/NewBoot/Source/makefile | 2 +- 6 files changed, 102 insertions(+), 15 deletions(-) diff --git a/Private/EFIKit/Api.hxx b/Private/EFIKit/Api.hxx index 7338235a..e91e983d 100644 --- a/Private/EFIKit/Api.hxx +++ b/Private/EFIKit/Api.hxx @@ -11,6 +11,7 @@ #define __EFI_API__ #include +#include #include #include @@ -27,7 +28,9 @@ Void Stop() noexcept; @brief Exit EFI API to let the OS load correctly. Bascially frees everything we have in the EFI side. */ -inline void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle) noexcept { +inline void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle, + HEL::HandoverProc &handOver, + HEL::HandoverInformationHeader *hdr) noexcept { if (!ST) return; /// The MapKey may be invalid. @@ -35,6 +38,8 @@ inline void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle) noexcept { if (ST->BootServices->ExitBootServices(ImageHandle, MapKey) != kEfiOk) { EFI::Stop(); } + + return handOver(hdr); } enum { diff --git a/Private/EFIKit/EFI.hxx b/Private/EFIKit/EFI.hxx index dfaf68eb..3358da44 100644 --- a/Private/EFIKit/EFI.hxx +++ b/Private/EFIKit/EFI.hxx @@ -416,6 +416,11 @@ typedef UInt64(EFI_API *EfiAllocatePages)(EfiAllocateType AllocType, typedef UInt64(EFI_API *EfiFreePages)(EfiPhysicalAddress *Memory, UInt32 Pages); +typedef UInt64(EFI_API *EfiGetMemoryMap)(UInt64 *MapSize, + EfiMemoryDescriptor *DescPtr, + UInt64 *MapKey, UInt64 *DescSize, + UInt64 *DescVersion); + /** * @brief GUID type, something you can also find in CFKit. */ @@ -459,7 +464,7 @@ typedef struct EfiBootServices { UIntPtr RestoreTPL; EfiAllocatePages AllocatePages; EfiFreePages FreePages; - UIntPtr GetMemoryMap; + EfiGetMemoryMap GetMemoryMap; EfiAllocatePool AllocatePool; EfiFreePool FreePool; UIntPtr CreateEvent; @@ -720,4 +725,6 @@ struct EfiFileInfo final { #define EFI_FILE_PROTOCOL_REVISION2 0x00020000 #define EFI_FILE_PROTOCOL_LATEST_REVISION EFI_FILE_PROTOCOL_REVISION2 +#define EFI_EXTRA_DESCRIPTOR_SIZE 8 + #endif // ifndef __EFI__ diff --git a/Private/EFIKit/Handover.hxx b/Private/EFIKit/Handover.hxx index eb79e934..840af2ec 100644 --- a/Private/EFIKit/Handover.hxx +++ b/Private/EFIKit/Handover.hxx @@ -61,22 +61,22 @@ typedef struct HandoverHeader final { } __attribute__((packed)) HandoverHeader, *HandoverHeaderPtr; struct HandoverInformationHeader { - HandoverHeaderPtr f_Header; + HandoverHeader f_Header; voidPtr f_VirtualStart; SizeT f_VirtualSize; voidPtr f_PhysicalStart; SizeT f_PhysicalSize; - Char f_FirmwareVendorName[32]; + WideChar f_FirmwareVendorName[32]; SizeT f_FirmwareVendorLen; voidPtr f_RsdPtr; voidPtr f_SmBIOS; voidPtr f_RTC; voidPtr f_GOP; - voidPtr f_GOPSize; + SizeT f_GOPSize; }; /** @brief Handover Jump Proc */ -typedef UInt64 (*HandoverProc)(HandoverInformationHeader* pHandover); +typedef void (*HandoverProc)(HandoverInformationHeader* pHandover); } // namespace HCore::HEL diff --git a/Private/KernelKit/MSDOS.hpp b/Private/KernelKit/MSDOS.hpp index 2276f3cb..1adf6082 100644 --- a/Private/KernelKit/MSDOS.hpp +++ b/Private/KernelKit/MSDOS.hpp @@ -48,7 +48,7 @@ namespace HCore { inline auto rt_find_exec_header(DosHeaderPtr ptrDos) -> VoidPtr { if (!ptrDos) return nullptr; if (ptrDos->eMagic[0] != kMagMz0) return nullptr; - if (ptrDos->eMagic[0] != kMagMz1) return nullptr; + if (ptrDos->eMagic[1] != kMagMz1) return nullptr; return (VoidPtr)(&ptrDos->eLfanew + 1); } diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx index 3c55c06b..cf1051fd 100644 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ b/Private/NewBoot/Source/RuntimeMain.cxx @@ -26,19 +26,17 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, #ifndef __DEBUG__ - writer.WriteString( - L"MahroussLogic (R) HCore Version 1.00 (Release Channel)\r\n"); + writer.WriteString(L"HCoreLdr: Version 1.00 (Release Channel)\r\n"); #else - writer.WriteString( - L"MahroussLogic (R) HCore Version 1.00 (Insiders Channel)\r\n"); + writer.WriteString(L"HCoreLdr: Version 1.00 (Insiders Channel)\r\n"); #endif const char strDate[] = __DATE__; - writer.WriteString(L"Build Date: "); + writer.WriteString(L"HCoreLdr: Build-Date"); for (auto& ch : strDate) { writer.WriteCharacter(ch); @@ -66,9 +64,84 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, if (ptrHdr && ptrHdr->mMachine == EFI::Platform() && ptrHdr->mMagic == kPeMagic) { if (ptrHdr->mNumberOfSections > 1) { + ExecOptionalHeaderPtr optHdr = reinterpret_cast( + ptrHdr + sizeof(ExecHeader)); + + UInt64 baseCode = optHdr->mBaseOfCode; + UInt64 codeSz = optHdr->mSizeOfCode; + VoidPtr codePtr = nullptr; + + img.File()->SetPosition(img.File(), &baseCode); + + BS->AllocatePool(EfiLoaderCode, codeSz, &codePtr); + + writer.WriteString(L"HCoreLdr: Init [0/2]...\r\n"); + + if (!codePtr) { + EFI::RaiseHardError(L"HCoreLdr-BadAlloc", L"Bad alloc!"); + } + + img.File()->Read(img.File(), &codeSz, codePtr); + + /// GET DATA + + UInt64 baseData = optHdr->mBaseOfData; + UInt64 sizeofData = + optHdr->mSizeOfInitializedData + optHdr->mSizeOfUninitializedData; + VoidPtr dataPtr = nullptr; + + BS->AllocatePool(EfiLoaderCode, sizeofData, &dataPtr); + + if (!dataPtr) { + EFI::RaiseHardError(L"HCoreLdr-BadAlloc", L"Bad alloc!"); + } + + writer.WriteString(L"HCoreLdr: Init [1/2]...\r\n"); + + img.File()->SetPosition(img.File(), &baseData); + + img.File()->Read(img.File(), &sizeofData, dataPtr); + UInt64 MapKey = 0; + UInt64 Size = 0; + EfiMemoryDescriptor* Descriptor = nullptr; + UInt64 SzDesc = 0; + UInt64 RevDesc = 0; + + BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor), + (VoidPtr*)&Descriptor); + + if (BS->GetMemoryMap(&Size, Descriptor, &MapKey, &SzDesc, &RevDesc) != + kEfiOk) { + EFI::RaiseHardError(L"HCoreLdr: Bad-Call", + L"Bad call! (GetMemoryMap)"); + } + + writer.WriteString(L"HCoreLdr: Init [2/2]...\r\n"); + + HEL::HandoverInformationHeader* handoverHdrPtr = nullptr; + + BS->AllocatePool(EfiLoaderData, sizeof(HEL::HandoverInformationHeader), + (VoidPtr*)&handoverHdrPtr); + + HEL::HandoverProc fn = reinterpret_cast( + (UIntPtr)optHdr->mAddressOfEntryPoint); + + handoverHdrPtr->f_GOP = (voidPtr)kGop->Mode->FrameBufferBase; + handoverHdrPtr->f_GOPSize = kGop->Mode->FrameBufferSize; + + handoverHdrPtr->f_PhysicalStart = (voidPtr)Descriptor->PhysicalStart; + handoverHdrPtr->f_PhysicalSize = Descriptor->NumberOfPages; + handoverHdrPtr->f_VirtualStart = (voidPtr)Descriptor->VirtualStart; + handoverHdrPtr->f_VirtualSize = 0; /* not known */ + handoverHdrPtr->f_FirmwareVendorLen = + BStrLen(SystemTable->FirmwareVendor); + + BCopyMem(handoverHdrPtr->f_FirmwareVendorName, + SystemTable->FirmwareVendor, + handoverHdrPtr->f_FirmwareVendorLen); - EFI::ExitBootServices(MapKey, ImageHandle); + EFI::ExitBootServices(MapKey, ImageHandle, fn, handoverHdrPtr); // Launch PE app. @@ -78,9 +151,11 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, } else { writer.WriteString(L"HCoreLdr: Error-Code: HLDR-0001\r\n"); } + } else { + writer.WriteString(L"HCoreLdr: Error-Code: HLDR-0002\r\n"); } } else { - writer.WriteString(L"HCoreLdr: Error-Code: HLDR-0002\r\n"); + writer.WriteString(L"HCoreLdr: Error-Code: HLDR-0003\r\n"); } EFI::Stop(); diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index fab7453d..d17f9671 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -27,7 +27,7 @@ bootloader-amd64: .PHONY: run-efi-amd64 run-efi-amd64: wget https://retrage.github.io/edk2-nightly/bin/DEBUGX64_OVMF.fd -O OVMF.fd - qemu-system-x86_64 -net none -smp 2 -m 4G -M q35 -bios OVMF.fd -drive file=fat:rw:CDROM,index=1,format=raw -serial stdio + qemu-system-x86_64 -net none -smp 2 -m 4G -M q35 -bios OVMF.fd -drive file=fat:rw:CDROM,index=1,format=raw -d int .PHONY: clean clean: -- cgit v1.2.3 From e1b37b42b50e4b72bd49eefa2ff2bcdc16140b5d Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 13 Feb 2024 20:31:23 +0100 Subject: HCR-18 : Update README. Signed-off-by: Amlal El Mahrouss --- ReadMe.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReadMe.md b/ReadMe.md index 3549c26e..201e6406 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -17,7 +17,7 @@ git clone git@github.com:Mahrouss-Logic/h-core.git And execute: ``` -make all +make h-core-- ``` You'd also need The SDK and MinGW, to build and link the components. For Tools look at `Public/Tools` -- cgit v1.2.3 From 2eb529e37a5e10ea2483fce04de778ac13be6f1d Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 14 Feb 2024 09:38:17 +0100 Subject: HCR-18: Fixed UEFI bootloader, loading stuff correctly. Signed-off-by: Amlal El Mahrouss --- Private/EFIKit/Api.hxx | 15 ++--- Private/EFIKit/EFI.hxx | 6 +- Private/EFIKit/Handover.hxx | 2 + .../HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp | 4 +- Private/HALKit/AMD64/HalDebugOutput.cxx | 2 +- Private/HALKit/AMD64/HalPageAlloc.hpp | 18 ++++-- Private/HALKit/PowerPC/HalHardware.cxx | 4 +- Private/NewBoot/BootKit/BootKit.hxx | 5 ++ Private/NewBoot/Source/RuntimeMain.cxx | 74 ++++++++++++++-------- Private/NewKit/Json.hpp | 2 +- Private/NewKit/Utils.hpp | 35 +++++----- Private/Source/Network/IP.cpp | 2 +- Private/Source/PEFSharedObjectRT.cxx | 2 +- Private/Source/String.cxx | 46 +++++++------- Private/Source/URL.cxx | 10 +-- Private/Source/UserHeap.cxx | 4 +- Private/Source/Utils.cxx | 14 ++-- Private/Source/compile_flags.txt | 1 + Private/makefile | 2 +- 19 files changed, 137 insertions(+), 111 deletions(-) diff --git a/Private/EFIKit/Api.hxx b/Private/EFIKit/Api.hxx index e91e983d..d2cb73f8 100644 --- a/Private/EFIKit/Api.hxx +++ b/Private/EFIKit/Api.hxx @@ -11,7 +11,6 @@ #define __EFI_API__ #include -#include #include #include @@ -28,18 +27,10 @@ Void Stop() noexcept; @brief Exit EFI API to let the OS load correctly. Bascially frees everything we have in the EFI side. */ -inline void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle, - HEL::HandoverProc &handOver, - HEL::HandoverInformationHeader *hdr) noexcept { +inline void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle) noexcept { if (!ST) return; - /// The MapKey may be invalid. - /// If so, then hang the computer. - if (ST->BootServices->ExitBootServices(ImageHandle, MapKey) != kEfiOk) { - EFI::Stop(); - } - - return handOver(hdr); + ST->BootServices->ExitBootServices(ImageHandle, MapKey); } enum { @@ -90,4 +81,6 @@ inline void InitEFI(EfiSystemTable *SystemTable) noexcept { #endif // ifdef __BOOTLOADER__ +#define kHCoreSubsystem 17 + #endif /* ifndef __EFI_API__ */ diff --git a/Private/EFIKit/EFI.hxx b/Private/EFIKit/EFI.hxx index 3358da44..f2810c8c 100644 --- a/Private/EFIKit/EFI.hxx +++ b/Private/EFIKit/EFI.hxx @@ -416,10 +416,10 @@ typedef UInt64(EFI_API *EfiAllocatePages)(EfiAllocateType AllocType, typedef UInt64(EFI_API *EfiFreePages)(EfiPhysicalAddress *Memory, UInt32 Pages); -typedef UInt64(EFI_API *EfiGetMemoryMap)(UInt64 *MapSize, +typedef UInt64(EFI_API *EfiGetMemoryMap)(UInt32 *MapSize, EfiMemoryDescriptor *DescPtr, - UInt64 *MapKey, UInt64 *DescSize, - UInt64 *DescVersion); + UInt32 *MapKey, UInt32 *DescSize, + UInt32 *DescVersion); /** * @brief GUID type, something you can also find in CFKit. diff --git a/Private/EFIKit/Handover.hxx b/Private/EFIKit/Handover.hxx index 840af2ec..f9b8044f 100644 --- a/Private/EFIKit/Handover.hxx +++ b/Private/EFIKit/Handover.hxx @@ -73,6 +73,8 @@ struct HandoverInformationHeader { voidPtr f_RTC; voidPtr f_GOP; SizeT f_GOPSize; + SizeT f_HeapCommitSize; + SizeT f_StackCommitSize; }; /** diff --git a/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp b/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp index 1e53140f..73c995c4 100644 --- a/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp +++ b/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp @@ -96,8 +96,8 @@ static Madt kApicMadtList[256]; Madt* system_find_core(Madt* madt) { madt = madt + sizeof(Madt); - if (string_compare(madt->fMag, kApicSignature, - string_length(kApicSignature)) == 0) + if (rt_string_cmp(madt->fMag, kApicSignature, + rt_string_len(kApicSignature)) == 0) return madt; return nullptr; diff --git a/Private/HALKit/AMD64/HalDebugOutput.cxx b/Private/HALKit/AMD64/HalDebugOutput.cxx index ac16cf70..ba8770fe 100644 --- a/Private/HALKit/AMD64/HalDebugOutput.cxx +++ b/Private/HALKit/AMD64/HalDebugOutput.cxx @@ -60,7 +60,7 @@ void ke_io_print(const char *bytes) { Detail::kState = kStateTransmit; SizeT index = 0; - SizeT len = string_length(bytes, 256); + SizeT len = rt_string_len(bytes, 256); while (index < len) { HAL::Out8(Detail::PORT, bytes[index]); diff --git a/Private/HALKit/AMD64/HalPageAlloc.hpp b/Private/HALKit/AMD64/HalPageAlloc.hpp index d6912318..9ca2c580 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.hpp +++ b/Private/HALKit/AMD64/HalPageAlloc.hpp @@ -17,13 +17,17 @@ #include -#ifndef PTE_MAX -#define PTE_MAX (0x200) -#endif //! PTE_MAX +#ifndef kPTEMax +#define kPTEMax (0x200) +#endif //! kPTEMax -#ifndef PTE_ALIGN -#define PTE_ALIGN (0x1000) -#endif //! PTE_ALIGN +#ifndef kPTEAlign +#define kPTEAlign (0x1000) +#endif //! kPTEAlign + +#ifndef kPTESize +#define kPTESize (0x1000) +#endif // !kPTESize extern "C" void flush_tlb(HCore::UIntPtr VirtualAddr); extern "C" void write_cr3(HCore::UIntPtr pde); @@ -68,7 +72,7 @@ inline UInt8 control_register_cast(ControlRegisterBits reg) { } // namespace Detail struct PageDirectory64 final { - PageTable64 ALIGN(PTE_ALIGN) Pte[PTE_MAX]; + PageTable64 ALIGN(kPTEAlign) Pte[kPTEMax]; }; PageTable64* hal_alloc_page(SizeT sz, Boolean rw, Boolean user); diff --git a/Private/HALKit/PowerPC/HalHardware.cxx b/Private/HALKit/PowerPC/HalHardware.cxx index 89b1a6ae..a5f11449 100644 --- a/Private/HALKit/PowerPC/HalHardware.cxx +++ b/Private/HALKit/PowerPC/HalHardware.cxx @@ -13,7 +13,7 @@ extern "C" void flush_tlb() {} extern "C" void rt_wait_for_io() {} -extern "C" HCore::HAL::StackFrame* rt_get_current_context() {} +extern "C" HCore::HAL::StackFrame* rt_get_current_context() { return nullptr; } namespace HCore { namespace HAL { @@ -37,7 +37,7 @@ void ke_com_print(const Char* bytes) { if (!bytes) return; SizeT index = 0; - SizeT len = string_length(bytes, 256); + SizeT len = rt_string_len(bytes, 256); while (index < len) { // TODO diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index 0c747643..d8865712 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -80,6 +80,11 @@ class BFileReader final { EfiFileProtocolPtr File() { return mFile; } UInt64 &Size() { return mSizeFile; } + UInt64 &Size(const UInt64 &Sz) { + mSizeFile = Sz; + return mSizeFile; + } + public: BFileReader &operator=(const BFileReader &) = default; BFileReader(const BFileReader &) = default; diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx index cf1051fd..4c9d3b8f 100644 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ b/Private/NewBoot/Source/RuntimeMain.cxx @@ -15,6 +15,16 @@ #include #include +#ifdef __x86_64__ + +#include + +#else + +#error Unknown CPU. + +#endif + #define kBufferReadSz 2048 EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, @@ -36,7 +46,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, const char strDate[] = __DATE__; - writer.WriteString(L"HCoreLdr: Build-Date"); + writer.WriteString(L"HCoreLdr: Build date: "); for (auto& ch : strDate) { writer.WriteCharacter(ch); @@ -46,13 +56,9 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, .WriteString(SystemTable->FirmwareVendor) .WriteString(L"\r\n"); - writer.WriteString(L"HCoreLdr: Reading: ") - .WriteString(L"HCOREKRNL.EXE") - .WriteString(L"\r\n"); - BFileReader img(L"HCOREKRNL.EXE", ImageHandle); - img.Size() = kBufferReadSz; + img.Size(kBufferReadSz); img.Read(); if (img.Error() == BFileReader::kOperationOkay) { @@ -78,7 +84,8 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, writer.WriteString(L"HCoreLdr: Init [0/2]...\r\n"); if (!codePtr) { - EFI::RaiseHardError(L"HCoreLdr-BadAlloc", L"Bad alloc!"); + EFI::RaiseHardError(L"HCoreLdr-BadAlloc", + L"Bad Alloc! (AllocatePool)"); } img.File()->Read(img.File(), &codeSz, codePtr); @@ -93,7 +100,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, BS->AllocatePool(EfiLoaderCode, sizeofData, &dataPtr); if (!dataPtr) { - EFI::RaiseHardError(L"HCoreLdr-BadAlloc", L"Bad alloc!"); + EFI::RaiseHardError(L"HCoreLdr: BadAlloc", L"(AllocatePool)"); } writer.WriteString(L"HCoreLdr: Init [1/2]...\r\n"); @@ -102,19 +109,30 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, img.File()->Read(img.File(), &sizeofData, dataPtr); - UInt64 MapKey = 0; - UInt64 Size = 0; + UInt32 MapKey = 0; + UInt32* Size = 0; EfiMemoryDescriptor* Descriptor = nullptr; - UInt64 SzDesc = 0; - UInt64 RevDesc = 0; + UInt32 SzDesc = 0; + UInt32 RevDesc = 0; - BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor), - (VoidPtr*)&Descriptor); + if (BS->AllocatePool(EfiLoaderData, sizeof(UInt64), (VoidPtr*)&Size) != + kEfiOk) { + EFI::RaiseHardError(L"HCoreLdr-BadAlloc", + L"Bad Alloc! (AllocatePool)"); + } + + *Size = sizeof(EfiMemoryDescriptor); + + if (BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor), + (VoidPtr*)&Descriptor) != kEfiOk) { + EFI::RaiseHardError(L"HCoreLdr-BadAlloc", + L"Bad Alloc! (AllocatePool)"); + } - if (BS->GetMemoryMap(&Size, Descriptor, &MapKey, &SzDesc, &RevDesc) != + if (BS->GetMemoryMap(Size, Descriptor, &MapKey, &SzDesc, &RevDesc) != kEfiOk) { - EFI::RaiseHardError(L"HCoreLdr: Bad-Call", - L"Bad call! (GetMemoryMap)"); + EFI::RaiseHardError(L"HCoreLdr-BadAlloc", + L"Bad Alloc! (GetMemoryMap)"); } writer.WriteString(L"HCoreLdr: Init [2/2]...\r\n"); @@ -124,16 +142,19 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, BS->AllocatePool(EfiLoaderData, sizeof(HEL::HandoverInformationHeader), (VoidPtr*)&handoverHdrPtr); - HEL::HandoverProc fn = reinterpret_cast( - (UIntPtr)optHdr->mAddressOfEntryPoint); - handoverHdrPtr->f_GOP = (voidPtr)kGop->Mode->FrameBufferBase; handoverHdrPtr->f_GOPSize = kGop->Mode->FrameBufferSize; - handoverHdrPtr->f_PhysicalStart = (voidPtr)Descriptor->PhysicalStart; - handoverHdrPtr->f_PhysicalSize = Descriptor->NumberOfPages; - handoverHdrPtr->f_VirtualStart = (voidPtr)Descriptor->VirtualStart; - handoverHdrPtr->f_VirtualSize = 0; /* not known */ + handoverHdrPtr->f_PhysicalStart = + reinterpret_cast(Descriptor->PhysicalStart); + handoverHdrPtr->f_PhysicalSize = Descriptor->NumberOfPages * kPTESize; + + handoverHdrPtr->f_VirtualStart = + reinterpret_cast(Descriptor->VirtualStart); + + handoverHdrPtr->f_VirtualSize = + Descriptor->NumberOfPages; /* # of pages */ + handoverHdrPtr->f_FirmwareVendorLen = BStrLen(SystemTable->FirmwareVendor); @@ -141,9 +162,10 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, SystemTable->FirmwareVendor, handoverHdrPtr->f_FirmwareVendorLen); - EFI::ExitBootServices(MapKey, ImageHandle, fn, handoverHdrPtr); + handoverHdrPtr->f_HeapCommitSize = optHdr->mSizeOfHeapCommit; + handoverHdrPtr->f_StackCommitSize = optHdr->mSizeOfStackCommit; - // Launch PE app. + EFI::ExitBootServices(MapKey, ImageHandle); EFI::Stop(); diff --git a/Private/NewKit/Json.hpp b/Private/NewKit/Json.hpp index 1c404397..3b201ecc 100644 --- a/Private/NewKit/Json.hpp +++ b/Private/NewKit/Json.hpp @@ -44,7 +44,7 @@ class JsonType final { struct JsonStreamTraits final { JsonType In(const char *full_array) { - SizeT len = string_length(full_array); + SizeT len = rt_string_len(full_array); if (full_array[0] == '\"' && full_array[len - 1] == ',' || full_array[len - 1] == '\"') { diff --git a/Private/NewKit/Utils.hpp b/Private/NewKit/Utils.hpp index c02e071f..dc3b1434 100644 --- a/Private/NewKit/Utils.hpp +++ b/Private/NewKit/Utils.hpp @@ -12,21 +12,20 @@ #include -namespace HCore -{ - Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len); - Int rt_move_memory(const voidPtr src, voidPtr dst, Size len); - voidPtr rt_set_memory(voidPtr dst, Char val, Size len); - void rt_zero_memory(voidPtr pointer, Size len); - Int string_compare(const Char *src, const Char *cmp, Size len); - const Char *alloc_string(const Char *text); - Size string_length(const Char *str); - Size string_length(const Char *str, SizeT _len); - Boolean rt_to_string(Char *buf, Int limit, Int base); - Boolean is_newln(Char chr); - Boolean is_space(Char chr); - Int rt_to_uppercase(Int c); - Int rt_to_lower(Int c); - voidPtr rt_string_in_string(const char* in, const char* needle); - char* rt_string_from_char(char* str, const char chr); -} // namespace HCore +namespace HCore { +Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len); +Int rt_move_memory(const voidPtr src, voidPtr dst, Size len); +voidPtr rt_set_memory(voidPtr dst, Char val, Size len); +void rt_zero_memory(voidPtr pointer, Size len); +Int rt_string_cmp(const Char *src, const Char *cmp, Size len); +const Char *alloc_string(const Char *text); +Size rt_string_len(const Char *str); +Size rt_string_len(const Char *str, SizeT _len); +Boolean rt_to_string(Char *buf, Int limit, Int base); +Boolean is_newln(Char chr); +Boolean is_space(Char chr); +Int rt_to_uppercase(Int c); +Int rt_to_lower(Int c); +voidPtr rt_string_in_string(const char *in, const char *needle); +char *rt_string_from_char(char *str, const char chr); +} // namespace HCore diff --git a/Private/Source/Network/IP.cpp b/Private/Source/Network/IP.cpp index 38cd6008..b1b45521 100644 --- a/Private/Source/Network/IP.cpp +++ b/Private/Source/Network/IP.cpp @@ -82,7 +82,7 @@ ErrorOr IPFactory::ToStringView(Ref ipv4) { bool IPFactory::IpCheckVersion4(const char* ip) { int cnter = 0; - for (Size base = 0; base < string_length(ip); ++base) { + for (Size base = 0; base < rt_string_len(ip); ++base) { if (ip[base] == '.') { cnter = 0; } else { diff --git a/Private/Source/PEFSharedObjectRT.cxx b/Private/Source/PEFSharedObjectRT.cxx index c6bec404..aceca259 100644 --- a/Private/Source/PEFSharedObjectRT.cxx +++ b/Private/Source/PEFSharedObjectRT.cxx @@ -64,7 +64,7 @@ extern "C" SharedObject *__LibInit() { } library->Get()->fImageEntrypointOffset = - library->Load(kPefStart, string_length(kPefStart, 0), kPefCode); + library->Load(kPefStart, rt_string_len(kPefStart, 0), kPefCode); kcout << "__LibInit: Task is successful!\n"; diff --git a/Private/Source/String.cxx b/Private/Source/String.cxx index 045545cd..0e1b7f41 100644 --- a/Private/Source/String.cxx +++ b/Private/Source/String.cxx @@ -15,7 +15,7 @@ Char *StringView::Data() { return m_Data; } const Char *StringView::CData() { return m_Data; } -Size StringView::Length() const { return string_length(m_Data); } +Size StringView::Length() const { return rt_string_len(m_Data); } bool StringView::operator==(const StringView &rhs) const { if (rhs.Length() != this->Length()) return false; @@ -28,9 +28,9 @@ bool StringView::operator==(const StringView &rhs) const { } bool StringView::operator==(const Char *rhs) const { - if (string_length(rhs) != this->Length()) return false; + if (rt_string_len(rhs) != this->Length()) return false; - for (Size index = 0; index < string_length(rhs); ++index) { + for (Size index = 0; index < rt_string_len(rhs); ++index) { if (rhs[index] != m_Data[index]) return false; } @@ -48,9 +48,9 @@ bool StringView::operator!=(const StringView &rhs) const { } bool StringView::operator!=(const Char *rhs) const { - if (string_length(rhs) != this->Length()) return false; + if (rt_string_len(rhs) != this->Length()) return false; - for (Size index = 0; index < string_length(rhs); ++index) { + for (Size index = 0; index < rt_string_len(rhs); ++index) { if (rhs[index] == m_Data[index]) return false; } @@ -60,7 +60,7 @@ bool StringView::operator!=(const Char *rhs) const { ErrorOr StringBuilder::Construct(const Char *data) { if (!data || *data == 0) return {}; - StringView view(string_length(data)); + StringView view(rt_string_len(data)); rt_copy_memory(reinterpret_cast(const_cast(data)), reinterpret_cast(view.Data()), view.Length()); @@ -71,7 +71,7 @@ ErrorOr StringBuilder::Construct(const Char *data) { const char *StringBuilder::FromInt(const char *fmt, int i) { if (!fmt) return ("-1"); - char *ret = (char *)__alloca(sizeof(char) * 8 + string_length(fmt)); + char *ret = (char *)__alloca(sizeof(char) * 8 + rt_string_len(fmt)); if (!ret) return ("-1"); @@ -81,8 +81,8 @@ const char *StringBuilder::FromInt(const char *fmt, int i) { return ("-1"); } - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(result); + const auto fmt_len = rt_string_len(fmt); + const auto res_len = rt_string_len(result); for (Size idx = 0; idx < fmt_len; ++idx) { if (fmt[idx] == '%') { @@ -106,12 +106,12 @@ const char *StringBuilder::FromBool(const char *fmt, bool i) { if (!fmt) return ("?"); const char *boolean_expr = i ? "true" : "false"; - char *ret = (char *)__alloca((sizeof(char) * i) ? 4 : 5 + string_length(fmt)); + char *ret = (char *)__alloca((sizeof(char) * i) ? 4 : 5 + rt_string_len(fmt)); if (!ret) return ("?"); - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(boolean_expr); + const auto fmt_len = rt_string_len(fmt); + const auto res_len = rt_string_len(boolean_expr); for (Size idx = 0; idx < fmt_len; ++idx) { if (fmt[idx] == '%') { @@ -132,9 +132,9 @@ const char *StringBuilder::FromBool(const char *fmt, bool i) { } bool StringBuilder::Equals(const char *lhs, const char *rhs) { - if (string_length(rhs) != string_length(lhs)) return false; + if (rt_string_len(rhs) != rt_string_len(lhs)) return false; - for (Size index = 0; index < string_length(rhs); ++index) { + for (Size index = 0; index < rt_string_len(rhs); ++index) { if (rhs[index] != lhs[index]) return false; } @@ -145,14 +145,14 @@ const char *StringBuilder::Format(const char *fmt, const char *fmt2) { if (!fmt || !fmt2) return ("?"); char *ret = - (char *)alloca(sizeof(char) * string_length(fmt2) + string_length(fmt2)); + (char *)alloca(sizeof(char) * rt_string_len(fmt2) + rt_string_len(fmt2)); if (!ret) return ("?"); - for (Size idx = 0; idx < string_length(fmt); ++idx) { + for (Size idx = 0; idx < rt_string_len(fmt); ++idx) { if (fmt[idx] == '%') { Size result_cnt = idx; - for (Size y_idx = 0; y_idx < string_length(fmt2); ++y_idx) { + for (Size y_idx = 0; y_idx < rt_string_len(fmt2); ++y_idx) { ret[result_cnt] = fmt2[y_idx]; ++result_cnt; } @@ -167,27 +167,27 @@ const char *StringBuilder::Format(const char *fmt, const char *fmt2) { } static void string_append(char *lhs, char *rhs, int cur) { - if (lhs && rhs && cur < string_length(lhs)) { - SizeT sz_rhs = string_length(rhs); + if (lhs && rhs && cur < rt_string_len(lhs)) { + SizeT sz_rhs = rt_string_len(rhs); rt_copy_memory(rhs, lhs + cur, sz_rhs); } } StringView &StringView::operator+=(const Char *rhs) { - if (string_length(rhs) > string_length(this->m_Data)) return *this; + if (rt_string_len(rhs) > rt_string_len(this->m_Data)) return *this; string_append(this->m_Data, const_cast(rhs), this->m_Cur); - this->m_Cur += string_length(rhs); + this->m_Cur += rt_string_len(rhs); return *this; } StringView &StringView::operator+=(const StringView &rhs) { - if (string_length(rhs.m_Data) > string_length(this->m_Data)) return *this; + if (rt_string_len(rhs.m_Data) > rt_string_len(this->m_Data)) return *this; string_append(this->m_Data, const_cast(rhs.m_Data), this->m_Cur); - this->m_Cur += string_length(const_cast(rhs.m_Data)); + this->m_Cur += rt_string_len(const_cast(rhs.m_Data)); return *this; } diff --git a/Private/Source/URL.cxx b/Private/Source/URL.cxx index ab289a66..e471c0c1 100644 --- a/Private/Source/URL.cxx +++ b/Private/Source/URL.cxx @@ -32,19 +32,19 @@ constexpr const int kProtosCount = 8; constexpr const int kRangeSz = 4096; static ErrorOr url_extract_location(const char *url) { - if (!url || *url == 0 || string_length(url, kRangeSz) > kRangeSz) + if (!url || *url == 0 || rt_string_len(url, kRangeSz) > kRangeSz) return ErrorOr{-1}; - StringView view(string_length(url)); + StringView view(rt_string_len(url)); SizeT i = 0; bool scheme_found = false; - for (; i < string_length(url); ++i) { + for (; i < rt_string_len(url); ++i) { if (!scheme_found) { for (int y = 0; kProtosCount; ++y) { if (rt_string_in_string(view.CData(), kURLProtocols[y])) { - i += string_length(kURLProtocols[y]) + kUrlOutSz; + i += rt_string_len(kURLProtocols[y]) + kUrlOutSz; scheme_found = true; break; @@ -59,7 +59,7 @@ static ErrorOr url_extract_location(const char *url) { } static ErrorOr url_extract_protocol(const char *url) { - if (!url || *url == 0 || string_length(url, kRangeSz) > kRangeSz) + if (!url || *url == 0 || rt_string_len(url, kRangeSz) > kRangeSz) return ErrorOr{-1}; ErrorOr view{-1}; diff --git a/Private/Source/UserHeap.cxx b/Private/Source/UserHeap.cxx index 5b086111..9b985ed4 100644 --- a/Private/Source/UserHeap.cxx +++ b/Private/Source/UserHeap.cxx @@ -46,7 +46,7 @@ Boolean HeapManager::s_PoolsAreEnabled = true; Array, kPoolMaxSz> HeapManager::s_Pool; STATIC VoidPtr ke_find_unused_heap(Int flags); -STATIC void ke_free_heap_internal(VoidPtr vaddr); +STATIC Void ke_free_heap_internal(VoidPtr vaddr); STATIC VoidPtr ke_make_heap(VoidPtr vaddr, Int flags); STATIC Boolean ke_check_and_free_heap(const SizeT& index, VoidPtr ptr); @@ -92,7 +92,7 @@ STATIC VoidPtr ke_make_heap(VoidPtr virtualAddress, Int flags) { return nullptr; } -STATIC void ke_free_heap_internal(VoidPtr virtualAddress) { +STATIC Void ke_free_heap_internal(VoidPtr virtualAddress) { HeapHeader* poolHdr = reinterpret_cast( reinterpret_cast(virtualAddress) - sizeof(HeapHeader)); diff --git a/Private/Source/Utils.cxx b/Private/Source/Utils.cxx index 6072ccd3..01975192 100644 --- a/Private/Source/Utils.cxx +++ b/Private/Source/Utils.cxx @@ -10,7 +10,7 @@ #include namespace HCore { -Int string_compare(const Char *src, const Char *cmp, Size size) { +Int rt_string_cmp(const Char *src, const Char *cmp, Size size) { Int32 counter = 0; for (Size index = 0; index < size; ++index) { @@ -24,7 +24,7 @@ void rt_zero_memory(voidPtr pointer, Size len) { rt_set_memory(pointer, 0, len); } -Size string_length(const Char *str, SizeT _len) { +Size rt_string_len(const Char *str, SizeT _len) { if (*str == '\0') return 0; Size len{0}; @@ -39,7 +39,7 @@ Size string_length(const Char *str, SizeT _len) { return len; } -Size string_length(const Char *str) { +Size rt_string_len(const Char *str) { if (*str == '\0') return 0; Size len{0}; @@ -98,12 +98,12 @@ Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len) { const Char *alloc_string(const Char *text) { if (!text) return nullptr; - const Char *string = new Char[string_length(text)]; + const Char *string = new Char[rt_string_len(text)]; if (!string) return nullptr; voidPtr vText = reinterpret_cast(const_cast(text)); voidPtr vStr = reinterpret_cast(const_cast(string)); - rt_copy_memory(vText, vStr, string_length(text)); + rt_copy_memory(vText, vStr, rt_string_len(text)); return string; } @@ -144,8 +144,8 @@ Boolean is_space(Char chr) { return chr == ' '; } Boolean is_newln(Char chr) { return chr == '\n'; } voidPtr rt_string_in_string(const char *in, const char *needle) { - for (size_t i = 0; i < string_length(in); ++i) { - if (string_compare(in + i, needle, string_length(needle)) == 0) + for (size_t i = 0; i < rt_string_len(in); ++i) { + if (rt_string_cmp(in + i, needle, rt_string_len(needle)) == 0) return reinterpret_cast(const_cast(in + i)); } diff --git a/Private/Source/compile_flags.txt b/Private/Source/compile_flags.txt index 1b0ad789..b2809811 100644 --- a/Private/Source/compile_flags.txt +++ b/Private/Source/compile_flags.txt @@ -3,3 +3,4 @@ -std=c++20 -I../ -I$(HOME)/ +-D__USE_NEWFS__ diff --git a/Private/makefile b/Private/makefile index 8ea6e7f3..1296117b 100644 --- a/Private/makefile +++ b/Private/makefile @@ -8,7 +8,7 @@ LD = x86_64-w64-mingw32-ld CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__USE_NEWFS__ -D__HAVE_HCORE_APIS__ -D__HCORE__ -I../ -I./ -I$(HOME)/ ASM = nasm ASMFLAGS = -f win64 -LDFLAGS = -e Main -shared --subsystem=10 +LDFLAGS = -e Main -shared --subsystem=17 LDOBJ = $(wildcard Obj/*.obj) # This file is the kernel, responsible of task management, memory, drivers and more. -- cgit v1.2.3 From f57ffb30b78efffde3b16572798d2b2dcffd04eb Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 14 Feb 2024 10:52:42 +0100 Subject: HCR-18: add TODO, ProcessManager.cxx line 141 Signed-off-by: Amlal El Mahrouss --- Private/Source/ProcessManager.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Private/Source/ProcessManager.cxx b/Private/Source/ProcessManager.cxx index 82b07c42..07f73ef3 100644 --- a/Private/Source/ProcessManager.cxx +++ b/Private/Source/ProcessManager.cxx @@ -138,7 +138,7 @@ void Process::Exit(Int32 exit_code) { this->Pool = nullptr; this->PoolCursor = nullptr; - this->FreeMemory = kPoolMaxSz; + this->FreeMemory = 0UL; // TODO: fill available heap. this->UsedMemory = 0UL; } -- cgit v1.2.3 From c49a4199373d546703ea2016f188131648a39a68 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 14 Feb 2024 11:52:34 +0100 Subject: HCR-18: Heap and scheduler improvements. Signed-off-by: Amlal El Mahrouss --- Private/KernelKit/ProcessManager.hpp | 12 +++++++++-- Private/NewKit/Macros.hpp | 4 ++++ Private/Source/ProcessManager.cxx | 41 ++++++++++++++++++++++++------------ Private/Source/UserHeap.cxx | 4 ++-- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/Private/KernelKit/ProcessManager.hpp b/Private/KernelKit/ProcessManager.hpp index e6d86959..543b3007 100644 --- a/Private/KernelKit/ProcessManager.hpp +++ b/Private/KernelKit/ProcessManager.hpp @@ -130,17 +130,25 @@ class Process final { ProcessStatus Status; // Memory, images. - HeapPtr PoolCursor{nullptr}; + HeapPtr HeapCursor{nullptr}; ImagePtr Image{nullptr}; - HeapPtr Pool{nullptr}; + HeapPtr HeapPtr{nullptr}; // memory usage SizeT UsedMemory{0}; SizeT FreeMemory{0}; + enum { + ExecutableType, + DLLType, + DriverType, + TypeCount, + }; + ProcessTime PTime; PID ProcessId{-1}; Int32 Ring{3}; + Int32 Kind{0}; public: //! @brief boolean operator, check status. diff --git a/Private/NewKit/Macros.hpp b/Private/NewKit/Macros.hpp index 739447b0..ecd2d678 100644 --- a/Private/NewKit/Macros.hpp +++ b/Private/NewKit/Macros.hpp @@ -89,3 +89,7 @@ #define EXTERN extern #define STATIC static + +#ifndef self +#define self this +#endif diff --git a/Private/Source/ProcessManager.cxx b/Private/Source/ProcessManager.cxx index 07f73ef3..ccf037ae 100644 --- a/Private/Source/ProcessManager.cxx +++ b/Private/Source/ProcessManager.cxx @@ -18,6 +18,8 @@ #include #include +#include "NewKit/RuntimeCheck.hpp" + ///! bugs = 0 /***********************************************************************************/ @@ -54,10 +56,9 @@ void Process::Wake(const bool should_wakeup) { VoidPtr Process::New(const SizeT &sz) { if (this->FreeMemory < 1) return nullptr; - // RAM allocation - if (this->PoolCursor) { - VoidPtr ptr = this->PoolCursor; - this->PoolCursor = (VoidPtr)((UIntPtr)this->PoolCursor + (sizeof(sz))); + if (this->HeapCursor) { + VoidPtr ptr = this->HeapCursor; + this->HeapCursor = (VoidPtr)((UIntPtr)this->HeapCursor + (sizeof(sz))); ++this->UsedMemory; --this->FreeMemory; @@ -86,13 +87,13 @@ bool rt_in_pool_region(VoidPtr pool_ptr, VoidPtr pool, const SizeT &sz) { /* @brief free pointer from usage. */ Boolean Process::Delete(VoidPtr ptr, const SizeT &sz) { - if (sz < 1 || this->PoolCursor == this->Pool) return false; + if (sz < 1 || this->HeapCursor == this->HeapPtr) return false; // also check for the amount of allocations we've done so far. if (this->UsedMemory < 1) return false; - if (rt_in_pool_region(ptr, this->PoolCursor, this->UsedMemory)) { - this->PoolCursor = (VoidPtr)((UIntPtr)this->PoolCursor - (sizeof(sz))); + if (rt_in_pool_region(ptr, this->HeapCursor, this->UsedMemory)) { + this->HeapCursor = (VoidPtr)((UIntPtr)this->HeapCursor - (sizeof(sz))); rt_zero_memory(ptr, sz); ++this->FreeMemory; @@ -131,12 +132,11 @@ void Process::Exit(Int32 exit_code) { kExitCode = exit_code; - if (this->Ring != (Int32)ProcessSelector::kRingDriver && - this->Ring != (Int32)ProcessSelector::kRingKernel) { - if (this->Pool) ke_free_heap(this->Pool); + if (this->Ring != (Int32)ProcessSelector::kRingDriver) { + if (this->HeapPtr) ke_free_heap(this->HeapPtr); - this->Pool = nullptr; - this->PoolCursor = nullptr; + this->HeapPtr = nullptr; + this->HeapCursor = nullptr; this->FreeMemory = 0UL; // TODO: fill available heap. this->UsedMemory = 0UL; @@ -155,11 +155,13 @@ void Process::Exit(Int32 exit_code) { bool ProcessManager::Add(Ref &process) { if (!process) return false; + if (process.Leak().Ring != (Int32)ProcessSelector::kRingKernel) return false; + kcout << "ProcessManager::Add(Ref& process)\r\n"; - process.Leak().Pool = ke_new_heap(kPoolUser | kPoolRw); + process.Leak().HeapPtr = ke_new_heap(kPoolUser | kPoolRw); process.Leak().ProcessId = this->m_Headers.Count(); - process.Leak().PoolCursor = process.Leak().Pool; + process.Leak().HeapCursor = process.Leak().HeapPtr; process.Leak().StackFrame = reinterpret_cast( ke_new_ke_heap(sizeof(HAL::StackFrame), true, false)); @@ -172,6 +174,17 @@ bool ProcessManager::Add(Ref &process) { this->m_Headers.Add(process); + if (!imageStart && process.Leak().Kind == Process::ExecutableType) { + process.Leak().Crash(); + } + + if (!imageStart && process.Leak().Kind == Process::DriverType) { + if (process.Leak().Ring == 3) + process.Leak().Crash(); + else + ke_stop(RUNTIME_CHECK_PROCESS); + } + return true; } diff --git a/Private/Source/UserHeap.cxx b/Private/Source/UserHeap.cxx index 9b985ed4..d3d60f6b 100644 --- a/Private/Source/UserHeap.cxx +++ b/Private/Source/UserHeap.cxx @@ -75,7 +75,7 @@ STATIC VoidPtr ke_make_heap(VoidPtr virtualAddress, Int flags) { HeapHeader* poolHdr = reinterpret_cast(virtualAddress); if (!poolHdr->Free) { - kcout << "[ke_make_heap] poolHdr->Free, Pool already exists\n"; + kcout << "[ke_make_heap] poolHdr->Free, HeapPtr already exists\n"; return nullptr; } @@ -115,7 +115,7 @@ STATIC Boolean ke_check_and_free_heap(const SizeT& index, VoidPtr ptr) { if (HeapManager::The()[index]) { // ErrorOr<>::operator Boolean /// if (address matches) - /// -> Free Pool. + /// -> Free heap. if (HeapManager::The()[index].Leak().Leak().Leak()->VirtualAddress() == (UIntPtr)ptr) { HeapManager::Leak().Leak().FreePage( -- cgit v1.2.3 From 41b1d0483dead754bb054bb4139cbc69fec84f36 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 14 Feb 2024 13:06:07 +0100 Subject: HCR-18: NewBoot: Getting more work done... Signed-off-by: Amlal El Mahrouss --- Private/HALKit/AMD64/HalStartSequence.asm | 4 --- Private/NewBoot/Source/RuntimeMain.cxx | 54 +++++++++++++++---------------- Private/NewBoot/Source/makefile | 2 +- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/Private/HALKit/AMD64/HalStartSequence.asm b/Private/HALKit/AMD64/HalStartSequence.asm index c8e0a4e2..1db3b218 100644 --- a/Private/HALKit/AMD64/HalStartSequence.asm +++ b/Private/HALKit/AMD64/HalStartSequence.asm @@ -31,10 +31,6 @@ section .text ;; Just a simple setup, we'd also need to tell some before Main: - push rcx - jmp RuntimeMain - pop rcx -L0: cli hlt jmp $ diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx index 4c9d3b8f..afca21eb 100644 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ b/Private/NewBoot/Source/RuntimeMain.cxx @@ -7,6 +7,7 @@ * ======================================================== */ +#include "EFIKit/EFI.hxx" #define __BOOTLOADER__ 1 #include @@ -25,7 +26,7 @@ #endif -#define kBufferReadSz 2048 +#define kBufferReadSz 4096 EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, EfiSystemTable* SystemTable) { @@ -73,42 +74,35 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, ExecOptionalHeaderPtr optHdr = reinterpret_cast( ptrHdr + sizeof(ExecHeader)); - UInt64 baseCode = optHdr->mBaseOfCode; - UInt64 codeSz = optHdr->mSizeOfCode; - VoidPtr codePtr = nullptr; + UInt64 baseSec = ptrHdr->mNumberOfSections; + img.File()->SetPosition(img.File(), &baseSec); - img.File()->SetPosition(img.File(), &baseCode); + baseSec = ptrHdr->mNumberOfSections; + ExecSectionHeaderPtr headers = + (ExecSectionHeaderPtr)(&ptrHdr->mCharacteristics + 1); - BS->AllocatePool(EfiLoaderCode, codeSz, &codePtr); + for (int i = 0u; i < ptrHdr->mNumberOfSections; ++i) { + auto& hdr = headers[i]; - writer.WriteString(L"HCoreLdr: Init [0/2]...\r\n"); + if (hdr.mName[0] != '.') continue; - if (!codePtr) { - EFI::RaiseHardError(L"HCoreLdr-BadAlloc", - L"Bad Alloc! (AllocatePool)"); - } + writer.WriteString(L"HCoreLdr: Mapping: "); + for (auto ch : hdr.mName) writer.WriteCharacter(ch); - img.File()->Read(img.File(), &codeSz, codePtr); + writer.WriteString(L"\r\n"); - /// GET DATA + UInt64 addr = hdr.mVirtualAddress; - UInt64 baseData = optHdr->mBaseOfData; - UInt64 sizeofData = - optHdr->mSizeOfInitializedData + optHdr->mSizeOfUninitializedData; - VoidPtr dataPtr = nullptr; + BS->AllocatePages(AllocateAnyPages, EfiLoaderCode, 1, &addr); - BS->AllocatePool(EfiLoaderCode, sizeofData, &dataPtr); + UInt64 pos = (optHdr->mImageBase + optHdr->mBaseOfData) + + hdr.mPointerToRawData; - if (!dataPtr) { - EFI::RaiseHardError(L"HCoreLdr: BadAlloc", L"(AllocatePool)"); + img.File()->SetPosition(img.File(), &pos); + img.Size(hdr.mSizeOfRawData); + img.File()->Read(img.File(), &img.Size(), (VoidPtr)addr); } - writer.WriteString(L"HCoreLdr: Init [1/2]...\r\n"); - - img.File()->SetPosition(img.File(), &baseData); - - img.File()->Read(img.File(), &sizeofData, dataPtr); - UInt32 MapKey = 0; UInt32* Size = 0; EfiMemoryDescriptor* Descriptor = nullptr; @@ -135,8 +129,6 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, L"Bad Alloc! (GetMemoryMap)"); } - writer.WriteString(L"HCoreLdr: Init [2/2]...\r\n"); - HEL::HandoverInformationHeader* handoverHdrPtr = nullptr; BS->AllocatePool(EfiLoaderData, sizeof(HEL::HandoverInformationHeader), @@ -167,6 +159,12 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, EFI::ExitBootServices(MapKey, ImageHandle); + HCore::HEL::HandoverProc proc = + reinterpret_cast( + optHdr->mAddressOfEntryPoint); + + proc(handoverHdrPtr); + EFI::Stop(); return kEfiOk; diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index d17f9671..fab7453d 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -27,7 +27,7 @@ bootloader-amd64: .PHONY: run-efi-amd64 run-efi-amd64: wget https://retrage.github.io/edk2-nightly/bin/DEBUGX64_OVMF.fd -O OVMF.fd - qemu-system-x86_64 -net none -smp 2 -m 4G -M q35 -bios OVMF.fd -drive file=fat:rw:CDROM,index=1,format=raw -d int + qemu-system-x86_64 -net none -smp 2 -m 4G -M q35 -bios OVMF.fd -drive file=fat:rw:CDROM,index=1,format=raw -serial stdio .PHONY: clean clean: -- cgit v1.2.3 From c6592da1f0461ee7b60fcc5a7f9e852273171b6d Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 14 Feb 2024 15:47:53 +0100 Subject: HCR-18: Fix: don't print sections. Signed-off-by: Amlal El Mahrouss --- Private/NewBoot/Source/RuntimeMain.cxx | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx index afca21eb..8d649459 100644 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ b/Private/NewBoot/Source/RuntimeMain.cxx @@ -86,11 +86,6 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, if (hdr.mName[0] != '.') continue; - writer.WriteString(L"HCoreLdr: Mapping: "); - for (auto ch : hdr.mName) writer.WriteCharacter(ch); - - writer.WriteString(L"\r\n"); - UInt64 addr = hdr.mVirtualAddress; BS->AllocatePages(AllocateAnyPages, EfiLoaderCode, 1, &addr); -- cgit v1.2.3 From f323b1049bb581403d903f95224c0476b1812d43 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 15 Feb 2024 11:09:38 +0100 Subject: HCR-18 Kernel: - Add back RuntimeMain jump. - Update DLL/SO runtime for PEF. NewBoot: - Figured out how to load this PE in a reusable way. Signed-off-by: Amlal El Mahrouss --- Private/HALKit/AMD64/HalStartSequence.asm | 4 ++++ Private/NewBoot/Source/RuntimeMain.cxx | 35 ++++++++++--------------------- Private/Source/PEFSharedObjectRT.cxx | 21 ++++++------------- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/Private/HALKit/AMD64/HalStartSequence.asm b/Private/HALKit/AMD64/HalStartSequence.asm index 1db3b218..c8e0a4e2 100644 --- a/Private/HALKit/AMD64/HalStartSequence.asm +++ b/Private/HALKit/AMD64/HalStartSequence.asm @@ -31,6 +31,10 @@ section .text ;; Just a simple setup, we'd also need to tell some before Main: + push rcx + jmp RuntimeMain + pop rcx +L0: cli hlt jmp $ diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx index 8d649459..4388d8f5 100644 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ b/Private/NewBoot/Source/RuntimeMain.cxx @@ -7,7 +7,6 @@ * ======================================================== */ -#include "EFIKit/EFI.hxx" #define __BOOTLOADER__ 1 #include @@ -17,16 +16,13 @@ #include #ifdef __x86_64__ - #include - #else - #error Unknown CPU. +#endif // ifdef __x86_64__ -#endif - -#define kBufferReadSz 4096 +#define kBufferReadSz \ + (sizeof(DosHeader) + sizeof(ExecHeader) + sizeof(ExecOptionalHeader)) EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, EfiSystemTable* SystemTable) { @@ -49,9 +45,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, writer.WriteString(L"HCoreLdr: Build date: "); - for (auto& ch : strDate) { - writer.WriteCharacter(ch); - } + for (auto& ch : strDate) writer.WriteCharacter(ch); writer.WriteString(L"\r\nHCoreLdr: Firmware Vendor: ") .WriteString(SystemTable->FirmwareVendor) @@ -81,22 +75,13 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, ExecSectionHeaderPtr headers = (ExecSectionHeaderPtr)(&ptrHdr->mCharacteristics + 1); - for (int i = 0u; i < ptrHdr->mNumberOfSections; ++i) { - auto& hdr = headers[i]; + EfiPhysicalAddress base = optHdr->mImageBase + optHdr->mBaseOfData; + BS->AllocatePages(AllocateAddress, EfiLoaderCode, 1, &base); - if (hdr.mName[0] != '.') continue; + UInt64 codeSz = optHdr->mSizeOfCode; + img.File()->Read(img.File(), &codeSz, (VoidPtr)&base); - UInt64 addr = hdr.mVirtualAddress; - - BS->AllocatePages(AllocateAnyPages, EfiLoaderCode, 1, &addr); - - UInt64 pos = (optHdr->mImageBase + optHdr->mBaseOfData) + - hdr.mPointerToRawData; - - img.File()->SetPosition(img.File(), &pos); - img.Size(hdr.mSizeOfRawData); - img.File()->Read(img.File(), &img.Size(), (VoidPtr)addr); - } + // TODO ExecReader class UInt32 MapKey = 0; UInt32* Size = 0; @@ -152,6 +137,8 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, handoverHdrPtr->f_HeapCommitSize = optHdr->mSizeOfHeapCommit; handoverHdrPtr->f_StackCommitSize = optHdr->mSizeOfStackCommit; + writer.WriteString(L"HCoreLdr: Exit...\r\n"); + EFI::ExitBootServices(MapKey, ImageHandle); HCore::HEL::HandoverProc proc = diff --git a/Private/Source/PEFSharedObjectRT.cxx b/Private/Source/PEFSharedObjectRT.cxx index aceca259..60a3ea8f 100644 --- a/Private/Source/PEFSharedObjectRT.cxx +++ b/Private/Source/PEFSharedObjectRT.cxx @@ -20,6 +20,7 @@ 01/02/24: Rework shared library ABI, except a __LibInit and __LibFini (amlel) + 15/02/24: Breaking changes, changed the name of the routines. (amlel) ------------------------------------------- */ @@ -31,14 +32,13 @@ using namespace HCore; /***********************************************************************************/ /***********************************************************************************/ -/* @brief Allocates a new library. */ +/* @brief Library runtime initializer. */ /***********************************************************************************/ -extern "C" SharedObject *__LibInit() { - SharedObject *library = hcore_tls_new_class(); +extern "C" SharedObjectPtr ke_library_init(void) { + SharedObjectPtr library = hcore_tls_new_class(); if (!library) { - kcout << "__LibInit: Out of Memory!\n"; ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); return nullptr; @@ -47,7 +47,6 @@ extern "C" SharedObject *__LibInit() { library->Mount(hcore_tls_new_class()); if (!library->Get()) { - kcout << "__LibInit: Out of Memory!\n"; ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); return nullptr; @@ -57,7 +56,6 @@ extern "C" SharedObject *__LibInit() { ProcessManager::Shared().Leak().GetCurrent().Leak().Image; if (!library->Get()->fImageObject) { - kcout << "__LibInit: Invalid image!\n"; ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); return nullptr; @@ -66,25 +64,20 @@ extern "C" SharedObject *__LibInit() { library->Get()->fImageEntrypointOffset = library->Load(kPefStart, rt_string_len(kPefStart, 0), kPefCode); - kcout << "__LibInit: Task is successful!\n"; - return library; } /***********************************************************************************/ - -/***********************************************************************************/ -/* @brief Frees the library. */ +/* @brief Ends the library. */ /* @note Please check if the lib got freed! */ /* @param SharedObjectPtr the library to free. */ /***********************************************************************************/ -extern "C" Void __LibFini(SharedObjectPtr lib, bool *successful) { +extern "C" Void ke_library_free(SharedObjectPtr lib, bool *successful) { MUST_PASS(successful); // sanity check (will also trigger a bug check) if (lib == nullptr) { - kcout << "__LibFini: Invalid image!\n"; *successful = false; ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } @@ -94,8 +87,6 @@ extern "C" Void __LibFini(SharedObjectPtr lib, bool *successful) { lib = nullptr; - kcout << "__LibFini: Task is successful!\n"; - *successful = true; } -- cgit v1.2.3 From 1c2f1e90396f2400fe576c451a26a38891dd6c1a Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 15 Feb 2024 19:13:29 +0100 Subject: HCR-18: Will load the CodeManager preferred format, PEF. - PEF is the de facto format for HCore. - PEF is compiled using the Amlal El Mahrouss Object Format. - PEF is big-endian by default. Signed-off-by: Amlal El Mahrouss --- Private/EFIKit/Handover.hxx | 2 -- Private/NewBoot/Source/RuntimeMain.cxx | 39 +++++++++++----------------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/Private/EFIKit/Handover.hxx b/Private/EFIKit/Handover.hxx index f9b8044f..840af2ec 100644 --- a/Private/EFIKit/Handover.hxx +++ b/Private/EFIKit/Handover.hxx @@ -73,8 +73,6 @@ struct HandoverInformationHeader { voidPtr f_RTC; voidPtr f_GOP; SizeT f_GOPSize; - SizeT f_HeapCommitSize; - SizeT f_StackCommitSize; }; /** diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx index 4388d8f5..78e73db2 100644 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ b/Private/NewBoot/Source/RuntimeMain.cxx @@ -68,45 +68,33 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, ExecOptionalHeaderPtr optHdr = reinterpret_cast( ptrHdr + sizeof(ExecHeader)); - UInt64 baseSec = ptrHdr->mNumberOfSections; - img.File()->SetPosition(img.File(), &baseSec); - - baseSec = ptrHdr->mNumberOfSections; - ExecSectionHeaderPtr headers = - (ExecSectionHeaderPtr)(&ptrHdr->mCharacteristics + 1); - - EfiPhysicalAddress base = optHdr->mImageBase + optHdr->mBaseOfData; - BS->AllocatePages(AllocateAddress, EfiLoaderCode, 1, &base); - - UInt64 codeSz = optHdr->mSizeOfCode; - img.File()->Read(img.File(), &codeSz, (VoidPtr)&base); - - // TODO ExecReader class - UInt32 MapKey = 0; - UInt32* Size = 0; - EfiMemoryDescriptor* Descriptor = nullptr; + UInt32* Size; + EfiMemoryDescriptor* Descriptor; UInt32 SzDesc = 0; UInt32 RevDesc = 0; if (BS->AllocatePool(EfiLoaderData, sizeof(UInt64), (VoidPtr*)&Size) != kEfiOk) { - EFI::RaiseHardError(L"HCoreLdr-BadAlloc", - L"Bad Alloc! (AllocatePool)"); + EFI::RaiseHardError( + L"HCoreLdr-BadAlloc", + L"The bootloader ran out of memory! Please check your specs."); } *Size = sizeof(EfiMemoryDescriptor); if (BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor), (VoidPtr*)&Descriptor) != kEfiOk) { - EFI::RaiseHardError(L"HCoreLdr-BadAlloc", - L"Bad Alloc! (AllocatePool)"); + EFI::RaiseHardError( + L"HCoreLdr-BadAlloc", + L"The bootloader ran out of memory! Please check your specs."); } if (BS->GetMemoryMap(Size, Descriptor, &MapKey, &SzDesc, &RevDesc) != kEfiOk) { - EFI::RaiseHardError(L"HCoreLdr-BadAlloc", - L"Bad Alloc! (GetMemoryMap)"); + EFI::RaiseHardError( + L"HCoreLdr-GetMemoryMap", + L"GetMemoryMap returned a value which isn't kEfiOk!"); } HEL::HandoverInformationHeader* handoverHdrPtr = nullptr; @@ -134,10 +122,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, SystemTable->FirmwareVendor, handoverHdrPtr->f_FirmwareVendorLen); - handoverHdrPtr->f_HeapCommitSize = optHdr->mSizeOfHeapCommit; - handoverHdrPtr->f_StackCommitSize = optHdr->mSizeOfStackCommit; - - writer.WriteString(L"HCoreLdr: Exit...\r\n"); + writer.WriteString(L"HCoreLdr: Leaving it to kernel...\r\n"); EFI::ExitBootServices(MapKey, ImageHandle); -- cgit v1.2.3 From 4c7aebf1b8964b99b89a25da0965b30fe6c7e6b3 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 16 Feb 2024 14:57:04 +0100 Subject: HCR-18: Last commit, working on HCR-12 now. Signed-off-by: Amlal El Mahrouss --- Private/NewBoot/BootKit/BootKit.hxx | 2 +- Private/NewBoot/Source/FileReader.cxx | 2 +- Private/NewBoot/Source/RuntimeMain.cxx | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index d8865712..dcb8daf4 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -64,7 +64,7 @@ class BFileReader final { explicit BFileReader(const CharacterType *path, EfiHandlePtr ImageHandle); ~BFileReader(); - Void Read(); + Void ReadAll(); enum { kOperationOkay, diff --git a/Private/NewBoot/Source/FileReader.cxx b/Private/NewBoot/Source/FileReader.cxx index 5768cae0..d81248ea 100644 --- a/Private/NewBoot/Source/FileReader.cxx +++ b/Private/NewBoot/Source/FileReader.cxx @@ -103,7 +103,7 @@ BFileReader::~BFileReader() { @brief this reads all of the buffer. @param ImageHandle used internally. */ -Void BFileReader::Read() { +Void BFileReader::ReadAll() { /// Allocate Handover page. if (mBlob == nullptr) { diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx index 78e73db2..38eb0ea8 100644 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ b/Private/NewBoot/Source/RuntimeMain.cxx @@ -21,7 +21,7 @@ #error Unknown CPU. #endif // ifdef __x86_64__ -#define kBufferReadSz \ +#define kHeadersSz \ (sizeof(DosHeader) + sizeof(ExecHeader) + sizeof(ExecOptionalHeader)) EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, @@ -53,8 +53,8 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, BFileReader img(L"HCOREKRNL.EXE", ImageHandle); - img.Size(kBufferReadSz); - img.Read(); + img.Size(kHeadersSz); + img.ReadAll(); if (img.Error() == BFileReader::kOperationOkay) { BlobType blob = (BlobType)img.Blob(); -- cgit v1.2.3