From e2bd3c7b6fcd6147fcbf699be087a475608ffdf7 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 23 May 2025 15:57:42 +0200 Subject: feat(krnl, libSystem, boot): Make space for LibSystem inside Handover boot structure. what: - Mostly related to the libSystem.sys library. - Updated the implementation of it, the specs, and added new APIs. - Fixed the hooks inside AsmProc.h Signed-off-by: Amlal El Mahrouss --- dev/user/AsmProc.h | 15 +++++++++ dev/user/ProcessCodes.h | 58 --------------------------------- dev/user/SciCalls.h | 15 --------- dev/user/SecurityPolicy.h | 12 +++++++ dev/user/SystemCalls.h | 33 ++++++++++--------- dev/user/SystemCodes.h | 58 +++++++++++++++++++++++++++++++++ dev/user/docs/SPECIFICATION_SYSCALLS.md | 23 +++++++++---- dev/user/src/GNUmakefile | 4 +-- dev/user/src/SystemCalls+IO.asm | 21 ++++++++++++ dev/user/src/SystemCalls.cc | 6 ++-- dev/user/user.json | 4 +-- 11 files changed, 146 insertions(+), 103 deletions(-) create mode 100644 dev/user/AsmProc.h delete mode 100644 dev/user/ProcessCodes.h delete mode 100644 dev/user/SciCalls.h create mode 100644 dev/user/SecurityPolicy.h create mode 100644 dev/user/SystemCodes.h (limited to 'dev/user') diff --git a/dev/user/AsmProc.h b/dev/user/AsmProc.h new file mode 100644 index 00000000..4b3b63c1 --- /dev/null +++ b/dev/user/AsmProc.h @@ -0,0 +1,15 @@ +/* ------------------------------------------- + + 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/user/ProcessCodes.h b/dev/user/ProcessCodes.h deleted file mode 100644 index 90457944..00000000 --- a/dev/user/ProcessCodes.h +++ /dev/null @@ -1,58 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include - -/// @file ProcessCodes.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/user/SciCalls.h b/dev/user/SciCalls.h deleted file mode 100644 index 4b3b63c1..00000000 --- a/dev/user/SciCalls.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/user/SecurityPolicy.h b/dev/user/SecurityPolicy.h new file mode 100644 index 00000000..a03c928b --- /dev/null +++ b/dev/user/SecurityPolicy.h @@ -0,0 +1,12 @@ +/* ------------------------------------------- + + 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/user/SystemCalls.h b/dev/user/SystemCalls.h index 61021a01..c1b70513 100644 --- a/dev/user/SystemCalls.h +++ b/dev/user/SystemCalls.h @@ -112,7 +112,7 @@ IMPORT_C UInt64 IoSeekFile(_Input Ref file_desc, UInt64 file_offset); /// @brief Spawns a Thread Information Block and Global Information Block inside the current /// process. /// @param process_id Target Process ID, must be valid. -/// @return > 0 error ocurred or already present, = 0 success. +/// @return > 0 error occurred or already present, = 0 success. IMPORT_C UInt32 RtlSpawnIB(UIntPtr process_id); /// @brief Spawns a process with a unique pid (stored as UIntPtr). @@ -125,10 +125,6 @@ IMPORT_C UIntPtr RtlSpawnProcess(const Char* process_path, SizeT argc, Char** ar /// @return if it has succeeded true, otherwise false. IMPORT_C Bool RtlExitProcess(UIntPtr handle, UIntPtr exit_code); -/// @brief Get current PID of process. -/// @return Current process ID. -IMPORT_C UIntPtr RtlCurrentPID(Void); - // ------------------------------------------------------------------------ // Memory Manager API. // ------------------------------------------------------------------------ @@ -192,14 +188,15 @@ IMPORT_C SInt32 ThrExitMainThread(_Input SInt32 exit_code); IMPORT_C SInt32 ThrExitThread(_Input ThreadRef thread, _Input SInt32 exit_code); /// @brief Thread procedure function type. -typedef SInt32 (*thread_proc_kind)(SInt32 argc, Char** argv); +typedef SInt32 (*ThrProcKind)(SInt32 argc, Char** argv); /// @brief Creates a thread. /// @param procedure the thread procedure. /// @param argument_count number of arguments inside that thread. /// @param flags Thread flags. /// @return the thread object. -IMPORT_C ThreadRef ThrCreateThread(thread_proc_kind procedure, SInt32 argument_count, SInt32 flags); +IMPORT_C ThreadRef ThrCreateThread(const Char* thread_name, ThrProcKind procedure, + SInt32 argument_count, SInt32 flags); /// @brief Yields the current thread. /// @param thread the thread to yield. @@ -287,8 +284,8 @@ IMPORT_C VoidPtr EvtDispatchEvent(_Input const Char* event_name, _Input VoidPtr // ------------------------------------------------------------------------------------------ // enum { - kPowerCodeInvalid, - kPowerCodeShutdown, + kPowerCodeInvalid = 0, + kPowerCodeShutdown = 12, kPowerCodeReboot, kPowerCodeSleep, kPowerCodeWake, @@ -328,15 +325,21 @@ IMPORT_C IORef PrintGet(const Char* path); // ------------------------------------------------------------------------------------------ // typedef SInt32 AffinityRef; -typedef UInt64 PID; +typedef UInt64 ProcessRef; + +IMPORT_C SInt32 SchedSetAffinity(ProcessRef, SInt32 req, AffinityRef* local); -IMPORT_C SInt32 SchedAffinity(PID, SInt32 req, AffinityRef* local); +IMPORT_C SInt32 SchedGetAffinity(ProcessRef, AffinityRef* local); -IMPORT_C SInt32 SchedTrace(PID, SInt32 req, VoidPtr address, VoidPtr data); +IMPORT_C SInt32 SchedFireSignal(ProcessRef, SInt32 req); -IMPORT_C SInt32 SchedKill(PID, SInt32 req); +IMPORT_C SInt32 SchedReadMemory(ProcessRef, SInt32 address, SInt32 data); -IMPORT_C SInt32 SchedBreakPoint(Void); +IMPORT_C SInt32 SchedWriteMemory(ProcessRef, SInt32 address, SInt32 data); + +/// @brief Get current ProcessRef of process. +/// @return Current process ID. +IMPORT_C UIntPtr RtlCurrentPID(Void); // ------------------------------------------------------------------------------------------ // // @brief Filesystem API. @@ -355,7 +358,7 @@ IMPORT_C BOOL FsCreateFile(const Char* path); IMPORT_C BOOL FsCreateAlias(const Char* path, const Char* from); // ------------------------------------------------------------------------------------------ // -// @brief Format API. +// @brief String API. // ------------------------------------------------------------------------------------------ // IMPORT_C Char* StrFmt(const Char* fmt, ...); diff --git a/dev/user/SystemCodes.h b/dev/user/SystemCodes.h new file mode 100644 index 00000000..90457944 --- /dev/null +++ b/dev/user/SystemCodes.h @@ -0,0 +1,58 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include + +/// @file ProcessCodes.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/user/docs/SPECIFICATION_SYSCALLS.md b/dev/user/docs/SPECIFICATION_SYSCALLS.md index 607c7532..b4b11c8c 100644 --- a/dev/user/docs/SPECIFICATION_SYSCALLS.md +++ b/dev/user/docs/SPECIFICATION_SYSCALLS.md @@ -1,16 +1,25 @@ =================================== - # 0: General Information - =================================== -- Programming Language: C/C++ -- Build System: Make/BTB -- Purpose: System Call Interface +- **Programming Language**: C / C++ +- **Build System**: Make / BTB (Build the Build) +- **Purpose**: System Call Interface (SCI) for NeKernel +=================================== +# 1: How It Works =================================== -# 1: How It works +- This header provides the raw API surface for accessing NeKernel's system calls. -=================================== +- It is **not** directly used by applications. + - Instead, it is abstracted by the **SystemSDK**, which presents a stable, high-level interface. + +- At runtime: + - System calls are routed from user-space code to the **kernel syscall manager**. + - The syscall manager executes the requested operation and returns control to `libSystem`. + - Finally, the result is delivered back to the originating process. +- This separation ensures that: + - The SCI remains low-level and close to the ABI. + - `SystemSDK` provides portability and shielding from changes in syscall internals. \ No newline at end of file diff --git a/dev/user/src/GNUmakefile b/dev/user/src/GNUmakefile index 09993d16..9b901f9f 100644 --- a/dev/user/src/GNUmakefile +++ b/dev/user/src/GNUmakefile @@ -1,6 +1,6 @@ ################################################## # (c) Amlal El Mahrouss, all rights reserved. -# This file is for user.sys's syscall stubs. +# This file is for libSystem.sys's syscall stubs. ################################################## ASM=nasm @@ -13,4 +13,4 @@ error: .PHONY: sci_asm_io_x64 sci_asm_io_x64: - $(ASM) $(FLAGS) SystemCalls+IO.asm -o SystemCalls+IO.asm.o + $(ASM) $(FLAGS) SystemCalls+IO.asm -o SystemCalls+IO.stub.obj diff --git a/dev/user/src/SystemCalls+IO.asm b/dev/user/src/SystemCalls+IO.asm index 77e22b59..9126b7de 100644 --- a/dev/user/src/SystemCalls+IO.asm +++ b/dev/user/src/SystemCalls+IO.asm @@ -19,17 +19,31 @@ 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 rbx, r8 mov r8, rcx @@ -37,9 +51,14 @@ sci_syscall_arg_3: mov r10, rbx syscall + pop rbp + ret sci_syscall_arg_4: + push rbp + mov rbp, rsp + mov rbx, r8 mov rax, r9 @@ -49,4 +68,6 @@ sci_syscall_arg_4: mov r11, rax syscall + pop rbp + ret diff --git a/dev/user/src/SystemCalls.cc b/dev/user/src/SystemCalls.cc index 874f607d..f004718c 100644 --- a/dev/user/src/SystemCalls.cc +++ b/dev/user/src/SystemCalls.cc @@ -4,11 +4,11 @@ ------------------------------------------- */ -#include +#include #include /// @file SystemCalls.cc -/// @brief Source file for the memory functions/syscalls for user.sys +/// @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) { @@ -51,8 +51,6 @@ IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt /// @internal //-----------------------------------------------------------------------------------------------------------// -constexpr auto kInvalidSyscall = 0UL; - 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))); diff --git a/dev/user/user.json b/dev/user/user.json index 05b90abf..2267175e 100644 --- a/dev/user/user.json +++ b/dev/user/user.json @@ -2,8 +2,8 @@ "compiler_path": "x86_64-w64-mingw32-g++", "compiler_std": "c++20", "headers_path": ["../", "./"], - "sources_path": ["src/*.cc", "src/*.o"], - "output_name": "user.sys", + "sources_path": ["src/*.cc", "src/*.stub.obj"], + "output_name": "libSystem.sys", "compiler_flags": [ "-ffreestanding", "-shared", -- cgit v1.2.3