238 lines
8.9 KiB
C
238 lines
8.9 KiB
C
#pragma once
|
|
// Syscall.h
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#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
|
|
// ---------------------------------------------------------------------------
|
|
// GS base points at SYS_TEB. GS:[0] is the process PEB pointer.
|
|
// This is this OS' ABI, not a Windows compatibility layout.
|
|
#define SYS_PEB_MAGIC 0x30424550u /* "PEB0" */
|
|
#define SYS_TEB_MAGIC 0x30424554u /* "TEB0" */
|
|
#define SYS_ENV_VERSION 1u
|
|
#define SYS_CWD_MAX 520u
|
|
|
|
typedef struct SYS_PEB {
|
|
uint32_t Magic;
|
|
uint32_t Version;
|
|
uint32_t ProcessId;
|
|
|
|
const char* ImagePath;
|
|
size_t ImagePathLength;
|
|
|
|
const char* CommandLine;
|
|
size_t CommandLineLength;
|
|
|
|
const char* CurrentDirectory;
|
|
size_t CurrentDirectoryLength;
|
|
} SYS_PEB;
|
|
|
|
typedef struct SYS_TEB {
|
|
SYS_PEB* Peb;
|
|
void* StackBase;
|
|
void* StackLimit;
|
|
struct SYS_TEB* Self;
|
|
uint32_t ThreadId;
|
|
uint32_t Magic;
|
|
} SYS_TEB;
|
|
|
|
static inline SYS_PEB* SysGetPeb(void) {
|
|
return (SYS_PEB*)(uintptr_t)__readgsqword(0);
|
|
}
|
|
|
|
static inline SYS_TEB* SysGetTeb(void) {
|
|
return (SYS_TEB*)(uintptr_t)__readgsqword(offsetof(SYS_TEB, Self));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 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);
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Trap-frame query ABI
|
|
// ---------------------------------------------------------------------------
|
|
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;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Process & thread lifecycle (0-14)
|
|
// ---------------------------------------------------------------------------
|
|
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);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Memory and handles
|
|
// ---------------------------------------------------------------------------
|
|
void* SysAlloc(size_t size);
|
|
uint32_t SysFree(void* ptr);
|
|
void* SysRealloc(void* ptr, size_t newSize);
|
|
uint32_t SysCloseHandle(uint32_t handle);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Synchronization primitives (19-31)
|
|
// ---------------------------------------------------------------------------
|
|
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);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Queues and named queues (32-38)
|
|
// ---------------------------------------------------------------------------
|
|
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);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Files, directories, and current directory (39-56)
|
|
// ---------------------------------------------------------------------------
|
|
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);
|
|
|
|
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);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Console and graphics (57-61)
|
|
// ---------------------------------------------------------------------------
|
|
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);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Input (62-69)
|
|
// ---------------------------------------------------------------------------
|
|
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);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Time and debug (70-72)
|
|
// ---------------------------------------------------------------------------
|
|
uint64_t SysGetTick(void);
|
|
uint32_t SysDebugPrint(const char* str);
|
|
uint32_t SysQueryTrapFrame(SYS_TRAP_FRAME_INFO* info);
|
|
|
|
uint64_t SysCatchAllDebugBucket(uint64_t a0, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5);
|