From 41703b62f9e7e83fa856fbf53101edc889502c45 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 22 Jun 2025 21:41:15 +0200 Subject: feat: use FNV in libSystem's syscall routing. fix: Add legacy string.h functions back, for GCC. Signed-off-by: Amlal El Mahrouss --- dev/boot/src/BootSupport.cc | 58 ++++++++++++++++++++++-- dev/libSystem/SystemKit/Syscall.h | 22 +++++++-- dev/libSystem/src/SystemAPI.cc | 94 --------------------------------------- dev/libSystem/src/SystemCalls.cc | 94 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 102 deletions(-) delete mode 100644 dev/libSystem/src/SystemAPI.cc create mode 100644 dev/libSystem/src/SystemCalls.cc (limited to 'dev') diff --git a/dev/boot/src/BootSupport.cc b/dev/boot/src/BootSupport.cc index 7cf7af95..96c6594e 100644 --- a/dev/boot/src/BootSupport.cc +++ b/dev/boot/src/BootSupport.cc @@ -18,7 +18,7 @@ /// @param dst destination pointer. /// @param byte value to fill in. /// @param len length of of src. -EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len, long long unsigned int dst_size) { +EXTERN_C VoidPtr memnset(void* dst, int byte, long long unsigned int len, long long unsigned int dst_size) { if (!dst || len > dst_size) { // For now, we return nullptr or an error status. return nullptr; @@ -35,7 +35,7 @@ EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len, long lo /// @param dst destination pointer. /// @param src source pointer. /// @param len length of of src. -EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len, long long unsigned int dst_size) { +EXTERN_C VoidPtr memncpy(void* dst, const void* src, long long unsigned int len, long long unsigned int dst_size) { if (!dst || !src || len > dst_size) { // Similar to memset, this is a critical failure. return nullptr; @@ -49,7 +49,7 @@ EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len, } /// @brief strlen definition in C++. -EXTERN_C size_t strlen(const char* whatToCheck, size_t max_len) { +EXTERN_C size_t strnlen(const char* whatToCheck, size_t max_len) { size_t len = 0; while (len < max_len && whatToCheck[len] != '\0') { ++len; @@ -58,7 +58,7 @@ EXTERN_C size_t strlen(const char* whatToCheck, size_t max_len) { } /// @brief strcmp definition in C++. -EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight, size_t max_len) { +EXTERN_C int strncmp(const char* whatToCheck, const char* whatToCheckRight, size_t max_len) { size_t i = 0; while (i < max_len && whatToCheck[i] == whatToCheckRight[i]) { if (whatToCheck[i] == '\0') return 0; @@ -73,4 +73,54 @@ EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight, size_ /// @brief something specific to the Microsoft's ABI, When the stack grows too big. EXTERN_C void ___chkstk_ms(void) {} +/// @note GCC expects them to be here. + +/// @brief memset definition in C++. +/// @param dst destination pointer. +/// @param byte value to fill in. +/// @param len length of of src. +EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len) { + for (size_t i = 0UL; i < len; ++i) { + ((int*) dst)[i] = byte; + } + + return dst; +} + +/// @brief memcpy definition in C++. +/// @param dst destination pointer. +/// @param src source pointer. +/// @param len length of of src. +EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len) { + for (size_t i = 0UL; i < len; ++i) { + ((int*) dst)[i] = ((int*) src)[i]; + } + + return dst; +} + +/// @brief strlen definition in C++. +EXTERN_C size_t strlen(const char* whatToCheck) { + SizeT len = 0; + + while (whatToCheck[len] != 0) { + ++len; + } + + return len; +} + +/// @brief strcmp definition in C++. +EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight) { + SizeT len = 0; + + while (whatToCheck[len] == whatToCheckRight[len]) { + if (whatToCheck[len] == 0) return 0; + + ++len; + } + + return len; +} + #endif diff --git a/dev/libSystem/SystemKit/Syscall.h b/dev/libSystem/SystemKit/Syscall.h index 436665ae..5a840df1 100644 --- a/dev/libSystem/SystemKit/Syscall.h +++ b/dev/libSystem/SystemKit/Syscall.h @@ -9,11 +9,25 @@ #include #include -#ifndef SYSCALL_HASH -#define SYSCALL_HASH(str) (UInt64) str -#endif // !SYSCALL_HASH - IMPORT_C VoidPtr libsys_syscall_arg_1(SizeT id); IMPORT_C VoidPtr libsys_syscall_arg_2(SizeT id, VoidPtr arg1); IMPORT_C VoidPtr libsys_syscall_arg_3(SizeT id, VoidPtr arg1, VoidPtr arg3); IMPORT_C VoidPtr libsys_syscall_arg_4(SizeT id, VoidPtr arg1, VoidPtr arg3, VoidPtr arg4); + +inline UInt64 libsys_hash_64(const Char* path) { + const UInt64 FNV_OFFSET_BASIS = 0xcbf29ce484222325ULL; + const UInt64 FNV_PRIME = 0x100000001b3ULL; + + UInt64 hash = FNV_OFFSET_BASIS; + + while (*path) { + hash ^= (Char) (*path++); + hash *= FNV_PRIME; + } + + return hash; +} + +#ifndef SYSCALL_HASH +#define SYSCALL_HASH(str) libsys_hash_64(str) +#endif // !SYSCALL_HASH \ No newline at end of file diff --git a/dev/libSystem/src/SystemAPI.cc b/dev/libSystem/src/SystemAPI.cc deleted file mode 100644 index d0682830..00000000 --- a/dev/libSystem/src/SystemAPI.cc +++ /dev/null @@ -1,94 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#include -#include - -/// @file SystemAPI.cc -/// @brief System wide API for NeKernel. - -IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len) { - if (!len || !dest || !src) { - return nullptr; - } - - for (SizeT i = 0; i < len; i++) { - ((Char*) dest)[i] = ((Char*) src)[i]; - } - - return dest; -} - -IMPORT_C SInt64 MmStrLen(const Char* in) { - if (!in) return 0; - - SizeT len{0}; - - do { - ++len; - } while (in[len] != '\0'); - - return len; -} - -IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) { - if (!len || !dest) { - return nullptr; - } - - for (SizeT i = 0; i < len; i++) { - ((Char*) dest)[i] = value; - } - - return dest; -} - -IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter) { - return (Ref) libsys_syscall_arg_3(SYSCALL_HASH('IoOpenFile'), - reinterpret_cast(const_cast(path)), - reinterpret_cast(const_cast(drv_letter))); -} - -IMPORT_C Void IoCloseFile(_Input Ref desc) { - libsys_syscall_arg_2(2, desc); -} - -IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) { - auto ret = (volatile UInt64*) libsys_syscall_arg_3( - SYSCALL_HASH('IoSeekFile'), reinterpret_cast(desc), reinterpret_cast(&off)); - - MUST_PASS((*ret) != ~0UL); - return *ret; -} - -IMPORT_C UInt64 IoTellFile(_Input Ref desc) { - auto ret = (volatile UInt64*) libsys_syscall_arg_2(SYSCALL_HASH('IoTellFile'), - reinterpret_cast(desc)); - return *ret; -} - -IMPORT_C SInt32 PrintOut(_Input IORef desc, const char* fmt, ...) { - va_list args; - - va_start(args, fmt); - - auto ret = (volatile UInt64*) libsys_syscall_arg_4( - SYSCALL_HASH('PrintOut'), reinterpret_cast(desc), - reinterpret_cast(const_cast(fmt)), args); - - va_end(args); - - return *ret; -} - -IMPORT_C Void _rtl_assert(Bool expr, const Char* origin) { - if (!expr) { - PrintOut(nullptr, "Assertion failed: %s\r", origin); - PrintOut(nullptr, "Origin: %s\r", origin); - - libsys_syscall_arg_1(SYSCALL_HASH('_rtl_debug_break')); - } -} diff --git a/dev/libSystem/src/SystemCalls.cc b/dev/libSystem/src/SystemCalls.cc new file mode 100644 index 00000000..6344cdac --- /dev/null +++ b/dev/libSystem/src/SystemCalls.cc @@ -0,0 +1,94 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include +#include + +/// @file SystemAPI.cc +/// @brief System wide API for NeKernel. + +IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len) { + if (!len || !dest || !src) { + return nullptr; + } + + for (SizeT i = 0; i < len; i++) { + ((Char*) dest)[i] = ((Char*) src)[i]; + } + + return dest; +} + +IMPORT_C SInt64 MmStrLen(const Char* in) { + if (!in) return 0; + + SizeT len{0}; + + do { + ++len; + } while (in[len] != '\0'); + + return len; +} + +IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) { + if (!len || !dest) { + return nullptr; + } + + for (SizeT i = 0; i < len; i++) { + ((Char*) dest)[i] = value; + } + + return dest; +} + +IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter) { + return (Ref) libsys_syscall_arg_3(SYSCALL_HASH("IoOpenFile"), + reinterpret_cast(const_cast(path)), + reinterpret_cast(const_cast(drv_letter))); +} + +IMPORT_C Void IoCloseFile(_Input Ref desc) { + libsys_syscall_arg_2(2, desc); +} + +IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) { + auto ret = (volatile UInt64*) libsys_syscall_arg_3( + SYSCALL_HASH("IoSeekFile"), reinterpret_cast(desc), reinterpret_cast(&off)); + + MUST_PASS((*ret) != ~0UL); + return *ret; +} + +IMPORT_C UInt64 IoTellFile(_Input Ref desc) { + auto ret = (volatile UInt64*) libsys_syscall_arg_2(SYSCALL_HASH("IoTellFile"), + reinterpret_cast(desc)); + return *ret; +} + +IMPORT_C SInt32 PrintOut(_Input IORef desc, const char* fmt, ...) { + va_list args; + + va_start(args, fmt); + + auto ret = (volatile UInt64*) libsys_syscall_arg_4( + SYSCALL_HASH("PrintOut"), reinterpret_cast(desc), + reinterpret_cast(const_cast(fmt)), args); + + va_end(args); + + return *ret; +} + +IMPORT_C Void _rtl_assert(Bool expr, const Char* origin) { + if (!expr) { + PrintOut(nullptr, "Assertion failed: %s\r", origin); + PrintOut(nullptr, "Origin: %s\r", origin); + + libsys_syscall_arg_1(SYSCALL_HASH("_rtl_debug_break")); + } +} -- cgit v1.2.3