Skip to content

Userspace

kazah-png edited this page Jul 11, 2026 · 4 revisions

Userspace

Ring-3 ELF64 loading with full address-space isolation, preemptive multitasking, and a growing suite of coreutils and TUI applications.

See also: Syscalls, Process Management, Security, Architecture

ELF loader (elf.c)

Validates ELF64 magic (4-byte header, program headers), parses PT_LOAD segments, maps code/data at p_vaddr (W^X per segment flags), allocates user stack at 0xD0000000 (a single demand-zero page). elf_load_image splits loading from process creation — used by both execve() and spawn_user_path().

Process lifecycle

  • fork() — COW clone (see Process Management)
  • execve() — replace image in-place: argv/argc passed on the SysV entry stack (crt0 reads [rsp] = argc, rsp+8 = argv)
  • exit() — zombie + wake parent; reaped by the parent's waitpid() or by reap_zombies() for orphans
  • Signalskill()/signal()/sigreturn() with SIG_DFL (terminate, status 128+signo), SIG_IGN, or user handlers entered via a sigreturn trampoline

Process isolation

  • Private PML4 per process (no identity mapping in user tables)
  • NX bit on user stack and data pages
  • SMEP (optional): kernel can't execute user pages
  • Kernel entry at PML4[511] cloned from master kernel PML4

File I/O

  • Per-process fd table (opaque small integers → VFS handles/pipe ends)
  • Per-process CWD (inherited on fork, kept across execve)
  • Fd-based I/O routes to ramdisk, EXT2 mounts (/mnt), and /dev special files (null, zero, random)
  • /proc pseudo-filesystem: version, meminfo, uptime, cpuinfo + per-pid status/cmdline

Inter-process communication

  • Pipes: anonymous, kernel-ring-buffered, reference-counted ends, blocking read
  • dup2(): fd redirection (wires pipes or VFS handles to fd 0/1/2 — the shell pipeline primitive)

libc (user/libc.c)

Minimal C library wrapping every Syscalls:

  • printf, sprintf, snprintf (with width/flag parsing but no left-justify)
  • malloc/free over sbrk() (lazy, demand-paged)
  • strlen, strcmp, strncpy, memset, memcpy, atoi
  • open, read, write, close, fsize, lseek (via per-fd offset)
  • fork, waitpid, execve, pipe, dup2, getdents, getprocs
  • kill, signal, ttymode, mmap, munmap, mprotect, chdir, getcwd, mkdir, unlink, readkey

Nyx C runtime (user/nyxrt.h, user/nyxrt.c)

Nyx C is a Go/Zig-inspired typed subset of C that transpiles to C and links against the nyxrt runtime. The runtime provides:

  • Types: nyx_str, nyx_slice, nyx_result, fixed-width ints
  • Syscall ABI: __nyx_syscall6 for all ring-3 syscalls
  • String interpolation: __nyx_fmt_begin/_str/_i64 for "{var}" syntax

crt0 (user/crt0.asm)

Assembly entry that extracts argc/argv from the SysV stack layout, calls main(argc, argv), then exit() with main's return value.

Available programs (24 ELFs)

Binary Description
hello.elf Minimal "Hello World" in assembly
init.elf Syscall regression test (fork, execve, signals, pipe, mmap, mprotect, /dev, /proc, readkey)
sh.elf Userspace shell (v0.7): line editor, history, tab completion, pipelines, redirection, job control, cd/pwd/export/$VAR
spin.elf Spin loop for multitasking / scheduling testing
fdleak.elf Tests fd cleanup on process exit
hello_nyx.elf First Nyx C program — write + getpid + string interpolation
echo.elf Print argv to stdout (pipeline-friendly)
upper.elf Stdin → uppercase → stdout filter (pipeline test)
args.elf Print argv and exit with argc (execve argv test)
cat.elf Concatenate files or stdin to stdout
wc.elf Line/word/byte counts of stdin or files
ls.elf Directory listing with getdents()
ps.elf Process table snapshot (PID, PPID, state, CPU time, command)
kill.elf Send a signal by pid (kill [-SIG] pid...)
top.elf Live process monitor — auto-refresh with readkey timeout, press 'q' to quit
edit.elf Full-screen text editor — insert/split/join, arrows/Home/End/PgUp/PgDn, Ctrl-O save, Ctrl-X exit, status bar
grep.elf Print stdin/file lines containing a literal pattern
head.elf Print first N lines of stdin/file (default 10)
tail.elf Print last N lines via circular buffer (default 10)
sort.elf Insertion-sort lines from stdin/file
find.elf Recursive directory walk with optional name-substring filter
mkdir.elf Create a directory
rm.elf Remove a file
touch.elf Create an empty file (open O_CREAT)

TUI features

  • top and edit use readkey(timeout) for timed input or blocking reads
  • ANSI escape sequences (CSI) are rendered by the GUI terminal: ESC[2J (clear), ESC[H/f (home), ESC[r;cH (cursor position), ESC[K (clear to EOL)
  • Terminal enters "screen mode" on first CSI sequence — lines[] becomes a fixed grid with a block cursor, reset when the command exits
  • Ctrl-A..Z map to control bytes 0x01..0x1A (Ctrl-C still → SIGINT)
  • Foreground TUI applications render live in the GUI window (compositor repaints at ~16 fps during exec)

Debug / test programs

Binary Description
cowtest.elf COW page-fork test
mtdemo.elf Multitasking demo (kernel threads)

Clone this wiki locally