summaryrefslogtreecommitdiffhomepage
path: root/dev/libSystem
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-08-16 19:56:21 +0200
committerGitHub <noreply@github.com>2025-08-16 19:56:21 +0200
commit1a32b9307357ac0fc9095e853b2b6d94f9fe62bb (patch)
treef41f723659c8926e38182fbe062746d821ab487e /dev/libSystem
parenteb9df5eea339812513c25a8d3b2eeb03c633e7ac (diff)
parentb301047903b79560dce69085fc271a653a1eb4b6 (diff)
Merge pull request #55 from nekernel-org/dev
v0.0.4
Diffstat (limited to 'dev/libSystem')
-rw-r--r--dev/libSystem/SystemKit/Jail.h18
-rw-r--r--dev/libSystem/SystemKit/Macros.h2
-rw-r--r--dev/libSystem/SystemKit/Syscall.h24
-rw-r--r--dev/libSystem/SystemKit/System.h8
-rw-r--r--dev/libSystem/docs/SPECIFICATION_SYSCALLS.md2
-rw-r--r--dev/libSystem/src/System.cc173
-rw-r--r--dev/libSystem/src/SystemAPI.cc94
7 files changed, 215 insertions, 106 deletions
diff --git a/dev/libSystem/SystemKit/Jail.h b/dev/libSystem/SystemKit/Jail.h
index 998173f9..c23942c5 100644
--- a/dev/libSystem/SystemKit/Jail.h
+++ b/dev/libSystem/SystemKit/Jail.h
@@ -9,7 +9,8 @@
#include <libSystem/SystemKit/System.h>
/// @file Jail.h
-/// @brief NeKernel Jail System
+/// @author Amlal El Mahrouss
+/// @brief NeKernel Jail System, part of OpenEnclave.
struct JAIL_INFO;
struct JAIL;
@@ -19,4 +20,17 @@ struct JAIL_INFO {
SInt32 fParentID;
SInt32 fJailHash;
SInt64 fACL;
-}; \ No newline at end of file
+};
+
+/// @brief Jail information (we grab a JAIL from JailGetCurrent())
+struct JAIL {
+ struct JAIL_INFO* fServer;
+ struct JAIL_INFO* fClient;
+ SInt32 fJailHash;
+ SInt32 fParentID;
+ SInt64 fACL;
+};
+
+/// @brief Get the current jail
+/// @return Pointer to the current jail structure, or NULL if not in a jail
+IMPORT_C struct JAIL* JailGetCurrent(Void);
diff --git a/dev/libSystem/SystemKit/Macros.h b/dev/libSystem/SystemKit/Macros.h
index 2bdeff9d..25bc77ba 100644
--- a/dev/libSystem/SystemKit/Macros.h
+++ b/dev/libSystem/SystemKit/Macros.h
@@ -94,7 +94,7 @@ typedef nullPtr NullPtr;
#endif
#ifndef kib_cast
-#define kib_cast(X) (UInt64)((X) *1024)
+#define kib_cast(X) (UInt64)((X) * 1024)
#endif
#ifndef MIB
diff --git a/dev/libSystem/SystemKit/Syscall.h b/dev/libSystem/SystemKit/Syscall.h
index 436665ae..a1505b46 100644
--- a/dev/libSystem/SystemKit/Syscall.h
+++ b/dev/libSystem/SystemKit/Syscall.h
@@ -9,11 +9,27 @@
#include <libSystem/SystemKit/System.h>
#include <cstdarg>
-#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) {
+ if (!path || *path == 0) return 0;
+
+ 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/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 <libSystem/SystemKit/Macros.h>
+/// @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/docs/SPECIFICATION_SYSCALLS.md b/dev/libSystem/docs/SPECIFICATION_SYSCALLS.md
index b4b11c8c..89f61498 100644
--- a/dev/libSystem/docs/SPECIFICATION_SYSCALLS.md
+++ b/dev/libSystem/docs/SPECIFICATION_SYSCALLS.md
@@ -3,7 +3,7 @@
===================================
- **Programming Language**: C / C++
-- **Build System**: Make / BTB (Build the Build)
+- **Build System**: Make / NeBuild
- **Purpose**: System Call Interface (SCI) for NeKernel
===================================
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 <libSystem/SystemKit/Err.h>
+#include <libSystem/SystemKit/Syscall.h>
+#include <libSystem/SystemKit/System.h>
+
+namespace Detail {
+template <typename T>
+static VoidPtr safe_void_cast(const T* ptr) {
+ _rtl_assert(ptr, "safe void cast failed!");
+ return static_cast<VoidPtr>(const_cast<T*>(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<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 {
+ // 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];
+ }
+ }
+ }
+
+ 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<SInt64>(p - in);
+}
+
+IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) {
+ if (!len || !dest) return nullptr;
+
+ auto d = static_cast<UInt8*>(dest);
+
+ 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 dw = reinterpret_cast<UInt64*>(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<Ref>(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<VoidPtr>(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),
+ reinterpret_cast<VoidPtr>(&off));
+
+ if (!ret_ptr) return ~0UL;
+
+ auto ret = static_cast<volatile UInt64*>(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<VoidPtr>(desc));
+ 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, ...) {
+ 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<VoidPtr>(desc),
+ Detail::safe_void_cast(buf));
+
+ if (!ret_ptr) return -kErrorInvalidData;
+
+ auto ret = static_cast<const volatile SInt32*>(ret_ptr);
+
+ return *ret;
+}
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 <libSystem/SystemKit/Syscall.h>
-#include <libSystem/SystemKit/System.h>
-
-/// @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<VoidPtr>(const_cast<Char*>(path)),
- reinterpret_cast<VoidPtr>(const_cast<Char*>(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<VoidPtr>(desc), reinterpret_cast<VoidPtr>(&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<VoidPtr>(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<VoidPtr>(desc),
- reinterpret_cast<VoidPtr>(const_cast<Char*>(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'));
- }
-}