From 2fa4193517c0dbafd181fe9d3a59091498712921 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 1 Nov 2023 21:22:54 -0400 Subject: [PATCH] Pass exception stack frame pointer to exception handler Show exception stack frame contents on exception --- src/hulk/idt.d | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/hulk/idt.d b/src/hulk/idt.d index 7902018..9a2a8d9 100644 --- a/src/hulk/idt.d +++ b/src/hulk/idt.d @@ -9,9 +9,21 @@ import hulk.fb; import hulk.console; import hulk.klog; import hulk.apic; +import hulk.cpu; struct Idt { + static struct ExceptionStackFrame + { + ulong rip; + ulong cs; + ulong rflags; + ulong rsp; + ulong ss; + } + + public static enum ulong EXC_PAGE_FAULT = 14; + /* The I/O APIC is configured to map IRQ 0 to interrupt 64 (0x40). */ public static enum ulong INT_APIC_BASE = 0x40u; /* IRQ 0 */ public static enum ulong INT_APIC_COUNT = 24u; @@ -74,10 +86,13 @@ struct Idt pushq %rdi pushq %rsi pushq %rax + pushq %rdx mov $$", isr, ", %rdi - " ~ (isr_has_error_code(isr) ? "mov 0x18(%rsp), %rsi" : "") ~ " + " ~ (isr_has_error_code(isr) ? "mov 0x20(%rsp), %rsi" : "") ~ " + " ~ (isr_has_error_code(isr) ? "lea 0x28(%rsp), %rdx" : "lea 0x20(%rsp), %rdx") ~ " movabs $$isr, %rax callq *%rax + popq %rdx popq %rax popq %rsi popq %rdi @@ -102,7 +117,7 @@ struct Idt } } -public extern(C) void isr(ulong vector, ulong arg) +public extern(C) void isr(ulong vector, ulong arg, Idt.ExceptionStackFrame * stack_frame) { if ((vector >= Idt.INT_APIC_BASE) && (vector < (Idt.INT_APIC_BASE + Idt.INT_APIC_COUNT))) { @@ -122,7 +137,13 @@ public extern(C) void isr(ulong vector, ulong arg) default: Console.clear(); Fb.clear(0xFF8000u); - Klog.writefln("ISR %u, 0x%x", vector, arg); + Klog.writefln("ISR %u, error code 0x%x", vector, arg); + Klog.writefln("RIP = 0x%p", stack_frame.rip); + Klog.writefln("CS = 0x%x", stack_frame.cs); + Klog.writefln("RFLAGS = 0x%x", stack_frame.rflags); + Klog.writefln("RSP = 0x%x", stack_frame.rsp); + Klog.writefln("SS = 0x%x", stack_frame.ss); + Klog.writefln("CR2 = 0x%p", read_cr2()); __asm("cli", ""); for (;;) {