my exception handler sucked so i redid it

This commit is contained in:
2025-06-05 17:32:38 -04:00
parent 87e5e06142
commit 79f04df82e
4 changed files with 73 additions and 20 deletions

View File

@ -3,7 +3,7 @@
CC = i686-elf-gcc 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) CFLAGS = -m32 -ffreestanding -Wall -Wextra -Werror -Wpedantic --sysroot=$(PWD)/sysroot -isystem=/usr/include -Iinclude -MD $(DEBUG_FLAGS)

View File

@ -1,4 +1,5 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#ifdef __TESTING__ #ifdef __TESTING__
#include <kernel/_kernel.h> #include <kernel/_kernel.h>
@ -18,17 +19,18 @@ static bool vectors[IDT_MAX_DESCRIPTORS];
extern struct keyboard_state keyb_state; 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__ #ifdef __TESTING__
kerror("EXCEPTION"); kerror("EXCEPTION");
printf("Exeption: %u\n", i); printf("Exeption: %2\n", interrupt);
#endif #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); unsigned char in = inb(0x60);
do_keypress(decode_scancode(in)); do_keypress(decode_scancode(in));
PIC_sendEOI(1); PIC_sendEOI(1);

View File

@ -1,27 +1,61 @@
extern exception_handler extern exception_handler
extern exception_handler_err
%macro isr_err_stub 1 %macro isr_err_stub 1
isr_stub_%+%1: isr_stub_%+%1:
pushad ;pushad
cld ;cld
push dword %1 ;push dword %1
call exception_handler ;call exception_handler_err
pop eax ; make sure to pop off the dword!! ;pop eax ; pop the error
popad ;pop eax ; make sure to pop off the dword!!
iret ;popad
;iret
push dword %1 ; push the interrupt number
jmp common_interrupt_handler
%endmacro %endmacro
%macro isr_no_err_stub 1 %macro isr_no_err_stub 1
isr_stub_%+%1: isr_stub_%+%1:
pushad ;pushad
cld ;cld
;push dword %1
;call exception_handler
;pop eax
;popad
;iret
push dword 0
push dword %1 push dword %1
call exception_handler jmp common_interrupt_handler
pop eax
popad
iret
%endmacro %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 0
isr_no_err_stub 1 isr_no_err_stub 1
isr_no_err_stub 2 isr_no_err_stub 2

View File

@ -28,7 +28,24 @@
#define EXCEPT_VIRT 20 #define EXCEPT_VIRT 20
#define EXCEPT_CTRL_PROT 21 #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 { typedef struct {
uint16_t isr_low; // The lower 16 bits of the ISR's address uint16_t isr_low; // The lower 16 bits of the ISR's address