From ee64ca1014ee2bdf07bd5011f6aa9f77da104a4d Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 4 Oct 2025 02:47:12 +0200 Subject: feat: kernel: new `kernel` process type, and usage of `explicit operator bool` feat: libSystem: minor tweaks. feat: indexer: important fixes in `RemoveFlag` Signed-off-by: Amlal El Mahrouss --- compile_flags.txt | 3 +- dev/kernel/FSKit/Ext2+IFS.h | 4 +- dev/kernel/KernelKit/CodeMgr.h | 3 +- dev/kernel/KernelKit/CoreProcessScheduler.h | 3 +- dev/kernel/KernelKit/KernelTaskScheduler.h | 4 +- dev/kernel/KernelKit/PECodeMgr.h | 77 ----------- dev/kernel/KernelKit/UserProcessScheduler.h | 4 +- dev/kernel/amd64-desktop.make | 2 +- dev/kernel/src/FS/Ext2+FileMgr.cc | 16 +-- dev/kernel/src/IndexableProperty.cc | 3 +- dev/libSystem/SystemKit/Verify.h | 4 +- dev/libSystem/src/System.cc | 193 ---------------------------- dev/libSystem/src/SystemCalls.cc | 193 ++++++++++++++++++++++++++++ 13 files changed, 219 insertions(+), 290 deletions(-) delete mode 100644 dev/kernel/KernelKit/PECodeMgr.h delete mode 100644 dev/libSystem/src/System.cc create mode 100644 dev/libSystem/src/SystemCalls.cc diff --git a/compile_flags.txt b/compile_flags.txt index 828dc123..8f006960 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -35,4 +35,5 @@ -D__FSKIT_INCLUDES_EXT2__ -D__NE_VIRTUAL_MEMORY_SUPPORT__ -DBOOTZ_GPT_SUPPORT --D__x86_64__ \ No newline at end of file +-D__x86_64__ +-D__NE_USE_PEF__ \ No newline at end of file diff --git a/dev/kernel/FSKit/Ext2+IFS.h b/dev/kernel/FSKit/Ext2+IFS.h index 661c8d5f..72e01ecd 100644 --- a/dev/kernel/FSKit/Ext2+IFS.h +++ b/dev/kernel/FSKit/Ext2+IFS.h @@ -144,7 +144,7 @@ inline Kernel::ErrorOr ext2_load_inode(Ext2Context* ctx, Kernel::UInt * * Provides high-level interface for EXT2 filesystem operations */ -class Ext2FileSystemParser { +class Ext2FileSystemParser final { private: Ext2Context ctx; // Internal EXT2 context @@ -157,6 +157,8 @@ class Ext2FileSystemParser { */ explicit Ext2FileSystemParser(DriveTrait* drive); + NE_COPY_DELETE(Ext2FileSystemParser) + /* * Open a file or directory by path * diff --git a/dev/kernel/KernelKit/CodeMgr.h b/dev/kernel/KernelKit/CodeMgr.h index c733bc47..fc737a90 100644 --- a/dev/kernel/KernelKit/CodeMgr.h +++ b/dev/kernel/KernelKit/CodeMgr.h @@ -14,9 +14,10 @@ #pragma once +#ifdef __NE_USE_PEF__ #include -#include #include +#endif /// @file CodeMgr.h /// @brief Code Manager header file. diff --git a/dev/kernel/KernelKit/CoreProcessScheduler.h b/dev/kernel/KernelKit/CoreProcessScheduler.h index f40ffc81..49e9363e 100644 --- a/dev/kernel/KernelKit/CoreProcessScheduler.h +++ b/dev/kernel/KernelKit/CoreProcessScheduler.h @@ -147,7 +147,8 @@ enum class ProcessSubsystem : Int32 { kProcessSubsystemUser, kProcessSubsystemService, kProcessSubsystemDriver, - kProcessSubsystemCount = kProcessSubsystemDriver - kProcessSubsystemSecurity + 1, + kProcessSubsystemKernel, + kProcessSubsystemCount = kProcessSubsystemKernel - kProcessSubsystemSecurity + 1, kProcessSubsystemInvalid = 0xFFFFFFF, }; diff --git a/dev/kernel/KernelKit/KernelTaskScheduler.h b/dev/kernel/KernelKit/KernelTaskScheduler.h index ce0ff30d..1bc8975a 100644 --- a/dev/kernel/KernelKit/KernelTaskScheduler.h +++ b/dev/kernel/KernelKit/KernelTaskScheduler.h @@ -24,7 +24,7 @@ typedef ProcessID KID; class KERNEL_TASK final { public: Char Name[kSchedNameLen] = {"KERNEL_TASK"}; - ProcessSubsystem SubSystem{ProcessSubsystem::kProcessSubsystemDriver}; + ProcessSubsystem SubSystem{ProcessSubsystem::kProcessSubsystemKernel}; HAL::StackFramePtr StackFrame{nullptr}; UInt8* StackReserve{nullptr}; SizeT StackSize{kSchedMaxStackSz}; @@ -40,7 +40,7 @@ class KernelTaskHelper final { public: STATIC Bool Switch(HAL::StackFramePtr frame_ptr, ProcessID new_kid); STATIC Bool CanBeScheduled(const KERNEL_TASK& process); - STATIC ErrorOr TheCurrentKID(); + STATIC ErrorOr TheCurrentKID(); STATIC SizeT StartScheduling(); }; } // namespace Kernel \ No newline at end of file diff --git a/dev/kernel/KernelKit/PECodeMgr.h b/dev/kernel/KernelKit/PECodeMgr.h deleted file mode 100644 index 15c2b7ee..00000000 --- a/dev/kernel/KernelKit/PECodeMgr.h +++ /dev/null @@ -1,77 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - - File: PECodeMgr.h - Purpose: PE32+ Code Mgr and Dylib mgr. - - Revision History: - - 12/02/24: Added file (amlel) - -------------------------------------------- */ - -#pragma once - -//////////////////////////////////////////////////// - -// LAST REV: Mon Feb 12 13:52:01 CET 2024 - -//////////////////////////////////////////////////// - -#include -#include -#include -#include -#include - -#ifndef INC_PROCESS_SCHEDULER_H -#include -#endif - -#define kPeApplicationMime "application/vnd-portable-executable" - -namespace Kernel { -/// -/// \name PE32Loader -/// \brief PE32+ loader class. -/// -class PE32Loader : public LoaderInterface { - private: - explicit PE32Loader() = delete; - - public: - explicit PE32Loader(const VoidPtr blob); - explicit PE32Loader(const Char* path); - ~PE32Loader() override; - - public: - NE_COPY_DEFAULT(PE32Loader) - - public: - const Char* Path() override; - const Char* AsString() override; - const Char* MIME() override; - - public: - ErrorOr FindStart() override; - ErrorOr FindSymbol(const Char* name, Int32 kind) override; - ErrorOr GetBlob() override; - - public: - bool IsLoaded() noexcept; - - private: -#ifdef __FSKIT_INCLUDES_NEFS__ - OwnPtr> fFile; -#elif defined(__FSKIT_INCLUDES_HEFS__) - OwnPtr> fFile; -#else - OwnPtr> fFile; -#endif // __FSKIT_INCLUDES_NEFS__ - - Ref fPath; - VoidPtr fCachedBlob; - bool fBad; -}; -} // namespace Kernel \ No newline at end of file diff --git a/dev/kernel/KernelKit/UserProcessScheduler.h b/dev/kernel/KernelKit/UserProcessScheduler.h index e5b81f76..d106e511 100644 --- a/dev/kernel/KernelKit/UserProcessScheduler.h +++ b/dev/kernel/KernelKit/UserProcessScheduler.h @@ -86,7 +86,7 @@ class USER_PROCESS final { /***********************************************************************************/ //! @brief boolean operator, check status. /***********************************************************************************/ - operator bool(); + explicit operator bool(); /***********************************************************************************/ ///! @brief Crashes the app, exits with code ~0. @@ -192,7 +192,7 @@ class UserProcessScheduler final : public ISchedulable { NE_MOVE_DELETE(UserProcessScheduler) public: - operator bool(); + explicit operator bool(); bool operator!(); public: diff --git a/dev/kernel/amd64-desktop.make b/dev/kernel/amd64-desktop.make index bfcca16b..a3040c89 100644 --- a/dev/kernel/amd64-desktop.make +++ b/dev/kernel/amd64-desktop.make @@ -5,7 +5,7 @@ CXX = x86_64-w64-mingw32-g++ LD = x86_64-w64-mingw32-ld -CCFLAGS = -fshort-wchar -c -D__NE_AMD64__ -D__NE_VEPM__ -Wall -Wpedantic -Wextra -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__FSKIT_INCLUDES_HEFS__ -D__FSKIT_INCLUDES_EXT2__ -D__NE_SUPPORT_NX__ -O0 -I../vendor -D__NEOSKRNL__ -D__HAVE_NE_APIS__ -D__FREESTANDING__ -D__NE_VIRTUAL_MEMORY_SUPPORT__ -D__NE_AUTO_FORMAT__ -D__NE__ -I./ -I../ -I../boot +CCFLAGS = -fshort-wchar -c -D__NE_AMD64__ -D__NE_USE_PEF__ -D__NE_VEPM__ -Wall -Wpedantic -Wextra -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__FSKIT_INCLUDES_HEFS__ -D__FSKIT_INCLUDES_EXT2__ -D__NE_SUPPORT_NX__ -O0 -I../vendor -D__NEOSKRNL__ -D__HAVE_NE_APIS__ -D__FREESTANDING__ -D__NE_VIRTUAL_MEMORY_SUPPORT__ -D__NE_AUTO_FORMAT__ -D__NE__ -I./ -I../ -I../boot ASM = nasm diff --git a/dev/kernel/src/FS/Ext2+FileMgr.cc b/dev/kernel/src/FS/Ext2+FileMgr.cc index 7c28c0c9..c0c9c84e 100644 --- a/dev/kernel/src/FS/Ext2+FileMgr.cc +++ b/dev/kernel/src/FS/Ext2+FileMgr.cc @@ -17,14 +17,14 @@ #include #include -constexpr UInt32 EXT2_DIRECT_BLOCKS = 12; -constexpr UInt32 EXT2_SINGLE_INDIRECT_INDEX = 12; -constexpr UInt32 EXT2_DOUBLE_INDIRECT_INDEX = 13; -constexpr UInt32 EXT2_TRIPLE_INDIRECT_INDEX = 14; -constexpr UInt32 EXT2_ROOT_INODE = 2; -constexpr UInt32 EXT2_SUPERBLOCK_BLOCK = 1; -constexpr UInt32 EXT2_GROUP_DESC_BLOCK_SMALL = 2; -constexpr UInt32 EXT2_GROUP_DESC_BLOCK_LARGE = 1; +constexpr static UInt32 EXT2_DIRECT_BLOCKS = 12; +constexpr static UInt32 EXT2_SINGLE_INDIRECT_INDEX = 12; +constexpr static UInt32 EXT2_DOUBLE_INDIRECT_INDEX = 13; +constexpr ATTRIBUTE(unused) static UInt32 EXT2_TRIPLE_INDIRECT_INDEX = 14; +constexpr static UInt32 EXT2_ROOT_INODE = 2; +constexpr ATTRIBUTE(unused) static UInt32 EXT2_SUPERBLOCK_BLOCK = 1; +constexpr static UInt32 EXT2_GROUP_DESC_BLOCK_SMALL = 2; +constexpr static UInt32 EXT2_GROUP_DESC_BLOCK_LARGE = 1; static inline SizeT ext2_min(SizeT a, SizeT b) { return a < b ? a : b; diff --git a/dev/kernel/src/IndexableProperty.cc b/dev/kernel/src/IndexableProperty.cc index c11e328d..56143607 100644 --- a/dev/kernel/src/IndexableProperty.cc +++ b/dev/kernel/src/IndexableProperty.cc @@ -22,7 +22,7 @@ namespace Indexer { Void IndexableProperty::AddFlag(Int16 flag) { fFlags |= flag; } - Void IndexableProperty::RemoveFlag(Int16 flag) { fFlags &= flag; } + Void IndexableProperty::RemoveFlag(Int16 flag) { fFlags &= ~(flag); } Int16 IndexableProperty::HasFlag(Int16 flag) { return fFlags & flag; } @@ -33,6 +33,7 @@ namespace Indexer { /// @return none, check before if indexer can be claimed (using indexer.HasFlag(kIndexerClaimed)). Void fs_index_file(const Char* filename, SizeT filenameLen, IndexableProperty& indexer) { if (!indexer.HasFlag(kIndexerClaimed)) { + indexer.RemoveFlag(kIndexerUnclaimed); indexer.AddFlag(kIndexerClaimed); rt_copy_memory_safe(reinterpret_cast(const_cast(filename)), (VoidPtr) indexer.Leak().Path, filenameLen, kIndexerCatalogNameLength); diff --git a/dev/libSystem/SystemKit/Verify.h b/dev/libSystem/SystemKit/Verify.h index cbf85830..5ad0dbff 100644 --- a/dev/libSystem/SystemKit/Verify.h +++ b/dev/libSystem/SystemKit/Verify.h @@ -11,7 +11,7 @@ #include -namespace LibSystem::Detail { +namespace LibSystem::Verify { /// @author 0xf00sec, and Amlal El Mahrouss /// @brief safe cast operator. template @@ -34,7 +34,7 @@ struct must_cast_traits { /// @brief Safe constexpr cast. template inline constexpr R* sys_constexpr_cast(T* ptr) { - static_assert(must_cast_traits::value, "constexpr cast failed! types are a mismatch!"); + static_assert(must_cast_traits::value, "constexpr cast failed! types are mismatching!"); return static_cast(ptr); } } // namespace LibSystem::Detail diff --git a/dev/libSystem/src/System.cc b/dev/libSystem/src/System.cc deleted file mode 100644 index da9931fe..00000000 --- a/dev/libSystem/src/System.cc +++ /dev/null @@ -1,193 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#include -#include -#include -#include - -using namespace LibSystem; - -IMPORT_C Void _rtl_assert(Bool expr, const Char* origin) { - if (!expr) { - PrintOut(nullptr, "Assertion failed: %s\r", origin); - libsys_syscall_arg_1(SYSCALL_HASH("_rtl_debug_break")); - } -} - -/// @note this uses the FNV 64-bit variant. -IMPORT_C UInt64 libsys_hash_64(const Char* path) { - if (!path || *path == 0) return 0; - - const UInt64 kFNVSeed = 0xcbf29ce484222325ULL; - const UInt64 kFNVPrime = 0x100000001b3ULL; - - UInt64 hash = kFNVSeed; - - while (*path) { - hash ^= (Char) (*path++); - hash *= kFNVPrime; - } - - return hash; -} - -IMPORT_C Char* StrFmt(const Char* fmt, ...) { - if (!fmt || *fmt == 0) return const_cast("(null)"); - - return const_cast(""); -} - -// memmove-style copy -IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len) { - // handles overlap, prefers 64-bit word copies when aligned - if (!len || !dest || !src) return nullptr; - - auto s = static_cast(src); - auto d = static_cast(dest); - - if (d == s) return dest; - - // decide direction - if (d > s && d < s + len) { - const UInt8* rs = s + len; - UInt8* rd = d + len; - - // try 64-bit aligned backward copy - if (len >= sizeof(UInt64) && (reinterpret_cast(rs) % sizeof(UInt64) == 0) && - (reinterpret_cast(rd) % sizeof(UInt64) == 0)) { - auto rsw = reinterpret_cast(rs); - auto rdw = reinterpret_cast(rd); - - SizeT words = len / sizeof(UInt64); - - for (SizeT i = 0; i < words; ++i) { - rdw[-1 - static_cast(i)] = rsw[-1 - static_cast(i)]; - } - - SizeT rem = len % sizeof(UInt64); - for (SizeT i = 0; i < rem; ++i) { - rd[-1 - i] = rs[-1 - i]; - } - } else { - // byte-wise backward - for (SizeT i = 0; i < len; ++i) { - rd[-1 - i] = rs[-1 - i]; - } - } - } else { - // try 64-bit aligned forward copy - if (len >= sizeof(UInt64) && (reinterpret_cast(s) % sizeof(UInt64) == 0) && - (reinterpret_cast(d) % sizeof(UInt64) == 0)) { - auto sw = reinterpret_cast(s); - auto dw = reinterpret_cast(d); - SizeT words = len / sizeof(UInt64); - - for (SizeT i = 0; i < words; ++i) { - dw[i] = sw[i]; - } - - SizeT rem = len % sizeof(UInt64); - const SizeT offset = words * sizeof(UInt64); - for (SizeT i = 0; i < rem; ++i) { - d[offset + i] = s[offset + i]; - } - } else { - for (SizeT i = 0; i < len; ++i) { - d[i] = s[i]; - } - } - } - - return dest; -} - -IMPORT_C SInt64 MmStrLen(const Char* in) { - // strlen via pointer walk - if (!in) return -kErrorInvalidData; - - const Char* p = in; - while (*p) ++p; - - return static_cast(p - in); -} - -IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) { - if (!len || !dest) return nullptr; - - auto d = static_cast(dest); - - if (len >= sizeof(UInt64) && (reinterpret_cast(d) % sizeof(UInt64)) == 0) { - UInt64 pattern = static_cast(value); - pattern |= (pattern << 8); - pattern |= (pattern << 16); - pattern |= (pattern << 32); - - auto dw = reinterpret_cast(d); - SizeT words = len / sizeof(UInt64); - - for (SizeT i = 0; i < words; ++i) { - dw[i] = pattern; - } - - SizeT rem = len % sizeof(UInt64); - const SizeT offset = words * sizeof(UInt64); - for (SizeT i = 0; i < rem; ++i) { - d[offset + i] = value; - } - } else { - for (SizeT i = 0; i < len; ++i) d[i] = value; - } - - return dest; -} - -IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter) { - return static_cast(libsys_syscall_arg_3( - SYSCALL_HASH("IoOpenFile"), Detail::sys_safe_cast(path), Detail::sys_safe_cast(drv_letter))); -} - -IMPORT_C Void IoCloseFile(_Input Ref desc) { - libsys_syscall_arg_2(SYSCALL_HASH("IoCloseFile"), static_cast(desc)); -} - -IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) { - auto ret_ptr = libsys_syscall_arg_3(SYSCALL_HASH("IoSeekFile"), static_cast(desc), - reinterpret_cast(&off)); - - if (!ret_ptr) return ~0UL; - - auto ret = static_cast(ret_ptr); - UInt64 result = *ret; - MUST_PASS(result != ~0UL); - return result; -} - -IMPORT_C UInt64 IoTellFile(_Input Ref desc) { - auto ret_ptr = libsys_syscall_arg_2(SYSCALL_HASH("IoTellFile"), static_cast(desc)); - if (!ret_ptr) return ~0UL; - auto ret = static_cast(ret_ptr); - return *ret; -} - -IMPORT_C SInt32 PrintOut(_Input IORef desc, const Char* fmt, ...) { - va_list args; - va_start(args, fmt); - - auto buf = StrFmt(fmt, args); - - va_end(args); - - // if truncated, `needed` >= kBufferSz; we still send truncated buffer - auto ret_ptr = libsys_syscall_arg_3(SYSCALL_HASH("PrintOut"), static_cast(desc), - Detail::sys_safe_cast(buf)); - - if (!ret_ptr) return -kErrorInvalidData; - - auto ret = static_cast(ret_ptr); - - return *ret; -} diff --git a/dev/libSystem/src/SystemCalls.cc b/dev/libSystem/src/SystemCalls.cc new file mode 100644 index 00000000..571a99a6 --- /dev/null +++ b/dev/libSystem/src/SystemCalls.cc @@ -0,0 +1,193 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include +#include +#include +#include + +using namespace LibSystem; + +IMPORT_C Void _rtl_assert(Bool expr, const Char* origin) { + if (!expr) { + PrintOut(nullptr, "Assertion failed: %s\r", origin); + libsys_syscall_arg_1(SYSCALL_HASH("_rtl_debug_break")); + } +} + +/// @note this uses the FNV 64-bit variant. +IMPORT_C UInt64 libsys_hash_64(const Char* path) { + if (!path || *path == 0) return 0; + + const UInt64 kFNVSeed = 0xcbf29ce484222325ULL; + const UInt64 kFNVPrime = 0x100000001b3ULL; + + UInt64 hash = kFNVSeed; + + while (*path) { + hash ^= (Char) (*path++); + hash *= kFNVPrime; + } + + return hash; +} + +IMPORT_C Char* StrFmt(const Char* fmt, ...) { + if (!fmt || *fmt == 0) return const_cast("(null)"); + + return const_cast(""); +} + +// memmove-style copy +IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len) { + // handles overlap, prefers 64-bit word copies when aligned + if (!len || !dest || !src) return nullptr; + + auto s = static_cast(src); + auto d = static_cast(dest); + + if (d == s) return dest; + + // decide direction + if (d > s && d < s + len) { + const UInt8* rs = s + len; + UInt8* rd = d + len; + + // try 64-bit aligned backward copy + if (len >= sizeof(UInt64) && (reinterpret_cast(rs) % sizeof(UInt64) == 0) && + (reinterpret_cast(rd) % sizeof(UInt64) == 0)) { + auto rsw = reinterpret_cast(rs); + auto rdw = reinterpret_cast(rd); + + SizeT words = len / sizeof(UInt64); + + for (SizeT i = 0; i < words; ++i) { + rdw[-1 - static_cast(i)] = rsw[-1 - static_cast(i)]; + } + + SizeT rem = len % sizeof(UInt64); + for (SizeT i = 0; i < rem; ++i) { + rd[-1 - i] = rs[-1 - i]; + } + } else { + // byte-wise backward + for (SizeT i = 0; i < len; ++i) { + rd[-1 - i] = rs[-1 - i]; + } + } + } else { + // try 64-bit aligned forward copy + if (len >= sizeof(UInt64) && (reinterpret_cast(s) % sizeof(UInt64) == 0) && + (reinterpret_cast(d) % sizeof(UInt64) == 0)) { + auto sw = reinterpret_cast(s); + auto dw = reinterpret_cast(d); + SizeT words = len / sizeof(UInt64); + + for (SizeT i = 0; i < words; ++i) { + dw[i] = sw[i]; + } + + SizeT rem = len % sizeof(UInt64); + const SizeT offset = words * sizeof(UInt64); + for (SizeT i = 0; i < rem; ++i) { + d[offset + i] = s[offset + i]; + } + } else { + for (SizeT i = 0; i < len; ++i) { + d[i] = s[i]; + } + } + } + + return dest; +} + +IMPORT_C SInt64 MmStrLen(const Char* in) { + // strlen via pointer walk + if (!in) return -kErrorInvalidData; + + const Char* p = in; + while (*p) ++p; + + return static_cast(p - in); +} + +IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value) { + if (!len || !dest) return nullptr; + + auto d = static_cast(dest); + + if (len >= sizeof(UInt64) && (reinterpret_cast(d) % sizeof(UInt64)) == 0) { + UInt64 pattern = static_cast(value); + pattern |= (pattern << 8); + pattern |= (pattern << 16); + pattern |= (pattern << 32); + + auto dw = reinterpret_cast(d); + SizeT words = len / sizeof(UInt64); + + for (SizeT i = 0; i < words; ++i) { + dw[i] = pattern; + } + + SizeT rem = len % sizeof(UInt64); + const SizeT offset = words * sizeof(UInt64); + for (SizeT i = 0; i < rem; ++i) { + d[offset + i] = value; + } + } else { + for (SizeT i = 0; i < len; ++i) d[i] = value; + } + + return dest; +} + +IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter) { + return static_cast(libsys_syscall_arg_3( + SYSCALL_HASH("IoOpenFile"), Verify::sys_safe_cast(path), Verify::sys_safe_cast(drv_letter))); +} + +IMPORT_C Void IoCloseFile(_Input Ref desc) { + libsys_syscall_arg_2(SYSCALL_HASH("IoCloseFile"), static_cast(desc)); +} + +IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) { + auto ret_ptr = libsys_syscall_arg_3(SYSCALL_HASH("IoSeekFile"), static_cast(desc), + reinterpret_cast(&off)); + + if (!ret_ptr) return ~0UL; + + auto ret = static_cast(ret_ptr); + UInt64 result = *ret; + MUST_PASS(result != ~0UL); + return result; +} + +IMPORT_C UInt64 IoTellFile(_Input Ref desc) { + auto ret_ptr = libsys_syscall_arg_2(SYSCALL_HASH("IoTellFile"), static_cast(desc)); + if (!ret_ptr) return ~0UL; + auto ret = static_cast(ret_ptr); + return *ret; +} + +IMPORT_C SInt32 PrintOut(_Input IORef desc, const Char* fmt, ...) { + va_list args; + va_start(args, fmt); + + auto buf = StrFmt(fmt, args); + + va_end(args); + + // if truncated, `needed` >= kBufferSz; we still send truncated buffer + auto ret_ptr = libsys_syscall_arg_3(SYSCALL_HASH("PrintOut"), static_cast(desc), + Verify::sys_safe_cast(buf)); + + if (!ret_ptr) return -kErrorInvalidData; + + auto ret = static_cast(ret_ptr); + + return *ret; +} -- cgit v1.2.3