refactor syscall ABI and FAT32 driver to use request structs and string views natively, enforce handle access rights on object lookup, add scheduler statistics tracking, fix KiSchedule to prevent 50% idle throttling on CPU-bound threads, better document x64 initial user stack alignment, fix process teardown to properly signal exit events and manage thread counts, optimize doom file I/O with a custom handle struct to internally track position and size, add SubsystemData to TEB for generic thread-local state, apply PAGE_NX to non-executable PE sections in the loader, update native apps, tests, and shells to match new APIs, update kernel32 directory retrieval.

This commit is contained in:
maksw committed 2026-05-26 14:46:49 +02:00
1 parent 603fbdfab1
commit e1248f4afe
43 files changed
+2892 -1857

No files matched your search

+82 -25
View File
@@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <intrin.h>
#include "sys_string.h"
#define SYS_STATUS_SUCCESS 0u
#define SYS_STATUS_TIMEOUT 3u
@@ -25,19 +26,17 @@
#define SYS_ENV_VERSION 1u
#define SYS_CWD_MAX 520u
#ifndef SYS_USER_ENV_TYPES_DEFINED
#define SYS_USER_ENV_TYPES_DEFINED 1
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_STRING_VIEW ImagePath;
SYS_STRING_VIEW CommandLine;
SYS_STRING_VIEW CurrentDirectory;
} SYS_PEB;
typedef struct SYS_TEB {
@@ -47,8 +46,15 @@ typedef struct SYS_TEB {
struct SYS_TEB* Self;
uint32_t ThreadId;
uint32_t Magic;
// Native per-thread extension pointer. The kernel leaves this NULL;
// user-mode personalities/libraries may hang their own thread-local
// state here without baking subsystem semantics into SYS_TEB.
void* SubsystemData;
} SYS_TEB;
#endif // SYS_USER_ENV_TYPES_DEFINED
static inline SYS_PEB* SysGetPeb(void) {
return (SYS_PEB*)(uintptr_t)__readgsqword(0);
}
@@ -124,12 +130,64 @@ typedef struct SYS_TRAP_FRAME_INFO {
uint64_t R11;
} SYS_TRAP_FRAME_INFO;
// ---------------------------------------------------------------------------
// String-view based syscall request structures
// ---------------------------------------------------------------------------
typedef struct SYS_CREATE_PROCESS_REQUEST {
SYS_STRING_VIEW Path;
SYS_STRING_VIEW CommandLine;
uint32_t* HandleOut;
} SYS_CREATE_PROCESS_REQUEST;
typedef struct SYS_PATH_HANDLE_REQUEST {
SYS_STRING_VIEW Path;
uint32_t* HandleOut;
} SYS_PATH_HANDLE_REQUEST;
typedef struct SYS_PATH_REQUEST {
SYS_STRING_VIEW Path;
} SYS_PATH_REQUEST;
typedef struct SYS_RENAME_FILE_REQUEST {
SYS_STRING_VIEW OldPath;
SYS_STRING_VIEW NewPath;
} SYS_RENAME_FILE_REQUEST;
typedef struct SYS_GET_CWD_REQUEST {
char* Buffer;
size_t Capacity;
size_t* LengthOut;
} SYS_GET_CWD_REQUEST;
typedef struct SYS_WRITE_CONSOLE_REQUEST {
SYS_STRING_VIEW Text;
} SYS_WRITE_CONSOLE_REQUEST;
typedef struct SYS_DEBUG_PRINT_REQUEST {
SYS_STRING_VIEW Text;
} SYS_DEBUG_PRINT_REQUEST;
typedef struct SYS_PUBLISH_QUEUE_REQUEST {
SYS_STRING_VIEW Name;
uint32_t QueueHandle;
} SYS_PUBLISH_QUEUE_REQUEST;
typedef struct SYS_OPEN_NAMED_QUEUE_REQUEST {
SYS_STRING_VIEW Name;
uint32_t* HandleOut;
} SYS_OPEN_NAMED_QUEUE_REQUEST;
typedef struct SYS_NAMED_QUEUE_OWNER_REQUEST {
SYS_STRING_VIEW Name;
uint32_t* PidOut;
} SYS_NAMED_QUEUE_OWNER_REQUEST;
// ---------------------------------------------------------------------------
// 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 SysCreateProcess(const SYS_CREATE_PROCESS_REQUEST* request);
uint32_t SysWaitForProcess(uint32_t handle);
uint32_t SysGetProcessExitCode(uint32_t handle, int* exitCodeOut);
uint32_t SysKillProcess(uint32_t pid, int exitCode);
@@ -178,16 +236,16 @@ uint32_t SysEventWait(uint32_t handle, uint32_t alertable);
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 SysPublishQueue(const SYS_PUBLISH_QUEUE_REQUEST* request);
uint32_t SysOpenNamedQueue(const SYS_OPEN_NAMED_QUEUE_REQUEST* request);
uint32_t SysQueueDuplicate(uint32_t handle, uint32_t targetPid, uint32_t* handleOut);
uint32_t SysNamedQueueOwnerPid(const char* name, uint32_t* pidOut);
uint32_t SysNamedQueueOwnerPid(const SYS_NAMED_QUEUE_OWNER_REQUEST* request);
// ---------------------------------------------------------------------------
// 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 SysOpenFile(const SYS_PATH_HANDLE_REQUEST* request);
uint32_t SysCreateFile(const SYS_PATH_HANDLE_REQUEST* request);
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);
@@ -197,20 +255,20 @@ 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 SysDeleteFile(const SYS_PATH_REQUEST* request);
uint32_t SysRenameFile(const SYS_RENAME_FILE_REQUEST* request);
uint32_t SysOpenDir(const char* path, uint32_t* handleOut);
uint32_t SysOpenDir(const SYS_PATH_HANDLE_REQUEST* request);
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);
uint32_t SysCreateDir(const SYS_PATH_REQUEST* request);
uint32_t SysRemoveDir(const SYS_PATH_REQUEST* request);
uint32_t SysSetCwd(const SYS_PATH_REQUEST* request);
uint32_t SysGetCwd(const SYS_GET_CWD_REQUEST* request);
// ---------------------------------------------------------------------------
// Console and graphics (57-61)
// ---------------------------------------------------------------------------
uint32_t SysWriteConsole(const char* buf, size_t len);
uint32_t SysWriteConsole(const SYS_WRITE_CONSOLE_REQUEST* request);
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);
@@ -286,8 +344,7 @@ typedef struct SYS_SOCKET_RECV_FROM_REQUEST {
typedef struct SYS_RESOLVE_HOST_REQUEST {
const char* Name;
size_t NameLength;
SYS_STRING_VIEW Name;
uint32_t Family;
uint32_t Flags;
SYS_SOCKET_ADDRESS_IPV4* AddressOut;
@@ -333,7 +390,7 @@ typedef struct SYS_MEMORY_STATUS {
// Time and diagnostics
// ---------------------------------------------------------------------------
uint64_t SysGetTick(void);
uint32_t SysDebugPrint(const char* str);
uint32_t SysDebugPrint(const SYS_DEBUG_PRINT_REQUEST* request);
uint32_t SysQueryTrapFrame(SYS_TRAP_FRAME_INFO* info);
uint32_t SysGetMemoryStatus(SYS_MEMORY_STATUS* statusOut);