From 93bce5a46d33462ff223c31a1f97f38d5d3e5c94 Mon Sep 17 00:00:00 2001 From: karina Date: Thu, 23 Apr 2026 22:03:18 +0400 Subject: [PATCH] feat(kernel): add vectors.S and Exceptions.h/c as a stub for future exceptions handling --- Kernel/CMakeLists.txt | 2 +- Kernel/Include/Arch/Exceptions.h | 67 ++++++++++++++++++++++++ Kernel/Source/Arch/Exceptions.c | 10 ++++ Kernel/Source/Arch/entry.S | 1 + Kernel/Source/Arch/vectors.S | 90 ++++++++++++++++++++++++++++++++ Kernel/Source/KernelMain.c | 2 + 6 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 Kernel/Include/Arch/Exceptions.h create mode 100644 Kernel/Source/Arch/Exceptions.c create mode 100644 Kernel/Source/Arch/vectors.S diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index 91025ab..be84ad4 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) file(GLOB_RECURSE KERNEL_SOURCES CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Source/KernelMain.c ${CMAKE_CURRENT_SOURCE_DIR}/Source/Arch/entry.S -# ${CMAKE_CURRENT_SOURCE_DIR}/Source/IO/*.c + ${CMAKE_CURRENT_SOURCE_DIR}/Source/Arch/vectors.S ${CMAKE_CURRENT_SOURCE_DIR}/Source/**/*.c ) diff --git a/Kernel/Include/Arch/Exceptions.h b/Kernel/Include/Arch/Exceptions.h new file mode 100644 index 0000000..df48045 --- /dev/null +++ b/Kernel/Include/Arch/Exceptions.h @@ -0,0 +1,67 @@ +#pragma once +#include + +typedef struct ExceptionsContext { + UInt64 x0; + UInt64 x1; + UInt64 x2; + UInt64 x3; + UInt64 x4; + UInt64 x5; + UInt64 x6; + UInt64 x7; + UInt64 x8; + UInt64 x9; + UInt64 x10; + UInt64 x11; + UInt64 x12; + UInt64 x13; + UInt64 x14; + UInt64 x15; + UInt64 x16; + UInt64 x17; + UInt64 x18; + UInt64 x19; + UInt64 x20; + UInt64 x21; + UInt64 x22; + UInt64 x23; + UInt64 x24; + UInt64 x25; + UInt64 x26; + UInt64 x27; + UInt64 x28; + UInt64 x29; // fp + UInt64 x30; // lr + UInt64 elr_el1; // pc + UInt64 spsr_el1; // cpu status + UInt64 esr_el1; // error reason +} ExceptionsContext; + +typedef enum ExceptionsType { + // curr el with sp0 (EL1t) + // usually dont happen cuz we switch to sp_el1, but just in case + ExceptionsSyncEl1t, + ExceptionsIRQEl1t, + ExceptionsFIQEl1t, + ExceptionsSErrorEl1t, + + // curr el with sp1 (EL1h) + // exception in kernel space + ExceptionsSyncEl1h, + ExceptionsIRQEl1h, + ExceptionsFIQEl1h, + ExceptionsSErrorEl1h, + + // lower EL 64-bit from userspace + ExceptionsSyncEl064, + ExceptionsIRQEl064, + ExceptionsFIQEl064, + ExceptionsSErrorEl064, + + // lower EL 32-bit from userspace + ExceptionsSyncEl032, + ExceptionsIRQEl032, + ExceptionsFIQEl032, + ExceptionsSErrorEl032, +} ExceptionsType; \ No newline at end of file diff --git a/Kernel/Source/Arch/Exceptions.c b/Kernel/Source/Arch/Exceptions.c new file mode 100644 index 0000000..952d356 --- /dev/null +++ b/Kernel/Source/Arch/Exceptions.c @@ -0,0 +1,10 @@ +#include +#include +#include + +void ExceptionsHandler(ExceptionsContext* context, ExceptionsType type) { + IOSerialPutString("Exception occurred"); + while (1) { + CPUWaitForInterrupt(); + } +} \ No newline at end of file diff --git a/Kernel/Source/Arch/entry.S b/Kernel/Source/Arch/entry.S index 59853a5..af21614 100644 --- a/Kernel/Source/Arch/entry.S +++ b/Kernel/Source/Arch/entry.S @@ -1,5 +1,6 @@ .section .text.boot, "ax" .global _start _start: + bl ExceptionsVectorsInit bl KernelMain b . \ No newline at end of file diff --git a/Kernel/Source/Arch/vectors.S b/Kernel/Source/Arch/vectors.S new file mode 100644 index 0000000..05e183e --- /dev/null +++ b/Kernel/Source/Arch/vectors.S @@ -0,0 +1,90 @@ +.macro ventry type + .align 7 + sub sp, sp, #272 // save 272 bytes of stack + stp x0, x1, [sp, #0] // move stack + mov x1, #\type // move type to x1 + b ExceptionsTrapEntry +.endm + +.section .text.vectors +.align 11 +.global vectors +ExceptionsVectorsTable: + // EL1t (curr EL with SP0) + ventry 0 // Sync + ventry 1 // IRQ + ventry 2 // FIQ + ventry 3 // SError + + // EL1h (curr EL with SP1) + ventry 4 // Sync + ventry 5 // IRQ + ventry 6 // FIQ + ventry 7 // SError + + // EL0 (lower EL 64-bit from userspace) + ventry 8 // Sync + ventry 9 // IRQ + ventry 10 // FIQ + ventry 11 // SError + + // EL0 (lower EL 32-bit from userspace) + ventry 12; ventry 13; ventry 14; ventry 15 + +ExceptionsTrapEntry: + stp x2, x3, [sp, #16 * 1] + stp x4, x5, [sp, #16 * 2] + stp x6, x7, [sp, #16 * 3] + stp x8, x9, [sp, #16 * 4] + stp x10, x11, [sp, #16 * 5] + stp x12, x13, [sp, #16 * 6] + stp x14, x15, [sp, #16 * 7] + stp x16, x17, [sp, #16 * 8] + stp x18, x19, [sp, #16 * 9] + stp x20, x21, [sp, #16 * 10] + stp x22, x23, [sp, #16 * 11] + stp x24, x25, [sp, #16 * 12] + stp x26, x27, [sp, #16 * 13] + stp x28, x29, [sp, #16 * 14] + + mrs x21, elr_el1 + mrs x22, spsr_el1 + mrs x23, esr_el1 + + stp x30, x21, [sp, #16 * 15] + stp x22, x23, [sp, #16 * 16] + + mov x0, sp + bl ExceptionsHandler + + ldp x22, x23, [sp, #16 * 16] + msr spsr_el1, x22 + + ldp x30, x21, [sp, #16 * 15] + msr elr_el1, x21 + + ldp x28, x29, [sp, #16 * 14] + ldp x26, x27, [sp, #16 * 13] + ldp x24, x25, [sp, #16 * 12] + ldp x22, x23, [sp, #16 * 11] + ldp x20, x21, [sp, #16 * 10] + ldp x18, x19, [sp, #16 * 9] + ldp x16, x17, [sp, #16 * 8] + ldp x14, x15, [sp, #16 * 7] + ldp x12, x13, [sp, #16 * 6] + ldp x10, x11, [sp, #16 * 5] + ldp x8, x9, [sp, #16 * 4] + ldp x6, x7, [sp, #16 * 3] + ldp x4, x5, [sp, #16 * 2] + ldp x2, x3, [sp, #16 * 1] + ldp x0, x1, [sp, #0] + + add sp, sp, #272 + eret + +.global ExceptionsVectorsInit +ExceptionsVectorsInit: + adr x0, ExceptionsVectorsTable + msr vbar_el1, x0 + isb + ret \ No newline at end of file diff --git a/Kernel/Source/KernelMain.c b/Kernel/Source/KernelMain.c index 10592dd..ddd4caf 100644 --- a/Kernel/Source/KernelMain.c +++ b/Kernel/Source/KernelMain.c @@ -3,4 +3,6 @@ void KernelMain(void) { IOSerialPutString("Meow nya!!\n"); + __asm__ volatile ("brk #0"); + IOSerialPutString("How\r\n"); } \ No newline at end of file