hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/boot/idt.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/idt.zig
parent6a5543bb8f1f535ca49737bcd7be0cafca3b2a08 (diff)
feat: fork/exec & wait
Diffstat (limited to 'boot/idt.zig')
-rw-r--r--boot/idt.zig61
1 files changed, 50 insertions, 11 deletions
diff --git a/boot/idt.zig b/boot/idt.zig
index 50b5c6f..79a8974 100644
--- a/boot/idt.zig
+++ b/boot/idt.zig
@@ -10,6 +10,7 @@ const pic = @import("pic.zig");
const keyboard = @import("keyboard.zig");
const timer = @import("timer.zig");
const sched = @import("sched.zig");
+const vmm = @import("vmm.zig");
const regs = @import("regs.zig");
const Frame = regs.Frame;
@@ -86,7 +87,13 @@ export fn isrCommon() callconv(.naked) void {
);
}
-export fn exceptionHandler(frame: *Frame) callconv(.c) noreturn {
+fn readCr2() u64 {
+ return asm volatile ("mov %%cr2, %[out]"
+ : [out] "=r" (-> u64),
+ );
+}
+
+fn dumpFrame(frame: *Frame) void {
serial.write("\r\n!!! CPU DID AN OOPSIE: ");
serial.write(name(frame.vector));
serial.write("\r\n vector=");
@@ -101,14 +108,45 @@ export fn exceptionHandler(frame: *Frame) callconv(.c) noreturn {
serial.writeHex(frame.rflags);
serial.write(" rsp=");
serial.writeHex(frame.rsp);
- // page faults stash the offending address in cr2
- if (frame.vector == VEC_PAGE_FAULT) {
- const cr2 = asm volatile ("mov %%cr2, %[out]"
- : [out] "=r" (-> u64),
- );
- serial.write("\r\n cr2=");
- serial.writeHex(cr2);
- }
+ serial.write("\r\n");
+}
+
+export fn exceptionHandler(frame: *Frame) callconv(.c) noreturn {
+ dumpFrame(frame);
+ while (true) asm volatile ("hlt");
+}
+
+// #pf gets its own return path so a write to a copy on write page is fixed by vmm
+// and we i retq to retry the faulting instruction, anything else just kills itself
+fn pfStub() callconv(.naked) void {
+ // the cpu already pushed the error code for #pf
+ asm volatile ("pushq %[v]\n jmp pfCommon"
+ :
+ : [v] "i" (@as(u32, VEC_PAGE_FAULT)),
+ );
+}
+
+export fn pfCommon() callconv(.naked) void {
+ asm volatile (regs.PUSH_GPRS ++
+ \\
+ \\ mov %rsp, %rdi
+ \\ call pfHandler
+ \\
+ ++ regs.POP_GPRS ++
+ \\
+ \\ add $16, %rsp
+ \\ iretq
+ );
+}
+
+export fn pfHandler(frame: *Frame) callconv(.c) void {
+ const fault_addr = readCr2();
+ // bit 1 of the error code = the fault was a write
+ if (frame.error_code & 0b10 != 0 and vmm.handleCow(fault_addr)) return;
+
+ dumpFrame(frame);
+ serial.write(" cr2=");
+ serial.writeHex(fault_addr);
serial.write("\r\n");
while (true) asm volatile ("hlt");
}
@@ -175,11 +213,12 @@ pub fn setGate(vector: u8, handler: u64, dpl: u2, ist: u3) void {
// fill the exception vectors and load the idt
pub fn init() void {
inline for (0..32) |vector| setGate(vector, @intFromPtr(&stub(vector)), 0, 0); // vectors 0..31 are exceptions
- // #df (8) and #pf (14) land on ist2s fault stack, so a kernel stack overflow
+ // #df (8) and #pf (14) land on ist2s fault stack so a kernel stack overflow
// produces a real exception dump instead of a silent triple fault
const ist_fault = 2;
setGate(VEC_DOUBLE_FAULT, @intFromPtr(&stub(VEC_DOUBLE_FAULT)), 0, ist_fault);
- setGate(VEC_PAGE_FAULT, @intFromPtr(&stub(VEC_PAGE_FAULT)), 0, ist_fault);
+ // #pf uses the returning path so copy on write faults can be fixed up
+ setGate(VEC_PAGE_FAULT, @intFromPtr(&pfStub), 0, ist_fault);
// irq0 (pit) -> vector 0x20, irq1 (keyboard) -> vector 0x21
// the pic is remapped separately in boot
setGate(pic.MASTER_OFFSET + 0, @intFromPtr(&irqStub(pic.MASTER_OFFSET + 0)), 0, 0);