diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-02 19:38:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-02 19:38:46 +0200 |
| commit | 997be16e5ac9a68d54882ab69529815860d62955 (patch) | |
| tree | 19d6129c2d776bb1edc5d4a7325e39ca176c3403 /dev/user | |
| parent | 618104e74c195d7508a18450524f8ed7f9af8cc6 (diff) | |
| parent | b3b4b1ebdcd6adeac914869017c86d892b7a8ced (diff) | |
Merge pull request #28 from nekernel-org/dev
0.0.2
Diffstat (limited to 'dev/user')
| -rw-r--r-- | dev/user/Macros.h | 57 | ||||
| -rw-r--r-- | dev/user/Opts.h | 15 | ||||
| -rw-r--r-- | dev/user/ProcessCodes.h | 73 | ||||
| -rw-r--r-- | dev/user/SystemCalls.h | 50 | ||||
| -rw-r--r-- | dev/user/src/SystemCalls+IO.asm | 2 | ||||
| -rw-r--r-- | dev/user/src/SystemCalls.cc | 130 | ||||
| -rw-r--r-- | dev/user/user.json | 1 |
7 files changed, 183 insertions, 145 deletions
diff --git a/dev/user/Macros.h b/dev/user/Macros.h index 556833ea..0bb98a59 100644 --- a/dev/user/Macros.h +++ b/dev/user/Macros.h @@ -19,7 +19,7 @@ Purpose: libsci Macros header. #define ATTRIBUTE(X) __attribute__((X)) #define IMPORT_CXX extern "C++" -#define IMPORT_C extern "C" +#define IMPORT_C extern "C" #define DEPRECATED ATTRIBUTE(deprecated) @@ -42,12 +42,12 @@ typedef void Void; #endif #define YES true -#define NO false +#define NO false typedef __UINT64_TYPE__ UInt64; typedef __UINT32_TYPE__ UInt32; typedef __UINT16_TYPE__ UInt16; -typedef __UINT8_TYPE__ UInt8; +typedef __UINT8_TYPE__ UInt8; typedef __SIZE_TYPE__ SizeT; @@ -56,29 +56,29 @@ typedef __INT32_TYPE__ SInt32; typedef __INT16_TYPE__ SInt16; typedef __INT8_TYPE__ SInt8; -typedef void* VoidPtr; +typedef void* VoidPtr; typedef __UINTPTR_TYPE__ UIntPtr; -typedef char Char; +typedef char Char; #ifdef __cplusplus typedef decltype(nullptr) nullPtr; -typedef nullPtr NullPtr; +typedef nullPtr NullPtr; -#define SCI_COPY_DELETE(KLASS) \ - KLASS& operator=(const KLASS&) = delete; \ - KLASS(const KLASS&) = delete; +#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_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_DELETE(KLASS) \ + KLASS& operator=(KLASS&&) = delete; \ + KLASS(KLASS&&) = delete; -#define SCI_MOVE_DEFAULT(KLASS) \ - KLASS& operator=(KLASS&&) = default; \ - KLASS(KLASS&&) = default; +#define SCI_MOVE_DEFAULT(KLASS) \ + KLASS& operator=(KLASS&&) = default; \ + KLASS(KLASS&&) = default; #endif @@ -87,9 +87,8 @@ 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<SizeT>(!(sizeof(X) % sizeof(*(X))))))) +#define ARRAY_SIZE(X) \ + (((sizeof(X) / sizeof(*(X))) / (static_cast<SizeT>(!(sizeof(X) % sizeof(*(X))))))) #endif #ifndef KIB @@ -97,29 +96,31 @@ IMPORT_C void _rtl_assert(Bool expr, const Char* origin); #endif #ifndef kib_cast -#define kib_cast(X) (UInt64)((X)*1024) +#define kib_cast(X) (UInt64)((X) * 1024) #endif #ifndef MIB -#define MIB(X) (UInt64)((UInt64)KIB(X) / 1024) +#define MIB(X) (UInt64)((UInt64) KIB(X) / 1024) #endif #ifndef mib_cast -#define mib_cast(X) (UInt64)((UInt64)kib_cast(X) * 1024) +#define mib_cast(X) (UInt64)((UInt64) kib_cast(X) * 1024) #endif #ifndef GIB -#define GIB(X) (UInt64)((UInt64)MIB(X) / 1024) +#define GIB(X) (UInt64)((UInt64) MIB(X) / 1024) #endif #ifndef gib_cast -#define gib_cast(X) (UInt64)((UInt64)mib_cast(X) * 1024) +#define gib_cast(X) (UInt64)((UInt64) mib_cast(X) * 1024) #endif #ifndef TIB -#define TIB(X) (UInt64)((UInt64)GIB(X) / 1024) +#define TIB(X) (UInt64)((UInt64) GIB(X) / 1024) #endif #ifndef tib_cast -#define tib_cast(X) ((UInt64)gib_cast(X) * 1024) +#define tib_cast(X) ((UInt64) gib_cast(X) * 1024) #endif + +#define SCI_UNUSED(X) ((void) X) diff --git a/dev/user/Opts.h b/dev/user/Opts.h new file mode 100644 index 00000000..4b3b63c1 --- /dev/null +++ b/dev/user/Opts.h @@ -0,0 +1,15 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <user/SystemCalls.h> +#include <cstdarg> + +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 index 1b1b955b..90457944 100644 --- a/dev/user/ProcessCodes.h +++ b/dev/user/ProcessCodes.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -12,46 +12,47 @@ /// @brief Process Codes type and values. /// @author Amlal El Mahrouss (amlal@nekernel.org) -#define err_local_ok() (kLastError == kErrorSuccess) +#define err_local_ok() (kLastError == kErrorSuccess) #define err_local_fail() (kLastError != kErrorSuccess) -#define err_local_get() (kLastError) +#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 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 kErrorUnimplemented = -1; +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/SystemCalls.h b/dev/user/SystemCalls.h index cfbd99a8..5bbe2f26 100644 --- a/dev/user/SystemCalls.h +++ b/dev/user/SystemCalls.h @@ -35,14 +35,14 @@ typedef Ref SocketRef; IMPORT_C Ref LdrGetDLLSymbolFromHandle(_Input const Char* symbol, _Input Ref dll_handle);
/// @brief Open Dylib handle.
-/// @param path
-/// @param drv
-/// @return
+/// @param path dll path.
+/// @param drv driver letter.
+/// @return a dylib ref.
IMPORT_C Ref LdrOpenDLLHandle(_Input const Char* path, _Input const Char* drive_letter);
/// @brief Close Dylib handle
-/// @param dll_handle
-/// @return
+/// @param dll_handle the dylib ref.
+/// @return whether it closed or not.
IMPORT_C UInt32 LdrCloseDLLHandle(_Input Ref* dll_handle);
// ------------------------------------------------------------------------------------------ //
@@ -60,11 +60,26 @@ IMPORT_C Ref IoOpenFile(const Char* fs_path, const Char* drive_letter); /// @return Function doesn't return a type.
IMPORT_C Void IoCloseFile(_Input Ref file_desc);
+/// @brief I/O control (ioctl) on a file.
+/// @param file_desc the file descriptor.
+/// @param ioctl_code the ioctl code.
+/// @param in_data the input data.
+/// @param out_data the output data.
+/// @return the number of bytes written.
+/// @note This function is used to control the file descriptor, introduced for HeFS.
+IMPORT_C SInt32 IoCtrlFile(_Input Ref file_desc, _Input UInt32 ioctl_code, _Input VoidPtr in_data,
+ _Output VoidPtr out_data);
+
/// @brief Gets the file mime (if any)
/// @param file_desc the file descriptor.
IMPORT_C const Char* IoMimeFile(_Input Ref file_desc);
-/// @brief Write data to a file.
+/// @brief Gets the dir DIM.
+/// @param dir_desc directory descriptor.
+/// @note only works in HeFS, will return nil-x/nil if used on any other filesystem.
+IMPORT_C const Char* IoDimFile(_Input Ref dir_desc);
+
+/// @brief Write data to a file ref
/// @param file_desc the file descriptor.
/// @param out_data the data to write.
/// @param sz_data the size of the data to write.
@@ -94,7 +109,8 @@ IMPORT_C UInt64 IoSeekFile(_Input Ref file_desc, UInt64 file_offset); // Process API.
// ------------------------------------------------------------------------
-/// @brief Spawns a Thread Information Block and Global Information Block inside the current process.
+/// @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);
@@ -102,7 +118,8 @@ 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);
+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.
@@ -269,14 +286,13 @@ IMPORT_C VoidPtr EvtDispatchEvent(_Input const Char* event_name, _Input VoidPtr // Power API.
// ------------------------------------------------------------------------------------------ //
-enum
-{
- kPowerCodeInvalid,
- kPowerCodeShutdown,
- kPowerCodeReboot,
- kPowerCodeSleep,
- kPowerCodeWake,
- kPowerCodeCount,
+enum {
+ kPowerCodeInvalid,
+ kPowerCodeShutdown,
+ kPowerCodeReboot,
+ kPowerCodeSleep,
+ kPowerCodeWake,
+ kPowerCodeCount,
};
IMPORT_C SInt32 PwrReadCode(_Output SInt32& code);
@@ -346,4 +362,4 @@ IMPORT_C Char* StrFmt(const Char* fmt, ...); IMPORT_C UInt64 StrMathToNumber(const Char* in, const Char** endp, const SInt16 base);
-#endif // ifndef SCI_SCI_H
+#endif // ifndef SCI_SCI_H
diff --git a/dev/user/src/SystemCalls+IO.asm b/dev/user/src/SystemCalls+IO.asm index 58ea79e7..77e22b59 100644 --- a/dev/user/src/SystemCalls+IO.asm +++ b/dev/user/src/SystemCalls+IO.asm @@ -1,7 +1,7 @@ ;; /* ;; * ======================================================== ;; * -;; * libsci +;; * user/src/SystemCalls+IO.asm ;; * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ;; * ;; * ======================================================== diff --git a/dev/user/src/SystemCalls.cc b/dev/user/src/SystemCalls.cc index 7ad0fe6f..d8366d42 100644 --- a/dev/user/src/SystemCalls.cc +++ b/dev/user/src/SystemCalls.cc @@ -1,91 +1,95 @@ /* -------------------------------------------
- Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
+ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
------------------------------------------- */
+#include <user/Opts.h>
#include <user/SystemCalls.h>
/// @file SystemCalls.cc
-/// @brief Source file for the memory functions of the libsci.
-
-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);
+/// @brief Source file for the memory functions of the user.sys.
/// @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;
+IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len) {
+ if (!len || !dest || !src) {
+ return nullptr;
+ }
+
+ for (SizeT i = 0; i < len; i++) {
+ ((Char*) dest)[i] = ((Char*) src)[i];
+ }
+
+ return dest;
}
-IMPORT_C SInt64 MmStrLen(const Char* in)
-{
- if (!in)
- return 0;
+/// @brief Get string length.
+IMPORT_C SInt64 MmStrLen(const Char* in) {
+ if (!in) return 0;
- SizeT len{0};
+ SizeT len{0};
- do
- {
- ++len;
- } while (in[len] != '\0');
+ do {
+ ++len;
+ } while (in[len] != '\0');
- return len;
+ return len;
}
/// @brief Fill memory region **dest** 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;
+IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) {
+ if (!len || !dest) {
+ return nullptr;
+ }
+
+ for (SizeT i = 0; i < len; i++) {
+ ((Char*) dest)[i] = value;
+ }
+
+ return dest;
}
-IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter)
-{
- return sci_syscall_arg_3(1, reinterpret_cast<VoidPtr>(const_cast<Char*>(path)),
- reinterpret_cast<VoidPtr>(const_cast<Char*>(drv_letter)));
+IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter) {
+ return sci_syscall_arg_3(1, reinterpret_cast<VoidPtr>(const_cast<Char*>(path)),
+ reinterpret_cast<VoidPtr>(const_cast<Char*>(drv_letter)));
}
-IMPORT_C Void IoCloseFile(_Input Ref desc)
-{
- sci_syscall_arg_2(2, desc);
+IMPORT_C Void IoCloseFile(_Input Ref desc) {
+ sci_syscall_arg_2(2, desc);
}
-IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off)
-{
- auto ret = (UInt64*)sci_syscall_arg_3(3, reinterpret_cast<VoidPtr>(desc),
- reinterpret_cast<VoidPtr>(&off));
+IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) {
+ auto ret = (UInt64*) sci_syscall_arg_3(3, reinterpret_cast<VoidPtr>(desc),
+ reinterpret_cast<VoidPtr>(&off));
+
+ MUST_PASS((*ret) != ~0UL);
+ return *ret;
+}
- MUST_PASS((*ret) != ~0UL);
- return *ret;
+IMPORT_C UInt64 IoTellFile(_Input Ref desc) {
+ auto ret = (UInt64*) sci_syscall_arg_2(4, reinterpret_cast<VoidPtr>(desc));
+ return *ret;
}
-IMPORT_C UInt64 IoTellFile(_Input Ref desc)
-{
- auto ret = (UInt64*)sci_syscall_arg_2(4, reinterpret_cast<VoidPtr>(desc));
- return *ret;
+/// @brief Print to the file descriptor.
+/// @param desc the file descriptor.
+IMPORT_C SInt32 PrintOut(_Input IORef desc, const char* fmt, ...) {
+ va_list args;
+
+ va_start(args, fmt);
+
+ auto ret = (UInt64*) sci_syscall_arg_4(5, reinterpret_cast<VoidPtr>(desc),
+ reinterpret_cast<VoidPtr>(const_cast<Char*>(fmt)), args);
+
+ va_end(args);
+
+ return *ret;
}
+
+/// @internal
+IMPORT_C Void _rtl_assert(Bool expr, const Char* origin) {
+ if (!expr) {
+ PrintOut(nullptr, "Assertion failed: %s\r", origin);
+ PrintOut(nullptr, "Origin: %s\r", origin);
+ }
+}
\ No newline at end of file diff --git a/dev/user/user.json b/dev/user/user.json index 9581d4d4..05b90abf 100644 --- a/dev/user/user.json +++ b/dev/user/user.json @@ -7,6 +7,7 @@ "compiler_flags": [ "-ffreestanding", "-shared", + "-fPIC", "-fno-rtti", "-fno-exceptions", "-Wl,--subsystem=17" |
