implement proper IRQL, DPCs, kernel and user APCs, alertable waits, async file I/O with cancellation, trap frames, selftests, a real driver/device model, native async FAT32, VFAT dotfile fixes, syscall ABI cleanup, async_apc and async_cancel tests, POSIX multi-volume paths, and pimp the readme.

This commit is contained in:
maksw committed 2026-05-19 20:56:15 +02:00
1 parent f5fd7bdd75
commit 529f816166
75 files changed
+9969 -5675

No files matched your search

+157 -91
View File
@@ -6,6 +6,12 @@
#include <stdbool.h>
#include <intrin.h>
#define SYS_STATUS_SUCCESS 0u
#define SYS_STATUS_PENDING 4u
#define SYS_STATUS_ALERTED 5u
#define SYS_STATUS_KERNEL_APC 6u
#define SYS_STATUS_USER_APC 7u
#define SYS_STATUS_CANCELLED 0x33u
// ---------------------------------------------------------------------------
// User process/thread environment
@@ -15,7 +21,7 @@
#define SYS_PEB_MAGIC 0x30424550u /* "PEB0" */
#define SYS_TEB_MAGIC 0x30424554u /* "TEB0" */
#define SYS_ENV_VERSION 1u
#define SYS_CWD_MAX 512u
#define SYS_CWD_MAX 520u
typedef struct SYS_PEB {
uint32_t Magic;
@@ -49,121 +55,181 @@ static inline SYS_TEB* SysGetTeb(void) {
return (SYS_TEB*)(uintptr_t)__readgsqword(offsetof(SYS_TEB, Self));
}
static inline const char* SysGetCommandLine(void) {
SYS_PEB* peb = SysGetPeb();
if (!peb || peb->Magic != SYS_PEB_MAGIC) return "";
return peb->CommandLine ? peb->CommandLine : "";
}
// ---------------------------------------------------------------------------
// APC and async I/O ABI
// ---------------------------------------------------------------------------
typedef struct SYS_IO_STATUS_BLOCK {
volatile uint32_t Status;
volatile uint64_t Information;
} SYS_IO_STATUS_BLOCK;
typedef void (*SYS_USER_APC_ROUTINE)(void* normalContext,
SYS_IO_STATUS_BLOCK* iosb,
uint64_t information);
typedef struct SYS_ASYNC_FILE_REQUEST {
uint32_t FileHandle;
uint32_t EventHandle; // optional; 0 means no event signal
uint32_t Flags; // reserved, must be 0 for now
uint32_t Reserved;
uint64_t Offset; // explicit byte offset; does not touch file pointer
void* Buffer;
size_t Size;
SYS_IO_STATUS_BLOCK* Iosb; // required; initialized to SYS_STATUS_PENDING
SYS_USER_APC_ROUTINE ApcRoutine; // optional; requires SysSetUserApcDispatcher
void* ApcContext;
uint32_t* RequestHandleOut; // optional; receives cancel/query handle
} SYS_ASYNC_FILE_REQUEST;
typedef struct SYS_USER_APC_FRAME {
uint64_t Magic;
uint64_t OriginalRip;
uint64_t OriginalRsp;
uint64_t OriginalRflags;
uint64_t OriginalRax;
uint64_t Routine;
uint64_t NormalContext;
uint64_t SystemArgument1;
uint64_t SystemArgument2;
} SYS_USER_APC_FRAME;
#define SYS_USER_APC_FRAME_MAGIC 0x3143504152555355ULL
void SysUserApcDispatcher(void);
// ---------------------------------------------------------------------------
// Thread & Process Management (0-7)
// Trap-frame query ABI
// ---------------------------------------------------------------------------
void SysExitProcess(int exitCode); // 00
void SysExitThread(int exitCode); // 01
uint32_t SysCreateThread(uintptr_t entry, uintptr_t arg, uint32_t priority, uint32_t* tidOut); // 02
uint32_t SysGetCurrentTid(void); // 03
uint32_t SysGetCurrentPid(void); // 04
uint32_t SysYield(void); // 05
uint32_t SysSleep(uint64_t ms); // 06
uint32_t SysSetThreadPriority(uint32_t priority); // 07
typedef struct SYS_TRAP_FRAME_INFO {
uint32_t Size;
uint32_t Kind;
uint32_t Vector;
uint32_t PreviousMode;
uint64_t Rip;
uint64_t Cs;
uint64_t Rflags;
uint64_t Rsp;
uint64_t Ss;
uint64_t Rax;
uint64_t Rcx;
uint64_t Rdx;
uint64_t R8;
uint64_t R9;
uint64_t R10;
uint64_t R11;
} SYS_TRAP_FRAME_INFO;
// ---------------------------------------------------------------------------
// Memory Management (8-10)
// Process & thread lifecycle (0-14)
// ---------------------------------------------------------------------------
void* SysAlloc(size_t size); // 08
uint32_t SysFree(void* ptr, size_t size); // 09
void* SysRealloc(void* ptr, size_t newSize); // 10
void SysExitProcess(int exitCode);
void SysExitThread(int exitCode);
uint32_t SysCreateProcess(const char* path, uint32_t* handleOut, const char* cmdline);
uint32_t SysWaitForProcess(uint32_t handle);
uint32_t SysGetProcessExitCode(uint32_t handle, int* exitCodeOut);
uint32_t SysKillProcess(uint32_t pid, int exitCode);
uint32_t SysGetProcessList(uint32_t* pidsOut, size_t maxCount, size_t* countOut);
uint32_t SysGetProcessPid(uint32_t handle, uint32_t* pidOut);
uint32_t SysCreateThread(uintptr_t entry, uintptr_t arg, uint32_t priority, uint32_t* tidOut);
uint32_t SysWaitThread(uint32_t tid);
uint32_t SysGetCurrentTid(void);
uint32_t SysGetCurrentPid(void);
uint32_t SysSetThreadPriority(uint32_t priority);
uint32_t SysYield(void);
uint32_t SysSleep(uint64_t ms);
uint32_t SysSetUserApcDispatcher(void* dispatcher);
uint32_t SysApcReturn(const SYS_USER_APC_FRAME* frame);
// ---------------------------------------------------------------------------
// Synchronization Primitives (11-22)
// Memory and handles
// ---------------------------------------------------------------------------
uint32_t SysMutexCreate(uint32_t* handleOut); // 11
uint32_t SysMutexAcquire(uint32_t handle); // 12
uint32_t SysMutexTryAcquire(uint32_t handle); // 13
uint32_t SysMutexRelease(uint32_t handle); // 14
uint32_t SysSemCreate(int32_t initial, int32_t max, uint32_t* handleOut); // 15
uint32_t SysSemWait(uint32_t handle); // 16
uint32_t SysSemTryWait(uint32_t handle); // 17
uint32_t SysSemSignal(uint32_t handle); // 18
uint32_t SysEventCreate(uint32_t type, uint32_t initialState, uint32_t* handleOut); // 19
uint32_t SysEventSignal(uint32_t handle); // 20
uint32_t SysEventClear(uint32_t handle); // 21
uint32_t SysEventWait(uint32_t handle); // 22
void* SysAlloc(size_t size);
uint32_t SysFree(void* ptr);
void* SysRealloc(void* ptr, size_t newSize);
uint32_t SysCloseHandle(uint32_t handle);
// ---------------------------------------------------------------------------
// Anonymous Queues & Unified Handle Path (23-26)
// Synchronization primitives (19-31)
// ---------------------------------------------------------------------------
uint32_t SysQueueCreate(uint32_t* handleOut); // 23
uint32_t SysQueueSend(uint32_t handle, const void* msg, bool blocking); // 24
uint32_t SysQueueRecv(uint32_t handle, void* msg, bool blocking); // 25
uint32_t SysCloseHandle(uint32_t handle); // 26
uint32_t SysMutexCreate(uint32_t* handleOut);
uint32_t SysMutexAcquire(uint32_t handle);
uint32_t SysMutexTryAcquire(uint32_t handle);
uint32_t SysMutexRelease(uint32_t handle);
uint32_t SysSemCreate(int32_t initial, int32_t max, uint32_t* handleOut);
uint32_t SysSemWait(uint32_t handle);
uint32_t SysSemTryWait(uint32_t handle);
uint32_t SysSemSignal(uint32_t handle);
uint32_t SysEventCreate(uint32_t type, uint32_t initialState, uint32_t* handleOut);
uint32_t SysEventSignal(uint32_t handle);
uint32_t SysEventClear(uint32_t handle);
uint32_t SysEventWait(uint32_t handle, uint32_t alertable);
// ---------------------------------------------------------------------------
// File System (27-35)
// Queues and named queues (32-38)
// ---------------------------------------------------------------------------
uint32_t SysOpenFile(const char* path, uint32_t* handleOut); // 27
uint32_t SysReadFile(uint32_t handle, void* buf, size_t size, size_t* bytesRead); // 28
uint32_t SysWriteFile(uint32_t handle, const void* buf, size_t size, size_t* written); // 29
uint32_t SysCreateFile(const char* path, uint32_t* handleOut); // 30
uint32_t SysDeleteFile(const char* path); // 31
uint32_t SysTruncateFile(uint32_t handle, uint64_t newSize); // 32
uint64_t SysGetFilePos(uint32_t handle); // 33
uint32_t SysSeekFile(uint32_t handle, uint64_t offset); // 34
uint64_t SysGetFileSize(uint32_t handle); // 35
uint32_t SysQueueCreate(uint32_t* handleOut);
uint32_t SysQueueSend(uint32_t handle, const void* msg, bool blocking);
uint32_t SysQueueRecv(uint32_t handle, void* msg, bool blocking);
uint32_t SysPublishQueue(const char* name, uint32_t handle);
uint32_t SysOpenNamedQueue(const char* name, uint32_t* handleOut);
uint32_t SysQueueDuplicate(uint32_t handle, uint32_t targetPid, uint32_t* handleOut);
uint32_t SysNamedQueueOwnerPid(const char* name, uint32_t* pidOut);
// ---------------------------------------------------------------------------
// Graphics & Input (36-47)
// Files, directories, and current directory (39-56)
// ---------------------------------------------------------------------------
uint32_t SysScreenBlit(int x, int y, int w, int h, const uint32_t* pixels); // 36
uint32_t SysScreenClear(uint32_t color); // 37
uint32_t SysScreenGetSize(int* widthOut, int* heightOut); // 38
uint32_t SysScreenPutChar(char c, int x, int y, uint32_t fg, uint32_t bg); // 39
uint32_t SysOpenFile(const char* path, uint32_t* handleOut);
uint32_t SysCreateFile(const char* path, uint32_t* handleOut);
uint32_t SysReadFile(uint32_t handle, void* buf, size_t size, size_t* bytesRead);
uint32_t SysWriteFile(uint32_t handle, const void* buf, size_t size, size_t* written);
uint32_t SysReadFileAsync(const SYS_ASYNC_FILE_REQUEST* request);
uint32_t SysWriteFileAsync(const SYS_ASYNC_FILE_REQUEST* request);
uint32_t SysCancelIo(uint32_t asyncRequestHandle);
uint32_t SysSeekFile(uint32_t handle, uint64_t offset);
uint64_t SysGetFilePos(uint32_t handle);
uint64_t SysGetFileSize(uint32_t handle);
uint32_t SysTruncateFile(uint32_t handle, uint64_t newSize);
uint32_t SysDeleteFile(const char* path);
uint32_t SysRenameFile(const char* oldPath, const char* newPath);
uint64_t SysKbPoll(void); // 40
uint64_t SysKbIsKeyDown(uint8_t scancode, bool extended); // 41
char SysKbReadChar(void); // 42
uint8_t SysKbReadScancode(void); // 43
uint32_t SysKbGetModifiers(void); // 44
uint64_t SysMousePoll(void); // 45
uint64_t SysMouseRead(int* dx, int* dy, uint8_t* buttons); // 46
void SysMouseGetPos(int* x, int* y); // 47
uint32_t SysOpenDir(const char* path, uint32_t* handleOut);
uint32_t SysReadDir(uint32_t handle, void* dirEntryBuffer);
uint32_t SysCreateDir(const char* path);
uint32_t SysRemoveDir(const char* path);
uint32_t SysSetCwd(const char* path);
uint32_t SysGetCwd(char* buf, size_t size);
// ---------------------------------------------------------------------------
// System Info & Process Management (48-65)
// Console and graphics (57-61)
// ---------------------------------------------------------------------------
uint64_t SysGetTick(void); // 48
uint32_t SysDebugPrint(const char* str); // 49
uint32_t SysCreateProcess(const char* path, uint32_t* handleOut, const char* cmdline); // 50
uint32_t SysWaitForProcess(uint32_t handle); // 51
uint32_t SysGetProcessExitCode(uint32_t handle, int* exitCodeOut); // 52
uint32_t SysWriteConsole(const char* buf, size_t len); // 53
uint32_t SysWriteConsole(const char* buf, size_t len);
uint32_t SysScreenBlit(int x, int y, int w, int h, const uint32_t* pixels);
uint32_t SysScreenClear(uint32_t color);
uint32_t SysScreenGetSize(int* widthOut, int* heightOut);
uint32_t SysScreenPutChar(char c, int x, int y, uint32_t fg, uint32_t bg);
// ---------------------------------------------------------------------------
// Directories & Environment (54-57)
// Input (62-69)
// ---------------------------------------------------------------------------
uint32_t SysOpenDir(const char* path, uint32_t* handleOut); // 54
uint32_t SysReadDir(uint32_t handle, void* dirEntryBuffer); // 55
uint32_t SysSetCwd(const char* path); // 56
uint32_t SysGetCwd(char* buf, size_t size); // 57
uint64_t SysKbPoll(void);
uint64_t SysKbIsKeyDown(uint8_t scancode, bool extended);
char SysKbReadChar(void);
uint8_t SysKbReadScancode(void);
uint32_t SysKbGetModifiers(void);
uint64_t SysMousePoll(void);
uint64_t SysMouseRead(int* dx, int* dy, uint8_t* buttons);
void SysMouseGetPos(int* x, int* y);
// ---------------------------------------------------------------------------
// Process/Thread Helpers & Named Queues (58-65)
// Time and debug (70-72)
// ---------------------------------------------------------------------------
uint32_t SysWaitThread(uint32_t tid); // 58
uint32_t SysGetProcessPid(uint32_t handle, uint32_t* pidOut); // 59
uint32_t SysPublishQueue(const char* name, uint32_t handle); // 60
uint32_t SysOpenNamedQueue(const char* name, uint32_t* handleOut); // 61
uint32_t SysQueueDuplicate(uint32_t handle, uint32_t targetPid, uint32_t* handleOut); // 62
uint32_t SysNamedQueueOwnerPid(const char* name, uint32_t* pidOut); // 63
uint32_t SysKillProcess(uint32_t pid, int exitCode); // 64
uint32_t SysGetProcessList(uint32_t* pidsOut, size_t maxCount, size_t* countOut); // 65
// ---------------------------------------------------------------------------
// Filesystem mutations (66-68)
// ---------------------------------------------------------------------------
uint32_t SysRenameFile(const char* oldPath, const char* newPath); // 66
uint32_t SysCreateDir(const char* path); // 67
uint32_t SysRemoveDir(const char* path); // 68
uint64_t SysGetTick(void);
uint32_t SysDebugPrint(const char* str);
uint32_t SysQueryTrapFrame(SYS_TRAP_FRAME_INFO* info);
+77 -71
View File
@@ -1,73 +1,79 @@
; syscall_nr.inc - auto-synced with syscall_nr.h and ke_syscall.c
; syscall_nr.inc
SYS_EXIT_PROCESS EQU 0
SYS_EXIT_THREAD EQU 1
SYS_CREATE_THREAD EQU 2
SYS_GET_CURRENT_TID EQU 3
SYS_GET_CURRENT_PID EQU 4
SYS_YIELD EQU 5
SYS_SLEEP EQU 6
SYS_SET_THREAD_PRIORITY EQU 7
SYS_ALLOC EQU 8
SYS_FREE EQU 9
SYS_REALLOC EQU 10
SYS_MUTEX_CREATE EQU 11
SYS_MUTEX_ACQUIRE EQU 12
SYS_MUTEX_TRY_ACQUIRE EQU 13
SYS_MUTEX_RELEASE EQU 14
SYS_SEM_CREATE EQU 15
SYS_SEM_WAIT EQU 16
SYS_SEM_TRY_WAIT EQU 17
SYS_SEM_SIGNAL EQU 18
SYS_EVENT_CREATE EQU 19
SYS_EVENT_SIGNAL EQU 20
SYS_EVENT_CLEAR EQU 21
SYS_EVENT_WAIT EQU 22
SYS_QUEUE_CREATE EQU 23
SYS_QUEUE_SEND EQU 24
SYS_QUEUE_RECV EQU 25
SYS_CLOSE_HANDLE EQU 26
SYS_OPEN_FILE EQU 27
SYS_READ_FILE EQU 28
SYS_WRITE_FILE EQU 29
SYS_CREATE_FILE EQU 30
SYS_DELETE_FILE EQU 31
SYS_TRUNCATE_FILE EQU 32
SYS_GET_FILE_POS EQU 33
SYS_SEEK_FILE EQU 34
SYS_GET_FILE_SIZE EQU 35
SYS_SCREEN_BLIT EQU 36
SYS_SCREEN_CLEAR EQU 37
SYS_SCREEN_GET_SIZE EQU 38
SYS_SCREEN_PUT_CHAR EQU 39
SYS_KB_POLL EQU 40
SYS_KB_IS_KEY_DOWN EQU 41
SYS_KB_READ_CHAR EQU 42
SYS_KB_READ_SCANCODE EQU 43
SYS_KB_GET_MODIFIERS EQU 44
SYS_MOUSE_POLL EQU 45
SYS_MOUSE_READ EQU 46
SYS_MOUSE_GET_POS EQU 47
SYS_GET_TICK EQU 48
SYS_DEBUG_PRINT EQU 49
SYS_CREATE_PROCESS EQU 50
SYS_WAIT_FOR_PROCESS EQU 51
SYS_GET_PROCESS_EXIT_CODE EQU 52
SYS_WRITE_CONSOLE EQU 53
SYS_OPEN_DIR EQU 54
SYS_READ_DIR EQU 55
SYS_SET_CWD EQU 56
SYS_GET_CWD EQU 57
SYS_WAIT_THREAD EQU 58
SYS_GET_PROCESS_PID EQU 59
SYS_PUBLISH_QUEUE EQU 60
SYS_OPEN_NAMED_QUEUE EQU 61
SYS_QUEUE_DUPLICATE EQU 62
SYS_NAMED_QUEUE_OWNER_PID EQU 63
SYS_KILL_PROCESS EQU 64
SYS_GET_PROCESS_LIST EQU 65
SYS_RENAME_FILE EQU 66
SYS_CREATE_DIR EQU 67
SYS_REMOVE_DIR EQU 68
SYS_EXIT_PROCESS EQU 0
SYS_EXIT_THREAD EQU 1
SYS_CREATE_PROCESS EQU 2
SYS_WAIT_FOR_PROCESS EQU 3
SYS_GET_PROCESS_EXIT_CODE EQU 4
SYS_KILL_PROCESS EQU 5
SYS_GET_PROCESS_LIST EQU 6
SYS_GET_PROCESS_PID EQU 7
SYS_CREATE_THREAD EQU 8
SYS_WAIT_THREAD EQU 9
SYS_GET_CURRENT_TID EQU 10
SYS_GET_CURRENT_PID EQU 11
SYS_SET_THREAD_PRIORITY EQU 12
SYS_YIELD EQU 13
SYS_SLEEP EQU 14
SYS_SET_USER_APC_DISPATCHER EQU 15
SYS_APC_RETURN EQU 16
SYS_ALLOC EQU 17
SYS_FREE EQU 18
SYS_REALLOC EQU 19
SYS_CLOSE_HANDLE EQU 20
SYS_MUTEX_CREATE EQU 21
SYS_MUTEX_ACQUIRE EQU 22
SYS_MUTEX_TRY_ACQUIRE EQU 23
SYS_MUTEX_RELEASE EQU 24
SYS_SEM_CREATE EQU 25
SYS_SEM_WAIT EQU 26
SYS_SEM_TRY_WAIT EQU 27
SYS_SEM_SIGNAL EQU 28
SYS_EVENT_CREATE EQU 29
SYS_EVENT_SIGNAL EQU 30
SYS_EVENT_CLEAR EQU 31
SYS_EVENT_WAIT EQU 32
SYS_QUEUE_CREATE EQU 33
SYS_QUEUE_SEND EQU 34
SYS_QUEUE_RECV EQU 35
SYS_PUBLISH_QUEUE EQU 36
SYS_OPEN_NAMED_QUEUE EQU 37
SYS_QUEUE_DUPLICATE EQU 38
SYS_NAMED_QUEUE_OWNER_PID EQU 39
SYS_OPEN_FILE EQU 40
SYS_CREATE_FILE EQU 41
SYS_READ_FILE EQU 42
SYS_WRITE_FILE EQU 43
SYS_READ_FILE_ASYNC EQU 44
SYS_WRITE_FILE_ASYNC EQU 45
SYS_CANCEL_IO EQU 46
SYS_SEEK_FILE EQU 47
SYS_GET_FILE_POS EQU 48
SYS_GET_FILE_SIZE EQU 49
SYS_TRUNCATE_FILE EQU 50
SYS_DELETE_FILE EQU 51
SYS_RENAME_FILE EQU 52
SYS_OPEN_DIR EQU 53
SYS_READ_DIR EQU 54
SYS_CREATE_DIR EQU 55
SYS_REMOVE_DIR EQU 56
SYS_SET_CWD EQU 57
SYS_GET_CWD EQU 58
SYS_WRITE_CONSOLE EQU 59
SYS_SCREEN_BLIT EQU 60
SYS_SCREEN_CLEAR EQU 61
SYS_SCREEN_GET_SIZE EQU 62
SYS_SCREEN_PUT_CHAR EQU 63
SYS_KB_POLL EQU 64
SYS_KB_IS_KEY_DOWN EQU 65
SYS_KB_READ_CHAR EQU 66
SYS_KB_READ_SCANCODE EQU 67
SYS_KB_GET_MODIFIERS EQU 68
SYS_MOUSE_POLL EQU 69
SYS_MOUSE_READ EQU 70
SYS_MOUSE_GET_POS EQU 71
SYS_GET_TICK EQU 72
SYS_DEBUG_PRINT EQU 73
SYS_QUERY_TRAP_FRAME EQU 74
SYS_MAX EQU 69
SYS_MAX EQU 75
+124 -139
View File
@@ -22,6 +22,16 @@
INCLUDE syscall_nr.inc
SYS_USER_APC_FRAME_Magic EQU 0
SYS_USER_APC_FRAME_OriginalRip EQU 8
SYS_USER_APC_FRAME_OriginalRsp EQU 16
SYS_USER_APC_FRAME_OriginalRflags EQU 24
SYS_USER_APC_FRAME_OriginalRax EQU 32
SYS_USER_APC_FRAME_Routine EQU 40
SYS_USER_APC_FRAME_NormalContext EQU 48
SYS_USER_APC_FRAME_SystemArgument1 EQU 56
SYS_USER_APC_FRAME_SystemArgument2 EQU 64
.CODE
; ---------------------------------------------------------------------------
@@ -47,169 +57,144 @@ Sys&name ENDP
ENDM
; ---------------------------------------------------------------------------
; Process & Thread
; Process & thread lifecycle
; ---------------------------------------------------------------------------
SYSCALL_STUB2 ExitProcess, SYS_EXIT_PROCESS ; (int exitCode) -> void
SYSCALL_STUB2 ExitThread, SYS_EXIT_THREAD ; (int exitCode) -> void
SYSCALL_STUB2 CreateThread, SYS_CREATE_THREAD ; (uint64_t entry, uint64_t arg, uint32_t priority, uint32_t* tidOut) -> KSTATUS
SYSCALL_STUB2 GetCurrentTid, SYS_GET_CURRENT_TID ; () -> uint32_t
SYSCALL_STUB2 GetCurrentPid, SYS_GET_CURRENT_PID ; () -> uint32_t
SYSCALL_STUB2 Yield, SYS_YIELD ; () -> void
SYSCALL_STUB2 Sleep, SYS_SLEEP ; (uint32_t ms) -> void
SYSCALL_STUB2 SetThreadPriority, SYS_SET_THREAD_PRIORITY ; (uint32_t priority) -> KSTATUS
SYSCALL_STUB2 ExitProcess, SYS_EXIT_PROCESS
SYSCALL_STUB2 ExitThread, SYS_EXIT_THREAD
SYSCALL_STUB2 CreateProcess, SYS_CREATE_PROCESS
SYSCALL_STUB2 WaitForProcess, SYS_WAIT_FOR_PROCESS
SYSCALL_STUB2 GetProcessExitCode, SYS_GET_PROCESS_EXIT_CODE
SYSCALL_STUB2 KillProcess, SYS_KILL_PROCESS
SYSCALL_STUB2 GetProcessList, SYS_GET_PROCESS_LIST
SYSCALL_STUB2 GetProcessPid, SYS_GET_PROCESS_PID
SYSCALL_STUB2 CreateThread, SYS_CREATE_THREAD
SYSCALL_STUB2 WaitThread, SYS_WAIT_THREAD
SYSCALL_STUB2 GetCurrentTid, SYS_GET_CURRENT_TID
SYSCALL_STUB2 GetCurrentPid, SYS_GET_CURRENT_PID
SYSCALL_STUB2 SetThreadPriority, SYS_SET_THREAD_PRIORITY
SYSCALL_STUB2 Yield, SYS_YIELD
SYSCALL_STUB2 Sleep, SYS_SLEEP
SYSCALL_STUB2 SetUserApcDispatcher, SYS_SET_USER_APC_DISPATCHER
SYSCALL_STUB2 ApcReturn, SYS_APC_RETURN
; ---------------------------------------------------------------------------
; Memory
; User APC dispatcher
; ---------------------------------------------------------------------------
SYSCALL_STUB2 Alloc, SYS_ALLOC ; (size_t size) -> void* (NULL on failure)
SYSCALL_STUB2 Free, SYS_FREE ; (void* ptr, size_t size) -> void
SYSCALL_STUB2 Realloc, SYS_REALLOC ; (void* ptr, size_t newSize) -> void*
PUBLIC SysUserApcDispatcher
SysUserApcDispatcher PROC EXPORT
; Kernel enters here with RSP pointing at SYS_USER_APC_FRAME. There is no
; return address because this is an IRET redirection. Preserve RBX because
; the original user context resumes after SysApcReturn.
push rbx
lea rbx, [rsp + 8] ; original injected RSP = frame pointer
sub rsp, 32 ; shadow space; RSP is already call-aligned
mov rcx, [rbx + SYS_USER_APC_FRAME_NormalContext]
mov rdx, [rbx + SYS_USER_APC_FRAME_SystemArgument1]
mov r8, [rbx + SYS_USER_APC_FRAME_SystemArgument2]
mov rax, [rbx + SYS_USER_APC_FRAME_Routine]
call rax
add rsp, 32
mov rcx, rbx ; SysApcReturn(frame)
mov rbx, [rsp] ; restore original user RBX
add rsp, 8
mov rax, SYS_APC_RETURN
int 40h
SysUserApcDispatcher_Failed:
jmp SysUserApcDispatcher_Failed
SysUserApcDispatcher ENDP
; ---------------------------------------------------------------------------
; Mutex
; Memory and handles
; ---------------------------------------------------------------------------
SYSCALL_STUB2 MutexCreate, SYS_MUTEX_CREATE ; (uint32_t* handleOut) -> KSTATUS
SYSCALL_STUB2 MutexAcquire, SYS_MUTEX_ACQUIRE ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 MutexTryAcquire, SYS_MUTEX_TRY_ACQUIRE ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 MutexRelease, SYS_MUTEX_RELEASE ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 Alloc, SYS_ALLOC
SYSCALL_STUB2 Free, SYS_FREE
SYSCALL_STUB2 Realloc, SYS_REALLOC
SYSCALL_STUB2 CloseHandle, SYS_CLOSE_HANDLE
; ---------------------------------------------------------------------------
; Semaphore
; Synchronization objects
; ---------------------------------------------------------------------------
SYSCALL_STUB2 SemCreate, SYS_SEM_CREATE ; (int32_t initial, int32_t max, uint32_t* handleOut) -> KSTATUS
SYSCALL_STUB2 SemWait, SYS_SEM_WAIT ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 SemTryWait, SYS_SEM_TRY_WAIT ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 SemSignal, SYS_SEM_SIGNAL ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 MutexCreate, SYS_MUTEX_CREATE
SYSCALL_STUB2 MutexAcquire, SYS_MUTEX_ACQUIRE
SYSCALL_STUB2 MutexTryAcquire, SYS_MUTEX_TRY_ACQUIRE
SYSCALL_STUB2 MutexRelease, SYS_MUTEX_RELEASE
SYSCALL_STUB2 SemCreate, SYS_SEM_CREATE
SYSCALL_STUB2 SemWait, SYS_SEM_WAIT
SYSCALL_STUB2 SemTryWait, SYS_SEM_TRY_WAIT
SYSCALL_STUB2 SemSignal, SYS_SEM_SIGNAL
SYSCALL_STUB2 EventCreate, SYS_EVENT_CREATE
SYSCALL_STUB2 EventSignal, SYS_EVENT_SIGNAL
SYSCALL_STUB2 EventClear, SYS_EVENT_CLEAR
SYSCALL_STUB2 EventWait, SYS_EVENT_WAIT
; ---------------------------------------------------------------------------
; Event
; Queues and named queues
; ---------------------------------------------------------------------------
SYSCALL_STUB2 EventCreate, SYS_EVENT_CREATE ; (uint32_t* handleOut) -> KSTATUS
SYSCALL_STUB2 EventSignal, SYS_EVENT_SIGNAL ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 EventClear, SYS_EVENT_CLEAR ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 EventWait, SYS_EVENT_WAIT ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 QueueCreate, SYS_QUEUE_CREATE
SYSCALL_STUB2 QueueSend, SYS_QUEUE_SEND
SYSCALL_STUB2 QueueRecv, SYS_QUEUE_RECV
SYSCALL_STUB2 PublishQueue, SYS_PUBLISH_QUEUE
SYSCALL_STUB2 OpenNamedQueue, SYS_OPEN_NAMED_QUEUE
SYSCALL_STUB2 QueueDuplicate, SYS_QUEUE_DUPLICATE
SYSCALL_STUB2 NamedQueueOwnerPid, SYS_NAMED_QUEUE_OWNER_PID
; ---------------------------------------------------------------------------
; Queue
; Files, directories, and current directory
; ---------------------------------------------------------------------------
SYSCALL_STUB2 QueueCreate, SYS_QUEUE_CREATE ; (uint32_t* handleOut) -> KSTATUS
SYSCALL_STUB2 QueueSend, SYS_QUEUE_SEND ; (uint32_t handle, void* msg, int blocking) -> KSTATUS
SYSCALL_STUB2 QueueRecv, SYS_QUEUE_RECV ; (uint32_t handle, void* msg, int blocking) -> KSTATUS
SYSCALL_STUB2 OpenFile, SYS_OPEN_FILE
SYSCALL_STUB2 CreateFile, SYS_CREATE_FILE
SYSCALL_STUB2 ReadFile, SYS_READ_FILE
SYSCALL_STUB2 WriteFile, SYS_WRITE_FILE
SYSCALL_STUB2 ReadFileAsync, SYS_READ_FILE_ASYNC
SYSCALL_STUB2 WriteFileAsync, SYS_WRITE_FILE_ASYNC
SYSCALL_STUB2 CancelIo, SYS_CANCEL_IO
SYSCALL_STUB2 SeekFile, SYS_SEEK_FILE
SYSCALL_STUB2 GetFilePos, SYS_GET_FILE_POS
SYSCALL_STUB2 GetFileSize, SYS_GET_FILE_SIZE
SYSCALL_STUB2 TruncateFile, SYS_TRUNCATE_FILE
SYSCALL_STUB2 DeleteFile, SYS_DELETE_FILE
SYSCALL_STUB2 RenameFile, SYS_RENAME_FILE
SYSCALL_STUB2 OpenDir, SYS_OPEN_DIR
SYSCALL_STUB2 ReadDir, SYS_READ_DIR
SYSCALL_STUB2 CreateDir, SYS_CREATE_DIR
SYSCALL_STUB2 RemoveDir, SYS_REMOVE_DIR
SYSCALL_STUB2 SetCwd, SYS_SET_CWD
SYSCALL_STUB2 GetCwd, SYS_GET_CWD
; ---------------------------------------------------------------------------
; Handle (unified - use for files, mutexes, semaphores, events, queues)
; Console and graphics
; ---------------------------------------------------------------------------
SYSCALL_STUB2 CloseHandle, SYS_CLOSE_HANDLE ; (uint32_t handle) -> KSTATUS
SYSCALL_STUB2 WriteConsole, SYS_WRITE_CONSOLE
SYSCALL_STUB6 ScreenBlit, SYS_SCREEN_BLIT
SYSCALL_STUB2 ScreenClear, SYS_SCREEN_CLEAR
SYSCALL_STUB2 ScreenGetSize, SYS_SCREEN_GET_SIZE
SYSCALL_STUB6 ScreenPutChar, SYS_SCREEN_PUT_CHAR
; ---------------------------------------------------------------------------
; File I/O
; (To close: SysCloseHandle(handle) - no dedicated SysCloseFile)
; Input
; ---------------------------------------------------------------------------
SYSCALL_STUB2 OpenFile, SYS_OPEN_FILE ; (const char* path, uint32_t* handleOut) -> KSTATUS
SYSCALL_STUB2 ReadFile, SYS_READ_FILE ; (uint32_t handle, void* buf, size_t size, size_t* bytesReadOut) -> KSTATUS
SYSCALL_STUB2 WriteFile, SYS_WRITE_FILE ; (uint32_t handle, const void* buf, size_t size, size_t* bytesWrittenOut) -> KSTATUS
SYSCALL_STUB2 CreateFile, SYS_CREATE_FILE ; (const char* path, uint32_t* handleOut) -> KSTATUS
SYSCALL_STUB2 DeleteFile, SYS_DELETE_FILE ; (const char* path) -> KSTATUS
SYSCALL_STUB2 TruncateFile, SYS_TRUNCATE_FILE ; (uint32_t handle, uint64_t newSize) -> KSTATUS
SYSCALL_STUB2 GetFilePos, SYS_GET_FILE_POS ; (uint32_t handle) -> uint64_t
SYSCALL_STUB2 SeekFile, SYS_SEEK_FILE ; (uint32_t handle, uint64_t offset) -> KSTATUS
SYSCALL_STUB2 GetFileSize, SYS_GET_FILE_SIZE ; (uint32_t handle) -> uint64_t
SYSCALL_STUB2 KbPoll, SYS_KB_POLL
SYSCALL_STUB2 KbIsKeyDown, SYS_KB_IS_KEY_DOWN
SYSCALL_STUB2 KbReadChar, SYS_KB_READ_CHAR
SYSCALL_STUB2 KbReadScancode, SYS_KB_READ_SCANCODE
SYSCALL_STUB2 KbGetModifiers, SYS_KB_GET_MODIFIERS
SYSCALL_STUB2 MousePoll, SYS_MOUSE_POLL
SYSCALL_STUB2 MouseRead, SYS_MOUSE_READ
SYSCALL_STUB2 MouseGetPos, SYS_MOUSE_GET_POS
; ---------------------------------------------------------------------------
; Screen
; SysScreenBlit has 5 args (x, y, w, h, pixels) - needs SYSCALL_STUB6.
; SysScreenPutChar has 5 args (c, x, y, fg, bg) - needs SYSCALL_STUB6.
; Time and debug
; ---------------------------------------------------------------------------
SYSCALL_STUB6 ScreenBlit, SYS_SCREEN_BLIT ; (int x, int y, int w, int h, const uint32_t* pixels) -> void
SYSCALL_STUB2 ScreenClear, SYS_SCREEN_CLEAR ; (uint32_t color) -> void
SYSCALL_STUB2 ScreenGetSize, SYS_SCREEN_GET_SIZE ; (int* widthOut, int* heightOut) -> void
SYSCALL_STUB6 ScreenPutChar, SYS_SCREEN_PUT_CHAR ; (char c, int x, int y, uint32_t fg, uint32_t bg) -> void
; ---------------------------------------------------------------------------
; Keyboard
; ---------------------------------------------------------------------------
SYSCALL_STUB2 KbPoll, SYS_KB_POLL ; () -> uint32_t (1 if key available, 0 otherwise)
SYSCALL_STUB2 KbIsKeyDown, SYS_KB_IS_KEY_DOWN ; (int scancode) -> uint32_t (1 if down, 0 otherwise)
SYSCALL_STUB2 KbReadChar, SYS_KB_READ_CHAR ; () -> char (blocks, yields while waiting)
SYSCALL_STUB2 KbReadScancode, SYS_KB_READ_SCANCODE ; () -> uint8_t (blocks, yields while waiting)
SYSCALL_STUB2 KbGetModifiers, SYS_KB_GET_MODIFIERS ; () -> uint32_t (current modifier bitmask)
; ---------------------------------------------------------------------------
; Mouse
; ---------------------------------------------------------------------------
SYSCALL_STUB2 MousePoll, SYS_MOUSE_POLL ; () -> uint32_t (1 if packet waiting, 0 otherwise)
SYSCALL_STUB2 MouseRead, SYS_MOUSE_READ ; (int* dx, int* dy, uint8_t* buttons) -> uint32_t (1=ok, 0=empty)
SYSCALL_STUB2 MouseGetPos, SYS_MOUSE_GET_POS ; (int* x, int* y) -> void
; ---------------------------------------------------------------------------
; Timer
; ---------------------------------------------------------------------------
SYSCALL_STUB2 GetTick, SYS_GET_TICK ; () -> uint64_t (milliseconds since boot)
; ---------------------------------------------------------------------------
; Debug
; ---------------------------------------------------------------------------
SYSCALL_STUB2 DebugPrint, SYS_DEBUG_PRINT ; (const char* str) -> void
; ---------------------------------------------------------------------------
; Process launch & lifecycle
; ---------------------------------------------------------------------------
SYSCALL_STUB2 CreateProcess, SYS_CREATE_PROCESS ; (const char* path, const char* args, uint32_t* pidOut) -> KSTATUS
SYSCALL_STUB2 WaitForProcess, SYS_WAIT_FOR_PROCESS ; (uint32_t pid) -> KSTATUS
SYSCALL_STUB2 GetProcessExitCode, SYS_GET_PROCESS_EXIT_CODE ; (uint32_t pid, int* exitCodeOut) -> KSTATUS
; ---------------------------------------------------------------------------
; Console output (white text, for user programs)
; ---------------------------------------------------------------------------
SYSCALL_STUB2 WriteConsole, SYS_WRITE_CONSOLE ; (const char* str, size_t len) -> void
; ---------------------------------------------------------------------------
; Directory enumeration
; ---------------------------------------------------------------------------
SYSCALL_STUB2 OpenDir, SYS_OPEN_DIR ; (const char* path, uint32_t* handleOut) -> KSTATUS
SYSCALL_STUB2 ReadDir, SYS_READ_DIR ; (uint32_t handle, DirEntry* entryOut) -> KSTATUS
; ---------------------------------------------------------------------------
; Current Working Directory
; ---------------------------------------------------------------------------
SYSCALL_STUB2 SetCwd, SYS_SET_CWD ; (const char* path) -> KSTATUS
SYSCALL_STUB2 GetCwd, SYS_GET_CWD ; (char* buf, size_t buflen) -> KSTATUS
; ---------------------------------------------------------------------------
; Thread join (used by pthread_join in posix.dll)
; ---------------------------------------------------------------------------
SYSCALL_STUB2 WaitThread, SYS_WAIT_THREAD ; (uint32_t tid) -> KSTATUS
; ---------------------------------------------------------------------------
; Process PID query (used by posix_spawn -> waitpid in posix.dll)
; ---------------------------------------------------------------------------
SYSCALL_STUB2 GetProcessPid, SYS_GET_PROCESS_PID ; (uint32_t pid) -> KSTATUS
; ---------------------------------------------------------------------------
; Named Queue Registry
; SysPublishQueue - register a queue under a name (call once at startup)
; SysOpenNamedQueue - resolve a name to a handle (retry until SUCCESS)
; ---------------------------------------------------------------------------
SYSCALL_STUB2 PublishQueue, SYS_PUBLISH_QUEUE ; (const char* name, uint32_t queueHandle) -> KSTATUS
SYSCALL_STUB2 OpenNamedQueue, SYS_OPEN_NAMED_QUEUE ; (const char* name, uint32_t* handleOut) -> KSTATUS
; ---------------------------------------------------------------------------
; Handle duplication
; ---------------------------------------------------------------------------
SYSCALL_STUB2 QueueDuplicate, SYS_QUEUE_DUPLICATE ; (uint32_t handle, uint32_t targetPid, uint32_t* handleOut) -> KSTATUS
SYSCALL_STUB2 NamedQueueOwnerPid, SYS_NAMED_QUEUE_OWNER_PID ; (const char* name, uint32_t* pidOut) -> KSTATUS
; ---------------------------------------------------------------------------
; Process utilities
; ---------------------------------------------------------------------------
SYSCALL_STUB2 KillProcess, SYS_KILL_PROCESS ; (uint32_t pid, int exitCode) -> KSTATUS
SYSCALL_STUB2 GetProcessList, SYS_GET_PROCESS_LIST ; (uint32_t* pidsOut, size_t maxCount, size_t* countOut) -> KSTATUS
; ---------------------------------------------------------------------------
; Filesystem mutations
; ---------------------------------------------------------------------------
SYSCALL_STUB2 RenameFile, SYS_RENAME_FILE ; (const char* oldPath, const char* newPath) -> KSTATUS
SYSCALL_STUB2 CreateDir, SYS_CREATE_DIR ; (const char* path) -> KSTATUS
SYSCALL_STUB2 RemoveDir, SYS_REMOVE_DIR ; (const char* path) -> KSTATUS
SYSCALL_STUB2 GetTick, SYS_GET_TICK
SYSCALL_STUB2 DebugPrint, SYS_DEBUG_PRINT
SYSCALL_STUB2 QueryTrapFrame, SYS_QUERY_TRAP_FRAME
END