From b8f7ef086d1d1b1cd686fff04d0a587f8fd39d81 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 27 Mar 2025 17:24:21 +0100 Subject: add: new driver device kit API. (DDK) refactor: rename SCIKit -> user (then libuser.dylib) boot/modules/netboot: fixed compilation for amd64. Signed-off-by: Amlal El Mahrouss --- dev/HintKit/CompilerHint.h | 25 +++ dev/HintsKit/CompilerHint.h | 25 --- dev/SCIKit/LPC.h | 56 ------ dev/SCIKit/Macros.h | 93 --------- dev/SCIKit/SystemCalls.h | 341 -------------------------------- dev/SCIKit/doc/LibSCI.drawio | 28 --- dev/SCIKit/libsci.json | 20 -- dev/SCIKit/src/LPC.cc | 10 - dev/SCIKit/src/LibSCI+IO.asm | 50 ----- dev/SCIKit/src/Makefile | 16 -- dev/SCIKit/src/SCI.cc | 45 ----- dev/boot/amd64-desktop.make | 4 +- dev/boot/modules/NetBoot/NetBoot.cc | 8 +- dev/boot/modules/NetBoot/NetBoot.h | 2 +- dev/boot/modules/NetBoot/build.json | 4 +- dev/boot/src/HEL/AMD64/BootEFI.cc | 2 +- dev/ddk/ddk.h | 112 +++++++++++ dev/ddk/ddk.json | 23 +++ dev/ddk/dev.h | 35 ++++ dev/ddk/docs/SPECIFICATION_DDK.md | 18 ++ dev/ddk/io.h | 18 ++ dev/ddk/src/ddk_alloc.c | 36 ++++ dev/ddk/src/ddk_dev.c | 30 +++ dev/ddk/src/ddk_io.c | 37 ++++ dev/ddk/src/ddk_kernel_call.c | 60 ++++++ dev/ddk/src/ddk_kernel_call_dispatch.S | 36 ++++ dev/ddk/src/ddk_rt_cxx.cc | 29 +++ dev/ddk/src/ddk_str.c | 40 ++++ dev/ddk/src/ddk_ver.c | 25 +++ dev/ddk/str.h | 17 ++ dev/kernel/FSKit/NeFS.h | 2 +- dev/kernel/KernelKit/FileMgr.h | 2 +- dev/kernel/KernelKit/LoaderInterface.h | 2 +- dev/kernel/KernelKit/MemoryMgr.h | 2 +- dev/kernel/KernelRsrc.rsrc | 25 --- dev/kernel/NetworkKit/IPC.h | 2 +- dev/kernel/NewKit/Variant.h | 3 +- dev/kernel/SwapKit/DiskSwap.h | 70 +++++++ dev/kernel/SwapKit/SwapDisk.h | 70 ------- dev/kernel/amd64-ci.make | 2 +- dev/kernel/amd64-desktop.make | 2 +- dev/kernel/kernel_rsrc.rsrc | 25 +++ dev/kernel/src/Swap/DiskSwap.cc | 67 +++++++ dev/kernel/src/Swap/SwapDisk.cc | 67 ------- dev/user/LPC.h | 56 ++++++ dev/user/Macros.h | 93 +++++++++ dev/user/SystemCalls.h | 341 ++++++++++++++++++++++++++++++++ dev/user/docs/SPECIFICATION_SYSCALLS.md | 9 + dev/user/libsci.json | 20 ++ dev/user/src/GNUmakefile | 16 ++ dev/user/src/LPC.cc | 10 + dev/user/src/SystemCall+IO.asm | 50 +++++ dev/user/src/SystemCalls.cc | 45 +++++ 53 files changed, 1363 insertions(+), 863 deletions(-) create mode 100644 dev/HintKit/CompilerHint.h delete mode 100644 dev/HintsKit/CompilerHint.h delete mode 100644 dev/SCIKit/LPC.h delete mode 100644 dev/SCIKit/Macros.h delete mode 100644 dev/SCIKit/SystemCalls.h delete mode 100644 dev/SCIKit/doc/LibSCI.drawio delete mode 100644 dev/SCIKit/libsci.json delete mode 100644 dev/SCIKit/src/LPC.cc delete mode 100644 dev/SCIKit/src/LibSCI+IO.asm delete mode 100644 dev/SCIKit/src/Makefile delete mode 100644 dev/SCIKit/src/SCI.cc create mode 100644 dev/ddk/ddk.h create mode 100644 dev/ddk/ddk.json create mode 100644 dev/ddk/dev.h create mode 100644 dev/ddk/docs/SPECIFICATION_DDK.md create mode 100644 dev/ddk/io.h create mode 100644 dev/ddk/src/ddk_alloc.c create mode 100644 dev/ddk/src/ddk_dev.c create mode 100644 dev/ddk/src/ddk_io.c create mode 100644 dev/ddk/src/ddk_kernel_call.c create mode 100644 dev/ddk/src/ddk_kernel_call_dispatch.S create mode 100644 dev/ddk/src/ddk_rt_cxx.cc create mode 100644 dev/ddk/src/ddk_str.c create mode 100644 dev/ddk/src/ddk_ver.c create mode 100644 dev/ddk/str.h delete mode 100644 dev/kernel/KernelRsrc.rsrc create mode 100644 dev/kernel/SwapKit/DiskSwap.h delete mode 100644 dev/kernel/SwapKit/SwapDisk.h create mode 100644 dev/kernel/kernel_rsrc.rsrc create mode 100644 dev/kernel/src/Swap/DiskSwap.cc delete mode 100644 dev/kernel/src/Swap/SwapDisk.cc create mode 100644 dev/user/LPC.h create mode 100644 dev/user/Macros.h create mode 100644 dev/user/SystemCalls.h create mode 100644 dev/user/docs/SPECIFICATION_SYSCALLS.md create mode 100644 dev/user/libsci.json create mode 100644 dev/user/src/GNUmakefile create mode 100644 dev/user/src/LPC.cc create mode 100644 dev/user/src/SystemCall+IO.asm create mode 100644 dev/user/src/SystemCalls.cc (limited to 'dev') diff --git a/dev/HintKit/CompilerHint.h b/dev/HintKit/CompilerHint.h new file mode 100644 index 00000000..9a1be9f6 --- /dev/null +++ b/dev/HintKit/CompilerHint.h @@ -0,0 +1,25 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#ifndef HINTKIT_HINT_H +#define HINTKIT_HINT_H + +#ifdef __LIBCOMPILER__ +#pragma compiler(hint_manifest) +#endif + +#define _Input +#define _Output + +#define _Optional + +#define _StrictCheckInput +#define _StrictCheckOutput + +#define _InOut +#define _StrictInOut + +#endif // ifndef HINTKIT_HINT_H diff --git a/dev/HintsKit/CompilerHint.h b/dev/HintsKit/CompilerHint.h deleted file mode 100644 index 9a1be9f6..00000000 --- a/dev/HintsKit/CompilerHint.h +++ /dev/null @@ -1,25 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. - -------------------------------------------- */ - -#ifndef HINTKIT_HINT_H -#define HINTKIT_HINT_H - -#ifdef __LIBCOMPILER__ -#pragma compiler(hint_manifest) -#endif - -#define _Input -#define _Output - -#define _Optional - -#define _StrictCheckInput -#define _StrictCheckOutput - -#define _InOut -#define _StrictInOut - -#endif // ifndef HINTKIT_HINT_H diff --git a/dev/SCIKit/LPC.h b/dev/SCIKit/LPC.h deleted file mode 100644 index b0bb2d83..00000000 --- a/dev/SCIKit/LPC.h +++ /dev/null @@ -1,56 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include - -/// @file LPC.h -/// @brief Local Procedure Code type and values. - -#define err_local_ok() (kLastError == kErrorSuccess) -#define err_local_fail() (kLastError != kErrorSuccess) -#define err_local_get() (kLastError) - -typedef SInt32 ErrObject; - -inline constexpr ErrObject kErrorSuccess = 0; -inline constexpr ErrObject kErrorExecutable = 33; -inline constexpr ErrObject kErrorExecutableLib = 34; -inline constexpr ErrObject kErrorFileNotFound = 35; -inline constexpr ErrObject kErrorDirectoryNotFound = 36; -inline constexpr ErrObject kErrorDiskReadOnly = 37; -inline constexpr ErrObject kErrorDiskIsFull = 38; -inline constexpr ErrObject kErrorProcessFault = 39; -inline constexpr ErrObject kErrorSocketHangUp = 40; -inline constexpr ErrObject kErrorThreadLocalStorage = 41; -inline constexpr ErrObject kErrorMath = 42; -inline constexpr ErrObject kErrorNoNetwork = 43; -inline constexpr ErrObject kErrorHeapOutOfMemory = 44; -inline constexpr ErrObject kErrorNoSuchDisk = 45; -inline constexpr ErrObject kErrorFileExists = 46; -inline constexpr ErrObject kErrorFormatFailed = 47; -inline constexpr ErrObject kErrorNetworkTimeout = 48; -inline constexpr ErrObject kErrorInternal = 49; -inline constexpr ErrObject kErrorForkAlreadyExists = 50; -inline constexpr ErrObject kErrorOutOfTeamSlot = 51; -inline constexpr ErrObject kErrorHeapNotPresent = 52; -inline constexpr ErrObject kErrorNoEntrypoint = 53; -inline constexpr ErrObject kErrorDiskIsCorrupted = 54; -inline constexpr ErrObject kErrorDisk = 55; -inline constexpr ErrObject kErrorInvalidData = 56; -inline constexpr ErrObject kErrorAsync = 57; -inline constexpr ErrObject kErrorNonBlocking = 58; -inline constexpr ErrObject kErrorIPC = 59; -inline constexpr ErrObject kErrorSign = 60; -inline constexpr ErrObject kErrorInvalidCreds = 61; -inline constexpr ErrObject kErrorCDTrayBroken = 62; -inline constexpr ErrObject kErrorUnrecoverableDisk = 63; -inline constexpr ErrObject kErrorFileLocked = 64; -inline constexpr ErrObject kErrorUnimplemented = -1; - -/// @brief The last error reported by the system to the process. -IMPORT_C ErrObject kLastError; diff --git a/dev/SCIKit/Macros.h b/dev/SCIKit/Macros.h deleted file mode 100644 index 174d644a..00000000 --- a/dev/SCIKit/Macros.h +++ /dev/null @@ -1,93 +0,0 @@ -/* ------------------------------------------- - -Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. - -File: Macros.h -Purpose: libsci Macros header. - -------------------------------------------- */ - -#pragma once - -/***********************************************************************************/ -/// @file SCIKit/Macros.h -/// @brief Macros and Core types. -/***********************************************************************************/ - -#include - -#define ATTRIBUTE(X) __attribute__((X)) - -#define IMPORT_CXX extern "C++" -#define IMPORT_C extern "C" - -#define DEPRECATED ATTRIBUTE(deprecated) - -#define EXIT_SUCCESS (0) -#define EXIT_FAILURE (1) - -#define FILE_MAX_LEN (256) - -#ifndef BOOL -#define BOOL bool -#endif - -typedef bool Bool; -typedef bool Boolean; -typedef void Void; - -#ifndef __cplusplus -#define true (1) -#define false (0) -#endif - -#define YES true -#define NO false - -typedef __UINT64_TYPE__ UInt64; -typedef __UINT32_TYPE__ UInt32; -typedef __UINT16_TYPE__ UInt16; -typedef __UINT8_TYPE__ UInt8; - -typedef __SIZE_TYPE__ SizeT; - -typedef __INT64_TYPE__ SInt64; -typedef __INT32_TYPE__ SInt32; -typedef __INT16_TYPE__ SInt16; -typedef __INT8_TYPE__ SInt8; - -typedef void* VoidPtr; -typedef __UINTPTR_TYPE__ UIntPtr; -typedef char Char; - -#ifdef __cplusplus -typedef decltype(nullptr) nullPtr; -typedef nullPtr NullPtr; - -#define SCI_COPY_DELETE(KLASS) \ - KLASS& operator=(const KLASS&) = delete; \ - KLASS(const KLASS&) = delete; - -#define SCI_COPY_DEFAULT(KLASS) \ - KLASS& operator=(const KLASS&) = default; \ - KLASS(const KLASS&) = default; - -#define SCI_MOVE_DELETE(KLASS) \ - KLASS& operator=(KLASS&&) = delete; \ - KLASS(KLASS&&) = delete; - -#define SCI_MOVE_DEFAULT(KLASS) \ - KLASS& operator=(KLASS&&) = default; \ - KLASS(KLASS&&) = default; - -#endif - -IMPORT_C void _rtl_assert(Bool expr, const Char* origin); - -#define MUST_PASS(X) _rtl_assert(X, __FILE__) - -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(X) \ - (((sizeof(X) / sizeof(*(X))) / \ - (static_cast(!(sizeof(X) % sizeof(*(X))))))) -#endif \ No newline at end of file diff --git a/dev/SCIKit/SystemCalls.h b/dev/SCIKit/SystemCalls.h deleted file mode 100644 index 7615a2d1..00000000 --- a/dev/SCIKit/SystemCalls.h +++ /dev/null @@ -1,341 +0,0 @@ -/* ------------------------------------------- - -Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. - -File: SystemCalls.h -Purpose: System Call Interface. - -------------------------------------------- */ - -#ifndef SCI_SCI_H -#define SCI_SCI_H - -#include - -// ------------------------------------------------------------------------------------------ // -/// @brief Types API. -// ------------------------------------------------------------------------------------------ // - -typedef VoidPtr SCIObject; - -typedef SCIObject IOObject; -typedef IOObject FSObject; -typedef SCIObject DLLObject; -typedef SCIObject ThreadObject; -typedef SCIObject SocketObject; - -// ------------------------------------------------------------------------------------------ // -/// @brief Dynamic Loader API. -// ------------------------------------------------------------------------------------------ // - -/// @brief Get function which is part of the DLL. -/// @param symbol the symbol to look for -/// @param dll_handle the DLL handle. -/// @return the proc pointer. -IMPORT_C SCIObject LdrGetDLLSymbolFromHandle(_Input const Char* symbol, _Input SCIObject dll_handle); - -/// @brief Open DLL handle. -/// @param path -/// @param drv -/// @return -IMPORT_C SCIObject LdrOpenDLLHandle(_Input const Char* path, _Input const Char* drive_letter); - -/// @brief Close DLL handle -/// @param dll_handle -/// @return -IMPORT_C Void LdrCloseDLLHandle(_Input SCIObject* dll_handle); - -// ------------------------------------------------------------------------------------------ // -// File API. -// ------------------------------------------------------------------------------------------ // - -/// @brief Opens a file from a drive. -/// @param fs_path the filesystem path. -/// @param drive_letter drive name, use NULL to use default drive location. -/// @return the file descriptor of the file. -IMPORT_C SCIObject IoOpenFile(const Char* fs_path, const Char* drive_letter); - -/// @brief Closes a file and flushes its content. -/// @param file_desc the file descriptor. -/// @return Function doesn't return a type. -IMPORT_C Void IoCloseFile(_Input SCIObject file_desc); - -/// @brief Write data to a file. -/// @param file_desc the file descriptor. -/// @param out_data the data to write. -/// @param sz_data the size of the data to write. -/// @return the number of bytes written. -IMPORT_C UInt32 IoWriteFile(_Input SCIObject file_desc, _Output VoidPtr out_data, SizeT sz_data); - -/// @brief Read data from a file. -/// @param file_desc the file descriptor. -/// @param out_data the data to read. -/// @param sz_data the size of the data to read. -IMPORT_C UInt32 IoReadFile(_Input SCIObject file_desc, _Output VoidPtr* out_data, SizeT sz_data); - -/// @brief Rewind the file pointer to the beginning of the file. -/// @param file_desc the file descriptor. -/// @return the number of bytes read. -IMPORT_C UInt64 IoRewindFile(_Input SCIObject file_desc); - -/// @brief Tell the current position of the file pointer. -/// @param file_desc the file descriptor. -/// @return the current position of the file pointer. -IMPORT_C UInt64 IoTellFile(_Input SCIObject file_desc); - -/// @brief Seek file offset from file descriptor. -IMPORT_C UInt64 IoSeekFile(_Input SCIObject file_desc, UInt64 file_offset); - -// ------------------------------------------------------------------------ -// Process API. -// ------------------------------------------------------------------------ - -/// @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. -IMPORT_C UInt32 RtlSpawnIB(UIntPtr process_id); - -/// @brief Spawns a process with a unique pid (stored as UIntPtr). -/// @param process_path process filesystem path. -/// @return > 0 process was created. -IMPORT_C UIntPtr RtlSpawnProcess(const Char* process_path, SizeT argc, Char** argv, Char** envp, SizeT envp_len); - -/// @brief Exits a process with an exit_code. -/// @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. -// ------------------------------------------------------------------------ - -/// @brief Creates a new heap from the process's address space. -/// @param len the length of it. -/// @param flags the flags of it. -/// @return heap pointer. -IMPORT_C VoidPtr MmCreateHeap(_Input SizeT len, _Input UInt32 flags); - -/// @brief Destroys the pointer -/// @param heap the heap itself. -/// @return void. -IMPORT_C Void MmDestroyHeap(_Input VoidPtr heap); - -/// @brief Change protection flags of a memory region. -IMPORT_C Void MmSetHeapFlags(_Input VoidPtr heap, _Input UInt32 flags); - -/// @brief Change protection flags of a memory region. -IMPORT_C UInt32 MmGetHeapFlags(_Input VoidPtr heap); - -/// @brief Fill memory region with CRC32. -IMPORT_C UInt32 MmFillCRC32Heap(_Input VoidPtr heap); - -/// @brief Copy memory region. -IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len); - -/// @brief Compare memory regions. -IMPORT_C SInt64 MmCmpMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len); - -/// @brief Fill memory region. -IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value); - -/// @brief Compare string regions. -IMPORT_C SInt64 MmStrCmp(_Input const Char* dest, _Input const Char* src); - -/// @brief Get length of string. -IMPORT_C SInt64 MmStrLen(const Char* str); - -// ------------------------------------------------------------------------ -// @brief Error API. -// ------------------------------------------------------------------------ - -IMPORT_C SInt32 ErrGetLastError(Void); - -// ------------------------------------------------------------------------ -// @brief Threading API. -// ------------------------------------------------------------------------ - -/// @brief Exit the current thread. -/// @param exit_code the exit code. -IMPORT_C Void ThrExitCurrentThread(_Input SInt32 exit_code); - -/// @brief Exit the main thread. -/// @param exit_code the exit code. -IMPORT_C Void ThrExitMainThread(_Input SInt32 exit_code); - -/// @brief Exit a thread. -/// @param thread the thread to exit. -/// @param exit_code the exit code. -IMPORT_C Void ThrExitThread(_Input ThreadObject thread, _Input SInt32 exit_code); - -/// @brief Thread procedure function type. -typedef Void (*thread_proc_kind)(int 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 ThreadObject ThrCreateThread(thread_proc_kind procedure, SInt32 argument_count, SInt32 flags); - -/// @brief Yields the current thread. -/// @param thread the thread to yield. -IMPORT_C Void ThrYieldThread(ThreadObject thrd); - -/// @brief Joins a thread. -/// @param thread the thread to join. -IMPORT_C Void ThrJoinThread(ThreadObject thrd); - -/// @brief Detach a thread. -/// @param thread the thread to detach. -IMPORT_C Void ThrDetachThread(ThreadObject thrd); - -// ------------------------------------------------------------------------ -// @brief Drive Management API. -// ------------------------------------------------------------------------ - -// ------------------------------------------------------------------------------------------ // -/// @brief Get the default drive letter. -/// @param void. -/// @return the drive letter. -// ------------------------------------------------------------------------------------------ // -IMPORT_C Char* DrvGetDefaultDriveLetter(Void); - -// ------------------------------------------------------------------------------------------ // -/// @brief Get the drive letter from a path. -/// @param path the path. -/// @return the drive letter. -// ------------------------------------------------------------------------------------------ // -IMPORT_C Char* DrvGetDriveLetterFromPath(_Input const Char* path); - -// ------------------------------------------------------------------------------------------ // -/// @brief Get a mounted drive from a letter. -/// @param letter the letter (A..Z). -/// @return the drive object. -// ------------------------------------------------------------------------------------------ // -IMPORT_C SCIObject DrvGetMountedDrive(_Input const Char letter); - -// ------------------------------------------------------------------------------------------ // -/// @brief Mount a drive. -/// @param path the path to mount. -/// @param letter the letter to mount. -// ------------------------------------------------------------------------------------------ // -IMPORT_C Void DrvMountDrive(_Input const Char* path, _Input const Char* letter); - -// ------------------------------------------------------------------------------------------ // -/// @brief Unmount a drive. -/// @param letter the letter to unmount. -// ------------------------------------------------------------------------------------------ // -IMPORT_C Void DrvUnmountDrive(_Input const Char letter); - -// ------------------------------------------------------------------------ -// Event handling API, use to listen to OS specific events. -// ------------------------------------------------------------------------ - -// ------------------------------------------------------------------------------------------ // -/// @brief Add an event listener. -/// @param event_name the event name. -/// @param listener the listener to add. -/// @return the event listener. -// ------------------------------------------------------------------------------------------ // - -IMPORT_C Void EvtAddListener(_Input const Char* event_name, _Input SCIObject listener); - -// ------------------------------------------------------------------------------------------ // -/// @brief Remove an event listener. -/// @param event_name the event name. -/// @param listener the listener to remove. -/// @return the event listener. -// ------------------------------------------------------------------------------------------ // - -IMPORT_C Void EvtRemoveListener(_Input const Char* event_name, _Input SCIObject listener); - -// ------------------------------------------------------------------------------------------ // -/// @brief Dispatch an event. -/// @param event_name the event name. -/// @param event_data the event data. -/// @return the event data. -// ------------------------------------------------------------------------------------------ // - -IMPORT_C VoidPtr EvtDispatchEvent(_Input const Char* event_name, _Input VoidPtr event_data); - -// ------------------------------------------------------------------------------------------ // -// Power API. -// ------------------------------------------------------------------------------------------ // - -enum -{ - kPowerCodeShutdown, - kPowerCodeReboot, - kPowerCodeSleep, - kPowerCodeWake, - kPowerCodeCount, -}; - -IMPORT_C SInt32 PwrReadCode(_Output SInt32& code); - -IMPORT_C SInt32 PwrSendCode(_Output SInt32& code); - -// ------------------------------------------------------------------------------------------ // -// CD-ROM API. -// ------------------------------------------------------------------------------------------ // - -IMPORT_C SInt32 CdEjectDrive(_Input const Char drv_letter); - -IMPORT_C SInt32 CdOpenTray(Void); - -IMPORT_C SInt32 CdCloseTray(Void); - -// ------------------------------------------------------------------------------------------ // -// Printer API. -// ------------------------------------------------------------------------------------------ // - -IMPORT_C SInt32 PrintOut(IOObject file /* nullptr to direct to stdout */, const Char* fmt, ...); - -IMPORT_C SInt32 PrintIn(IOObject file /* nullptr to direct to stdout */, const Char* fmt, ...); - -IMPORT_C IOObject PrintCreate(Void); - -IMPORT_C SInt32 PrintRelease(IOObject); - -IMPORT_C IOObject PrintGet(const Char* path); - -// ------------------------------------------------------------------------------------------ // -// @brief Scheduler/Debug API. -// ------------------------------------------------------------------------------------------ // - -typedef SInt32 AffinityKind; -typedef UInt64 PID; - -IMPORT_C SInt32 SchedAffinity(PID, SInt32 req, AffinityKind* local); - -IMPORT_C SInt32 SchedTrace(PID, SInt32 req, VoidPtr address, VoidPtr data); - -IMPORT_C SInt32 SchedKill(PID, SInt32 req); - -IMPORT_C SInt32 SchedBreakPoint(Void); - -// ------------------------------------------------------------------------------------------ // -// @brief Filesystem API. -// ------------------------------------------------------------------------------------------ // - -IMPORT_C BOOL FsCopy(const char* path, const char* dst); -IMPORT_C BOOL FsMove(const char* path, const char* dst); - -IMPORT_C BOOL FsExists(const char* path); - -IMPORT_C BOOL FsCreateDir(const char* path); -IMPORT_C BOOL FsCreateFile(const char* path); -IMPORT_C BOOL FsCreateAlias(const char* path, const char* from); - -// ------------------------------------------------------------------------------------------ // -// @brief Format API. -// ------------------------------------------------------------------------------------------ // - -IMPORT_C Char* StrFmt(const Char* fmt, ...); - -IMPORT_C UInt64 MathToNumber(const Char* in, const Char** endp, const SInt16 base); - -#endif // ifndef SCI_SCI_H diff --git a/dev/SCIKit/doc/LibSCI.drawio b/dev/SCIKit/doc/LibSCI.drawio deleted file mode 100644 index 4f566c57..00000000 --- a/dev/SCIKit/doc/LibSCI.drawio +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/dev/SCIKit/libsci.json b/dev/SCIKit/libsci.json deleted file mode 100644 index 453e29dc..00000000 --- a/dev/SCIKit/libsci.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "compiler_path": "x86_64-w64-mingw32-g++", - "compiler_std": "c++20", - "headers_path": ["../", "./"], - "sources_path": ["src/*.cc", "src/*.o"], - "output_name": "libSCIKit.dylib", - "compiler_flags": [ - "-fPIC", - "-ffreestanding", - "-shared", - "-fno-rtti", - "-fno-exceptions", - "-Wl,--subsystem=17" - ], - "cpp_macros": [ - "kSCIVersion=0x0100", - "kSCIVersionHighest=0x0100", - "kSCIVersionLowest=0x0100" - ] -} diff --git a/dev/SCIKit/src/LPC.cc b/dev/SCIKit/src/LPC.cc deleted file mode 100644 index f4433161..00000000 --- a/dev/SCIKit/src/LPC.cc +++ /dev/null @@ -1,10 +0,0 @@ -/* ------------------------------------------- - -Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. - -File: LPC.cc -Purpose: Local Procedure Codes. - -------------------------------------------- */ - -#include diff --git a/dev/SCIKit/src/LibSCI+IO.asm b/dev/SCIKit/src/LibSCI+IO.asm deleted file mode 100644 index 769ae513..00000000 --- a/dev/SCIKit/src/LibSCI+IO.asm +++ /dev/null @@ -1,50 +0,0 @@ -;; /* -;; * ======================================================== -;; * -;; * libsci -;; * 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: - mov r8, rcx - syscall - ret - -sci_syscall_arg_2: - mov r8, rcx - mov r9, rdx - syscall - ret - -sci_syscall_arg_3: - mov rbx, r8 - - mov r8, rcx - mov r9, rdx - mov r10, rbx - - syscall - ret - -sci_syscall_arg_4: - mov rbx, r8 - mov rax, r9 - - mov r8, rcx - mov r9, rdx - mov r10, rbx - mov r11, rax - - syscall - ret diff --git a/dev/SCIKit/src/Makefile b/dev/SCIKit/src/Makefile deleted file mode 100644 index f8f9fbe4..00000000 --- a/dev/SCIKit/src/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -################################################## -# (c) Amlal EL Mahrouss, all rights reserved. -# This is the bootloader makefile. -################################################## - -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) libsci+IO.asm -o libsci+IO.o diff --git a/dev/SCIKit/src/SCI.cc b/dev/SCIKit/src/SCI.cc deleted file mode 100644 index 6556754d..00000000 --- a/dev/SCIKit/src/SCI.cc +++ /dev/null @@ -1,45 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. - -------------------------------------------- */ - -#include - -/// @file libsci.cc -/// @brief Source file for the memory functions of the libsci. - -/// @brief Copy memory region. -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; -} - -/// @brief Fill memory region with **value**. -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; -} diff --git a/dev/boot/amd64-desktop.make b/dev/boot/amd64-desktop.make index 22023923..5a827643 100644 --- a/dev/boot/amd64-desktop.make +++ b/dev/boot/amd64-desktop.make @@ -75,7 +75,7 @@ BOOTLOADER=bootz.exe KERNEL=neoskrnl.exe SYSCHK=syschk.sys STARTUP=startup.sys -SCIKIT=libSCIKit.dylib +SCIKIT=libuser.dylib .PHONY: invalid-recipe invalid-recipe: @@ -90,7 +90,7 @@ all: compile-amd64 $(COPY) src/$(BOOTLOADER) src/Root/EFI/BOOT/BOOTZ.EFI $(COPY) ../kernel/$(KERNEL) src/Root/$(KERNEL) $(COPY) ./modules/SysChk/$(SYSCHK) src/Root/$(SYSCHK) - $(COPY) ../SCIKit/$(SCIKIT) src/Root/$(SCIKIT) + $(COPY) ../user/$(SCIKIT) src/Root/$(SCIKIT) $(COPY) src/$(BOOTLOADER) src/Root/$(BOOTLOADER) .PHONY: disk diff --git a/dev/boot/modules/NetBoot/NetBoot.cc b/dev/boot/modules/NetBoot/NetBoot.cc index 16a9ae1e..1787045e 100644 --- a/dev/boot/modules/NetBoot/NetBoot.cc +++ b/dev/boot/modules/NetBoot/NetBoot.cc @@ -15,7 +15,7 @@ EXTERN_C Int32 ModuleMain(NeOS::HEL::BootInfoHeader* handover) { NETBOOT_INTERNET_HEADER inet{}; - /// TODO: Read Packet from localhost + /// TODO: Read packet from JSON file 'netboot.json' if (inet.PatchLength < 0) { @@ -27,7 +27,7 @@ EXTERN_C Int32 ModuleMain(NeOS::HEL::BootInfoHeader* handover) if (!inet.EEPROM) { - Boot::BootThread thread(inet.Data); + Boot::BootThread thread(inet.PatchData); if (thread.IsValid()) return thread.Start(handover, YES); @@ -35,7 +35,9 @@ EXTERN_C Int32 ModuleMain(NeOS::HEL::BootInfoHeader* handover) else { Boot::BootTextWriter writer; - writer.Write("NetBootLauncher: EEPROM flash not available for now.\r"); + writer.Write("NetBootLauncher: EEPROM flash is not available as of right now.\r"); + + /// TODO: Program new firmware to EEPROM (if crc and size matches) return kEfiFail; // TODO: Add support for EEPROM firmware update. } diff --git a/dev/boot/modules/NetBoot/NetBoot.h b/dev/boot/modules/NetBoot/NetBoot.h index aeeaf18b..e250ef27 100644 --- a/dev/boot/modules/NetBoot/NetBoot.h +++ b/dev/boot/modules/NetBoot/NetBoot.h @@ -29,5 +29,5 @@ typedef struct NETBOOT_INTERNET_HEADER NeOS::Char PatchTarget[kNetBootNameLen]; /// the target file. NeOS::Boolean EEPROM : 1; /// does it imply an EEPROM reprogram? NeOS::Boolean Preflight : 1; /// is it a preflight packet. - NeOS::Char Data[]; /// non preflight packet has a patch blob for a **PatchTarget** + NeOS::Char PatchData[]; /// non preflight packet has a patch blob for a **PatchTarget** } ATTRIBUTE(packed) NETBOOT_INTERNET_HEADER; diff --git a/dev/boot/modules/NetBoot/build.json b/dev/boot/modules/NetBoot/build.json index 2c92567d..db035094 100644 --- a/dev/boot/modules/NetBoot/build.json +++ b/dev/boot/modules/NetBoot/build.json @@ -2,10 +2,9 @@ "compiler_path": "x86_64-w64-mingw32-g++", "compiler_std": "c++20", "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], - "sources_path": [".cc", "*.S"], + "sources_path": ["*.cc", "*.S", "../../src/HEL/AMD64/*.cc", "../../src/HEL/AMD64/*.S", "../../src/*.cc"], "output_name": "netboot.sys", "compiler_flags": [ - "-ffreestanding", "-nostdlib", "-std=c++20", "-fPIC", @@ -16,6 +15,7 @@ "cpp_macros": [ "__NEOSKRNL__", "__BOOTZ__", + "__BOOTLDR_STANDALONE__", "__NE_AMD64__", "kNetBootVersionHighest=0x0100", "kNetBootVersionLowest=0x0100", diff --git a/dev/boot/src/HEL/AMD64/BootEFI.cc b/dev/boot/src/HEL/AMD64/BootEFI.cc index 5af3643b..0840bc31 100644 --- a/dev/boot/src/HEL/AMD64/BootEFI.cc +++ b/dev/boot/src/HEL/AMD64/BootEFI.cc @@ -280,7 +280,7 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr image_handle, } UInt32 sz_ver = sizeof(UInt64); - UInt64 ver = KERNEL_VERSION_BCD; + UInt64 ver = KERNEL_VERSION_BCD; ST->RuntimeServices->GetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver, &ver); diff --git a/dev/ddk/ddk.h b/dev/ddk/ddk.h new file mode 100644 index 00000000..f6f8d48e --- /dev/null +++ b/dev/ddk/ddk.h @@ -0,0 +1,112 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + FILE: ddk.h + PURPOSE: DDK Driver model base header. + +------------------------------------------- */ + +#pragma once + +#include +#include + +#if defined(__cplusplus) +#define BOOL bool +#define YES true +#define NO false +#define DDK_EXTERN extern "C" __declspec(dllexport) +#define nil nullptr +#undef NULL +#define NULL 0 +#define DDK_FINAL final +#else +#define BOOL char +#define YES 1 +#define NO 0 +#define DDK_EXTERN extern __declspec(dllexport) +#define nil ((void*)0) +#undef NULL +#define NULL ((void*)0) +#define DDK_FINAL +#endif // defined(__cplusplus) + +#ifndef __DDK__ +#undef DDK_EXTERN +#if defined(__cplusplus) +#define DDK_EXTERN extern "C" __declspec(dllimport) +#else +#define DDK_EXTERN __declspec(dllimport) +#endif +#endif + +#define ATTRIBUTE(X) __attribute__((X)) + +#ifndef __NEOSKRNL__ +#error !!! Do not include header in EL0/Ring 3 mode !!! +#endif // __MINOSKRNL__ + +struct DDK_STATUS_STRUCT; +struct DDK_OBJECT_MANIFEST; + +/// \brief Object handle manifest. +struct DDK_OBJECT_MANIFEST DDK_FINAL +{ + char* p_name; + int32_t p_kind; + void* p_object; +}; + +/// \brief DDK status ping structure. +struct DDK_STATUS_STRUCT DDK_FINAL +{ + int32_t s_action_id; + int32_t s_issuer_id; + int32_t s_group_id; + void* s_object; +}; + +/// @brief Call Kernel procedure. +/// @param name the procedure name. +/// @param cnt number of elements in **dat** +/// @param dat data argument pointer. +/// @param sz sz of whole data argument pointer. +/// @return result of call +DDK_EXTERN void* ke_call(const char* name, int32_t cnt, void* dat, size_t sz); + +/// @brief add a system call. +/// @param slot system call slot id. +/// @param slotFn, syscall slot. +DDK_EXTERN void ke_add_syscall(const int32_t slot, void (*slotFn)(void* a0)); + +/// @brief Allocates an heap ptr. +/// @param sz size of the allocated struct/type. +/// @return the pointer allocated or **nil**. +DDK_EXTERN void* kalloc(size_t sz); + +/// @brief Frees an heap ptr. +/// @param pointer kernel pointer to free. +DDK_EXTERN void kfree(void* the_ptr); + +/// @brief Gets a Kernel object. +/// @param slot object id (can be 0) +/// @param name the property's name. +/// @return DDK_OBJECT_MANIFEST. +DDK_EXTERN struct DDK_OBJECT_MANIFEST* ke_get_obj(const int slot, const char* name); + +/// @brief Set a Kernel object. +/// @param slot object id (can be 0) +/// @param name the property's name. +/// @param ddk_pr pointer to a object's DDK_OBJECT_MANIFEST. +/// @return returned object. +DDK_EXTERN void* ke_set_obj(const int32_t slot, const struct DDK_OBJECT_MANIFEST* ddk_pr); + +/// @brief The highest API version of the DDK. +DDK_EXTERN int32_t kApiVersionHighest; + +/// @brief The lowest API version of the DDK. +DDK_EXTERN int32_t kApiVersionLowest; + +/// @brief API version in BCD. +DDK_EXTERN int32_t kApiVersion; diff --git a/dev/ddk/ddk.json b/dev/ddk/ddk.json new file mode 100644 index 00000000..ffc05f5f --- /dev/null +++ b/dev/ddk/ddk.json @@ -0,0 +1,23 @@ +{ + "compiler_path": "x86_64-w64-mingw32-gcc", + "compiler_std": "c++20", + "headers_path": ["../", "./"], + "sources_path": ["src/*.c", "src/*.cc", "src/*.S"], + "output_name": "ddk.sys", + "compiler_flags": [ + "-ffreestanding", + "-shared", + "-std=c17", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17" + ], + "cpp_macros": [ + "__NEOSKRNL__", + "__DDK_AMD64__", + "__DDK__", + "kDDKVersionHighest=0x0100", + "kDDKVersionLowest=0x0100", + "kDDKVersion=0x0100" + ] +} diff --git a/dev/ddk/dev.h b/dev/ddk/dev.h new file mode 100644 index 00000000..98979e52 --- /dev/null +++ b/dev/ddk/dev.h @@ -0,0 +1,35 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + File: dev.h + Purpose: DDK device support. + +------------------------------------------- */ + +#pragma once + +#include + +struct _DDK_DEVICE; + +#define DDK_DEVICE_NAME_LEN (255) + +/// @brief Kernel Device driver. +typedef struct _DDK_DEVICE DDK_FINAL +{ + char d_name[DDK_DEVICE_NAME_LEN]; // the device name. Could be /./DEVICE_NAME/ + void* (*d_read)(void* arg, int len); // read from device. + void (*d_write)(void* arg, int len); + void (*d_wait)(void); // write to device. + struct _DDK_DEVICE* (*d_open)(const char* path); // open device. + void (*d_close)(struct _DDK_DEVICE* dev); // close device. +} DDK_DEVICE, *DDK_DEVICE_PTR; + +/// @brief Open a new device from path. +/// @param path the device's path. +DDK_EXTERN DDK_DEVICE_PTR open(const char* path); + +/// @brief Close any device. +/// @param device valid device. +DDK_EXTERN BOOL close(DDK_DEVICE_PTR device); diff --git a/dev/ddk/docs/SPECIFICATION_DDK.md b/dev/ddk/docs/SPECIFICATION_DDK.md new file mode 100644 index 00000000..5fbff990 --- /dev/null +++ b/dev/ddk/docs/SPECIFICATION_DDK.md @@ -0,0 +1,18 @@ +=================================== + +# 0: General Information + +=================================== + +- Programming Language: C/C++ +- Build System: Make/BTB +- Purpose: Driver Tool Kit, which you link against libddk.sys + +=================================== + +# 1: How It works + +=================================== + +- Driver shall directly call the kernel at specific ports. (Or a kernel call) +- Kernel must respond according to kernel call, otherwise a panic will occur. \ No newline at end of file diff --git a/dev/ddk/io.h b/dev/ddk/io.h new file mode 100644 index 00000000..cfd6d2d2 --- /dev/null +++ b/dev/ddk/io.h @@ -0,0 +1,18 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + Purpose: DDK Text I/O. + +------------------------------------------- */ + +#pragma once + +#include + +/// @brief print character into UART. +DDK_EXTERN void kputc(const char ch); + +/// @brief print string to UART. +/// @param message string to transmit to UART. +DDK_EXTERN void kprint(const char* message); diff --git a/dev/ddk/src/ddk_alloc.c b/dev/ddk/src/ddk_alloc.c new file mode 100644 index 00000000..9d0ac0db --- /dev/null +++ b/dev/ddk/src/ddk_alloc.c @@ -0,0 +1,36 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + Purpose: DDK allocator. + +------------------------------------------- */ + +#include + +/** + \brief Allocates a new heap on the Kernel's side. + \param sz the size of the heap block. + \return the newly allocated pointer. +*/ +DDK_EXTERN void* kalloc(size_t sz) +{ + if (!sz) + ++sz; + + void* ptr = ke_call("mm_new_heap", 1, &sz, sizeof(size_t)); + + return ptr; +} + +/** + \brief Frees a pointer from the heap. + \param ptr the pointer to free. +*/ +DDK_EXTERN void kfree(void* ptr) +{ + if (!ptr) + return; + + ke_call("mm_delete_heap", 1, ptr, 0); +} diff --git a/dev/ddk/src/ddk_dev.c b/dev/ddk/src/ddk_dev.c new file mode 100644 index 00000000..7514bf84 --- /dev/null +++ b/dev/ddk/src/ddk_dev.c @@ -0,0 +1,30 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + Purpose: DDK Text I/O. + +------------------------------------------- */ + +#include +#include + +/// @brief Open a new binary device from path. +DDK_EXTERN DDK_DEVICE_PTR open(const char* devicePath) +{ + if (!devicePath) + return nil; + + return ke_call("sk_open_dev", 1, (void*)devicePath, kstrlen(devicePath)); +} + +/// @brief Close any device. +/// @param device valid device. +DDK_EXTERN BOOL close(DDK_DEVICE_PTR device) +{ + if (!device) + return NO; + + ke_call("sk_close_dev", 1, device, sizeof(DDK_DEVICE)); + return YES; +} diff --git a/dev/ddk/src/ddk_io.c b/dev/ddk/src/ddk_io.c new file mode 100644 index 00000000..0e10a0ad --- /dev/null +++ b/dev/ddk/src/ddk_io.c @@ -0,0 +1,37 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + Purpose: DDK Text I/O. + +------------------------------------------- */ + +#include + +DDK_EXTERN void kputc(const char ch) +{ + char assembled[2] = {0}; + assembled[0] = ch; + assembled[1] = 0; + + ke_call("ke_put_string", 1, assembled, 1); +} + +/// @brief print string to UART. +/// @param message UART to transmit. +DDK_EXTERN void kprint(const char* message) +{ + if (!message) + return; + if (*message == 0) + return; + + size_t index = 0; + size_t len = kstrlen(message); + + while (index < len) + { + kputc(message[index]); + ++index; + } +} diff --git a/dev/ddk/src/ddk_kernel_call.c b/dev/ddk/src/ddk_kernel_call.c new file mode 100644 index 00000000..71dbebeb --- /dev/null +++ b/dev/ddk/src/ddk_kernel_call.c @@ -0,0 +1,60 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + Purpose: DDK Kernel call. + +------------------------------------------- */ + +#include +#include + +/// @brief this is an internal call, do not use it. +DDK_EXTERN ATTRIBUTE(naked) void* ke_call_dispatch(const char* name, int32_t cnt, void* data, size_t sz); + +/// @brief Interupt Kernel and call it's RPC. +/// @param KernelRpcName RPC name +/// @param cnt number of elements in **data** pointer. +/// @param data data pointer. +/// @param sz The size of the whole data pointer. +/// @retval void* Kernel call was successful. +/// @retval nil Kernel call failed, call KernelLastError(void) +DDK_EXTERN void* ke_call(const char* name, int32_t cnt, void* data, size_t sz) +{ + if (!name || *name == 0 || cnt == 0) + return nil; + + return ke_call_dispatch(name, cnt, data, sz); +} + +/// @brief Add system call. +/// @param slot system call slot +/// @param slotFn, syscall slot. +DDK_EXTERN void ke_add_syscall(const int slot, void (*slotFn)(void* a0)) +{ + ke_call("ke_add_syscall", slot, slotFn, 1); +} + +/// @brief Get a Kernel object. +/// @param slot property id (always 0) +/// @param name the object's name. +/// @return Object manifest. +DDK_EXTERN struct DDK_OBJECT_MANIFEST* ke_get_obj(const int slot, const char* name) +{ + struct DDK_OBJECT_MANIFEST* manifest = (struct DDK_OBJECT_MANIFEST*)ke_call("cf_get_kobj", slot, (void*)name, 1); + + if (!manifest) + return nil; + + return manifest; +} + +/// @brief Set a Kernel object. +/// @param slot property id (always 0) +/// @param name the object's name. +/// @param ddk_pr pointer to a object's DDK_OBJECT_MANIFEST. +/// @return property's object. +DDK_EXTERN void* ke_set_obj(const int slot, const struct DDK_OBJECT_MANIFEST* ddk_pr) +{ + return ke_call("cf_set_kobj", slot, (void*)ddk_pr, 1); +} diff --git a/dev/ddk/src/ddk_kernel_call_dispatch.S b/dev/ddk/src/ddk_kernel_call_dispatch.S new file mode 100644 index 00000000..9b32d0c2 --- /dev/null +++ b/dev/ddk/src/ddk_kernel_call_dispatch.S @@ -0,0 +1,36 @@ +/** + lang: asm + compiler: gnu + */ + +.globl ke_call_dispatch + +.text + +/* Really simple function, takes our va-list, + and brings it to the trap handler in the Kernel. */ + +#if defined(__DDK_AMD64__) + +/* args rcx, rdx, r8, r9 */ +ke_call_dispatch: + int $0x33 + ret + +#elif defined(__DDK_POWER64__) + +/* args r8, r9, r10, r11 */ +ke_call_dispatch: + /* There is no specific interrupt request id for a system call in POWER. */ + sc + blr + +#elif defined(__DDK_ARM64__) + +/* args x0, x8, x9, x10, x11 is kept to tell that this is a Kernel call */ +ke_call_dispatch: + /* There is no specific interrupt request id for a system call in ARM64 as well. */ + mov x9, #0x33 + svc #0 + +#endif diff --git a/dev/ddk/src/ddk_rt_cxx.cc b/dev/ddk/src/ddk_rt_cxx.cc new file mode 100644 index 00000000..1ac66457 --- /dev/null +++ b/dev/ddk/src/ddk_rt_cxx.cc @@ -0,0 +1,29 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + Purpose: DDK C++ runtime. + +------------------------------------------- */ + +#include + +void* operator new(size_t sz) +{ + return kalloc(sz); +} + +void operator delete(void* ptr) +{ + kfree(ptr); +} + +void* operator new[](size_t sz) +{ + return kalloc(sz); +} + +void operator delete[](void* ptr) +{ + kfree(ptr); +} diff --git a/dev/ddk/src/ddk_str.c b/dev/ddk/src/ddk_str.c new file mode 100644 index 00000000..1558e636 --- /dev/null +++ b/dev/ddk/src/ddk_str.c @@ -0,0 +1,40 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + Purpose: DDK Strings. + +------------------------------------------- */ + +#include + +DDK_EXTERN size_t kstrlen(const char* in) +{ + if (in == nil) + return 0; + + if (*in == 0) + return 0; + + size_t index = 0; + + while (in[index] != 0) + { + ++index; + } + + return index; +} + +DDK_EXTERN int kstrncpy(char* dst, const char* src, size_t len) +{ + size_t index = 0; + + while (index != len) + { + dst[index] = src[index]; + ++index; + } + + return index; +} diff --git a/dev/ddk/src/ddk_ver.c b/dev/ddk/src/ddk_ver.c new file mode 100644 index 00000000..9641fb34 --- /dev/null +++ b/dev/ddk/src/ddk_ver.c @@ -0,0 +1,25 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + Purpose: DDK version symbols. + +------------------------------------------- */ + +#include + +#ifndef kDDKVersionHighest +#define kDDKVersionHighest 1 +#endif // !kDDKVersionHighest + +#ifndef kDDKVersionLowest +#define kDDKVersionLowest 1 +#endif // !kDDKVersionLowest + +#ifndef kDDKVersion +#define kDDKVersion 1 +#endif // !kDDKVersion + +int32_t kApiVersionHighest = kDDKVersionHighest; +int32_t kApiVersionLowest = kDDKVersionLowest; +int32_t kApiVersion = kDDKVersion; diff --git a/dev/ddk/str.h b/dev/ddk/str.h new file mode 100644 index 00000000..e1e206a8 --- /dev/null +++ b/dev/ddk/str.h @@ -0,0 +1,17 @@ +/* ------------------------------------------- + + Copyright Amlal EL Mahrouss. + + Purpose: DDK Strings. + +------------------------------------------- */ + +#pragma once + +#include + +/// @brief DDK equivalent of POSIX's string.h +/// @file str.h + +DDK_EXTERN size_t kstrlen(const char* in); +DDK_EXTERN int kstrncpy(char* dst, const char* src, size_t len); diff --git a/dev/kernel/FSKit/NeFS.h b/dev/kernel/FSKit/NeFS.h index b2f7095d..8ff9d9d2 100644 --- a/dev/kernel/FSKit/NeFS.h +++ b/dev/kernel/FSKit/NeFS.h @@ -17,7 +17,7 @@ default. #pragma once #include -#include +#include #include #include #include diff --git a/dev/kernel/KernelKit/FileMgr.h b/dev/kernel/KernelKit/FileMgr.h index 59203998..fec83d17 100644 --- a/dev/kernel/KernelKit/FileMgr.h +++ b/dev/kernel/KernelKit/FileMgr.h @@ -30,7 +30,7 @@ #endif // __FSKIT_INCLUDES_HeFS__ #include -#include +#include #include #include #include diff --git a/dev/kernel/KernelKit/LoaderInterface.h b/dev/kernel/KernelKit/LoaderInterface.h index 04e197aa..19c21ddb 100644 --- a/dev/kernel/KernelKit/LoaderInterface.h +++ b/dev/kernel/KernelKit/LoaderInterface.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include #include diff --git a/dev/kernel/KernelKit/MemoryMgr.h b/dev/kernel/KernelKit/MemoryMgr.h index 23a4721b..03bd4a2d 100644 --- a/dev/kernel/KernelKit/MemoryMgr.h +++ b/dev/kernel/KernelKit/MemoryMgr.h @@ -13,7 +13,7 @@ #include #include -#include +#include namespace NeOS { diff --git a/dev/kernel/KernelRsrc.rsrc b/dev/kernel/KernelRsrc.rsrc deleted file mode 100644 index d437c176..00000000 --- a/dev/kernel/KernelRsrc.rsrc +++ /dev/null @@ -1,25 +0,0 @@ -#include "CompilerKit/Version.h" - -1 VERSIONINFO -FILEVERSION 1,0,0,0 -PRODUCTVERSION 1,0,0,0 -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "080904E4" - BEGIN - VALUE "CompanyName", "Amlal El Mahrouss." - VALUE "FileDescription", "NeKernel." - VALUE "FileVersion", KERNEL_VERSION - VALUE "InternalName", "neoskrnl" - VALUE "LegalCopyright", "(c) 2024 Amlal El Mahrouss, all rights reserved." - VALUE "OriginalFilename", "neoskrnl.exe" - VALUE "ProductName", "NeKernel." - VALUE "ProductVersion", KERNEL_VERSION - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x809, 1252 - END -END diff --git a/dev/kernel/NetworkKit/IPC.h b/dev/kernel/NetworkKit/IPC.h index e8f0898f..0d28c080 100644 --- a/dev/kernel/NetworkKit/IPC.h +++ b/dev/kernel/NetworkKit/IPC.h @@ -12,7 +12,7 @@ #include #include -#include +#include /// @file IPC.h /// @brief IPC comm. protocol. diff --git a/dev/kernel/NewKit/Variant.h b/dev/kernel/NewKit/Variant.h index 99768016..3fa04496 100644 --- a/dev/kernel/NewKit/Variant.h +++ b/dev/kernel/NewKit/Variant.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include namespace NeOS { @@ -24,6 +24,7 @@ namespace NeOS kJson, kXML, kSwap, + kInvalid, }; public: diff --git a/dev/kernel/SwapKit/DiskSwap.h b/dev/kernel/SwapKit/DiskSwap.h new file mode 100644 index 00000000..b9c31a65 --- /dev/null +++ b/dev/kernel/SwapKit/DiskSwap.h @@ -0,0 +1,70 @@ + +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrouss Labs, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include +#include + +#define kSwapBlockMaxSize (mib_cast(16)) +#define kSwapPageFile "/boot/pagefile.sys" + +/// @file SwapDisk.h +/// @brief Virtual memory swap disk. + +namespace NeOS +{ + struct SWAP_DISK_HEADER; + + /// @brief Virtual memory interface to swap memory chunks onto disk. + class SwapDiskInterface final + { + public: + explicit SwapDiskInterface() = default; + ~SwapDiskInterface() = default; + + NE_COPY_DEFAULT(SwapDiskInterface); + + public: + /***********************************************************************************/ + /// @brief Write memory chunk onto disk. + /// @param fork_name The swap name to recognize this memory region. + /// @param fork_name_len length of fork name. + /// @param data the data packet. + /// @return Whether the swap was written to disk, or not. + /***********************************************************************************/ + BOOL Write(const Char* fork_name, const SizeT fork_name_len, SWAP_DISK_HEADER* data); + + /***********************************************************************************/ + /// @brief Read memory chunk from disk. + /// @param fork_name The swap name to recognize this memory region. + /// @param fork_name_len length of fork name. + /// @param data the data packet length. + /// @return Whether the swap was fetched to disk, or not. + /***********************************************************************************/ + SWAP_DISK_HEADER* Read(const Char* fork_name, const SizeT fork_name_len, const SizeT data_len); + }; + + /// @brief Swap disk header, containing information about the held virtual memory. + /// @param fMagic Ident number. + /// @param fHeaderSz This header size. + /// @param fTeamID Process Team ID. + /// @param fProcessID Process ID. + /// @param fVirtualAddress Virtual address pointed by data. + /// @param fBlobSz Blob's size. + /// @param fBlob Data blob. + typedef struct SWAP_DISK_HEADER + { + UInt32 fMagic; + SizeT fHeaderSz; + UInt64 fTeamID; + UInt64 fProcessID; + UInt64 fVirtualAddress; + SizeT fBlobSz; + UInt8 fBlob[]; + } PACKED SWAP_DISK_HEADER; +} // namespace NeOS diff --git a/dev/kernel/SwapKit/SwapDisk.h b/dev/kernel/SwapKit/SwapDisk.h deleted file mode 100644 index b9c31a65..00000000 --- a/dev/kernel/SwapKit/SwapDisk.h +++ /dev/null @@ -1,70 +0,0 @@ - -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrouss Labs, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include -#include - -#define kSwapBlockMaxSize (mib_cast(16)) -#define kSwapPageFile "/boot/pagefile.sys" - -/// @file SwapDisk.h -/// @brief Virtual memory swap disk. - -namespace NeOS -{ - struct SWAP_DISK_HEADER; - - /// @brief Virtual memory interface to swap memory chunks onto disk. - class SwapDiskInterface final - { - public: - explicit SwapDiskInterface() = default; - ~SwapDiskInterface() = default; - - NE_COPY_DEFAULT(SwapDiskInterface); - - public: - /***********************************************************************************/ - /// @brief Write memory chunk onto disk. - /// @param fork_name The swap name to recognize this memory region. - /// @param fork_name_len length of fork name. - /// @param data the data packet. - /// @return Whether the swap was written to disk, or not. - /***********************************************************************************/ - BOOL Write(const Char* fork_name, const SizeT fork_name_len, SWAP_DISK_HEADER* data); - - /***********************************************************************************/ - /// @brief Read memory chunk from disk. - /// @param fork_name The swap name to recognize this memory region. - /// @param fork_name_len length of fork name. - /// @param data the data packet length. - /// @return Whether the swap was fetched to disk, or not. - /***********************************************************************************/ - SWAP_DISK_HEADER* Read(const Char* fork_name, const SizeT fork_name_len, const SizeT data_len); - }; - - /// @brief Swap disk header, containing information about the held virtual memory. - /// @param fMagic Ident number. - /// @param fHeaderSz This header size. - /// @param fTeamID Process Team ID. - /// @param fProcessID Process ID. - /// @param fVirtualAddress Virtual address pointed by data. - /// @param fBlobSz Blob's size. - /// @param fBlob Data blob. - typedef struct SWAP_DISK_HEADER - { - UInt32 fMagic; - SizeT fHeaderSz; - UInt64 fTeamID; - UInt64 fProcessID; - UInt64 fVirtualAddress; - SizeT fBlobSz; - UInt8 fBlob[]; - } PACKED SWAP_DISK_HEADER; -} // namespace NeOS diff --git a/dev/kernel/amd64-ci.make b/dev/kernel/amd64-ci.make index 692b7b54..71f49082 100644 --- a/dev/kernel/amd64-ci.make +++ b/dev/kernel/amd64-ci.make @@ -49,7 +49,7 @@ WINDRES=x86_64-w64-mingw32-windres .PHONY: newos-amd64-epm newos-amd64-epm: clean - $(WINDRES) KernelRsrc.rsrc -O coff -o KernelRsrc.obj + $(WINDRES) kernel_rsrc.rsrc -O coff -o kernel_rsrc.obj $(CXX) $(CCFLAGS) $(DISK_DRV) $(DEBUG_MACRO) $(wildcard src/*.cc) $(wildcard HALKit/AMD64/PCI/*.cc) $(wildcard src/Network/*.cc) $(wildcard src/Storage/*.cc) $(wildcard src/FS/*.cc) $(wildcard HALKit/AMD64/Storage/*.cc) $(wildcard HALKit/AMD64/*.cc) $(wildcard src/Swap/*.cc) $(wildcard HALKit/AMD64/*.s) $(ASM) $(ASMFLAGS) HALKit/AMD64/HalInterruptAPI.asm $(ASM) $(ASMFLAGS) HALKit/AMD64/HalCommonAPI.asm diff --git a/dev/kernel/amd64-desktop.make b/dev/kernel/amd64-desktop.make index 692b7b54..71f49082 100644 --- a/dev/kernel/amd64-desktop.make +++ b/dev/kernel/amd64-desktop.make @@ -49,7 +49,7 @@ WINDRES=x86_64-w64-mingw32-windres .PHONY: newos-amd64-epm newos-amd64-epm: clean - $(WINDRES) KernelRsrc.rsrc -O coff -o KernelRsrc.obj + $(WINDRES) kernel_rsrc.rsrc -O coff -o kernel_rsrc.obj $(CXX) $(CCFLAGS) $(DISK_DRV) $(DEBUG_MACRO) $(wildcard src/*.cc) $(wildcard HALKit/AMD64/PCI/*.cc) $(wildcard src/Network/*.cc) $(wildcard src/Storage/*.cc) $(wildcard src/FS/*.cc) $(wildcard HALKit/AMD64/Storage/*.cc) $(wildcard HALKit/AMD64/*.cc) $(wildcard src/Swap/*.cc) $(wildcard HALKit/AMD64/*.s) $(ASM) $(ASMFLAGS) HALKit/AMD64/HalInterruptAPI.asm $(ASM) $(ASMFLAGS) HALKit/AMD64/HalCommonAPI.asm diff --git a/dev/kernel/kernel_rsrc.rsrc b/dev/kernel/kernel_rsrc.rsrc new file mode 100644 index 00000000..b5273787 --- /dev/null +++ b/dev/kernel/kernel_rsrc.rsrc @@ -0,0 +1,25 @@ +#include "CompilerKit/Version.h" + +1 VERSIONINFO +FILEVERSION 1,0,0,0 +PRODUCTVERSION 1,0,0,0 +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "080904E4" + BEGIN + VALUE "CompanyName", "Amlal El Mahrouss." + VALUE "FileDescription", "NeKernel" + VALUE "FileVersion", KERNEL_VERSION + VALUE "InternalName", "neoskrnl" + VALUE "LegalCopyright", "(c) 2024-2025 Amlal El Mahrouss, all rights reserved" + VALUE "OriginalFilename", "neoskrnl.exe" + VALUE "ProductName", "NeKernel" + VALUE "ProductVersion", KERNEL_VERSION + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x809, 1252 + END +END diff --git a/dev/kernel/src/Swap/DiskSwap.cc b/dev/kernel/src/Swap/DiskSwap.cc new file mode 100644 index 00000000..c50a96a9 --- /dev/null +++ b/dev/kernel/src/Swap/DiskSwap.cc @@ -0,0 +1,67 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrouss Labs, all rights reserved. + +------------------------------------------- */ + +#include +#include + +namespace NeOS +{ + /***********************************************************************************/ + /// @brief Write memory chunk onto disk. + /// @param fork_name The swap name to recognize this memory region. + /// @param fork_name_len length of fork name. + /// @param data the data packet. + /// @return Whether the swap was written to disk, or not. + /***********************************************************************************/ + BOOL SwapDiskInterface::Write(const Char* fork_name, const SizeT fork_name_len, SWAP_DISK_HEADER* data) + { + if (!fork_name || !fork_name_len) + return NO; + + if (*fork_name == 0) + return NO; + + if (!data) + return NO; + + FileStream file(kSwapPageFile, "wb"); + + auto ret = file.Write(fork_name, data, sizeof(SWAP_DISK_HEADER) + data->fBlobSz); + + if (ret.Error()) + return NO; + + return YES; + } + + /***********************************************************************************/ + /// @brief Read memory chunk from disk. + /// @param fork_name The swap name to recognize this memory region. + /// @param fork_name_len length of fork name. + /// @param data the data packet length. + /// @return Whether the swap was fetched to disk, or not. + /***********************************************************************************/ + SWAP_DISK_HEADER* SwapDiskInterface::Read(const Char* fork_name, const SizeT fork_name_len, const SizeT data_len) + { + if (!fork_name || !fork_name_len) + return nullptr; + + if (*fork_name == 0) + return nullptr; + + if (data_len > kSwapBlockMaxSize) + return nullptr; + + if (data_len == 0) + return nullptr; + + FileStream file(kSwapPageFile, "rb"); + + VoidPtr blob = file.Read(fork_name, sizeof(SWAP_DISK_HEADER) + data_len); + + return reinterpret_cast(blob); + } +} // namespace NeOS diff --git a/dev/kernel/src/Swap/SwapDisk.cc b/dev/kernel/src/Swap/SwapDisk.cc deleted file mode 100644 index fbc63b2f..00000000 --- a/dev/kernel/src/Swap/SwapDisk.cc +++ /dev/null @@ -1,67 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrouss Labs, all rights reserved. - -------------------------------------------- */ - -#include -#include - -namespace NeOS -{ - /***********************************************************************************/ - /// @brief Write memory chunk onto disk. - /// @param fork_name The swap name to recognize this memory region. - /// @param fork_name_len length of fork name. - /// @param data the data packet. - /// @return Whether the swap was written to disk, or not. - /***********************************************************************************/ - BOOL SwapDiskInterface::Write(const Char* fork_name, const SizeT fork_name_len, SWAP_DISK_HEADER* data) - { - if (!fork_name || !fork_name_len) - return NO; - - if (*fork_name == 0) - return NO; - - if (!data) - return NO; - - FileStream file(kSwapPageFile, "wb"); - - auto ret = file.Write(fork_name, data, sizeof(SWAP_DISK_HEADER) + data->fBlobSz); - - if (ret.Error()) - return NO; - - return YES; - } - - /***********************************************************************************/ - /// @brief Read memory chunk from disk. - /// @param fork_name The swap name to recognize this memory region. - /// @param fork_name_len length of fork name. - /// @param data the data packet length. - /// @return Whether the swap was fetched to disk, or not. - /***********************************************************************************/ - SWAP_DISK_HEADER* SwapDiskInterface::Read(const Char* fork_name, const SizeT fork_name_len, const SizeT data_len) - { - if (!fork_name || !fork_name_len) - return nullptr; - - if (*fork_name == 0) - return nullptr; - - if (data_len > kSwapBlockMaxSize) - return nullptr; - - if (data_len == 0) - return nullptr; - - FileStream file(kSwapPageFile, "rb"); - - VoidPtr blob = file.Read(fork_name, sizeof(SWAP_DISK_HEADER) + data_len); - - return reinterpret_cast(blob); - } -} // namespace NeOS diff --git a/dev/user/LPC.h b/dev/user/LPC.h new file mode 100644 index 00000000..87e5e500 --- /dev/null +++ b/dev/user/LPC.h @@ -0,0 +1,56 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include + +/// @file LPC.h +/// @brief Local Procedure Code type and values. + +#define err_local_ok() (kLastError == kErrorSuccess) +#define err_local_fail() (kLastError != kErrorSuccess) +#define err_local_get() (kLastError) + +typedef SInt32 ErrObject; + +inline constexpr ErrObject kErrorSuccess = 0; +inline constexpr ErrObject kErrorExecutable = 33; +inline constexpr ErrObject kErrorExecutableLib = 34; +inline constexpr ErrObject kErrorFileNotFound = 35; +inline constexpr ErrObject kErrorDirectoryNotFound = 36; +inline constexpr ErrObject kErrorDiskReadOnly = 37; +inline constexpr ErrObject kErrorDiskIsFull = 38; +inline constexpr ErrObject kErrorProcessFault = 39; +inline constexpr ErrObject kErrorSocketHangUp = 40; +inline constexpr ErrObject kErrorThreadLocalStorage = 41; +inline constexpr ErrObject kErrorMath = 42; +inline constexpr ErrObject kErrorNoNetwork = 43; +inline constexpr ErrObject kErrorHeapOutOfMemory = 44; +inline constexpr ErrObject kErrorNoSuchDisk = 45; +inline constexpr ErrObject kErrorFileExists = 46; +inline constexpr ErrObject kErrorFormatFailed = 47; +inline constexpr ErrObject kErrorNetworkTimeout = 48; +inline constexpr ErrObject kErrorInternal = 49; +inline constexpr ErrObject kErrorForkAlreadyExists = 50; +inline constexpr ErrObject kErrorOutOfTeamSlot = 51; +inline constexpr ErrObject kErrorHeapNotPresent = 52; +inline constexpr ErrObject kErrorNoEntrypoint = 53; +inline constexpr ErrObject kErrorDiskIsCorrupted = 54; +inline constexpr ErrObject kErrorDisk = 55; +inline constexpr ErrObject kErrorInvalidData = 56; +inline constexpr ErrObject kErrorAsync = 57; +inline constexpr ErrObject kErrorNonBlocking = 58; +inline constexpr ErrObject kErrorIPC = 59; +inline constexpr ErrObject kErrorSign = 60; +inline constexpr ErrObject kErrorInvalidCreds = 61; +inline constexpr ErrObject kErrorCDTrayBroken = 62; +inline constexpr ErrObject kErrorUnrecoverableDisk = 63; +inline constexpr ErrObject kErrorFileLocked = 64; +inline constexpr ErrObject kErrorUnimplemented = -1; + +/// @brief The last error reported by the system to the process. +IMPORT_C ErrObject kLastError; diff --git a/dev/user/Macros.h b/dev/user/Macros.h new file mode 100644 index 00000000..e2759258 --- /dev/null +++ b/dev/user/Macros.h @@ -0,0 +1,93 @@ +/* ------------------------------------------- + +Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. + +File: Macros.h +Purpose: libsci Macros header. + +------------------------------------------- */ + +#pragma once + +/***********************************************************************************/ +/// @file user/Macros.h +/// @brief Macros and Core types of the SCI (System Call Interface). +/***********************************************************************************/ + +#include + +#define ATTRIBUTE(X) __attribute__((X)) + +#define IMPORT_CXX extern "C++" +#define IMPORT_C extern "C" + +#define DEPRECATED ATTRIBUTE(deprecated) + +#define EXIT_SUCCESS (0) +#define EXIT_FAILURE (1) + +#define FILE_MAX_LEN (256) + +#ifndef BOOL +#define BOOL bool +#endif + +typedef bool Bool; +typedef bool Boolean; +typedef void Void; + +#ifndef __cplusplus +#define true (1) +#define false (0) +#endif + +#define YES true +#define NO false + +typedef __UINT64_TYPE__ UInt64; +typedef __UINT32_TYPE__ UInt32; +typedef __UINT16_TYPE__ UInt16; +typedef __UINT8_TYPE__ UInt8; + +typedef __SIZE_TYPE__ SizeT; + +typedef __INT64_TYPE__ SInt64; +typedef __INT32_TYPE__ SInt32; +typedef __INT16_TYPE__ SInt16; +typedef __INT8_TYPE__ SInt8; + +typedef void* VoidPtr; +typedef __UINTPTR_TYPE__ UIntPtr; +typedef char Char; + +#ifdef __cplusplus +typedef decltype(nullptr) nullPtr; +typedef nullPtr NullPtr; + +#define SCI_COPY_DELETE(KLASS) \ + KLASS& operator=(const KLASS&) = delete; \ + KLASS(const KLASS&) = delete; + +#define SCI_COPY_DEFAULT(KLASS) \ + KLASS& operator=(const KLASS&) = default; \ + KLASS(const KLASS&) = default; + +#define SCI_MOVE_DELETE(KLASS) \ + KLASS& operator=(KLASS&&) = delete; \ + KLASS(KLASS&&) = delete; + +#define SCI_MOVE_DEFAULT(KLASS) \ + KLASS& operator=(KLASS&&) = default; \ + KLASS(KLASS&&) = default; + +#endif + +IMPORT_C void _rtl_assert(Bool expr, const Char* origin); + +#define MUST_PASS(X) _rtl_assert(X, __FILE__) + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(X) \ + (((sizeof(X) / sizeof(*(X))) / \ + (static_cast(!(sizeof(X) % sizeof(*(X))))))) +#endif \ No newline at end of file diff --git a/dev/user/SystemCalls.h b/dev/user/SystemCalls.h new file mode 100644 index 00000000..6a0c2845 --- /dev/null +++ b/dev/user/SystemCalls.h @@ -0,0 +1,341 @@ +/* ------------------------------------------- + +Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. + +File: SystemCalls.h +Purpose: System Call Interface. + +------------------------------------------- */ + +#ifndef SCI_SCI_H +#define SCI_SCI_H + +#include + +// ------------------------------------------------------------------------------------------ // +/// @brief Types API. +// ------------------------------------------------------------------------------------------ // + +typedef VoidPtr SCIObject; + +typedef SCIObject IOObject; +typedef IOObject FSObject; +typedef SCIObject DLLObject; +typedef SCIObject ThreadObject; +typedef SCIObject SocketObject; + +// ------------------------------------------------------------------------------------------ // +/// @brief Dynamic Loader API. +// ------------------------------------------------------------------------------------------ // + +/// @brief Get function which is part of the DLL. +/// @param symbol the symbol to look for +/// @param dll_handle the DLL handle. +/// @return the proc pointer. +IMPORT_C SCIObject LdrGetDLLSymbolFromHandle(_Input const Char* symbol, _Input SCIObject dll_handle); + +/// @brief Open DLL handle. +/// @param path +/// @param drv +/// @return +IMPORT_C SCIObject LdrOpenDLLHandle(_Input const Char* path, _Input const Char* drive_letter); + +/// @brief Close DLL handle +/// @param dll_handle +/// @return +IMPORT_C Void LdrCloseDLLHandle(_Input SCIObject* dll_handle); + +// ------------------------------------------------------------------------------------------ // +// File API. +// ------------------------------------------------------------------------------------------ // + +/// @brief Opens a file from a drive. +/// @param fs_path the filesystem path. +/// @param drive_letter drive name, use NULL to use default drive location. +/// @return the file descriptor of the file. +IMPORT_C SCIObject IoOpenFile(const Char* fs_path, const Char* drive_letter); + +/// @brief Closes a file and flushes its content. +/// @param file_desc the file descriptor. +/// @return Function doesn't return a type. +IMPORT_C Void IoCloseFile(_Input SCIObject file_desc); + +/// @brief Write data to a file. +/// @param file_desc the file descriptor. +/// @param out_data the data to write. +/// @param sz_data the size of the data to write. +/// @return the number of bytes written. +IMPORT_C UInt32 IoWriteFile(_Input SCIObject file_desc, _Output VoidPtr out_data, SizeT sz_data); + +/// @brief Read data from a file. +/// @param file_desc the file descriptor. +/// @param out_data the data to read. +/// @param sz_data the size of the data to read. +IMPORT_C UInt32 IoReadFile(_Input SCIObject file_desc, _Output VoidPtr* out_data, SizeT sz_data); + +/// @brief Rewind the file pointer to the beginning of the file. +/// @param file_desc the file descriptor. +/// @return the number of bytes read. +IMPORT_C UInt64 IoRewindFile(_Input SCIObject file_desc); + +/// @brief Tell the current position of the file pointer. +/// @param file_desc the file descriptor. +/// @return the current position of the file pointer. +IMPORT_C UInt64 IoTellFile(_Input SCIObject file_desc); + +/// @brief Seek file offset from file descriptor. +IMPORT_C UInt64 IoSeekFile(_Input SCIObject file_desc, UInt64 file_offset); + +// ------------------------------------------------------------------------ +// Process API. +// ------------------------------------------------------------------------ + +/// @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. +IMPORT_C UInt32 RtlSpawnIB(UIntPtr process_id); + +/// @brief Spawns a process with a unique pid (stored as UIntPtr). +/// @param process_path process filesystem path. +/// @return > 0 process was created. +IMPORT_C UIntPtr RtlSpawnProcess(const Char* process_path, SizeT argc, Char** argv, Char** envp, SizeT envp_len); + +/// @brief Exits a process with an exit_code. +/// @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. +// ------------------------------------------------------------------------ + +/// @brief Creates a new heap from the process's address space. +/// @param len the length of it. +/// @param flags the flags of it. +/// @return heap pointer. +IMPORT_C VoidPtr MmCreateHeap(_Input SizeT len, _Input UInt32 flags); + +/// @brief Destroys the pointer +/// @param heap the heap itself. +/// @return void. +IMPORT_C Void MmDestroyHeap(_Input VoidPtr heap); + +/// @brief Change protection flags of a memory region. +IMPORT_C Void MmSetHeapFlags(_Input VoidPtr heap, _Input UInt32 flags); + +/// @brief Change protection flags of a memory region. +IMPORT_C UInt32 MmGetHeapFlags(_Input VoidPtr heap); + +/// @brief Fill memory region with CRC32. +IMPORT_C UInt32 MmFillCRC32Heap(_Input VoidPtr heap); + +/// @brief Copy memory region. +IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len); + +/// @brief Compare memory regions. +IMPORT_C SInt64 MmCmpMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len); + +/// @brief Fill memory region. +IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value); + +/// @brief Compare string regions. +IMPORT_C SInt64 MmStrCmp(_Input const Char* dest, _Input const Char* src); + +/// @brief Get length of string. +IMPORT_C SInt64 MmStrLen(const Char* str); + +// ------------------------------------------------------------------------ +// @brief Error API. +// ------------------------------------------------------------------------ + +IMPORT_C SInt32 ErrGetLastError(Void); + +// ------------------------------------------------------------------------ +// @brief Threading API. +// ------------------------------------------------------------------------ + +/// @brief Exit the current thread. +/// @param exit_code the exit code. +IMPORT_C Void ThrExitCurrentThread(_Input SInt32 exit_code); + +/// @brief Exit the main thread. +/// @param exit_code the exit code. +IMPORT_C Void ThrExitMainThread(_Input SInt32 exit_code); + +/// @brief Exit a thread. +/// @param thread the thread to exit. +/// @param exit_code the exit code. +IMPORT_C Void ThrExitThread(_Input ThreadObject thread, _Input SInt32 exit_code); + +/// @brief Thread procedure function type. +typedef Void (*thread_proc_kind)(int 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 ThreadObject ThrCreateThread(thread_proc_kind procedure, SInt32 argument_count, SInt32 flags); + +/// @brief Yields the current thread. +/// @param thread the thread to yield. +IMPORT_C Void ThrYieldThread(ThreadObject thrd); + +/// @brief Joins a thread. +/// @param thread the thread to join. +IMPORT_C Void ThrJoinThread(ThreadObject thrd); + +/// @brief Detach a thread. +/// @param thread the thread to detach. +IMPORT_C Void ThrDetachThread(ThreadObject thrd); + +// ------------------------------------------------------------------------ +// @brief Drive Management API. +// ------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------ // +/// @brief Get the default drive letter. +/// @param void. +/// @return the drive letter. +// ------------------------------------------------------------------------------------------ // +IMPORT_C Char* DrvGetDefaultDriveLetter(Void); + +// ------------------------------------------------------------------------------------------ // +/// @brief Get the drive letter from a path. +/// @param path the path. +/// @return the drive letter. +// ------------------------------------------------------------------------------------------ // +IMPORT_C Char* DrvGetDriveLetterFromPath(_Input const Char* path); + +// ------------------------------------------------------------------------------------------ // +/// @brief Get a mounted drive from a letter. +/// @param letter the letter (A..Z). +/// @return the drive object. +// ------------------------------------------------------------------------------------------ // +IMPORT_C SCIObject DrvGetMountedDrive(_Input const Char letter); + +// ------------------------------------------------------------------------------------------ // +/// @brief Mount a drive. +/// @param path the path to mount. +/// @param letter the letter to mount. +// ------------------------------------------------------------------------------------------ // +IMPORT_C Void DrvMountDrive(_Input const Char* path, _Input const Char* letter); + +// ------------------------------------------------------------------------------------------ // +/// @brief Unmount a drive. +/// @param letter the letter to unmount. +// ------------------------------------------------------------------------------------------ // +IMPORT_C Void DrvUnmountDrive(_Input const Char letter); + +// ------------------------------------------------------------------------ +// Event handling API, use to listen to OS specific events. +// ------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------ // +/// @brief Add an event listener. +/// @param event_name the event name. +/// @param listener the listener to add. +/// @return the event listener. +// ------------------------------------------------------------------------------------------ // + +IMPORT_C Void EvtAddListener(_Input const Char* event_name, _Input SCIObject listener); + +// ------------------------------------------------------------------------------------------ // +/// @brief Remove an event listener. +/// @param event_name the event name. +/// @param listener the listener to remove. +/// @return the event listener. +// ------------------------------------------------------------------------------------------ // + +IMPORT_C Void EvtRemoveListener(_Input const Char* event_name, _Input SCIObject listener); + +// ------------------------------------------------------------------------------------------ // +/// @brief Dispatch an event. +/// @param event_name the event name. +/// @param event_data the event data. +/// @return the event data. +// ------------------------------------------------------------------------------------------ // + +IMPORT_C VoidPtr EvtDispatchEvent(_Input const Char* event_name, _Input VoidPtr event_data); + +// ------------------------------------------------------------------------------------------ // +// Power API. +// ------------------------------------------------------------------------------------------ // + +enum +{ + kPowerCodeShutdown, + kPowerCodeReboot, + kPowerCodeSleep, + kPowerCodeWake, + kPowerCodeCount, +}; + +IMPORT_C SInt32 PwrReadCode(_Output SInt32& code); + +IMPORT_C SInt32 PwrSendCode(_Output SInt32& code); + +// ------------------------------------------------------------------------------------------ // +// CD-ROM API. +// ------------------------------------------------------------------------------------------ // + +IMPORT_C SInt32 CdEjectDrive(_Input const Char drv_letter); + +IMPORT_C SInt32 CdOpenTray(Void); + +IMPORT_C SInt32 CdCloseTray(Void); + +// ------------------------------------------------------------------------------------------ // +// Printer API. +// ------------------------------------------------------------------------------------------ // + +IMPORT_C SInt32 PrintOut(IOObject file /* nullptr to direct to stdout */, const Char* fmt, ...); + +IMPORT_C SInt32 PrintIn(IOObject file /* nullptr to direct to stdout */, const Char* fmt, ...); + +IMPORT_C IOObject PrintCreate(Void); + +IMPORT_C SInt32 PrintRelease(IOObject); + +IMPORT_C IOObject PrintGet(const Char* path); + +// ------------------------------------------------------------------------------------------ // +// @brief Scheduler/Debug API. +// ------------------------------------------------------------------------------------------ // + +typedef SInt32 AffinityKind; +typedef UInt64 PID; + +IMPORT_C SInt32 SchedAffinity(PID, SInt32 req, AffinityKind* local); + +IMPORT_C SInt32 SchedTrace(PID, SInt32 req, VoidPtr address, VoidPtr data); + +IMPORT_C SInt32 SchedKill(PID, SInt32 req); + +IMPORT_C SInt32 SchedBreakPoint(Void); + +// ------------------------------------------------------------------------------------------ // +// @brief Filesystem API. +// ------------------------------------------------------------------------------------------ // + +IMPORT_C BOOL FsCopy(const char* path, const char* dst); +IMPORT_C BOOL FsMove(const char* path, const char* dst); + +IMPORT_C BOOL FsExists(const char* path); + +IMPORT_C BOOL FsCreateDir(const char* path); +IMPORT_C BOOL FsCreateFile(const char* path); +IMPORT_C BOOL FsCreateAlias(const char* path, const char* from); + +// ------------------------------------------------------------------------------------------ // +// @brief Format API. +// ------------------------------------------------------------------------------------------ // + +IMPORT_C Char* StrFmt(const Char* fmt, ...); + +IMPORT_C UInt64 MathToNumber(const Char* in, const Char** endp, const SInt16 base); + +#endif // ifndef SCI_SCI_H diff --git a/dev/user/docs/SPECIFICATION_SYSCALLS.md b/dev/user/docs/SPECIFICATION_SYSCALLS.md new file mode 100644 index 00000000..b01018c7 --- /dev/null +++ b/dev/user/docs/SPECIFICATION_SYSCALLS.md @@ -0,0 +1,9 @@ +=================================== + +# 0: General Information + +=================================== + +- Programming Language: C/C++ +- Build System: Make/BTB +- Purpose: System Call Interface diff --git a/dev/user/libsci.json b/dev/user/libsci.json new file mode 100644 index 00000000..ed9a8739 --- /dev/null +++ b/dev/user/libsci.json @@ -0,0 +1,20 @@ +{ + "compiler_path": "x86_64-w64-mingw32-g++", + "compiler_std": "c++20", + "headers_path": ["../", "./"], + "sources_path": ["src/*.cc", "src/*.o"], + "output_name": "libuser.dylib", + "compiler_flags": [ + "-fPIC", + "-ffreestanding", + "-shared", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17" + ], + "cpp_macros": [ + "kSCIVersion=0x0100", + "kSCIVersionHighest=0x0100", + "kSCIVersionLowest=0x0100" + ] +} diff --git a/dev/user/src/GNUmakefile b/dev/user/src/GNUmakefile new file mode 100644 index 00000000..471e22d1 --- /dev/null +++ b/dev/user/src/GNUmakefile @@ -0,0 +1,16 @@ +################################################## +# (c) Amlal EL Mahrouss, all rights reserved. +# This is the bootloader makefile. +################################################## + +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) SystemCall+IO.asm -o SystemCall+IO.o diff --git a/dev/user/src/LPC.cc b/dev/user/src/LPC.cc new file mode 100644 index 00000000..f3f320b7 --- /dev/null +++ b/dev/user/src/LPC.cc @@ -0,0 +1,10 @@ +/* ------------------------------------------- + +Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. + +File: LPC.cc +Purpose: Local Procedure Codes. + +------------------------------------------- */ + +#include diff --git a/dev/user/src/SystemCall+IO.asm b/dev/user/src/SystemCall+IO.asm new file mode 100644 index 00000000..769ae513 --- /dev/null +++ b/dev/user/src/SystemCall+IO.asm @@ -0,0 +1,50 @@ +;; /* +;; * ======================================================== +;; * +;; * libsci +;; * 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: + mov r8, rcx + syscall + ret + +sci_syscall_arg_2: + mov r8, rcx + mov r9, rdx + syscall + ret + +sci_syscall_arg_3: + mov rbx, r8 + + mov r8, rcx + mov r9, rdx + mov r10, rbx + + syscall + ret + +sci_syscall_arg_4: + mov rbx, r8 + mov rax, r9 + + mov r8, rcx + mov r9, rdx + mov r10, rbx + mov r11, rax + + syscall + ret diff --git a/dev/user/src/SystemCalls.cc b/dev/user/src/SystemCalls.cc new file mode 100644 index 00000000..eadb07ce --- /dev/null +++ b/dev/user/src/SystemCalls.cc @@ -0,0 +1,45 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include + +/// @file libsci.cc +/// @brief Source file for the memory functions of the libsci. + +/// @brief Copy memory region. +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; +} + +/// @brief Fill memory region with **value**. +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; +} -- cgit v1.2.3