Built GDT + IDT + exception handlers

This commit is contained in:
2026-07-03 12:05:41 +01:00
parent 6312e84262
commit 0cc71ec8aa
9 changed files with 458 additions and 23 deletions
+20 -15
View File
@@ -30,6 +30,11 @@ fn kmain(boot_info: *const BootInfo) noreturn {
con.clear();
con_ready = true;
// Catch CPU exceptions before doing anything that might fault: install our
// reporter, then bring up the GDT + IDT.
arch.setFaultHandler(onException);
arch.init();
con.write("danos: framebuffer console online\n");
con.print(" resolution : {d}x{d}\n", .{ fb.width, fb.height });
con.print(" pitch : {d} bytes\n", .{fb.pitch});
@@ -76,21 +81,6 @@ fn kmain(boot_info: *const BootInfo) noreturn {
if (f2) |p| pmm.free(p);
con.print(" after free : {d} frames free\n", .{pmm.stats().free_frames});
// Bring up the physical frame allocator over that map, and prove it works:
// allocate three frames, then hand them back.
pmm.init(boot_info.memory_map);
const s = pmm.stats();
con.print("\ndanos: frame allocator online\n", .{});
con.print(" free frames: {d} ({d} MiB)\n", .{ s.free_frames, s.free_frames * danos.page_size / (1024 * 1024) });
const f0 = pmm.alloc();
const f1 = pmm.alloc();
const f2 = pmm.alloc();
con.print(" alloc x3 : 0x{x} 0x{x} 0x{x}\n", .{ f0 orelse 0, f1 orelse 0, f2 orelse 0 });
if (f0) |p| pmm.free(p);
if (f1) |p| pmm.free(p);
if (f2) |p| pmm.free(p);
con.print(" after free : {d} frames free\n", .{pmm.stats().free_frames});
con.write("\nkernel initialised; nothing left to do, halting.\n");
arch.halt();
@@ -101,6 +91,21 @@ fn mib(pages: u64) u64 {
return pages * danos.page_size / (1024 * 1024);
}
/// Report a CPU exception in red and halt. There's no fault recovery yet, so any
/// exception is terminal — but now it prints what and where instead of silently
/// resetting the machine.
fn onException(state: *const arch.CpuState) noreturn {
if (con_ready) {
con.fg = 0x00ff_5555;
con.print("\nCPU EXCEPTION: {s} (vector {d})\n", .{ arch.vectorName(state.vector), state.vector });
con.print(" error code : 0x{x}\n", .{state.error_code});
con.print(" RIP : 0x{x:0>16}\n", .{state.rip});
con.print(" RSP : 0x{x:0>16}\n", .{state.rsp});
if (state.vector == 14) con.print(" CR2 (addr) : 0x{x:0>16}\n", .{arch.readCr2()});
}
arch.halt();
}
/// Freestanding has no OS to receive a panic. Print it to the console (if it is
/// up yet) in red, then halt.
pub const panic = std.debug.FullPanic(struct {