hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/boot/sched.zig
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-10-17 12:24:17 +0200
committerhachem <im@hachem.wtf>2026-10-17 12:24:17 +0200
commit20a921db8bd8d5e99c0e9dcc72f22ac00a5e5222 (patch)
tree3454816f3642fe468f806aac495c04e55993b102 /boot/sched.zig
parent6a5543bb8f1f535ca49737bcd7be0cafca3b2a08 (diff)
feat: fork/exec & wait
Diffstat (limited to 'boot/sched.zig')
-rw-r--r--boot/sched.zig215
1 files changed, 198 insertions, 17 deletions
diff --git a/boot/sched.zig b/boot/sched.zig
index e267edd..6b79ba8 100644
--- a/boot/sched.zig
+++ b/boot/sched.zig
@@ -15,7 +15,17 @@ const MAX_PATH = 128;
// user heap lives here in the gap between the program and the arg/stack
const HEAP_BASE: u64 = 0x1000_0000;
-const State = enum { free, ready, dead, blocked };
+const State = enum {
+ free, // slot unused
+ ready, // runable
+ dead, // exited orphan
+ blocked, // parked in read
+ zombie, // exited and kept
+ waiting, // parked in wait
+};
+
+// where the arg string is mapped in a freshly loaded program
+const ARG_VADDR: u64 = 0x5000_0000;
// file descriptor not "foid decimator"
pub const FdKind = enum { closed, keyboard, console, file };
@@ -33,6 +43,8 @@ const Task = struct {
state: State = .free,
brk_cur: u64 = 0, // the program break, top of the user heap
brk_top: u64 = 0, // first page above the break thats actually mapped
+ parent: usize = 0, // pid of the task that forked this one, 0 = kernel spawned
+ exit_code: i32 = 0, // left behind for a waiting parent to collect
};
var tasks: [MAX_TASKS]Task = @splat(.{});
@@ -132,6 +144,148 @@ pub fn sbrk(increment: i64) i64 {
return @intCast(old);
}
+pub fn getpid() u64 {
+ return current;
+}
+
+pub fn fork(child_frame: Frame) i64 {
+ if (current == 0) return -1; // the kernel task cant fork
+
+ var slot: usize = 1;
+ while (slot < MAX_TASKS and tasks[slot].state != .free) slot += 1;
+ if (slot == MAX_TASKS) return -1;
+
+ const child_cr3 = vmm.forkAddressSpace(tasks[current].cr3) orelse return -1;
+
+ tasks[slot] = .{
+ .frame = child_frame,
+ .cr3 = child_cr3,
+ .kstack_top = @intFromPtr(&kstacks[slot]) + KSTACK_SIZE,
+ .state = .ready,
+ .brk_cur = tasks[current].brk_cur,
+ .brk_top = tasks[current].brk_top,
+ .parent = current,
+ };
+ fdtabs[slot] = fdtabs[current]; // the child inherits the open fds
+ return @intCast(slot);
+}
+
+pub const ExecImage = struct {
+ cr3: u64,
+ entry: u64,
+ stack: u64,
+ arg_ptr: u64,
+ arg_len: u64,
+};
+
+var exec_scratch: u64 = 0; // phys base of a staging buffer for the elf image
+const EXEC_SCRATCH_PAGES = 64;
+
+fn readInt(comptime T: type, addr: u64) T {
+ return @as(*align(1) const T, @ptrFromInt(addr)).*;
+}
+
+pub fn loadExec(path: []const u8, arg: []const u8) ?ExecImage {
+ const size = bridge.fsSize(path);
+ if (size < 0) return null;
+ const bytes: usize = @intCast(size);
+ if (bytes > EXEC_SCRATCH_PAGES * pmm.PAGE_SIZE) return null;
+
+ if (exec_scratch == 0) exec_scratch = pmm.allocContig(EXEC_SCRATCH_PAGES) orelse return null;
+ const base = pmm.physToVirt(exec_scratch);
+ const staging: [*]u8 = @ptrFromInt(base);
+ _ = bridge.fsReadInto(path, 0, staging[0..bytes]);
+
+ if (readInt(u32, base) != 0x464c_457f) return null; // elf magic
+
+ const space = vmm.createAddressSpace() orelse return null;
+ const entry = readInt(u64, base + 24);
+ const ph_off = readInt(u64, base + 32);
+ const ph_count = readInt(u16, base + 56);
+ const ph_size = readInt(u16, base + 54);
+
+ var i: usize = 0;
+ while (i < ph_count) : (i += 1) {
+ const ph = base + ph_off + i * ph_size;
+ if (readInt(u32, ph) != 1) continue; // PT_LOAD
+ const file_off = readInt(u64, ph + 8);
+ const vaddr = readInt(u64, ph + 16);
+ const file_size = readInt(u64, ph + 32);
+ const mem_size = readInt(u64, ph + 40);
+
+ var off: u64 = 0;
+ while (off < mem_size) : (off += pmm.PAGE_SIZE) {
+ const frame = pmm.alloc() orelse return null;
+ const page: [*]u8 = @ptrFromInt(pmm.physToVirt(frame));
+ @memset(page[0..pmm.PAGE_SIZE], 0);
+ if (off < file_size) {
+ const n = @min(file_size - off, pmm.PAGE_SIZE);
+ const src: [*]const u8 = @ptrFromInt(base + file_off + off);
+ @memcpy(page[0..n], src[0..n]);
+ }
+ _ = vmm.map(space, vaddr + off, frame, USER_PAGE);
+ }
+ }
+
+ const stack_top: u64 = 0x7fff_0000;
+ var p: usize = 0;
+ while (p < 4) : (p += 1) {
+ const frame = pmm.alloc() orelse return null;
+ const page: [*]u8 = @ptrFromInt(pmm.physToVirt(frame));
+ @memset(page[0..pmm.PAGE_SIZE], 0);
+ _ = vmm.map(space, stack_top - (p + 1) * pmm.PAGE_SIZE, frame, USER_PAGE);
+ }
+
+ var arg_ptr: u64 = 0;
+ if (arg.len > 0) {
+ const frame = pmm.alloc() orelse return null;
+ const page: [*]u8 = @ptrFromInt(pmm.physToVirt(frame));
+ @memset(page[0..pmm.PAGE_SIZE], 0);
+ const n = @min(arg.len, pmm.PAGE_SIZE);
+ @memcpy(page[0..n], arg[0..n]);
+ _ = vmm.map(space, ARG_VADDR, frame, USER_PAGE);
+ arg_ptr = ARG_VADDR;
+ }
+
+ return .{ .cr3 = space, .entry = entry, .stack = stack_top, .arg_ptr = arg_ptr, .arg_len = arg.len };
+}
+
+pub fn execReplace(new_cr3: u64) void {
+ const old = tasks[current].cr3;
+ tasks[current].cr3 = new_cr3;
+ tasks[current].brk_cur = HEAP_BASE;
+ tasks[current].brk_top = HEAP_BASE;
+ vmm.loadPml4(new_cr3);
+ vmm.freeAddressSpace(old);
+}
+
+pub fn reapZombie(code_out: *i32) i64 {
+ var i: usize = 1;
+ while (i < MAX_TASKS) : (i += 1) {
+ if (tasks[i].state == .zombie and tasks[i].parent == current) {
+ code_out.* = tasks[i].exit_code;
+ vmm.freeAddressSpace(tasks[i].cr3);
+ tasks[i].state = .free;
+ return @intCast(i);
+ }
+ }
+ return -1;
+}
+
+pub fn hasChildren() bool {
+ var i: usize = 1;
+ while (i < MAX_TASKS) : (i += 1) {
+ if (tasks[i].parent == current and tasks[i].state != .free) return true;
+ }
+ return false;
+}
+
+pub fn blockCurrentOnWait(resume_frame: Frame) void {
+ if (current == 0) return;
+ tasks[current].frame = resume_frame;
+ tasks[current].state = .waiting;
+}
+
// stdin at keyboard
fn initFds(i: usize) void {
for (&fdtabs[i]) |*slot| slot.* = .{};
@@ -176,7 +330,7 @@ pub fn procCount() u64 {
var count: u64 = 0;
var i: usize = 1;
while (i < MAX_TASKS) : (i += 1) {
- if (tasks[i].state == .ready or tasks[i].state == .blocked) count += 1;
+ if (tasks[i].state == .ready or tasks[i].state == .blocked or tasks[i].state == .waiting) count += 1;
}
return count;
}
@@ -193,17 +347,40 @@ pub fn taskState(i: u64) u64 {
var proc_exited_flag: bool = false;
-// mark the running process dead then sti+hlts
-pub fn exitCurrent() void {
- if (current != 0) {
- tasks[current].state = .dead;
- proc_exited_flag = true;
- if (foreground == current) {
- foreground = null;
- stdin_head = 0;
- stdin_tail = 0;
+// record the exit code and park the task. an orphan goes .dead (tick reclaims
+// it), a task with a parent goes .zombie until the parent reaps it in wait
+pub fn exitCurrent(code: i32) void {
+ if (current == 0) return;
+ const me = current;
+ tasks[me].exit_code = code;
+
+ // hand our children to the kernel, reaping any that already exited so a
+ // dead parent never strands a zombie
+ var i: usize = 1;
+ while (i < MAX_TASKS) : (i += 1) {
+ if (tasks[i].parent == me) {
+ tasks[i].parent = 0;
+ if (tasks[i].state == .zombie) {
+ vmm.freeAddressSpace(tasks[i].cr3);
+ tasks[i].state = .free;
+ }
}
}
+
+ if (tasks[me].parent == 0) {
+ tasks[me].state = .dead;
+ } else {
+ tasks[me].state = .zombie;
+ if (tasks[tasks[me].parent].state == .waiting)
+ tasks[tasks[me].parent].state = .ready; // wake it to reap us
+ }
+
+ if (foreground == me) {
+ foreground = null;
+ stdin_head = 0;
+ stdin_tail = 0;
+ }
+ proc_exited_flag = true;
}
pub fn takeProcExited() bool {
@@ -241,12 +418,16 @@ pub fn deliverInput(byte: u8) bool {
pub fn tick(frame: *Frame) void {
if (bridge.vmBusy()) return;
- if (tasks[current].state == .dead) {
- tasks[current].state = .free;
- } else if (tasks[current].state == .blocked) {
- // dont clobber it you bitch
- } else {
- tasks[current].frame = frame.*;
+ switch (tasks[current].state) {
+ // an orphan that exited: now that we are about to switch away, reclaim
+ // its address space and free the slot
+ .dead => {
+ vmm.freeAddressSpace(tasks[current].cr3);
+ tasks[current].state = .free;
+ },
+ // parked tasks keep their saved frame, dont clobber it you bitch
+ .blocked, .waiting, .zombie => {},
+ else => tasks[current].frame = frame.*,
}
var next = current;