Working kernel written on C and userspace-ready #1

Merged
sonya merged 61 commits from dev into main 2026-05-03 09:13:20 +00:00
4 changed files with 69 additions and 9 deletions
Showing only changes of commit 560bdc8b1d - Show all commits
+6
View File
@@ -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;
}
+1
View File
@@ -22,3 +22,4 @@ typedef int Int;
typedef UInt64 Size;
typedef char ASCII;
typedef _Bool Boolean;
+1
View File
@@ -1,4 +1,5 @@
#include <OS/Log.h>
void KernelMain(void) {
OSLog("Hi meow! ;3\n");
}
+61 -9
View File
@@ -1,5 +1,5 @@
#include <OS/Panic.h>
#include <IO/Serial.h>
#include <OS/Log.h>
#include <Arch/CPU.h>
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();