summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--dev/libSystem/src/SystemCalls.cc167
1 files changed, 92 insertions, 75 deletions
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 <libSystem/SystemKit/Syscall.h>
#include <libSystem/SystemKit/System.h>
-/// @file SystemAPI.cc
-/// @brief System wide API for NeKernel.
-
namespace Detail {
template<typename T>
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<const UInt8*>(src);
- auto dst_bytes = static_cast<UInt8*>(dest);
-
- if (len >= sizeof(UInt64) &&
- (reinterpret_cast<UIntPtr>(src) % sizeof(UInt64)) == 0 &&
- (reinterpret_cast<UIntPtr>(dest) % sizeof(UInt64)) == 0) {
-
- auto src_words = static_cast<const UInt64*>(src);
- auto dst_words = static_cast<UInt64*>(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<const UInt8*>(src);
+ auto d = static_cast<UInt8*>(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<UIntPtr>(rs) % sizeof(UInt64) == 0) &&
+ (reinterpret_cast<UIntPtr>(rd) % sizeof(UInt64) == 0)) {
+
+ auto rsw = reinterpret_cast<const UInt64*>(rs);
+ auto rdw = reinterpret_cast<UInt64*>(rd);
+ SizeT words = len / sizeof(UInt64);
+
+ for (SizeT i = 0; i < words; ++i) {
+ rdw[-1 - static_cast<SizeT>(i)] = rsw[-1 - static_cast<SizeT>(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<UIntPtr>(s) % sizeof(UInt64) == 0) &&
+ (reinterpret_cast<UIntPtr>(d) % sizeof(UInt64) == 0)) {
+
+ auto sw = reinterpret_cast<const UInt64*>(s);
+ auto dw = reinterpret_cast<UInt64*>(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<SInt64>(len);
+ const Char* p = in;
+ while (*p) ++p;
+ return static_cast<SInt64>(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<UInt8*>(dest);
- auto dst_bytes = static_cast<UInt8*>(dest);
-
- if (len >= sizeof(UInt64) && (reinterpret_cast<UIntPtr>(dest) % sizeof(UInt64)) == 0) {
- // 64-bit pattern
+ if (len >= sizeof(UInt64) && (reinterpret_cast<UIntPtr>(d) % sizeof(UInt64)) == 0) {
UInt64 pattern = static_cast<UInt64>(value);
pattern |= (pattern << 8);
pattern |= (pattern << 16);
pattern |= (pattern << 32);
-
- auto dst_words = static_cast<UInt64*>(dest);
- SizeT word_count = len / sizeof(UInt64);
-
- for (SizeT i = 0; i < word_count; ++i) {
- dst_words[i] = pattern;
+
+ auto dw = reinterpret_cast<UInt64*>(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<VoidPtr>(desc),
+ SYSCALL_HASH("IoSeekFile"),
+ static_cast<VoidPtr>(desc),
reinterpret_cast<VoidPtr>(&off));
- if (!ret_ptr) {
- return ~0UL;
- }
+ if (!ret_ptr) return ~0UL;
auto ret = static_cast<volatile UInt64*>(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<VoidPtr>(desc));
-
- if (!ret_ptr) {
- return ~0UL;
- }
-
+ if (!ret_ptr) return ~0UL;
auto ret = static_cast<volatile UInt64*>(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<VoidPtr>(desc),
- Detail::safe_void_cast(fmt),
- static_cast<VoidPtr>(&args));
+ Detail::safe_void_cast(buf));
- va_end(args);
-
- if (!ret_ptr) {
- return -1;
- }
-
+ if (!ret_ptr) return -1;
auto ret = static_cast<const volatile SInt32*>(ret_ptr);
return *ret;
}