From b5ebbd7406e87d19beee3760ef2417e1444a10d2 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 17 Oct 2024 08:16:38 +0200 Subject: FIX/IMP: A set of fixes and new features. (MHR-40, 46 and 47 related) Signed-off-by: Amlal El Mahrouss --- dev/crt/base_exception.hxx | 7 ++++- dev/crt/crt.json | 20 -------------- dev/crt/crtx64.json | 20 ++++++++++++++ dev/crt/defines.hxx | 1 + dev/crt/src/hal/x86/base_alloc.cxx | 55 ++++++++++++++++++++++++++++++++++++++ dev/crt/src/hal/x86/exit.cxx | 19 +++++++++++++ dev/crt/src/new+delete.cxx | 52 ----------------------------------- dev/zba/amd64-efi.make | 2 +- 8 files changed, 102 insertions(+), 74 deletions(-) delete mode 100644 dev/crt/crt.json create mode 100644 dev/crt/crtx64.json create mode 100644 dev/crt/src/hal/x86/base_alloc.cxx create mode 100644 dev/crt/src/hal/x86/exit.cxx delete mode 100644 dev/crt/src/new+delete.cxx (limited to 'dev') diff --git a/dev/crt/base_exception.hxx b/dev/crt/base_exception.hxx index e73ac11b..e4ee3487 100644 --- a/dev/crt/base_exception.hxx +++ b/dev/crt/base_exception.hxx @@ -9,7 +9,6 @@ #include #include -/// @brief Standard C++ namespace namespace std { inline void __throw_general(void) @@ -23,6 +22,12 @@ namespace std __builtin_unreachable(); // prevent from continuing. } + inline void __throw_bad_alloc(void) + { + __throw_general(); + __builtin_unreachable(); // prevent from continuing. + } + inline void __throw_bad_array_new_length(void) { __throw_general(); diff --git a/dev/crt/crt.json b/dev/crt/crt.json deleted file mode 100644 index fbd54bf8..00000000 --- a/dev/crt/crt.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "compiler_path": "x86_64-w64-mingw32-g++", - "compiler_std": "c++20", - "headers_path": ["../", "./"], - "sources_path": ["src/*.cxx"], - "output_name": "crt.dll", - "compiler_flags": [ - "-ffreestanding", - "-shared", - "-fno-rtti", - "-fno-exceptions", - "-Wl,--subsystem=17" - ], - "cpp_macros": [ - "__CRT_AMD64__", - "cCRTVersion=0x0100", - "cEFSVersionHighest=0x0100", - "cEFSVersionLowest=0x0100" - ] -} diff --git a/dev/crt/crtx64.json b/dev/crt/crtx64.json new file mode 100644 index 00000000..3e321e5d --- /dev/null +++ b/dev/crt/crtx64.json @@ -0,0 +1,20 @@ +{ + "compiler_path": "x86_64-w64-mingw32-g++", + "compiler_std": "c++20", + "headers_path": ["../", "./"], + "sources_path": ["src/*.cxx", "src/hal/x86/*.cxx"], + "output_name": "crtx64.dll", + "compiler_flags": [ + "-ffreestanding", + "-shared", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17" + ], + "cpp_macros": [ + "__CRT_AMD64__", + "cCRTVersion=0x0100", + "cEFSVersionHighest=0x0100", + "cEFSVersionLowest=0x0100" + ] +} diff --git a/dev/crt/defines.hxx b/dev/crt/defines.hxx index 5636c337..73422165 100644 --- a/dev/crt/defines.hxx +++ b/dev/crt/defines.hxx @@ -79,6 +79,7 @@ typedef union double_cast { #endif // ifdef __STD_CXX__ +/// @brief Standard C++ namespace. namespace std { /// @brief Forward object. diff --git a/dev/crt/src/hal/x86/base_alloc.cxx b/dev/crt/src/hal/x86/base_alloc.cxx new file mode 100644 index 00000000..effb6c94 --- /dev/null +++ b/dev/crt/src/hal/x86/base_alloc.cxx @@ -0,0 +1,55 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include + +#define kAllocSyscallId "mov $10, %%r9\n\t" +#define kFreeSyscallId "mov $11, %%r9\n\t" + +void* operator new(size_t length) +{ + void* ptr = nullptr; + + // do syscall according to PEF convention. + asm("mov %0, %%r8\n\t" kAllocSyscallId + "syscall" + : "=r"(ptr)); + + if (!ptr) + std::__throw_bad_alloc(); + + return ptr; +} + +void* operator new[](size_t length) +{ + ptr_type ptr = nullptr; + + // do syscall according to PEF convention. + asm("mov %0, %%r8\n\t" kAllocSyscallId + "syscall" + : "=r"(ptr)); + + if (!ptr) + std::__throw_bad_alloc(); + + return ptr; +} + +void operator delete(void* ptr) noexcept +{ + asm("mov %0, %%r8\n\t" kFreeSyscallId + "syscall" + : "=r"(ptr)); +} + +void operator delete[](void* ptr) noexcept +{ + asm("mov %0, %%r8\n\t" kFreeSyscallId + "syscall" + : "=r"(ptr)); +} diff --git a/dev/crt/src/hal/x86/exit.cxx b/dev/crt/src/hal/x86/exit.cxx new file mode 100644 index 00000000..d593adf9 --- /dev/null +++ b/dev/crt/src/hal/x86/exit.cxx @@ -0,0 +1,19 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include + +#define kAllocSyscallId "mov $12, %%r9\n\t" + +/// @note Just here for building. +extern "C" int exit(int code) +{ + asm("mov 0, %%r8\n\t" kAllocSyscallId + "syscall" + : "=r"(code)); + + return 1; +} diff --git a/dev/crt/src/new+delete.cxx b/dev/crt/src/new+delete.cxx deleted file mode 100644 index 1241bf66..00000000 --- a/dev/crt/src/new+delete.cxx +++ /dev/null @@ -1,52 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include - -#define kAllocSyscallId "mov $10, %%r9\n\t" -#define kFreeSyscallId "mov $11, %%r9\n\t" - -void* operator new(size_t length) -{ - void* ptr = nullptr; - - // do syscall according to PEF convention. - asm ("mov %0, %%r8\n\t" - kAllocSyscallId - "syscall" - : "=r" (ptr)); - - return ptr; -} - -void* operator new[](size_t length) -{ - ptr_type ptr = nullptr; - - // do syscall according to PEF convention. - asm ("mov %0, %%r8\n\t" - kAllocSyscallId - "syscall" - : "=r" (ptr)); - - return ptr; -} - -void operator delete(void* ptr) noexcept -{ - asm ("mov %0, %%r8\n\t" - kFreeSyscallId - "syscall" - : "=r" (ptr)); -} - -void operator delete[](void* ptr) noexcept -{ - asm ("mov %0, %%r8\n\t" - kFreeSyscallId - "syscall" - : "=r" (ptr)); -} diff --git a/dev/zba/amd64-efi.make b/dev/zba/amd64-efi.make index 7bf3ceaa..e1a212ef 100644 --- a/dev/zba/amd64-efi.make +++ b/dev/zba/amd64-efi.make @@ -55,7 +55,7 @@ BOOT_LOADER=newosldr.exe KERNEL=minoskrnl.exe DDK=ddk.dll SCI=sci.dll -CRT=crt.dll +CRT=crtx64.dll SYS_CHK=syschk.sys STARTUP=startup.sys -- cgit v1.2.3