From 79f04df82e9b756cadf667bc42f967132e113d64 Mon Sep 17 00:00:00 2001 From: Nathan Singer Date: Thu, 5 Jun 2025 17:32:38 -0400 Subject: [PATCH] my exception handler sucked so i redid it --- Makefile | 2 +- kernel/arch/idt/idt.c | 12 ++++--- kernel/arch/idt/idt_src.s | 60 ++++++++++++++++++++++++++------- kernel/include/kernel/x86/idt.h | 19 ++++++++++- 4 files changed, 73 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 04d77f2..0ceaa68 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CC = i686-elf-gcc -DEBUG_FLAGS = -D__TESTING__ -g +DEBUG_FLAGS = -D__TESTING__ -g -Wno-div-by-zero CFLAGS = -m32 -ffreestanding -Wall -Wextra -Werror -Wpedantic --sysroot=$(PWD)/sysroot -isystem=/usr/include -Iinclude -MD $(DEBUG_FLAGS) diff --git a/kernel/arch/idt/idt.c b/kernel/arch/idt/idt.c index 8e211bf..a688f3f 100644 --- a/kernel/arch/idt/idt.c +++ b/kernel/arch/idt/idt.c @@ -1,4 +1,5 @@ #include +#include #ifdef __TESTING__ #include @@ -18,17 +19,18 @@ static bool vectors[IDT_MAX_DESCRIPTORS]; extern struct keyboard_state keyb_state; -void exception_handler(unsigned int i) +void exception_handler(struct cpu_state __attribute__((unused)) cpu, uint32_t interrupt, struct stack_state stack) { - if (i <= 31) { // TODO: implement proper handling for each exception, also implement the proper gates & error code checking + printf("%1 %2\n", interrupt, stack.eflags); + if (interrupt <= 31) { // TODO: implement proper handling for each exception, also implement the proper gates & error code checking #ifdef __TESTING__ kerror("EXCEPTION"); - printf("Exeption: %u\n", i); + printf("Exeption: %2\n", interrupt); #endif - __asm__ volatile ("cli; hlt"); // hangs the computer + //__asm__ volatile ("cli; hlt"); // hangs the computer } - if (i == PIC_KEYB) { + if (interrupt == PIC_KEYB) { unsigned char in = inb(0x60); do_keypress(decode_scancode(in)); PIC_sendEOI(1); diff --git a/kernel/arch/idt/idt_src.s b/kernel/arch/idt/idt_src.s index b144030..469c920 100644 --- a/kernel/arch/idt/idt_src.s +++ b/kernel/arch/idt/idt_src.s @@ -1,27 +1,61 @@ extern exception_handler +extern exception_handler_err %macro isr_err_stub 1 isr_stub_%+%1: - pushad - cld - push dword %1 - call exception_handler - pop eax ; make sure to pop off the dword!! - popad - iret + ;pushad + ;cld + ;push dword %1 + ;call exception_handler_err + ;pop eax ; pop the error + ;pop eax ; make sure to pop off the dword!! + ;popad + ;iret + push dword %1 ; push the interrupt number + jmp common_interrupt_handler + %endmacro %macro isr_no_err_stub 1 isr_stub_%+%1: - pushad - cld + ;pushad + ;cld + ;push dword %1 + ;call exception_handler + ;pop eax + ;popad + ;iret + push dword 0 push dword %1 - call exception_handler - pop eax - popad - iret + jmp common_interrupt_handler %endmacro +common_interrupt_handler: + ; lets save the registers + push eax + push ebx + push ecx + push edx + push esi + push edi + push ebp + + call exception_handler + + ; restore the registers + pop ebp + pop edi + pop esi + pop edx + pop ecx + pop ebx + pop eax + + ; restore the esp + add esp, 8 + + iret + isr_no_err_stub 0 isr_no_err_stub 1 isr_no_err_stub 2 diff --git a/kernel/include/kernel/x86/idt.h b/kernel/include/kernel/x86/idt.h index ed037e9..64e10e4 100644 --- a/kernel/include/kernel/x86/idt.h +++ b/kernel/include/kernel/x86/idt.h @@ -28,7 +28,24 @@ #define EXCEPT_VIRT 20 #define EXCEPT_CTRL_PROT 21 -void exception_handler(unsigned int i); +struct cpu_state { + uint32_t eax; + uint32_t ebx; + uint32_t ecx; + uint32_t edx; + uint32_t esi; + uint32_t edi; + uint32_t ebp; +} __attribute__((packed)); + +struct stack_state { + uint32_t error_code; + uint32_t eip; + uint32_t cs; + uint32_t eflags; +} __attribute__((packed)); + +void exception_handler(struct cpu_state cpu, uint32_t interrupt, struct stack_state stack); typedef struct { uint16_t isr_low; // The lower 16 bits of the ISR's address