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/libSystem/src/SystemAPI.cc | 94 ---------------------------------------- dev/libSystem/src/SystemCalls.cc | 94 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 94 deletions(-) delete mode 100644 dev/libSystem/src/SystemAPI.cc create mode 100644 dev/libSystem/src/SystemCalls.cc (limited to 'dev/libSystem/src') 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 From d9f1a4f656ced76df3f23eeec678e1a3be1fd432 Mon Sep 17 00:00:00 2001 From: 0xf00sec <159052166+0xf00sec@users.noreply.github.com> Date: Sat, 9 Aug 2025 23:14:12 +0300 Subject: Update SystemCalls.cc --- dev/libSystem/src/SystemCalls.cc | 121 ++++++++++++++++++++++++++++++--------- 1 file changed, 95 insertions(+), 26 deletions(-) (limited to 'dev/libSystem/src') diff --git a/dev/libSystem/src/SystemCalls.cc b/dev/libSystem/src/SystemCalls.cc index 6344cdac..71eebc62 100644 --- a/dev/libSystem/src/SystemCalls.cc +++ b/dev/libSystem/src/SystemCalls.cc @@ -10,13 +10,41 @@ /// @file SystemAPI.cc /// @brief System wide API for NeKernel. +namespace Detail { + template + inline VoidPtr safe_void_cast(const T* ptr) { + return const_cast(static_cast(ptr)); + } +} + 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]; + auto src_bytes = static_cast(src); + auto dst_bytes = static_cast(dest); + + if (len >= sizeof(UInt64) && + (reinterpret_cast(src) % sizeof(UInt64)) == 0 && + (reinterpret_cast(dest) % sizeof(UInt64)) == 0) { + + auto src_words = static_cast(src); + auto dst_words = static_cast(dest); + SizeT word_count = len / sizeof(UInt64); + + for (SizeT i = 0; i < word_count; ++i) { + dst_words[i] = src_words[i]; + } + + SizeT remaining = len % sizeof(UInt64); + for (SizeT i = 0; i < remaining; ++i) { + dst_bytes[len - remaining + i] = src_bytes[len - remaining + i]; + } + } else { + for (SizeT i = 0; i < len; ++i) { + dst_bytes[i] = src_bytes[i]; + } } return dest; @@ -25,13 +53,13 @@ IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input Si IMPORT_C SInt64 MmStrLen(const Char* in) { if (!in) return 0; - SizeT len{0}; - - do { + SizeT len = 0; + + while (in[len] != '\0') { ++len; - } while (in[len] != '\0'); + } - return len; + return static_cast(len); } IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) { @@ -39,56 +67,97 @@ IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt return nullptr; } - for (SizeT i = 0; i < len; i++) { - ((Char*) dest)[i] = value; + auto dst_bytes = static_cast(dest); + + if (len >= sizeof(UInt64) && (reinterpret_cast(dest) % sizeof(UInt64)) == 0) { + // 64-bit pattern + UInt64 pattern = static_cast(value); + pattern |= (pattern << 8); + pattern |= (pattern << 16); + pattern |= (pattern << 32); + + auto dst_words = static_cast(dest); + SizeT word_count = len / sizeof(UInt64); + + for (SizeT i = 0; i < word_count; ++i) { + dst_words[i] = pattern; + } + + SizeT remaining = len % sizeof(UInt64); + for (SizeT i = 0; i < remaining; ++i) { + dst_bytes[len - remaining + i] = value; + } + } else { + for (SizeT i = 0; i < len; ++i) { + dst_bytes[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))); + return static_cast(libsys_syscall_arg_3( + SYSCALL_HASH("IoOpenFile"), + Detail::safe_void_cast(path), + Detail::safe_void_cast(drv_letter))); } IMPORT_C Void IoCloseFile(_Input Ref desc) { - libsys_syscall_arg_2(2, desc); + libsys_syscall_arg_2(SYSCALL_HASH("IoCloseFile"), static_cast(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)); + auto ret_ptr = libsys_syscall_arg_3( + SYSCALL_HASH("IoSeekFile"), + static_cast(desc), + reinterpret_cast(&off)); - MUST_PASS((*ret) != ~0UL); - return *ret; + if (!ret_ptr) { + return ~0UL; + } + + auto ret = static_cast(ret_ptr); + UInt64 result = *ret; + MUST_PASS(result != ~0UL); + return result; } IMPORT_C UInt64 IoTellFile(_Input Ref desc) { - auto ret = (volatile UInt64*) libsys_syscall_arg_2(SYSCALL_HASH("IoTellFile"), - reinterpret_cast(desc)); + auto ret_ptr = libsys_syscall_arg_2(SYSCALL_HASH("IoTellFile"), + static_cast(desc)); + + if (!ret_ptr) { + return ~0UL; + } + + auto ret = static_cast(ret_ptr); 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); + auto ret_ptr = libsys_syscall_arg_4( + SYSCALL_HASH("PrintOut"), + static_cast(desc), + Detail::safe_void_cast(fmt), + static_cast(&args)); va_end(args); - + + if (!ret_ptr) { + return -1; + } + + auto ret = static_cast(ret_ptr); 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 From c896d07fec6d949328a4ef09a80c47fe6f682e67 Mon Sep 17 00:00:00 2001 From: 0xf00sec <159052166+0xf00sec@users.noreply.github.com> Date: Sun, 10 Aug 2025 15:41:43 +0300 Subject: feat(libSystem): implement memory ops with alignment & overlap handling --- dev/libSystem/src/SystemCalls.cc | 167 +++++++++++++++++++++------------------ 1 file changed, 92 insertions(+), 75 deletions(-) (limited to 'dev/libSystem/src') diff --git a/dev/libSystem/src/SystemCalls.cc b/dev/libSystem/src/SystemCalls.cc index 71eebc62..83569b6f 100644 --- a/dev/libSystem/src/SystemCalls.cc +++ b/dev/libSystem/src/SystemCalls.cc @@ -7,9 +7,6 @@ #include #include -/// @file SystemAPI.cc -/// @brief System wide API for NeKernel. - namespace Detail { template inline VoidPtr safe_void_cast(const T* ptr) { @@ -17,33 +14,67 @@ namespace Detail { } } +// memmove-style copy IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len) { - if (!len || !dest || !src) { - return nullptr; - } - - auto src_bytes = static_cast(src); - auto dst_bytes = static_cast(dest); - - if (len >= sizeof(UInt64) && - (reinterpret_cast(src) % sizeof(UInt64)) == 0 && - (reinterpret_cast(dest) % sizeof(UInt64)) == 0) { - - auto src_words = static_cast(src); - auto dst_words = static_cast(dest); - SizeT word_count = len / sizeof(UInt64); - - for (SizeT i = 0; i < word_count; ++i) { - dst_words[i] = src_words[i]; - } - - SizeT remaining = len % sizeof(UInt64); - for (SizeT i = 0; i < remaining; ++i) { - dst_bytes[len - remaining + i] = src_bytes[len - remaining + i]; + // handles overlap, prefers 64-bit word copies when aligned + if (!len || !dest || !src) return nullptr; + + auto s = static_cast(src); + auto d = static_cast(dest); + + if (d == s) return dest; + + // decide direction + if (d > s && d < s + len) { + const UInt8* rs = s + len; + UInt8* rd = d + len; + + // try 64-bit aligned backward copy + if (len >= sizeof(UInt64) && + (reinterpret_cast(rs) % sizeof(UInt64) == 0) && + (reinterpret_cast(rd) % sizeof(UInt64) == 0)) { + + auto rsw = reinterpret_cast(rs); + auto rdw = reinterpret_cast(rd); + SizeT words = len / sizeof(UInt64); + + for (SizeT i = 0; i < words; ++i) { + rdw[-1 - static_cast(i)] = rsw[-1 - static_cast(i)]; + } + + SizeT rem = len % sizeof(UInt64); + for (SizeT i = 0; i < rem; ++i) { + rd[-1 - i] = rs[-1 - i]; + } + } else { + // byte-wise backward + for (SizeT i = 0; i < len; ++i) { + rd[-1 - i] = rs[-1 - i]; + } } } else { - for (SizeT i = 0; i < len; ++i) { - dst_bytes[i] = src_bytes[i]; + // try 64-bit aligned forward copy + if (len >= sizeof(UInt64) && + (reinterpret_cast(s) % sizeof(UInt64) == 0) && + (reinterpret_cast(d) % sizeof(UInt64) == 0)) { + + auto sw = reinterpret_cast(s); + auto dw = reinterpret_cast(d); + SizeT words = len / sizeof(UInt64); + + for (SizeT i = 0; i < words; ++i) { + dw[i] = sw[i]; + } + + SizeT rem = len % sizeof(UInt64); + const SizeT offset = words * sizeof(UInt64); + for (SizeT i = 0; i < rem; ++i) { + d[offset + i] = s[offset + i]; + } + } else { + for (SizeT i = 0; i < len; ++i) { + d[i] = s[i]; + } } } @@ -51,46 +82,38 @@ IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input Si } IMPORT_C SInt64 MmStrLen(const Char* in) { + // strlen via pointer walk if (!in) return 0; - - SizeT len = 0; - - while (in[len] != '\0') { - ++len; - } - - return static_cast(len); + const Char* p = in; + while (*p) ++p; + return static_cast(p - in); } IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) { - if (!len || !dest) { - return nullptr; - } + if (!len || !dest) return nullptr; + + auto d = static_cast(dest); - auto dst_bytes = static_cast(dest); - - if (len >= sizeof(UInt64) && (reinterpret_cast(dest) % sizeof(UInt64)) == 0) { - // 64-bit pattern + if (len >= sizeof(UInt64) && (reinterpret_cast(d) % sizeof(UInt64)) == 0) { UInt64 pattern = static_cast(value); pattern |= (pattern << 8); pattern |= (pattern << 16); pattern |= (pattern << 32); - - auto dst_words = static_cast(dest); - SizeT word_count = len / sizeof(UInt64); - - for (SizeT i = 0; i < word_count; ++i) { - dst_words[i] = pattern; + + auto dw = reinterpret_cast(d); + SizeT words = len / sizeof(UInt64); + + for (SizeT i = 0; i < words; ++i) { + dw[i] = pattern; } - - SizeT remaining = len % sizeof(UInt64); - for (SizeT i = 0; i < remaining; ++i) { - dst_bytes[len - remaining + i] = value; + + SizeT rem = len % sizeof(UInt64); + const SizeT offset = words * sizeof(UInt64); + for (SizeT i = 0; i < rem; ++i) { + d[offset + i] = value; } } else { - for (SizeT i = 0; i < len; ++i) { - dst_bytes[i] = value; - } + for (SizeT i = 0; i < len; ++i) d[i] = value; } return dest; @@ -109,13 +132,11 @@ IMPORT_C Void IoCloseFile(_Input Ref desc) { IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) { auto ret_ptr = libsys_syscall_arg_3( - SYSCALL_HASH("IoSeekFile"), - static_cast(desc), + SYSCALL_HASH("IoSeekFile"), + static_cast(desc), reinterpret_cast(&off)); - if (!ret_ptr) { - return ~0UL; - } + if (!ret_ptr) return ~0UL; auto ret = static_cast(ret_ptr); UInt64 result = *ret; @@ -126,31 +147,27 @@ IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) { IMPORT_C UInt64 IoTellFile(_Input Ref desc) { auto ret_ptr = libsys_syscall_arg_2(SYSCALL_HASH("IoTellFile"), static_cast(desc)); - - if (!ret_ptr) { - return ~0UL; - } - + if (!ret_ptr) return ~0UL; auto ret = static_cast(ret_ptr); return *ret; } IMPORT_C SInt32 PrintOut(_Input IORef desc, const char* fmt, ...) { + constexpr SizeT BUF_SZ = 1024; + char buf[BUF_SZ]; + va_list args; va_start(args, fmt); + int needed = vsnprintf(buf, BUF_SZ, fmt, args); + va_end(args); - auto ret_ptr = libsys_syscall_arg_4( - SYSCALL_HASH("PrintOut"), + // if truncated, `needed` >= BUF_SZ; we still send truncated buffer + auto ret_ptr = libsys_syscall_arg_3( + SYSCALL_HASH("PrintOut"), static_cast(desc), - Detail::safe_void_cast(fmt), - static_cast(&args)); + Detail::safe_void_cast(buf)); - va_end(args); - - if (!ret_ptr) { - return -1; - } - + if (!ret_ptr) return -1; auto ret = static_cast(ret_ptr); return *ret; } -- cgit v1.2.3 From 73e85b95355f56081a8a83e29b4ba6005bf163b2 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sun, 10 Aug 2025 23:00:00 +0200 Subject: feat(libSystem): improved 'PrintOut' to use 'StrFmt' instead of stdc's string format. Signed-off-by: Amlal --- dev/libSystem/SystemKit/System.h | 8 +- dev/libSystem/src/System.cc | 173 +++++++++++++++++++++++++++++++++++++ dev/libSystem/src/SystemCalls.cc | 180 --------------------------------------- 3 files changed, 177 insertions(+), 184 deletions(-) create mode 100644 dev/libSystem/src/System.cc delete mode 100644 dev/libSystem/src/SystemCalls.cc (limited to 'dev/libSystem/src') diff --git a/dev/libSystem/SystemKit/System.h b/dev/libSystem/SystemKit/System.h index 421868ae..f46fe523 100644 --- a/dev/libSystem/SystemKit/System.h +++ b/dev/libSystem/SystemKit/System.h @@ -12,6 +12,10 @@ Purpose: System Call Interface. #include +/// @brief TTY device path. +#define kPrintDevicePath "/devices/tty{}" +#define kCDDevicePath "/devices/dvd{}" + // ------------------------------------------------------------------------------------------ // /// @brief Types API. // ------------------------------------------------------------------------------------------ // @@ -309,8 +313,6 @@ IMPORT_C SInt32 PwrSendCode(_Output SInt32& code); // CD-ROM API. // ------------------------------------------------------------------------------------------ // -#define kCDDevicePath "/devices/dvd{}" - IMPORT_C IORef CdOpenTray(Void); IMPORT_C SInt32 CdEjectDrive(_Input IORef cdrom); @@ -321,8 +323,6 @@ IMPORT_C SInt32 CdCloseTray(Void); // TTY API. // ------------------------------------------------------------------------------------------ // -#define kPrintDevicePath "/devices/tty{}" - IMPORT_C SInt32 PrintOut(IORef file /* nullptr to direct to stdout */, const Char* fmt, ...); IMPORT_C SInt32 PrintIn(IORef file /* nullptr to direct to stdout */, const Char* fmt, ...); diff --git a/dev/libSystem/src/System.cc b/dev/libSystem/src/System.cc new file mode 100644 index 00000000..3870ff18 --- /dev/null +++ b/dev/libSystem/src/System.cc @@ -0,0 +1,173 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include +#include +#include + +namespace Detail { +template +static VoidPtr safe_void_cast(const T* ptr) { + _rtl_assert(ptr, "safe void cast failed!"); + return static_cast(const_cast(ptr)); +} +} // namespace Detail + +IMPORT_C Void _rtl_assert(Bool expr, const Char* origin) { + if (!expr) { + PrintOut(nullptr, "Assertion failed: %s\r", origin); + libsys_syscall_arg_1(SYSCALL_HASH("_rtl_debug_break")); + } +} + +// memmove-style copy +IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len) { + // handles overlap, prefers 64-bit word copies when aligned + if (!len || !dest || !src) return nullptr; + + auto s = static_cast(src); + auto d = static_cast(dest); + + if (d == s) return dest; + + // decide direction + if (d > s && d < s + len) { + const UInt8* rs = s + len; + UInt8* rd = d + len; + + // try 64-bit aligned backward copy + if (len >= sizeof(UInt64) && (reinterpret_cast(rs) % sizeof(UInt64) == 0) && + (reinterpret_cast(rd) % sizeof(UInt64) == 0)) { + auto rsw = reinterpret_cast(rs); + auto rdw = reinterpret_cast(rd); + SizeT words = len / sizeof(UInt64); + + for (SizeT i = 0; i < words; ++i) { + rdw[-1 - static_cast(i)] = rsw[-1 - static_cast(i)]; + } + + SizeT rem = len % sizeof(UInt64); + for (SizeT i = 0; i < rem; ++i) { + rd[-1 - i] = rs[-1 - i]; + } + } else { + // byte-wise backward + for (SizeT i = 0; i < len; ++i) { + rd[-1 - i] = rs[-1 - i]; + } + } + } else { + // try 64-bit aligned forward copy + if (len >= sizeof(UInt64) && (reinterpret_cast(s) % sizeof(UInt64) == 0) && + (reinterpret_cast(d) % sizeof(UInt64) == 0)) { + auto sw = reinterpret_cast(s); + auto dw = reinterpret_cast(d); + SizeT words = len / sizeof(UInt64); + + for (SizeT i = 0; i < words; ++i) { + dw[i] = sw[i]; + } + + SizeT rem = len % sizeof(UInt64); + const SizeT offset = words * sizeof(UInt64); + for (SizeT i = 0; i < rem; ++i) { + d[offset + i] = s[offset + i]; + } + } else { + for (SizeT i = 0; i < len; ++i) { + d[i] = s[i]; + } + } + } + + return dest; +} + +IMPORT_C SInt64 MmStrLen(const Char* in) { + // strlen via pointer walk + if (!in) return 0; + const Char* p = in; + while (*p) ++p; + return static_cast(p - in); +} + +IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) { + if (!len || !dest) return nullptr; + + auto d = static_cast(dest); + + if (len >= sizeof(UInt64) && (reinterpret_cast(d) % sizeof(UInt64)) == 0) { + UInt64 pattern = static_cast(value); + pattern |= (pattern << 8); + pattern |= (pattern << 16); + pattern |= (pattern << 32); + + auto dw = reinterpret_cast(d); + SizeT words = len / sizeof(UInt64); + + for (SizeT i = 0; i < words; ++i) { + dw[i] = pattern; + } + + SizeT rem = len % sizeof(UInt64); + const SizeT offset = words * sizeof(UInt64); + for (SizeT i = 0; i < rem; ++i) { + d[offset + i] = value; + } + } else { + for (SizeT i = 0; i < len; ++i) d[i] = value; + } + + return dest; +} + +IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter) { + return static_cast(libsys_syscall_arg_3(SYSCALL_HASH("IoOpenFile"), + Detail::safe_void_cast(path), + Detail::safe_void_cast(drv_letter))); +} + +IMPORT_C Void IoCloseFile(_Input Ref desc) { + libsys_syscall_arg_2(SYSCALL_HASH("IoCloseFile"), static_cast(desc)); +} + +IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) { + auto ret_ptr = libsys_syscall_arg_3(SYSCALL_HASH("IoSeekFile"), static_cast(desc), + reinterpret_cast(&off)); + + if (!ret_ptr) return ~0UL; + + auto ret = static_cast(ret_ptr); + UInt64 result = *ret; + MUST_PASS(result != ~0UL); + return result; +} + +IMPORT_C UInt64 IoTellFile(_Input Ref desc) { + auto ret_ptr = libsys_syscall_arg_2(SYSCALL_HASH("IoTellFile"), static_cast(desc)); + if (!ret_ptr) return ~0UL; + auto ret = static_cast(ret_ptr); + return *ret; +} + +IMPORT_C SInt32 PrintOut(_Input IORef desc, const Char* fmt, ...) { + va_list args; + va_start(args, fmt); + + auto buf = StrFmt(fmt, args); + + va_end(args); + + // if truncated, `needed` >= kBufferSz; we still send truncated buffer + auto ret_ptr = libsys_syscall_arg_3(SYSCALL_HASH("PrintOut"), static_cast(desc), + Detail::safe_void_cast(buf)); + + if (!ret_ptr) return -kErrorInvalidData; + + auto ret = static_cast(ret_ptr); + + return *ret; +} diff --git a/dev/libSystem/src/SystemCalls.cc b/dev/libSystem/src/SystemCalls.cc deleted file mode 100644 index 83569b6f..00000000 --- a/dev/libSystem/src/SystemCalls.cc +++ /dev/null @@ -1,180 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#include -#include - -namespace Detail { - template - inline VoidPtr safe_void_cast(const T* ptr) { - return const_cast(static_cast(ptr)); - } -} - -// memmove-style copy -IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len) { - // handles overlap, prefers 64-bit word copies when aligned - if (!len || !dest || !src) return nullptr; - - auto s = static_cast(src); - auto d = static_cast(dest); - - if (d == s) return dest; - - // decide direction - if (d > s && d < s + len) { - const UInt8* rs = s + len; - UInt8* rd = d + len; - - // try 64-bit aligned backward copy - if (len >= sizeof(UInt64) && - (reinterpret_cast(rs) % sizeof(UInt64) == 0) && - (reinterpret_cast(rd) % sizeof(UInt64) == 0)) { - - auto rsw = reinterpret_cast(rs); - auto rdw = reinterpret_cast(rd); - SizeT words = len / sizeof(UInt64); - - for (SizeT i = 0; i < words; ++i) { - rdw[-1 - static_cast(i)] = rsw[-1 - static_cast(i)]; - } - - SizeT rem = len % sizeof(UInt64); - for (SizeT i = 0; i < rem; ++i) { - rd[-1 - i] = rs[-1 - i]; - } - } else { - // byte-wise backward - for (SizeT i = 0; i < len; ++i) { - rd[-1 - i] = rs[-1 - i]; - } - } - } else { - // try 64-bit aligned forward copy - if (len >= sizeof(UInt64) && - (reinterpret_cast(s) % sizeof(UInt64) == 0) && - (reinterpret_cast(d) % sizeof(UInt64) == 0)) { - - auto sw = reinterpret_cast(s); - auto dw = reinterpret_cast(d); - SizeT words = len / sizeof(UInt64); - - for (SizeT i = 0; i < words; ++i) { - dw[i] = sw[i]; - } - - SizeT rem = len % sizeof(UInt64); - const SizeT offset = words * sizeof(UInt64); - for (SizeT i = 0; i < rem; ++i) { - d[offset + i] = s[offset + i]; - } - } else { - for (SizeT i = 0; i < len; ++i) { - d[i] = s[i]; - } - } - } - - return dest; -} - -IMPORT_C SInt64 MmStrLen(const Char* in) { - // strlen via pointer walk - if (!in) return 0; - const Char* p = in; - while (*p) ++p; - return static_cast(p - in); -} - -IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) { - if (!len || !dest) return nullptr; - - auto d = static_cast(dest); - - if (len >= sizeof(UInt64) && (reinterpret_cast(d) % sizeof(UInt64)) == 0) { - UInt64 pattern = static_cast(value); - pattern |= (pattern << 8); - pattern |= (pattern << 16); - pattern |= (pattern << 32); - - auto dw = reinterpret_cast(d); - SizeT words = len / sizeof(UInt64); - - for (SizeT i = 0; i < words; ++i) { - dw[i] = pattern; - } - - SizeT rem = len % sizeof(UInt64); - const SizeT offset = words * sizeof(UInt64); - for (SizeT i = 0; i < rem; ++i) { - d[offset + i] = value; - } - } else { - for (SizeT i = 0; i < len; ++i) d[i] = value; - } - - return dest; -} - -IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter) { - return static_cast(libsys_syscall_arg_3( - SYSCALL_HASH("IoOpenFile"), - Detail::safe_void_cast(path), - Detail::safe_void_cast(drv_letter))); -} - -IMPORT_C Void IoCloseFile(_Input Ref desc) { - libsys_syscall_arg_2(SYSCALL_HASH("IoCloseFile"), static_cast(desc)); -} - -IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) { - auto ret_ptr = libsys_syscall_arg_3( - SYSCALL_HASH("IoSeekFile"), - static_cast(desc), - reinterpret_cast(&off)); - - if (!ret_ptr) return ~0UL; - - auto ret = static_cast(ret_ptr); - UInt64 result = *ret; - MUST_PASS(result != ~0UL); - return result; -} - -IMPORT_C UInt64 IoTellFile(_Input Ref desc) { - auto ret_ptr = libsys_syscall_arg_2(SYSCALL_HASH("IoTellFile"), - static_cast(desc)); - if (!ret_ptr) return ~0UL; - auto ret = static_cast(ret_ptr); - return *ret; -} - -IMPORT_C SInt32 PrintOut(_Input IORef desc, const char* fmt, ...) { - constexpr SizeT BUF_SZ = 1024; - char buf[BUF_SZ]; - - va_list args; - va_start(args, fmt); - int needed = vsnprintf(buf, BUF_SZ, fmt, args); - va_end(args); - - // if truncated, `needed` >= BUF_SZ; we still send truncated buffer - auto ret_ptr = libsys_syscall_arg_3( - SYSCALL_HASH("PrintOut"), - static_cast(desc), - Detail::safe_void_cast(buf)); - - if (!ret_ptr) return -1; - auto ret = static_cast(ret_ptr); - return *ret; -} - -IMPORT_C Void _rtl_assert(Bool expr, const Char* origin) { - if (!expr) { - PrintOut(nullptr, "Assertion failed: %s\r", origin); - libsys_syscall_arg_1(SYSCALL_HASH("_rtl_debug_break")); - } -} -- cgit v1.2.3