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/src/hal/x86/base_alloc.cxx | 55 ++++++++++++++++++++++++++++++++++++++ dev/crt/src/hal/x86/exit.cxx | 19 +++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 dev/crt/src/hal/x86/base_alloc.cxx create mode 100644 dev/crt/src/hal/x86/exit.cxx (limited to 'dev/crt/src/hal') 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; +} -- cgit v1.2.3