From e559cf01834340d0a4dfcb45f7bdf081e8994032 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 2 Feb 2024 13:49:50 +0100 Subject: Kernel: Port toolchain to mingw (PE/COFF) Signed-off-by: Amlal El Mahrouss --- .vscode/c_cpp_properties.json | 4 +-- Private/ArchKit/Arch.hpp | 4 +-- Private/HALKit/AMD64/DebugManager.asm | 31 ---------------- Private/HALKit/AMD64/HalProcessPrimitives.cxx | 2 -- Private/HALKit/AMD64/SMPCoreManager.asm | 34 +++++++++--------- Private/HALKit/AMD64/StartSequence.asm | 9 ----- Private/HALKit/AMD64/crti.s | 16 --------- Private/HALKit/AMD64/crtn.s | 10 ------ Private/KernelKit/PEFCodeManager.hxx | 2 -- Private/KernelKit/ProcessManager.hpp | 5 ++- Private/KernelKit/ThreadLocalStorage.hxx | 27 +++++++------- Private/KernelKit/ThreadLocalStorage.inl | 45 ++++++++++++----------- Private/Linker/Platforms/PC.lds | 51 --------------------------- Private/MoveAll.sh | 7 ++++ Private/Source/CxxAbi.cxx | 7 ++-- Private/Source/KernelHeap.cxx | 2 -- Private/Source/PEFCodeManager.cxx | 3 +- Private/Source/PEFSharedObjectRT.cxx | 2 -- Private/Source/ProcessManager.cxx | 2 -- Private/Source/RuntimeMain.cxx | 10 ------ Private/Source/SMPManager.cxx | 6 ++-- Private/Source/Storage/ATA.cxx | 2 -- Private/makefile | 25 ++++++------- ReadMe.md | 2 +- 24 files changed, 91 insertions(+), 217 deletions(-) delete mode 100644 Private/HALKit/AMD64/DebugManager.asm delete mode 100644 Private/HALKit/AMD64/crti.s delete mode 100644 Private/HALKit/AMD64/crtn.s delete mode 100644 Private/Linker/Platforms/PC.lds create mode 100755 Private/MoveAll.sh diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 892f41cf..86e71f01 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,12 +4,12 @@ "name": "HCore", "includePath": [ "${workspaceFolder}/Private/**", - "${workspaceFolder}/Private/NewBoot/*", - "${workspaceFolder}/Private/acpiSDK/source/include/*" + "${workspaceFolder}/Private/NewBoot/*" ], "defines": ["__HCORE__", "__HAVE_HCORE_APIS__"], "cStandard": "c17", "cppStandard": "c++20", + "compilerPath": "/opt/homebrew/bin/x86_64-elf-g++", "intelliSenseMode": "gcc-x64" } ], diff --git a/Private/ArchKit/Arch.hpp b/Private/ArchKit/Arch.hpp index 903e72c8..4216f9b3 100644 --- a/Private/ArchKit/Arch.hpp +++ b/Private/ArchKit/Arch.hpp @@ -80,7 +80,7 @@ namespace HCore extern HCore::Array kSyscalls; extern "C" void rt_wait_for_io(); -extern "C" void rt_syscall_handle(HCore::HAL::StackFrame *stack); +extern "C" void rt_syscall_handle(HCore::HAL::StackFrame* stackFrame); extern "C" HCore::HAL::StackFrame* rt_get_current_context(); -extern "C" int rt_do_context_switch(HCore::HAL::StackFrame* stackLeft, HCore::HAL::StackFrame* stackRight); +extern "C" int rt_do_context_switch(HCore::HAL::StackFrame* stackFrame); diff --git a/Private/HALKit/AMD64/DebugManager.asm b/Private/HALKit/AMD64/DebugManager.asm deleted file mode 100644 index 5cc0cd10..00000000 --- a/Private/HALKit/AMD64/DebugManager.asm +++ /dev/null @@ -1,31 +0,0 @@ -;; /* -;; * ======================================================== -;; * -;; * HCore -;; * Copyright Mahrouss Logic, all rights reserved. -;; * -;; * ======================================================== -;; */ - -[global rt_debug_fence] -[global __rt_debug_int_3] - -;; //////////////////////////////////////////////////// ;; - -__rt_debug_record_table: - db "DebugMgr/HCore", 0xa, 0xd, 0 -__rt_debug_int_3: - push 0x6677 ;; Debug check error -;; then halt and goes back to L0, thens halts... -L0: - hlt - jmp $ - -rt_debug_fence: - mov [__rt_debug_record_table], rsi - push rsi - jmp [rbx] - pop rsi - ret - -;; //////////////////////////////////////////////////// ;; diff --git a/Private/HALKit/AMD64/HalProcessPrimitives.cxx b/Private/HALKit/AMD64/HalProcessPrimitives.cxx index d24afad6..86f77e35 100644 --- a/Private/HALKit/AMD64/HalProcessPrimitives.cxx +++ b/Private/HALKit/AMD64/HalProcessPrimitives.cxx @@ -9,8 +9,6 @@ #include -#include "NewKit/Defines.hpp" - using namespace HCore; Void Process::AssignStart(UIntPtr &imageStart) noexcept { diff --git a/Private/HALKit/AMD64/SMPCoreManager.asm b/Private/HALKit/AMD64/SMPCoreManager.asm index de7d8356..1a994258 100644 --- a/Private/HALKit/AMD64/SMPCoreManager.asm +++ b/Private/HALKit/AMD64/SMPCoreManager.asm @@ -11,21 +11,23 @@ [global rt_do_context_switch] +section .text + rt_do_context_switch: - mov [rdi+0], rax - mov [rdi+8], rbx - mov [rdi+16], rcx - mov [rdi+24], rdx - mov [rdi+32], rsi - mov [rdi+40], rdi - mov [rdi+48], rbp - mov [rdi+56], rsp - mov [rdi+64], r8 - mov [rdi+72], r9 - mov [rdi+80], r10 - mov [rdi+88], r11 - mov [rdi+96], r12 - mov [rdi+104], r13 - mov [rdi+112], r14 - mov [rdi+120], r15 + mov [r15+0], rax + mov [r15+8], rbx + mov [r15+16], rcx + mov [r15+24], rdx + mov [r15+32], rsi + mov [r15+40], rdi + mov [r15+48], rbp + mov [r15+56], rsp + mov [r15+64], r8 + mov [r15+72], r9 + mov [r15+80], r10 + mov [r15+88], r11 + mov [r15+96], r12 + mov [r15+104], r13 + mov [r15+112], r14 + mov [r15+120], r15 ret diff --git a/Private/HALKit/AMD64/StartSequence.asm b/Private/HALKit/AMD64/StartSequence.asm index 09cb1d65..5ef8ed55 100644 --- a/Private/HALKit/AMD64/StartSequence.asm +++ b/Private/HALKit/AMD64/StartSequence.asm @@ -32,17 +32,8 @@ section .text ;; Just a simple setup, we'd also need to tell some before Main: - mov rsp, __SYSTEM_STACK_END - mov ebp, RuntimeMain jmp RuntimeMain L0: cli hlt jmp $ - -;; @brief this one is jumped on when an unsupported then gets through the boot stage. -;; @note: must be ISA compatible! -MainUnsupported: - cli - hlt - jmp $ diff --git a/Private/HALKit/AMD64/crti.s b/Private/HALKit/AMD64/crti.s deleted file mode 100644 index a85782ea..00000000 --- a/Private/HALKit/AMD64/crti.s +++ /dev/null @@ -1,16 +0,0 @@ -/* x86_64 crti.s */ -.section .init -.global _init -.type _init, @function -_init: - push %rbp - movq %rsp, %rbp - /* gcc will nicely put the contents of crtbegin.o's .init section here. */ - -.section .fini -.global _fini -.type _fini, @function -_fini: - push %rbp - movq %rsp, %rbp - /* gcc will nicely put the contents of crtbegin.o's .fini section here. */ diff --git a/Private/HALKit/AMD64/crtn.s b/Private/HALKit/AMD64/crtn.s deleted file mode 100644 index b446d9f3..00000000 --- a/Private/HALKit/AMD64/crtn.s +++ /dev/null @@ -1,10 +0,0 @@ -/* x86_64 crtn.s */ -.section .init - /* gcc will nicely put the contents of crtend.o's .init section here. */ - popq %rbp - ret - -.section .fini - /* gcc will nicely put the contents of crtend.o's .fini section here. */ - popq %rbp - ret diff --git a/Private/KernelKit/PEFCodeManager.hxx b/Private/KernelKit/PEFCodeManager.hxx index 3541d11b..775f299b 100644 --- a/Private/KernelKit/PEFCodeManager.hxx +++ b/Private/KernelKit/PEFCodeManager.hxx @@ -14,8 +14,6 @@ #include #include -#include "NewKit/Defines.hpp" - namespace HCore { /// /// \name PEFLoader diff --git a/Private/KernelKit/ProcessManager.hpp b/Private/KernelKit/ProcessManager.hpp index 5637dd6f..14c6eb28 100644 --- a/Private/KernelKit/ProcessManager.hpp +++ b/Private/KernelKit/ProcessManager.hpp @@ -7,7 +7,8 @@ * ======================================================== */ -#pragma once +#ifndef __PROCESS_MANAGER__ +#define __PROCESS_MANAGER__ #include #include @@ -211,3 +212,5 @@ const Int32 &rt_get_exit_code() noexcept; #include //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#endif /* ifndef __PROCESS_MANAGER__ */ diff --git a/Private/KernelKit/ThreadLocalStorage.hxx b/Private/KernelKit/ThreadLocalStorage.hxx index 910daf31..5db78ca3 100644 --- a/Private/KernelKit/ThreadLocalStorage.hxx +++ b/Private/KernelKit/ThreadLocalStorage.hxx @@ -18,24 +18,27 @@ #define kCookieMag1 'C' #define kCookieMag2 'o' -template T *hcore_tls_new_ptr(void); +template +T *hcore_tls_new_ptr(void); -template bool hcore_tls_delete_ptr(T *ptr); +template +bool hcore_tls_delete_ptr(T *ptr); -template T *hcore_tls_new_class(Args &&...args); +template +T *hcore_tls_new_class(Args &&...args); typedef char rt_cookie_type[3]; /// @brief Thread Information Block for Local Storage. /// Located in GS on AMD64, Virtual Address 0x10000 (64x0, 32x0, ARM64) -struct ThreadInformationBlock final -{ - HCore::Char Name[255]; // Module Name - HCore::UIntPtr StartCode; // Start Address - HCore::UIntPtr StartData; // Allocation Heap - HCore::UIntPtr StartStack; // Stack Pointer. - HCore::Int32 Arch; // Architecture and/or platform. - rt_cookie_type Cookie; // Not shown in public header, this is the way we tell something went wrong. +struct ThreadInformationBlock final { + HCore::Char Name[255]; // Module Name + HCore::UIntPtr StartCode; // Start Address + HCore::UIntPtr StartData; // Allocation Heap + HCore::UIntPtr StartStack; // Stack Pointer. + HCore::Int32 Arch; // Architecture and/or platform. + rt_cookie_type Cookie; // Not shown in public header, this is the way we tell + // something went wrong. }; /// @brief TLS install TIB @@ -44,7 +47,7 @@ extern void rt_install_tib(ThreadInformationBlock *pTib); ///! @brief Cookie Sanity check. HCore::Boolean hcore_tls_check(ThreadInformationBlock *ptr); -#include "ThreadLocalStorage.inl" +#include // last rev 1/29/24 diff --git a/Private/KernelKit/ThreadLocalStorage.inl b/Private/KernelKit/ThreadLocalStorage.inl index e73d8672..59d4c525 100644 --- a/Private/KernelKit/ThreadLocalStorage.inl +++ b/Private/KernelKit/ThreadLocalStorage.inl @@ -9,42 +9,41 @@ //! @brief Allocates a pointer from the process's tls. +#ifndef __PROCESS_MANAGER__ +#include +#endif + template -inline T* hcore_tls_new_ptr(void) -{ - using namespace HCore; +inline T* hcore_tls_new_ptr(void) { + using namespace HCore; - auto ref_process = ProcessManager::Shared().Leak().GetCurrent(); + auto ref_process = ProcessManager::Shared().Leak().GetCurrent(); - T* pointer = (T*)ref_process.Leak().New(sizeof(T)); - return pointer; + T* pointer = (T*)ref_process.Leak().New(sizeof(T)); + return pointer; } //! @brief TLS delete implementation. template -inline bool hcore_tls_delete_ptr(T* ptr) -{ - if (!ptr) - return false; +inline bool hcore_tls_delete_ptr(T* ptr) { + if (!ptr) return false; - using namespace HCore; + using namespace HCore; - auto ref_process = ProcessManager::Shared().Leak().GetCurrent(); - ptr->~T(); + auto ref_process = ProcessManager::Shared().Leak().GetCurrent(); + ptr->~T(); - return ref_process.Leak().Delete(ptr, sizeof(T)); + return ref_process.Leak().Delete(ptr, sizeof(T)); } template -T* hcore_tls_new_class(Args&&... args) -{ - T* ptr = hcore_tls_new_ptr(); +T* hcore_tls_new_class(Args&&... args) { + T* ptr = hcore_tls_new_ptr(); - if (ptr) - { - *ptr = T(HCore::forward(args)...); - return ptr; - } + if (ptr) { + *ptr = T(HCore::forward(args)...); + return ptr; + } - return nullptr; + return nullptr; } diff --git a/Private/Linker/Platforms/PC.lds b/Private/Linker/Platforms/PC.lds deleted file mode 100644 index 2340258e..00000000 --- a/Private/Linker/Platforms/PC.lds +++ /dev/null @@ -1,51 +0,0 @@ -OUTPUT_FORMAT("elf64-x86-64") - -ENTRY(Main) - -PHDRS { - null PT_NULL FLAGS(0) ; - text PT_LOAD FLAGS((1 << 0) | (1 << 2) | (1 << 3)); - rodata PT_LOAD FLAGS((1 << 2)) ; - data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; -} - -SECTIONS { - . = 8M; - - .text : { - *(.NewBoot) - *(.text .text.*) - } :text - - . += CONSTANT(MAXPAGESIZE); - - .init : { - __SYSTEM_INIT = .; - *(.initl) - __SYSTEM_FINI = .; - } - - __SYSTEM_INIT_END = .; - - . += CONSTANT(MAXPAGESIZE); - - .rodata : { - *(.rodata .rodata.*) - } :rodata - - . += CONSTANT(MAXPAGESIZE); - - .data : { - *(.data .data.*) - } :data - - . += CONSTANT(MAXPAGESIZE); - - .bss : { - *(COMMON) - *(.bss .bss.*) - } :data - - __SYSTEM_STACK_PTR = .; - __SYSTEM_STACK_END = . + 0x4000; -} diff --git a/Private/MoveAll.sh b/Private/MoveAll.sh new file mode 100755 index 00000000..0544cd8b --- /dev/null +++ b/Private/MoveAll.sh @@ -0,0 +1,7 @@ +#! /bin/sh + +for file in *.o; do + mv -- "$file" "${file%.o}.obj" +done + +mv *.obj HALKit/AMD64/*.obj Obj/ \ No newline at end of file diff --git a/Private/Source/CxxAbi.cxx b/Private/Source/CxxAbi.cxx index ea200708..c062219b 100644 --- a/Private/Source/CxxAbi.cxx +++ b/Private/Source/CxxAbi.cxx @@ -20,12 +20,9 @@ uarch_t __atexit_func_count; extern "C" void __cxa_pure_virtual() { HCore::kcout << "[C++] Placeholder\n"; } -extern "C" void __stack_chk_fail() { - HCore::kcout << "[HCoreKrnl] Buffer overflow detected\n"; - HCore::ke_stop(RUNTIME_CHECK_POINTER); -} +extern "C" void ___chkstk_ms() {} -extern "C" int __cxa_atexit(void (*f)(void *), void *arg, void *dso) { +extern "C" int atexit(void (*f)(void *), void *arg, void *dso) { if (__atexit_func_count >= DSO_MAX_OBJECTS) return -1; __atexit_funcs[__atexit_func_count].destructor_func = f; diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx index 603b91b3..3c3cb9ce 100644 --- a/Private/Source/KernelHeap.cxx +++ b/Private/Source/KernelHeap.cxx @@ -9,8 +9,6 @@ #include -#include "NewKit/PageManager.hpp" - //! @file KernelHeap.cpp //! @brief Kernel allocator. diff --git a/Private/Source/PEFCodeManager.cxx b/Private/Source/PEFCodeManager.cxx index 63e8659f..efebef48 100644 --- a/Private/Source/PEFCodeManager.cxx +++ b/Private/Source/PEFCodeManager.cxx @@ -53,8 +53,7 @@ PEFLoader::PEFLoader(const char *path) : fCachedBlob(nullptr), fBad(false) { PEFContainer *container = reinterpret_cast(fCachedBlob); auto fFree = [&]() -> void { - kcout << "CodeManager: Warning: Bad executable, program will not be " - "started!\n"; + kcout << "CodeManager: Warning: Executable format error!\n"; fBad = true; ke_delete_ke_heap(fCachedBlob); diff --git a/Private/Source/PEFSharedObjectRT.cxx b/Private/Source/PEFSharedObjectRT.cxx index 018b618e..035b068f 100644 --- a/Private/Source/PEFSharedObjectRT.cxx +++ b/Private/Source/PEFSharedObjectRT.cxx @@ -14,8 +14,6 @@ #include #include -#include "NewKit/RuntimeCheck.hpp" - /* ------------------------------------------- Revision History: diff --git a/Private/Source/ProcessManager.cxx b/Private/Source/ProcessManager.cxx index a8a03b52..1bba7f7e 100644 --- a/Private/Source/ProcessManager.cxx +++ b/Private/Source/ProcessManager.cxx @@ -17,8 +17,6 @@ #include #include -#include "NewKit/RuntimeCheck.hpp" - ///! bugs = 0 /***********************************************************************************/ diff --git a/Private/Source/RuntimeMain.cxx b/Private/Source/RuntimeMain.cxx index f103e98c..8c18c0a3 100644 --- a/Private/Source/RuntimeMain.cxx +++ b/Private/Source/RuntimeMain.cxx @@ -15,18 +15,8 @@ #include #include -/// PRIVATE SYMBOLS EXPORTED BY C++ COMPILER. -extern "C" void (*__SYSTEM_FINI)(); -extern "C" void (**__SYSTEM_INIT)(); - extern "C" void RuntimeMain( HCore::HEL::HandoverInformationHeader* HandoverHeader) { - /// Init C++ globals - for (HCore::SizeT index_init = 0UL; - __SYSTEM_INIT[index_init] != __SYSTEM_FINI; ++index_init) { - __SYSTEM_INIT[index_init](); - } - HCore::ke_init_heap(); HCore::ke_init_ke_heap(); diff --git a/Private/Source/SMPManager.cxx b/Private/Source/SMPManager.cxx index 27bf5892..37c2e592 100644 --- a/Private/Source/SMPManager.cxx +++ b/Private/Source/SMPManager.cxx @@ -63,7 +63,9 @@ void HardwareThread::Wake(const bool wakeup) noexcept { bool HardwareThread::Switch(HAL::StackFrame* stack) { if (stack == nullptr) return false; - return rt_do_context_switch(m_Stack, stack) == 0; + m_Stack = stack; + + return rt_do_context_switch(m_Stack) == 0; } ///! @brief Tells if processor is waked up. @@ -110,7 +112,7 @@ bool SMPManager::Switch(HAL::StackFrame* stack) { m_ThreadList[idx].Leak().Leak().Busy(true); - Boolean ret = (rt_do_context_switch(rt_get_current_context(), stack) == 0); + Boolean ret = (rt_do_context_switch(stack) == 0); m_ThreadList[idx].Leak().Leak().Busy(false); diff --git a/Private/Source/Storage/ATA.cxx b/Private/Source/Storage/ATA.cxx index 65a4457e..184bb043 100644 --- a/Private/Source/Storage/ATA.cxx +++ b/Private/Source/Storage/ATA.cxx @@ -10,8 +10,6 @@ #include #include -#include "NewKit/Utils.hpp" - //! @brief ATA DMA Driver //! mapped by UEFI. diff --git a/Private/makefile b/Private/makefile index f521b36a..f21a037e 100644 --- a/Private/makefile +++ b/Private/makefile @@ -3,11 +3,13 @@ # This is the microkernel makefile. ################################################## -CC = x86_64-elf-gcc -LD = x86_64-elf-ld +CC = x86_64-w64-mingw32-g++ +LD = x86_64-w64-mingw32-ld CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I../ -I./ -I$(HOME)/ ASM = nasm -ASMFLAGS = -f elf64 +ASMFLAGS = -f win64 +LDFLAGS = -e Main -filealign:16 -shared --subsystem=10 +LDOBJ = Obj/*.obj # This file is the kernel, responsible of task management, memory, drivers and more. KERNEL = HCoreKrnl.exe @@ -22,23 +24,22 @@ FMT = elf64 invalid-recipe: @echo "[HCoreKrnl] invalid-recipe: Use make all instead." +MOVEALL=./MoveAll.sh + .PHONY: h-core-amd64 h-core-amd64: $(CC) $(CCFLAGS) Source/*.cxx HALKit/AMD64/PCI/*.cpp Source/Network/*.cpp\ Source/Storage/*.cxx HALKit/AMD64/*.cxx HALKit/AMD64/*.cpp HALKit/AMD64/*.s - $(ASM) -f elf64 HALKit/AMD64/DebugManager.asm - $(ASM) -f elf64 HALKit/AMD64/HalInterruptRouting.asm - $(ASM) -f elf64 HALKit/AMD64/SMPCoreManager.asm - $(ASM) -f elf64 HALKit/AMD64/StartSequence.asm - mv *.o HALKit/AMD64/*.o Obj/ - + $(ASM) $(ASMFLAGS) HALKit/AMD64/HalInterruptRouting.asm + $(ASM) $(ASMFLAGS) HALKit/AMD64/SMPCoreManager.asm + $(ASM) $(ASMFLAGS) HALKit/AMD64/StartSequence.asm + $(MOVEALL) OBJCOPY = x86_64-elf-objcopy .PHONY: link-amd64 link-amd64: - $(LD) $(SCRIPT) Obj/*.o -o $(KERNEL) - $(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 $(KERNEL) $(KERNEL) + $(LD) $(LDFLAGS) $(LDOBJ) -o $(KERNEL) cp $(KERNEL) Root/System .PHONY: all @@ -47,4 +48,4 @@ all: h-core link .PHONY: clean clean: - rm -f Obj/*.o $(KERNEL) + rm -f $(LDOBJ) $(KERNEL) diff --git a/ReadMe.md b/ReadMe.md index ea49f6d4..d01c3c92 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -14,6 +14,6 @@ And execute: make all ``` -You'd also need The MultiProcessor UNIX SDK tools, to build and link the components. For Tools look at `Public/Tools` +You'd also need The SDK and MinGW, to build and link the components. For Tools look at `Public/Tools` ##### Copyright 2024, Mahrouss Logic, all rights reserved. \ No newline at end of file -- cgit v1.2.3