From 81027667166d9624ee12f45f011426678d1bbbf4 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 29 May 2025 17:19:57 +0200 Subject: feat: Improve libSystem's architecture and implementation. fix: Fix NeKit's Ref, and ErrorOr classes. fix: Fix userland tools. next: - Finish the latest tickets and then release nekernel 0.0.3 Signed-off-by: Amlal El Mahrouss --- dev/libSystem/AsmProc.h | 15 ------ dev/libSystem/Codes.h | 58 ---------------------- dev/libSystem/Err.h | 58 ++++++++++++++++++++++ dev/libSystem/Jail.h | 15 ++++++ dev/libSystem/Macros.h | 10 ++-- dev/libSystem/SecurityPolicy.h | 12 ----- dev/libSystem/Syscall.h | 19 ++++++++ dev/libSystem/System.h | 14 +++--- dev/libSystem/src/GNUmakefile | 16 ------ dev/libSystem/src/Makefile | 16 ++++++ dev/libSystem/src/System.cc | 94 ------------------------------------ dev/libSystem/src/SystemAPI.cc | 91 ++++++++++++++++++++++++++++++++++ dev/libSystem/src/SystemCalls+IO.asm | 66 ------------------------- dev/libSystem/src/SystemProc.asm | 66 +++++++++++++++++++++++++ 14 files changed, 277 insertions(+), 273 deletions(-) delete mode 100644 dev/libSystem/AsmProc.h delete mode 100644 dev/libSystem/Codes.h create mode 100644 dev/libSystem/Err.h create mode 100644 dev/libSystem/Jail.h delete mode 100644 dev/libSystem/SecurityPolicy.h create mode 100644 dev/libSystem/Syscall.h delete mode 100644 dev/libSystem/src/GNUmakefile create mode 100644 dev/libSystem/src/Makefile delete mode 100644 dev/libSystem/src/System.cc create mode 100644 dev/libSystem/src/SystemAPI.cc delete mode 100644 dev/libSystem/src/SystemCalls+IO.asm create mode 100644 dev/libSystem/src/SystemProc.asm (limited to 'dev/libSystem') diff --git a/dev/libSystem/AsmProc.h b/dev/libSystem/AsmProc.h deleted file mode 100644 index b707d533..00000000 --- a/dev/libSystem/AsmProc.h +++ /dev/null @@ -1,15 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include -#include - -IMPORT_C VoidPtr sci_syscall_arg_1(SizeT id); -IMPORT_C VoidPtr sci_syscall_arg_2(SizeT id, VoidPtr arg1); -IMPORT_C VoidPtr sci_syscall_arg_3(SizeT id, VoidPtr arg1, VoidPtr arg3); -IMPORT_C VoidPtr sci_syscall_arg_4(SizeT id, VoidPtr arg1, VoidPtr arg3, VoidPtr arg4); diff --git a/dev/libSystem/Codes.h b/dev/libSystem/Codes.h deleted file mode 100644 index 0451df64..00000000 --- a/dev/libSystem/Codes.h +++ /dev/null @@ -1,58 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include - -/// @file Codes.h -/// @brief Process Codes type and values. -/// @author Amlal El Mahrouss (amlal@nekernel.org) - -#define err_local_ok() (kLastError == kErrorSuccess) -#define err_local_fail() (kLastError != kErrorSuccess) -#define err_local_get() (kLastError) - -typedef SInt32 ErrRef; - -inline constexpr ErrRef kErrorSuccess = 0; -inline constexpr ErrRef kErrorExecutable = 33; -inline constexpr ErrRef kErrorExecutableLib = 34; -inline constexpr ErrRef kErrorFileNotFound = 35; -inline constexpr ErrRef kErrorDirectoryNotFound = 36; -inline constexpr ErrRef kErrorDiskReadOnly = 37; -inline constexpr ErrRef kErrorDiskIsFull = 38; -inline constexpr ErrRef kErrorProcessFault = 39; -inline constexpr ErrRef kErrorSocketHangUp = 40; -inline constexpr ErrRef kErrorThreadLocalStorage = 41; -inline constexpr ErrRef kErrorMath = 42; -inline constexpr ErrRef kErrorNoNetwork = 43; -inline constexpr ErrRef kErrorHeapOutOfMemory = 44; -inline constexpr ErrRef kErrorNoSuchDisk = 45; -inline constexpr ErrRef kErrorFileExists = 46; -inline constexpr ErrRef kErrorFormatFailed = 47; -inline constexpr ErrRef kErrorNetworkTimeout = 48; -inline constexpr ErrRef kErrorInternal = 49; -inline constexpr ErrRef kErrorForkAlreadyExists = 50; -inline constexpr ErrRef kErrorOutOfTeamSlot = 51; -inline constexpr ErrRef kErrorHeapNotPresent = 52; -inline constexpr ErrRef kErrorNoEntrypoint = 53; -inline constexpr ErrRef kErrorDiskIsCorrupted = 54; -inline constexpr ErrRef kErrorDisk = 55; -inline constexpr ErrRef kErrorInvalidData = 56; -inline constexpr ErrRef kErrorAsync = 57; -inline constexpr ErrRef kErrorNonBlocking = 58; -inline constexpr ErrRef kErrorIPC = 59; -inline constexpr ErrRef kErrorSign = 60; -inline constexpr ErrRef kErrorInvalidCreds = 61; -inline constexpr ErrRef kErrorCDTrayBroken = 62; -inline constexpr ErrRef kErrorUnrecoverableDisk = 63; -inline constexpr ErrRef kErrorFileLocked = 64; -inline constexpr ErrRef kErrorDiskIsTooTiny = 65; -inline constexpr ErrRef kErrorUnimplemented = -1; - -/// @brief The last error reported by the system to the process. -IMPORT_C ErrRef kLastError; diff --git a/dev/libSystem/Err.h b/dev/libSystem/Err.h new file mode 100644 index 00000000..e9fe013b --- /dev/null +++ b/dev/libSystem/Err.h @@ -0,0 +1,58 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include + +/// @file Err.h +/// @brief Process Codes type and values. +/// @author Amlal El Mahrouss (amlal@nekernel.org) + +#define err_local_ok() (kLastError == kErrorSuccess) +#define err_local_fail() (kLastError != kErrorSuccess) +#define err_local_get() (kLastError) + +typedef SInt32 ErrRef; + +inline constexpr ErrRef kErrorSuccess = 0; +inline constexpr ErrRef kErrorExecutable = 33; +inline constexpr ErrRef kErrorExecutableLib = 34; +inline constexpr ErrRef kErrorFileNotFound = 35; +inline constexpr ErrRef kErrorDirectoryNotFound = 36; +inline constexpr ErrRef kErrorDiskReadOnly = 37; +inline constexpr ErrRef kErrorDiskIsFull = 38; +inline constexpr ErrRef kErrorProcessFault = 39; +inline constexpr ErrRef kErrorSocketHangUp = 40; +inline constexpr ErrRef kErrorThreadLocalStorage = 41; +inline constexpr ErrRef kErrorMath = 42; +inline constexpr ErrRef kErrorNoNetwork = 43; +inline constexpr ErrRef kErrorHeapOutOfMemory = 44; +inline constexpr ErrRef kErrorNoSuchDisk = 45; +inline constexpr ErrRef kErrorFileExists = 46; +inline constexpr ErrRef kErrorFormatFailed = 47; +inline constexpr ErrRef kErrorNetworkTimeout = 48; +inline constexpr ErrRef kErrorInternal = 49; +inline constexpr ErrRef kErrorForkAlreadyExists = 50; +inline constexpr ErrRef kErrorOutOfTeamSlot = 51; +inline constexpr ErrRef kErrorHeapNotPresent = 52; +inline constexpr ErrRef kErrorNoEntrypoint = 53; +inline constexpr ErrRef kErrorDiskIsCorrupted = 54; +inline constexpr ErrRef kErrorDisk = 55; +inline constexpr ErrRef kErrorInvalidData = 56; +inline constexpr ErrRef kErrorAsync = 57; +inline constexpr ErrRef kErrorNonBlocking = 58; +inline constexpr ErrRef kErrorIPC = 59; +inline constexpr ErrRef kErrorSign = 60; +inline constexpr ErrRef kErrorInvalidCreds = 61; +inline constexpr ErrRef kErrorCDTrayBroken = 62; +inline constexpr ErrRef kErrorUnrecoverableDisk = 63; +inline constexpr ErrRef kErrorFileLocked = 64; +inline constexpr ErrRef kErrorDiskIsTooTiny = 65; +inline constexpr ErrRef kErrorUnimplemented = -1; + +/// @brief The last error reported by the system to the process. +IMPORT_C ErrRef kLastError; diff --git a/dev/libSystem/Jail.h b/dev/libSystem/Jail.h new file mode 100644 index 00000000..6a9259fc --- /dev/null +++ b/dev/libSystem/Jail.h @@ -0,0 +1,15 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include + +/// @file Jail.h +/// @brief NeKernel Jail System + +struct JAIL_INFO; +struct JAIL; \ No newline at end of file diff --git a/dev/libSystem/Macros.h b/dev/libSystem/Macros.h index 52dc904c..5b3a5ce1 100644 --- a/dev/libSystem/Macros.h +++ b/dev/libSystem/Macros.h @@ -64,19 +64,19 @@ typedef char Char; typedef decltype(nullptr) nullPtr; typedef nullPtr NullPtr; -#define SCI_COPY_DELETE(KLASS) \ +#define LIBSYS_COPY_DELETE(KLASS) \ KLASS& operator=(const KLASS&) = delete; \ KLASS(const KLASS&) = delete; -#define SCI_COPY_DEFAULT(KLASS) \ +#define LIBSYS_COPY_DEFAULT(KLASS) \ KLASS& operator=(const KLASS&) = default; \ KLASS(const KLASS&) = default; -#define SCI_MOVE_DELETE(KLASS) \ +#define LIBSYS_MOVE_DELETE(KLASS) \ KLASS& operator=(KLASS&&) = delete; \ KLASS(KLASS&&) = delete; -#define SCI_MOVE_DEFAULT(KLASS) \ +#define LIBSYS_MOVE_DEFAULT(KLASS) \ KLASS& operator=(KLASS&&) = default; \ KLASS(KLASS&&) = default; @@ -123,4 +123,4 @@ IMPORT_C void _rtl_assert(Bool expr, const Char* origin); #define tib_cast(X) ((UInt64) gib_cast(X) * 1024) #endif -#define SCI_UNUSED(X) ((void) X) +#define LIBSYS_UNUSED(X) ((void) X) diff --git a/dev/libSystem/SecurityPolicy.h b/dev/libSystem/SecurityPolicy.h deleted file mode 100644 index 812f52e2..00000000 --- a/dev/libSystem/SecurityPolicy.h +++ /dev/null @@ -1,12 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include - -/// @file SecurityPolicy.h -/// @brief Hardened Security Policy, used to restrict access to certain system calls. \ No newline at end of file diff --git a/dev/libSystem/Syscall.h b/dev/libSystem/Syscall.h new file mode 100644 index 00000000..21f8287c --- /dev/null +++ b/dev/libSystem/Syscall.h @@ -0,0 +1,19 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#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); diff --git a/dev/libSystem/System.h b/dev/libSystem/System.h index 920ea2b2..88e1f173 100644 --- a/dev/libSystem/System.h +++ b/dev/libSystem/System.h @@ -7,8 +7,8 @@ Purpose: System Call Interface. ------------------------------------------- */ -#ifndef SCI_SYSTEM_CALLS_H -#define SCI_SYSTEM_CALLS_H +#ifndef LIBSYS_SYSTEM_CALLS_H +#define LIBSYS_SYSTEM_CALLS_H #include @@ -16,12 +16,12 @@ Purpose: System Call Interface. /// @brief Types API. // ------------------------------------------------------------------------------------------ // -struct RefType { - UInt32 __hash; - VoidPtr __self; +struct REF_TYPE { + UInt64 __hash; /// @brief Hash of the syscall + VoidPtr __self; /// @brief Syscall self value. }; -typedef RefType* Ref; +typedef REF_TYPE* Ref; typedef Ref IORef; typedef Ref FSRef; @@ -382,4 +382,4 @@ IMPORT_C Char* StrFmt(const Char* fmt, ...); IMPORT_C UInt64 StrMathToNumber(const Char* in, const Char** endp, const SInt16 base); -#endif // ifndef SCI_SYSTEM_CALLS_H +#endif // ifndef LIBSYS_SYSTEM_CALLS_H diff --git a/dev/libSystem/src/GNUmakefile b/dev/libSystem/src/GNUmakefile deleted file mode 100644 index 9b901f9f..00000000 --- a/dev/libSystem/src/GNUmakefile +++ /dev/null @@ -1,16 +0,0 @@ -################################################## -# (c) Amlal El Mahrouss, all rights reserved. -# This file is for libSystem.sys's syscall stubs. -################################################## - -ASM=nasm -FLAGS=-f win64 - -.PHONY: error -error: - @echo "==> Invalid rule." - @echo "==> Use sci_asm_io_ instead." - -.PHONY: sci_asm_io_x64 -sci_asm_io_x64: - $(ASM) $(FLAGS) SystemCalls+IO.asm -o SystemCalls+IO.stub.obj diff --git a/dev/libSystem/src/Makefile b/dev/libSystem/src/Makefile new file mode 100644 index 00000000..41c99c4d --- /dev/null +++ b/dev/libSystem/src/Makefile @@ -0,0 +1,16 @@ +################################################## +# (c) Amlal El Mahrouss, all rights reserved. +# This file is for libSystem.sys's syscall stubs. +################################################## + +ASM=nasm +FLAGS=-f win64 + +.PHONY: error +error: + @echo "==> Invalid rule." + @echo "==> Use libsys_asm_io_ instead." + +.PHONY: libsys_asm_io_x64 +libsys_asm_io_x64: + $(ASM) $(FLAGS) SystemProc.asm -o SystemProc.asm.obj diff --git a/dev/libSystem/src/System.cc b/dev/libSystem/src/System.cc deleted file mode 100644 index 1c28303d..00000000 --- a/dev/libSystem/src/System.cc +++ /dev/null @@ -1,94 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#include -#include - -/// @file SystemCalls.cc -/// @brief Source file for the memory functions/syscalls for libSystem.sys - -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; -} - -//-----------------------------------------------------------------------------------------------------------// -/// @brief Systems Calls implementation. -/// @internal -//-----------------------------------------------------------------------------------------------------------// - -IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter) { - return sci_syscall_arg_3(1, reinterpret_cast(const_cast(path)), - reinterpret_cast(const_cast(drv_letter))); -} - -IMPORT_C Void IoCloseFile(_Input Ref desc) { - sci_syscall_arg_2(2, desc); -} - -IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) { - auto ret = (volatile UInt64*) sci_syscall_arg_3(3, reinterpret_cast(desc), - reinterpret_cast(&off)); - - MUST_PASS((*ret) != ~0UL); - return *ret; -} - -IMPORT_C UInt64 IoTellFile(_Input Ref desc) { - auto ret = (volatile UInt64*) sci_syscall_arg_2(4, 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*) sci_syscall_arg_4( - 5, 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); - } -} diff --git a/dev/libSystem/src/SystemAPI.cc b/dev/libSystem/src/SystemAPI.cc new file mode 100644 index 00000000..37e835c1 --- /dev/null +++ b/dev/libSystem/src/SystemAPI.cc @@ -0,0 +1,91 @@ +/* ------------------------------------------- + + 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+IO.asm b/dev/libSystem/src/SystemCalls+IO.asm deleted file mode 100644 index 097046af..00000000 --- a/dev/libSystem/src/SystemCalls+IO.asm +++ /dev/null @@ -1,66 +0,0 @@ -;; /* -;; * ======================================================== -;; * -;; * libSystem/src/SystemCalls+IO.asm -;; * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. -;; * -;; * ======================================================== -;; */ - -[bits 64] - -section .text - -global sci_syscall_arg_1 -global sci_syscall_arg_2 -global sci_syscall_arg_3 -global sci_syscall_arg_4 - -sci_syscall_arg_1: - push rbp - mov rbp, rsp - - mov r8, rcx - syscall - - pop rbp - - ret - -sci_syscall_arg_2: - push rbp - mov rbp, rsp - - mov r8, rcx - mov r9, rdx - syscall - pop rbp - - ret - -sci_syscall_arg_3: - push rbp - mov rbp, rsp - - mov r8, rcx - mov r9, rdx - mov r10, rbx - - syscall - pop rbp - - ret - -sci_syscall_arg_4: - push rbp - mov rbp, rsp - - mov r8, rcx - mov r9, rdx - mov r10, rbx - mov r11, rax - - syscall - pop rbp - - ret diff --git a/dev/libSystem/src/SystemProc.asm b/dev/libSystem/src/SystemProc.asm new file mode 100644 index 00000000..bc41059a --- /dev/null +++ b/dev/libSystem/src/SystemProc.asm @@ -0,0 +1,66 @@ +;; /* +;; * ======================================================== +;; * +;; * libSystem/src/SystemCalls+IO.asm +;; * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +;; * +;; * ======================================================== +;; */ + +[bits 64] + +section .text + +global libsys_syscall_arg_1 +global libsys_syscall_arg_2 +global libsys_syscall_arg_3 +global libsys_syscall_arg_4 + +libsys_syscall_arg_1: + push rbp + mov rbp, rsp + + mov r8, rcx + syscall + + pop rbp + + ret + +libsys_syscall_arg_2: + push rbp + mov rbp, rsp + + mov r8, rcx + mov r9, rdx + syscall + pop rbp + + ret + +libsys_syscall_arg_3: + push rbp + mov rbp, rsp + + mov r8, rcx + mov r9, rdx + mov r10, rbx + + syscall + pop rbp + + ret + +libsys_syscall_arg_4: + push rbp + mov rbp, rsp + + mov r8, rcx + mov r9, rdx + mov r10, rbx + mov r11, rax + + syscall + pop rbp + + ret -- cgit v1.2.3