From 560bdc8b1d6f810babad8f875bdf4bdb969f443a Mon Sep 17 00:00:00 2001 From: karina Date: Fri, 24 Apr 2026 18:14:04 +0400 Subject: [PATCH] feat: error parse in panic --- Kernel/Include/Arch/CPU.h | 6 ++++ Kernel/Include/types.h | 1 + Kernel/Source/KernelMain.c | 1 + Kernel/Source/OS/Panic.c | 70 +++++++++++++++++++++++++++++++++----- 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/Kernel/Include/Arch/CPU.h b/Kernel/Include/Arch/CPU.h index ae6aafa..d142bc9 100644 --- a/Kernel/Include/Arch/CPU.h +++ b/Kernel/Include/Arch/CPU.h @@ -16,3 +16,9 @@ static inline void CPUDisableInterrupts() { static inline void CPUEnableInterrupts() { __asm__ volatile ("msr daifclr, #3" ::: "memory"); } + +static inline UInt64 CPUGetFAR() { + UInt64 far; + __asm__ volatile ("mrs %0, far_el1" : "=r" (far)); + return far; +} \ No newline at end of file diff --git a/Kernel/Include/types.h b/Kernel/Include/types.h index 442cefc..3e4261f 100644 --- a/Kernel/Include/types.h +++ b/Kernel/Include/types.h @@ -22,3 +22,4 @@ typedef int Int; typedef UInt64 Size; typedef char ASCII; +typedef _Bool Boolean; diff --git a/Kernel/Source/KernelMain.c b/Kernel/Source/KernelMain.c index d21d0c3..3976a24 100644 --- a/Kernel/Source/KernelMain.c +++ b/Kernel/Source/KernelMain.c @@ -1,4 +1,5 @@ #include void KernelMain(void) { + OSLog("Hi meow! ;3\n"); } \ No newline at end of file diff --git a/Kernel/Source/OS/Panic.c b/Kernel/Source/OS/Panic.c index 769d159..13883ac 100644 --- a/Kernel/Source/OS/Panic.c +++ b/Kernel/Source/OS/Panic.c @@ -1,5 +1,5 @@ #include -#include +#include #include static const ASCII* GetExceptionClassString(UInt32 class) { @@ -20,6 +20,10 @@ static const ASCII* GetExceptionClassString(UInt32 class) { } } +static void PrintSeparator() { + OSLog("--------------------------------\n"); +} + __attribute__((noreturn)) void OSPanicException(ExceptionsContext* frame) { UInt32 esr = frame->esr_el1; @@ -27,14 +31,62 @@ __attribute__((noreturn)) void OSPanicException(ExceptionsContext* frame) { 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"); + PrintSeparator(); + OSLog("Kernel Panic! :(\n"); + PrintSeparator(); + OSLog("CPU Exception: %s (%d)\n", GetExceptionClassString(class), class); + OSLog("ESR_EL1 (Syndrome): 0x%x (%d)\n", esr, syndrome); + OSLog("Instruction pointer: 0x%x (%s)\n", frame->elr_el1, length ? "32-bit" : "64-bit"); + OSLog("CPU status: 0x%x\n", frame->spsr_el1); + + if (class == 0x25 || class == 0x24 || class == 0x20 || class == 0x21 ) { + PrintSeparator(); + OSLog("Memory abort helper:\n"); + PrintSeparator(); + + UInt64 far = CPUGetFAR(); + OSLog("Faulting address: 0x%x\n", far); + + UInt32 wnr = (syndrome >> 6) & 0x1; + if (class == 0x24 || class == 0x25) { + OSLog("[W] Caused by %s\n", wnr ? "WRITE" : "READ"); + } else { + OSLog("[T] Tried to execute code from NX/Invalid memory\n"); + } + + + UInt32 dfsc = syndrome & 0x3F; + switch (dfsc & 0b111100) { + case 0b000000: OSLog("[P] Reason: Address size fault (Bad pointer format)\n"); break; + case 0b000100: OSLog("[P] Reason: Translation fault\n"); break; + case 0b001100: OSLog("[P] Reason: Permission fault (Page protection violation)\n"); break; + case 0b010000: OSLog("[P] Reason: Synchronous external abort\n"); break; + case 0b100000: OSLog("[P] Reason: Alignment fault\n"); break; + default: OSLog("[P] Reason: Unknown fault code (0x%X)\n", dfsc); break; + } + } + PrintSeparator(); + OSLog("Registers:\n"); + PrintSeparator(); + OSLog("x0 = 0x%X; x1 = 0x%X\n", frame->x0, frame->x1); + OSLog("x2 = 0x%X; x3 = 0x%X\n", frame->x2, frame->x3); + OSLog("x4 = 0x%X; x5 = 0x%X\n", frame->x4, frame->x5); + OSLog("x6 = 0x%X; x7 = 0x%X\n", frame->x6, frame->x7); + OSLog("x8 = 0x%X; x9 = 0x%X\n", frame->x8, frame->x9); + OSLog("x10 = 0x%X; x11 = 0x%X\n", frame->x10, frame->x11); + OSLog("x12 = 0x%X; x13 = 0x%X\n", frame->x12, frame->x13); + OSLog("x14 = 0x%X; x15 = 0x%X\n", frame->x14, frame->x15); + OSLog("x16 = 0x%X; x17 = 0x%X\n", frame->x16, frame->x17); + OSLog("x18 = 0x%X; x19 = 0x%X\n", frame->x18, frame->x19); + OSLog("x20 = 0x%X; x21 = 0x%X\n", frame->x20, frame->x21); + OSLog("x22 = 0x%X; x23 = 0x%X\n", frame->x22, frame->x23); + OSLog("x24 = 0x%X; x25 = 0x%X\n", frame->x24, frame->x25); + OSLog("x26 = 0x%X; x27 = 0x%X\n", frame->x26, frame->x27); + OSLog("\t\tx28 = 0x%X\n", frame->x28); + OSLog("FP = 0x%X; LR = 0x%X\n", frame->x29, frame->x30); + PrintSeparator(); + + OSLog("System halted.\n"); while (1) { CPUDisableInterrupts();