chore: reorganize file directories

We have reorganized all the file directories that previously looked ugly as hell (cough cough arch/x86_64 cough)

Now it looks much cleaner

Signed-off-by: kaguya <vpshinomiya@protonmail.com>
This commit is contained in:
kaguya
2026-04-26 14:44:40 -04:00
parent d22d5ab2e4
commit 336af1c2ad
67 changed files with 96 additions and 99 deletions
+124
View File
@@ -0,0 +1,124 @@
// ... includes ...
#include "arch/x86_64/cpu/io.h"
#include "libk/debug.h"
#include "arch/x86_64/boot/isr.h"
#include "idt.h"
#define MODULE "ISR"
static inline uint64_t x86_64_read_cr2(void)
{
uint64_t value;
__asm__ volatile ("mov %%cr2, %0" : "=r"(value));
return value;
}
ISRHandler g_ISRHandlers[256];
static const char* const g_Exceptions[] = {
"Divide by zero error",
"Debug",
"Non-maskable Interrupt",
"Breakpoint",
"Overflow",
"Bound Range Exceeded",
"Invalid Opcode",
"Device Not Available",
"Double Fault",
"Coprocessor Segment Overrun",
"Invalid TSS",
"Segment Not Present",
"Stack-Segment Fault",
"General Protection Fault",
"Page Fault",
"",
"x87 Floating-Point Exception",
"Alignment Check",
"Machine Check",
"SIMD Floating-Point Exception",
"Virtualization Exception",
"Control Protection Exception ",
"",
"",
"",
"",
"",
"",
"Hypervisor Injection Exception",
"VMM Communication Exception",
"Security Exception",
""
};
extern void x86_64_ISR_InitializeGates(void); // defined in isrs_gen.c
void x86_64_ISR_Initialize(void)
{
x86_64_ISR_InitializeGates();
for (int i = 0; i < 256; i++)
x86_64_IDT_EnableGate(i);
x86_64_IDT_DisableGate(0x80); // syscall gate if you want
}
void page_fault_handler(Registers* regs, uint64_t cr2)
{
// You can decode error bits here:
// bit 0: present
// bit 1: write
// bit 2: user-mode
// bit 3: reserved overwrite
// bit 4: instruction fetch
log_crit(MODULE, "KERNEL PANIC! Exception %d (%s)", regs->interrupt, g_Exceptions[regs->interrupt]);
log_crit(MODULE, " rax=%x rbx=%x rcx=%x rdx=%x rsi=%x rdi=%x",
regs->rax, regs->rbx, regs->rcx, regs->rdx, regs->rsi, regs->rdi);
log_crit(MODULE, " rsp=%x rbp=%x rip=%x rflags=%x cs=%x ss=%x",
regs->rsp, regs->rbp, regs->rip, regs->rflags, regs->cs, regs->ss);
log_crit(MODULE, " interrupt=%x errorcode=%x", regs->interrupt, regs->error);
log_crit("PAGEFAULT", "Fault at address %lx (rip=%lx, err=%x)",
cr2, regs->rip, regs->error);
log_crit(MODULE, "KERNEL PANIC!");
x86_64_Panic(); // or attempt recovery
}
void __attribute__((used)) x86_64_ISR_Handler(Registers* regs)
{
if (regs->interrupt == 14)
{
uint64_t cr2 = x86_64_read_cr2();
page_fault_handler(regs, cr2);
}
if (g_ISRHandlers[regs->interrupt])
g_ISRHandlers[regs->interrupt](regs);
else if (regs->interrupt >= 32)
log_err(MODULE, "Unhandled interrupt %d!", regs->interrupt);
else {
// panic log (use regs->rax etc. now)
log_crit(MODULE, "KERNEL PANIC! Exception %d (%s)", regs->interrupt, g_Exceptions[regs->interrupt]);
// ... print registers with rax/rbx etc. ...
log_crit(MODULE, " rax=%x rbx=%x rcx=%x rdx=%x rsi=%x rdi=%x",
regs->rax, regs->rbx, regs->rcx, regs->rdx, regs->rsi, regs->rdi);
log_crit(MODULE, " rsp=%x rbp=%x rip=%x rflags=%x cs=%x ss=%x",
regs->rsp, regs->rbp, regs->rip, regs->rflags, regs->cs, regs->ss);
log_crit(MODULE, " interrupt=%x errorcode=%x", regs->interrupt, regs->error);
log_crit(MODULE, "KERNEL PANIC!");
x86_64_Panic();
}
}
void x86_64_ISR_RegisterHandler(int interrupt, ISRHandler handler)
{
g_ISRHandlers[interrupt] = handler;
x86_64_IDT_EnableGate(interrupt);
}