From d348ac109ed66dc431a52972e4e542a3c070a92d Mon Sep 17 00:00:00 2001 From: 0x35c Date: Tue, 8 Oct 2024 10:56:15 +0200 Subject: [PATCH] fix: interrupt handler function uses an array fix: gdt calls cli asm instruction at init --- src/gdt/set_gdt.s | 1 + src/interrupt/handler.c | 98 +++++++++++++++-------------------------- 2 files changed, 36 insertions(+), 63 deletions(-) diff --git a/src/gdt/set_gdt.s b/src/gdt/set_gdt.s index eaf5902..7b5ebb1 100644 --- a/src/gdt/set_gdt.s +++ b/src/gdt/set_gdt.s @@ -4,6 +4,7 @@ .global set_gdt set_gdt: + cli // disable all interrupts mov eax, [esp+4] // 1st parameter : pointer to the IDT lgdt [eax] mov ax, 0x10 // 0x10 is the offset in the GDT to our data segment diff --git a/src/interrupt/handler.c b/src/interrupt/handler.c index 1aea8cd..0bb0aa0 100644 --- a/src/interrupt/handler.c +++ b/src/interrupt/handler.c @@ -1,76 +1,48 @@ #include "interrupts.h" #include "kpanic.h" +#include "kprintf.h" +#include "utils.h" #include +const char *faults[] = { + "division by zero", + "debugger", + "NMI", + "breakpoint", + "overflow", + "bounds", + "invalid opcode", + "coprocessor not available", + "double fault", + "coprocessor segement overrun", + "invalid task state segment", + "segment not present", + "stack fault", + "general protection fault", + "page fault", + "reserved", + "math fault", + "alignment check", + "machine check", + "SIMD floating point exception", +}; + void exception_handler(void) { int8_t index = -1; __asm__ volatile("movb %%bl, %0" ::"m"(index)); - switch (index) { - case 0: - kpanic("interrupt: division by zero\n"); - break; - case 1: - kpanic("interrupt: debugger\n"); - break; - case 2: - kpanic("interrupt: NMI\n"); - break; - case 3: - kpanic("interrupt: breakpoint\n"); - break; - case 4: - kpanic("interrupt: overflow\n"); - break; - case 5: - kpanic("interrupt: bounds\n"); - break; - case 6: - kpanic("interrupt: invalid opcode\n"); - break; - case 7: - kpanic("interrupt: coprocessor not available\n"); - break; - case 8: - kpanic("interrupt: double fault\n"); - break; - case 9: - kpanic("interrupt: coprocessor segement overrun\n"); - break; - case 10: - kpanic("interrupt: invalid task state segment\n"); - break; - case 11: - kpanic("interrupt: segment not present\n"); - break; - case 12: - kpanic("interrupt: stack fault\n"); - break; - case 13: - kpanic("interrupt: general protection fault\n"); - break; - case 14: - kpanic("interrupt: page fault\n"); - break; - case 15: - kpanic("interrupt: reserved\n"); - break; - case 16: - kpanic("interrupt: math fault\n"); - break; - case 17: - kpanic("interrupt: alignment check\n"); - break; - case 18: - kpanic("interrupt: machine check\n"); - break; - case 19: - kpanic("interrupt: SIMD floating point exception\n"); - break; - default: + if (index == -1) { + kprintf(KERN_ERR "interrupt triggered without a code\n"); return; - break; } + + uint8_t i = 0; + while (i < ARRAY_SIZE(faults)) { + if (i == index) + kpanic("interrupt: %s\n", faults[i]); + i++; + } + kprintf(KERN_ERR "interrupt triggered with no matching code\n"); }