From da9920a33d12a2ebd04f4572a6d134dfb6e6cab6 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 22 Feb 2024 11:18:10 +0100 Subject: Kernel: Got HCoreLite up and running. - Working on standard edition of HCoreLdr (Kernel and Bootloader separated) Signed-off-by: Amlal El Mahrouss --- Private/HALKit/AMD64/HalPageAlloc.cpp | 7 +++--- Private/HALKit/PowerPC/HalHardware.cxx | 2 -- Private/NewBoot/BootKit/BootKit.hxx | 4 ++++ Private/NewBoot/Source/RuntimeMain.cxx | 42 ++++++++++++++++++++++++++++++---- Private/NewBoot/Source/bundle.mk | 35 ++++++++++++++++++++++++++++ Private/NewBoot/Source/makefile | 34 --------------------------- Private/NewBoot/Source/standard.mk | 34 +++++++++++++++++++++++++++ Private/Source/KernelHeap.cxx | 2 -- Private/Source/PageManager.cxx | 6 ----- 9 files changed, 114 insertions(+), 52 deletions(-) create mode 100644 Private/NewBoot/Source/bundle.mk delete mode 100644 Private/NewBoot/Source/makefile create mode 100644 Private/NewBoot/Source/standard.mk diff --git a/Private/HALKit/AMD64/HalPageAlloc.cpp b/Private/HALKit/AMD64/HalPageAlloc.cpp index 8920f295..64752520 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.cpp +++ b/Private/HALKit/AMD64/HalPageAlloc.cpp @@ -22,14 +22,15 @@ namespace HCore { namespace HAL { static auto hal_try_alloc_new_page(SizeT sz, Boolean rw, Boolean user) -> PageTable64 * { - PageTable64 *pte = reinterpret_cast( - (UIntPtr)kKernelVirtualStart + kPageCnt + kPagePad); + PageTable64 *pte = + reinterpret_cast((UIntPtr)kKernelVirtualStart); pte->Rw = rw; pte->User = user; pte->Present = true; - kKernelVirtualStart = (VoidPtr)((UIntPtr)kKernelVirtualStart + kPageCnt + sz); + kKernelVirtualStart = + (VoidPtr)((UIntPtr)kKernelVirtualStart + kPageCnt + sz + kPagePad); return pte; } diff --git a/Private/HALKit/PowerPC/HalHardware.cxx b/Private/HALKit/PowerPC/HalHardware.cxx index a5f11449..c066051c 100644 --- a/Private/HALKit/PowerPC/HalHardware.cxx +++ b/Private/HALKit/PowerPC/HalHardware.cxx @@ -44,6 +44,4 @@ void ke_com_print(const Char* bytes) { ++index; } } - -TerminalDevice kcout(HCore::ke_com_print, nullptr); } // namespace HCore diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index dcb8daf4..093b20dc 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -14,6 +14,10 @@ #pragma once +class BTextWriter; +class BFileReader; +class BFileRunner; + #include #include #include diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx index 65cd1e44..0db90e6f 100644 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ b/Private/NewBoot/Source/RuntimeMain.cxx @@ -24,6 +24,10 @@ #define kHeadersSz \ (sizeof(DosHeader) + sizeof(ExecHeader) + sizeof(ExecOptionalHeader)) +#ifdef __BUNDLE_KERNEL__ +EXTERN_C EFI_API void RuntimeMain(HEL::HandoverInformationHeader* HIH); +#endif + EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, EfiSystemTable* SystemTable) { InitEFI(SystemTable); @@ -31,25 +35,41 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, BTextWriter writer; +#ifdef __BUNDLE_KERNEL__ + writer.WriteString(L"HCoreLite: "); +#else + writer.WriteString(L"HCoreLdr:"); +#endif + #ifndef __DEBUG__ - writer.WriteString(L"HCoreLdr: Version 1.00 (Release Channel)\r\n"); + writer.WriteString(L"Version 1.00 (Release Channel)\r\n"); #else - writer.WriteString(L"HCoreLdr: Version 1.00 (Insiders Channel)\r\n"); + writer.WriteString(L"Version 1.00 (Insiders Channel)\r\n"); #endif const char strDate[] = __DATE__; +#ifdef __BUNDLE_KERNEL__ + writer.WriteString(L"HCoreLite: Build: "); +#else writer.WriteString(L"HCoreLdr: Build: "); +#endif for (auto& ch : strDate) writer.WriteCharacter(ch); +#ifdef __BUNDLE_KERNEL__ + writer.WriteString(L"\r\nHCoreLite: Firmware Vendor: ") + .WriteString(SystemTable->FirmwareVendor) + .WriteString(L"\r\n"); +#else writer.WriteString(L"\r\nHCoreLdr: Firmware Vendor: ") .WriteString(SystemTable->FirmwareVendor) .WriteString(L"\r\n"); +#endif BFileReader img(L"HCOREKRNL.EXE", ImageHandle); @@ -75,7 +95,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, UInt32 SzDesc = 0; UInt32 RevDesc = 0; - if (BS->AllocatePool(EfiLoaderData, sizeof(UInt64), (VoidPtr*)&Size) != + if (BS->AllocatePool(EfiLoaderData, sizeof(UInt32), (VoidPtr*)&Size) != kEfiOk) { EFI::RaiseHardError( L"HCoreLdr-BadAlloc", @@ -108,7 +128,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, handoverHdrPtr->f_PhysicalStart = reinterpret_cast(Descriptor->PhysicalStart); - handoverHdrPtr->f_PhysicalSize = Descriptor->NumberOfPages * kPTESize; + handoverHdrPtr->f_PhysicalSize = Descriptor->NumberOfPages; handoverHdrPtr->f_VirtualStart = reinterpret_cast(Descriptor->VirtualStart); @@ -123,10 +143,22 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, SystemTable->FirmwareVendor, handoverHdrPtr->f_FirmwareVendorLen); - writer.WriteString(L"HCoreLdr: Booting HCore...\r\n"); +#ifdef __BUNDLE_KERNEL__ + writer.WriteString(L"HCoreLite: Exit Boot...").WriteString(L"\r\n"); +#else + writer.WriteString(L"HCoreLdr: Load File succeeded, running it...") + .WriteString(L"\r\n"); +#endif EFI::ExitBootServices(MapKey, ImageHandle); +#ifdef __BUNDLE_KERNEL__ + RuntimeMain(handoverHdrPtr); +#else + // Load HCoreKrnl.exe (TODO) + +#endif // ifdef __BUNDLE_KERNEL__ + EFI::Stop(); return kEfiOk; diff --git a/Private/NewBoot/Source/bundle.mk b/Private/NewBoot/Source/bundle.mk new file mode 100644 index 00000000..c5a83ffb --- /dev/null +++ b/Private/NewBoot/Source/bundle.mk @@ -0,0 +1,35 @@ +################################################## +# ; (C) Mahrouss Logic, 2024, all rights reserved. +# This is the bootloader makefile. +################################################## + +CC_GNU=x86_64-w64-mingw32-g++ +LD_GNU=x86_64-w64-mingw32-ld +LD_FLAGS=-e efi_main -shared --subsystem=10 -ffreestanding +ASM=nasm +OBJ=$(wildcard *.obj) $(wildcard *.o) $(wildcard ../../Obj/*.obj) $(wildcard HEL/AMD64/*.obj) $(wildcard HEL/AMD64/*.o) + +FLAG_ASM=-f win64 +FLAG_GNU=-fshort-wchar -O0 -D__BUNDLE_KERNEL__ -D__DEBUG__ -fPIC -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ + +.PHONY: invalid-recipe +invalid-recipe: + @echo "invalid-recipe: Use make bootloader- all instead." + +.PHONY: bootloader-amd64 +bootloader-amd64: + $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx *.cxx + $(ASM) $(FLAG_ASM) HEl/AMD64/AMD64-VirtualMemory.asm + $(LD_GNU) $(OBJ) $(LD_FLAGS) -o HCoreKrnl.exe + cp HCoreKrnl.exe CDROM/EFI/BOOT/BOOTX64.EFI + cp HCoreKrnl.exe CDROM/HCOREKRNL.EXE + cp ../../Root/System/LookAndFeel.css CDROM/LOOK.CSS + +.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 8G -M q35 -bios OVMF.fd -drive file=fat:rw:CDROM,index=1,format=raw -serial stdio + +.PHONY: clean +clean: + rm -f $(OBJ) HCoreLdr.exe OVMF.fd diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile deleted file mode 100644 index fab7453d..00000000 --- a/Private/NewBoot/Source/makefile +++ /dev/null @@ -1,34 +0,0 @@ -################################################## -# ; (C) Mahrouss Logic, 2024, all rights reserved. -# This is the bootloader makefile. -################################################## - -CC_GNU=x86_64-w64-mingw32-g++ -LD_GNU=x86_64-w64-mingw32-ld -LD_FLAGS=-e efi_main -shared --subsystem=10 -ffreestanding -ASM=nasm -OBJ=$(wildcard *.obj) $(wildcard *.o) $(wildcard HEL/AMD64/*.obj) $(wildcard HEL/AMD64/*.o) - -FLAG_ASM=-f win64 -FLAG_GNU=-fshort-wchar -O0 -D__DEBUG__ -fPIC -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ - -.PHONY: invalid-recipe -invalid-recipe: - @echo "invalid-recipe: Use make bootloader- all instead." - -.PHONY: bootloader-amd64 -bootloader-amd64: - $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx *.cxx - $(ASM) $(FLAG_ASM) HEl/AMD64/AMD64-VirtualMemory.asm - $(LD_GNU) $(OBJ) $(LD_FLAGS) -o HCoreLdr.exe - cp HCoreLdr.exe CDROM/EFI/BOOT/BOOTX64.EFI - cp ../../HCoreKrnl.exe CDROM/HCOREKRNL.EXE && cp ../../Root/System/LookAndFeel.css CDROM/LOOK.CSS - -.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 - -.PHONY: clean -clean: - rm -f $(OBJ) HCoreLdr.exe OVMF.fd diff --git a/Private/NewBoot/Source/standard.mk b/Private/NewBoot/Source/standard.mk new file mode 100644 index 00000000..fab7453d --- /dev/null +++ b/Private/NewBoot/Source/standard.mk @@ -0,0 +1,34 @@ +################################################## +# ; (C) Mahrouss Logic, 2024, all rights reserved. +# This is the bootloader makefile. +################################################## + +CC_GNU=x86_64-w64-mingw32-g++ +LD_GNU=x86_64-w64-mingw32-ld +LD_FLAGS=-e efi_main -shared --subsystem=10 -ffreestanding +ASM=nasm +OBJ=$(wildcard *.obj) $(wildcard *.o) $(wildcard HEL/AMD64/*.obj) $(wildcard HEL/AMD64/*.o) + +FLAG_ASM=-f win64 +FLAG_GNU=-fshort-wchar -O0 -D__DEBUG__ -fPIC -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ + +.PHONY: invalid-recipe +invalid-recipe: + @echo "invalid-recipe: Use make bootloader- all instead." + +.PHONY: bootloader-amd64 +bootloader-amd64: + $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx *.cxx + $(ASM) $(FLAG_ASM) HEl/AMD64/AMD64-VirtualMemory.asm + $(LD_GNU) $(OBJ) $(LD_FLAGS) -o HCoreLdr.exe + cp HCoreLdr.exe CDROM/EFI/BOOT/BOOTX64.EFI + cp ../../HCoreKrnl.exe CDROM/HCOREKRNL.EXE && cp ../../Root/System/LookAndFeel.css CDROM/LOOK.CSS + +.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 + +.PHONY: clean +clean: + rm -f $(OBJ) HCoreLdr.exe OVMF.fd diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx index 358330f0..10d3fdd2 100644 --- a/Private/Source/KernelHeap.cxx +++ b/Private/Source/KernelHeap.cxx @@ -9,8 +9,6 @@ #include -#include "KernelKit/DebugOutput.hpp" - //! @file KernelHeap.cpp //! @brief Kernel allocator. diff --git a/Private/Source/PageManager.cxx b/Private/Source/PageManager.cxx index c520da78..c801fe07 100644 --- a/Private/Source/PageManager.cxx +++ b/Private/Source/PageManager.cxx @@ -41,7 +41,6 @@ PTEWrapper::PTEWrapper(Boolean Rw, Boolean User, Boolean ExecDisable, PTEWrapper::~PTEWrapper() { PTE *raw = reinterpret_cast(m_VirtAddr); - MUST_PASS(raw); raw->Present = false; raw->Rw = false; @@ -78,11 +77,6 @@ PTEWrapper *PageManager::Request(Boolean Rw, Boolean User, PTEWrapper *PageTableEntry = reinterpret_cast( HCore::HAL::hal_alloc_page(sizeof(PTEWrapper), Rw, User)); - if (PageTableEntry == nullptr) { - kcout << "PTEWrapper : Page table is nullptr!, ke_new_ke_heap failed!\n"; - return nullptr; - } - PageTableEntry->NoExecute(ExecDisable); *PageTableEntry = PTEWrapper{Rw, User, ExecDisable, -- cgit v1.2.3