diff --git a/Kernel/Include/Arch/CPU.h b/Kernel/Include/Arch/CPU.h index ce9532a..ae6aafa 100644 --- a/Kernel/Include/Arch/CPU.h +++ b/Kernel/Include/Arch/CPU.h @@ -1,4 +1,5 @@ #pragma once +#include static inline void CPUYield() { __asm__ volatile ("yield" ::: "memory"); @@ -6,4 +7,12 @@ static inline void CPUYield() { static inline void CPUWaitForInterrupt() { __asm__ volatile ("wfi" ::: "memory"); -} \ No newline at end of file +} + +static inline void CPUDisableInterrupts() { + __asm__ volatile ("msr daifset, #3" ::: "memory"); +} + +static inline void CPUEnableInterrupts() { + __asm__ volatile ("msr daifclr, #3" ::: "memory"); +} diff --git a/Kernel/Include/OS/Panic.h b/Kernel/Include/OS/Panic.h new file mode 100644 index 0000000..c8d69d5 --- /dev/null +++ b/Kernel/Include/OS/Panic.h @@ -0,0 +1,6 @@ +#pragma once + +#include +#include + +__attribute__((noreturn)) void OSPanicException(ExceptionsContext* frame); \ No newline at end of file diff --git a/Kernel/Source/Arch/Exceptions.c b/Kernel/Source/Arch/Exceptions.c index 200e529..532bbb2 100644 --- a/Kernel/Source/Arch/Exceptions.c +++ b/Kernel/Source/Arch/Exceptions.c @@ -1,10 +1,8 @@ #include #include #include +#include -void ExceptionsHandler(ExceptionsContext* context, ExceptionsType type) { - IOSerialPutString("Exception occurred"); - while (1) { - CPUWaitForInterrupt(); - } +void ExceptionsHandler(ExceptionsContext* frame, ExceptionsType type) { + OSPanicException(frame); } \ No newline at end of file diff --git a/Kernel/Source/KernelMain.c b/Kernel/Source/KernelMain.c index 131de92..7cc4f2d 100644 --- a/Kernel/Source/KernelMain.c +++ b/Kernel/Source/KernelMain.c @@ -3,6 +3,4 @@ void KernelMain(void) { IOSerialPutString("Meow nya!!\n"); - __asm__ volatile ("brk #0"); - IOSerialPutString("How\r\n"); } \ No newline at end of file diff --git a/Kernel/Source/OS/Panic.c b/Kernel/Source/OS/Panic.c new file mode 100644 index 0000000..769d159 --- /dev/null +++ b/Kernel/Source/OS/Panic.c @@ -0,0 +1,43 @@ +#include +#include +#include + +static const ASCII* GetExceptionClassString(UInt32 class) { + switch (class) { + case 0x00: return "Unknown reason"; + case 0x01: return "Trapped WFI | WFE instruction"; + case 0x07: return "Trapped SIMD | FP instruction"; + case 0x11: return "SVC from EL1"; + case 0x15: return "SVC from EL0"; + case 0x20: return "Instruction abort (LoEL)"; // User Execute Fault + case 0x21: return "Instruction abort (CurrEL)"; // Kernel Execute Fault + case 0x22: return "PC Alignment Fault"; // Jumped to a misaligned address + case 0x24: return "Data abort (LoEL)"; // User Memory Fault + case 0x25: return "Data abort (CurrEL)"; // Kernel Memory Fault + case 0x26: return "SP Alignment Fault"; // SP must be 16-byte aligned + case 0x3C: return "Breakpoint"; + default: return "Reserved"; + } +} + + +__attribute__((noreturn)) void OSPanicException(ExceptionsContext* frame) { + UInt32 esr = frame->esr_el1; + UInt32 class = (esr >> 26) & 0x3F; // Exception class (Bits 31:26) + UInt32 length = (esr >> 25) & 0x1; // Instruction length (Bit 25) + UInt32 syndrome = esr & 0x1FFFFFF; // Syndrome (Bits 24:0) + + // i cant show any info since i have no formatter yet :( + IOSerialPutString("\n-------------------\n"); + IOSerialPutString("Kernel Panic! :(\n"); + IOSerialPutString("-------------------\n"); + IOSerialPutString("CPU Exception: "); + IOSerialPutString(GetExceptionClassString(class)); + IOSerialPutString("\n-------------------\n"); + IOSerialPutString("System halted.\n"); + + while (1) { + CPUDisableInterrupts(); + CPUWaitForInterrupt(); + } +} \ No newline at end of file