add page cache with read-ahead and dirty writeback, file-backed and anonymous mmap with CoW, VAD-based virtual memory replacing the old user region model, PFN database with eviction and reclaim, file identity for cross-open section sharing, FAT32 path cache and per-file cluster chain cache, serial.sys for COM1 keyboard input, xHCI MSI with INTx and polled fallback, PciEnableMsiAuto, waitable input queue, KeWaitForAnyEvent, per-process thread info syscall, expanded kernel stats (PFN/PTE/VAD/file-cache/disk), pool allocation tags, async I/O user buffer locking, file flush on close, add docs.md.

This commit is contained in:
maksw committed 2026-06-20 13:17:39 +02:00
1 parent 61ec2442eb
commit 2c4a68a75f
114 files changed
+18069 -2620

No files matched your search

+209 -95
View File
@@ -1,110 +1,231 @@
// User application entry points for video.
// Native NTLite Bink video player.
//
// This application intentionally uses the native syscall ABI directly. Do not
// include <windows.h> here; Win32 compatibility belongs in the Win32 personality
// layer, not in native apps.
#include <windows.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <intrin.h>
#include <ntlite/syscall.h>
#include "bink.h"
// Implements the command Line Second Token operation.
static const char* commandLineSecondToken(const char* cmdline, char* out, size_t outSize) {
if (!out || outSize == 0) return "video.bk2";
strcpy(out, "video.bk2");
if (!cmdline) return out;
#define VIDEO_DEFAULT_PATH "video.bk2"
while (*cmdline == ' ' || *cmdline == '\t') cmdline++;
if (*cmdline == '\0') return out;
static void print_span(const char* s, size_t len) {
if (!s || !len) return;
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromParts(s, len) };
SysWriteConsole(&req);
}
if (*cmdline == '"') {
cmdline++;
while (*cmdline && *cmdline != '"') cmdline++;
if (*cmdline == '"') cmdline++;
static void print(const char* s) {
if (!s) return;
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromCstr(s) };
SysWriteConsole(&req);
}
static void print_u64(uint64_t v) {
char buf[32];
int i = 0;
if (v == 0) {
print("0");
return;
}
while (v && i < (int)sizeof(buf)) {
buf[i++] = (char)('0' + (v % 10));
v /= 10;
}
while (i) {
char c = buf[--i];
print_span(&c, 1);
}
}
static void fail_status(const char* what, uint32_t status) {
print("video: ");
print(what);
print(" failed status=");
print_u64(status);
print("\n");
for (;;) SysExitProcess(1);
}
static void exit_now(int code) {
for (;;) SysExitProcess(code);
}
static SYS_STRING_VIEW command_line_view(void) {
SYS_PEB* peb = SysGetPeb();
if (!peb || peb->Magic != SYS_PEB_MAGIC)
return SYS_SV_NULL;
return peb->CommandLine;
}
static int sv_is_space(char c) {
return c == ' ' || c == '\t';
}
static SYS_STRING_VIEW skip_one_token(SYS_STRING_VIEW s) {
size_t i = 0;
while (i < s.Length && sv_is_space(s.Data[i]))
i++;
if (i < s.Length && s.Data[i] == '"') {
i++;
while (i < s.Length && s.Data[i] != '"')
i++;
if (i < s.Length && s.Data[i] == '"')
i++;
} else {
while (*cmdline && *cmdline != ' ' && *cmdline != '\t') cmdline++;
while (i < s.Length && !sv_is_space(s.Data[i]))
i++;
}
while (*cmdline == ' ' || *cmdline == '\t') cmdline++;
if (*cmdline == '\0') return out;
while (i < s.Length && sv_is_space(s.Data[i]))
i++;
size_t pos = 0;
if (*cmdline == '"') {
cmdline++;
while (*cmdline && *cmdline != '"' && pos + 1 < outSize)
out[pos++] = *cmdline++;
return SysSvFromParts(s.Data + i, s.Length - i);
}
static SYS_STRING_VIEW command_line_second_token(SYS_STRING_VIEW cmdline) {
SYS_STRING_VIEW rest = skip_one_token(cmdline);
if (!rest.Data || rest.Length == 0)
return SYS_SV_LITERAL(VIDEO_DEFAULT_PATH);
size_t begin = 0;
size_t end = 0;
if (rest.Data[0] == '"') {
begin = 1;
end = begin;
while (end < rest.Length && rest.Data[end] != '"')
end++;
} else {
while (*cmdline && *cmdline != ' ' && *cmdline != '\t' && pos + 1 < outSize)
out[pos++] = *cmdline++;
begin = 0;
end = 0;
while (end < rest.Length && !sv_is_space(rest.Data[end]))
end++;
}
out[pos] = '\0';
if (end <= begin)
return SYS_SV_LITERAL(VIDEO_DEFAULT_PATH);
return SysSvFromParts(rest.Data + begin, end - begin);
}
static const char* path_to_cstr(SYS_STRING_VIEW path, char* out, size_t outSize) {
if (!out || outSize == 0)
return VIDEO_DEFAULT_PATH;
if (!path.Data || path.Length == 0)
path = SYS_SV_LITERAL(VIDEO_DEFAULT_PATH);
size_t n = path.Length;
if (n + 1 > outSize)
n = outSize - 1;
memcpy(out, path.Data, n);
out[n] = '\0';
return out;
}
// Runs the requested operation.
static uint32_t open_file_view(SYS_STRING_VIEW path, uint32_t* handleOut) {
SYS_PATH_HANDLE_REQUEST req = { path, handleOut };
return SysOpenFile(&req);
}
int main(void) {
const char* cmdline = GetCommandLineA();
char videoPath[260];
const char* path = commandLineSecondToken(cmdline, videoPath, sizeof(videoPath));
SYS_STRING_VIEW cmdline = command_line_view();
SYS_STRING_VIEW pathView = command_line_second_token(cmdline);
void* h = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleA(h, "video: starting\n", 16, 0, 0);
WriteConsoleA(h, cmdline, (unsigned int)strlen(cmdline), 0, 0);
WriteConsoleA(h, "\n", 1, 0, 0);
void* f = CreateFileA(path, 0, 0, 0, 3, 0, 0);
if (!f || f == (void*)(unsigned long long)0xFFFFFFFF) { // fuckass
WriteConsoleA(h, "video: failed to open input file\n", 33, 0, 0);
ExitProcess(1);
print("video: starting\n");
if (cmdline.Data && cmdline.Length) {
print_span(cmdline.Data, cmdline.Length);
print("\n");
}
unsigned long s = GetFileSize(f, 0);
if (!s || s == 0xFFFFFFFF) {
WriteConsoleA(h, "video: GetFileSize failed!\n", 27, 0, 0);
ExitProcess(1);
}
void* buf = HeapAlloc(0, 0, s);
if (!buf) {
WriteConsoleA(h, "video: HeapAlloc failed\n", 24, 0, 0);
ExitProcess(1);
uint32_t file = 0;
uint32_t status = open_file_view(pathView, &file);
if (status != SYS_STATUS_SUCCESS) {
char pathBuf[260];
print("video: failed to open input file: ");
print(path_to_cstr(pathView, pathBuf, sizeof(pathBuf)));
print("\n");
fail_status("SysOpenFile", status);
}
int r = ReadFile(f, buf, s, 0, 0);
if (!r) {
WriteConsoleA(h, "video: ReadFile failed\n", 23, 0, 0);
ExitProcess(1);
uint64_t fileSize = SysGetFileSize(file);
if (fileSize == 0) {
SysCloseHandle(file);
print("video: input file is empty\n");
exit_now(1);
}
WriteConsoleA(h, "video: input file loaded\n", 25, 0, 0);
CloseHandle(h);
HBINK b = BinkOpen(buf, BINKFROMMEMORY);
uint32_t mapping = 0;
status = SysCreateFileMapping(file, SYS_VM_READ, SYS_MAP_FLAG_NONE, fileSize, &mapping);
if (status != SYS_STATUS_SUCCESS || !mapping) {
SysCloseHandle(file);
fail_status("SysCreateFileMapping", status);
}
void* buf = 0;
status = SysMapFile(mapping, 0, (size_t)fileSize,
SYS_MAP_ACCESS_READ, SYS_MAP_FLAG_NONE, &buf);
if (status != SYS_STATUS_SUCCESS || !buf) {
SysCloseHandle(mapping);
SysCloseHandle(file);
fail_status("SysMapFile", status);
}
// The mapped VAD owns the file reference now; keep only the mapped pointer
// alive while Bink is open. This avoids a second whole-file heap allocation
// and lets the VM fault file pages in on demand.
SysCloseHandle(mapping);
SysCloseHandle(file);
print("video: input file mapped\n");
HBINK b = BinkOpen((const char*)buf, BINKFROMMEMORY);
if (!b) {
WriteConsoleA(h, "video: BinkOpen failed: ", 24, 0, 0);
print("video: BinkOpen failed: ");
const char* err = BinkGetError();
WriteConsoleA(h, err, (unsigned int)strlen(err), 0, 0);
WriteConsoleA(h, "\n", 1, 0, 0);
ExitProcess(1);
print(err ? err : "unknown");
print("\n");
SysFreeVm(buf);
exit_now(1);
}
uint32_t width = b->Width;
uint32_t width = b->Width;
uint32_t height = b->Height;
uint32_t* fb = HeapAlloc(0, 0, width * height * sizeof(uint32_t));
if (!fb) {
WriteConsoleA(h, "video: framebuffer alloc failed\n", 32, 0, 0);
ExitProcess(1);
if (!width || !height) {
print("video: invalid video dimensions\n");
BinkClose(b);
SysFreeVm(buf);
exit_now(1);
}
// Prepare the Win32 BITMAPINFO structure.
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -(LONG)height; // Negative means top-down drawing!
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
uint32_t surface = 0;
status = SysCreateSurface(width, height, SYS_SURFACE_FORMAT_XRGB8888,
SYS_SURFACE_FLAG_NONE, &surface);
if (status != SYS_STATUS_SUCCESS || !surface) {
BinkClose(b);
SysFreeVm(buf);
fail_status("SysCreateSurface", status);
}
void* surfaceBase = 0;
uint32_t pitchBytes = 0;
status = SysMapSurface(surface, &surfaceBase, &pitchBytes);
if (status != SYS_STATUS_SUCCESS || !surfaceBase || pitchBytes < width * sizeof(uint32_t)) {
SysCloseHandle(surface);
BinkClose(b);
SysFreeVm(buf);
fail_status("SysMapSurface", status);
}
print("video: native surface ready\n");
DISPLAY_RECT fullFrame = { 0, 0, (int32_t)width, (int32_t)height };
while (1) {
if (BinkWait(b)) {
@@ -120,33 +241,26 @@ int main(void) {
continue;
}
BinkCopyToBuffer(b, fb, width * 4, height, 0, 0,
BINKSURFACE32);
BinkCopyToBuffer(b, surfaceBase, pitchBytes, height, 0, 0, BINKSURFACE32);
// Welcome to the GDI pipeline!
PAINTSTRUCT ps;
// Using NULL/0 as the HWND since we don't have a real window handle yet.
HDC hdc = BeginPaint((HWND)0, &ps);
StretchDIBits(
hdc,
0, 0, width, height, // Destination rect
0, 0, width, height, // Source rect
fb,
&bmi,
DIB_RGB_COLORS,
SRCCOPY
);
EndPaint((HWND)0, &ps);
status = SysPresentSurface(surface, &fullFrame, 1);
if (status != SYS_STATUS_SUCCESS) {
SysCloseHandle(surface);
BinkClose(b);
SysFreeVm(buf);
fail_status("SysPresentSurface", status);
}
if (b->FrameNum == b->Frames)
break;
BinkNextFrame(b);
}
WriteConsoleA(h, "video: goodbye\n", 15, 0, 0);
ExitProcess(0);
SysCloseHandle(surface);
BinkClose(b);
SysFreeVm(buf);
print("video: goodbye\n");
exit_now(0);
}