From d751d35a71f6f2e9a35f1a42b5f049b7d4c90437 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sun, 5 Feb 2023 15:29:26 +0100 Subject: [PATCH] implemented exception handling --- kernel/inc/platform/exceptions.h | 57 ++++++++++++++++++++++++++++++++ kernel/inc/utils/core.h | 5 ++- kernel/inc/utils/panic.h | 24 ++++++++++++++ kernel/src/kmain.c | 4 +-- kernel/src/platform/exceptions.c | 55 ++++++++++++++++++++++++++++++ kernel/src/platform/interrupts.c | 11 ++++-- kernel/src/utils/panic.c | 25 ++++++++++++++ 7 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 kernel/inc/platform/exceptions.h create mode 100644 kernel/inc/utils/panic.h create mode 100644 kernel/src/platform/exceptions.c create mode 100644 kernel/src/utils/panic.c diff --git a/kernel/inc/platform/exceptions.h b/kernel/inc/platform/exceptions.h new file mode 100644 index 0000000..3699aa2 --- /dev/null +++ b/kernel/inc/platform/exceptions.h @@ -0,0 +1,57 @@ +/* Copyright (C) Antifallobst + * + * NoxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#ifndef NOX_EXCEPTIONS_H +#define NOX_EXCEPTIONS_H + +#include "platform/cpu.h" +#include "utils/string.h" + +typedef enum { + EXCEPTION_DIVISION = 0x00, + EXCEPTION_DEBUG = 0x01, + EXCEPTION_NON_MASKABLE_INTERRUPT = 0x02, + EXCEPTION_BREAKPOINT = 0x03, + EXCEPTION_OVERFLOW = 0x04, + EXCEPTION_BOUND_RANGE_EXCEEDED = 0x05, + EXCEPTION_INVALID_OPCODE = 0x06, + EXCEPTION_DEVICE_NOT_AVAILABLE = 0x07, + EXCEPTION_DOUBLE_FAULT = 0x08, + + EXCEPTION_INVALID_TSS = 0x0A, + EXCEPTION_SEGMENT_NOT_PRESENT = 0x0B, + EXCEPTION_STACK_SEGMENT_FAULT = 0x0C, + EXCEPTION_GENERAL_PROTECTION_FAULT = 0x0D, + EXCEPTION_PAGE_FAULT = 0x0E, + + EXCEPTION_X87_FLOATING_POINT = 0x10, + EXCEPTION_ALIGNMENT_CHECK = 0x11, + EXCEPTION_MACHINE_CHECK = 0x12, + EXCEPTION_SIMD_FLOATING_POINT = 0x13, + EXCEPTION_VIRTUALIZATION = 0x14, + EXCEPTION_CONTROL_PROTECTION = 0x15, + + EXCEPTION_HYPERVISOR_INJECTION = 0x1C, + EXCEPTION_VMM_COMMUNICATION = 0x1D, + EXCEPTION_SECURITY_EXCEPTION = 0x1E, + + EXCEPTIONS_ENUM_END = 0x1F // not an exception code +} exception_type_E; + +extern string_t g_exception_type_strings[EXCEPTIONS_ENUM_END]; + +cpu_state_T* exception_handle(cpu_state_T* state); + +#endif //NOX_EXCEPTIONS_H diff --git a/kernel/inc/utils/core.h b/kernel/inc/utils/core.h index 853620c..3e9dfd2 100644 --- a/kernel/inc/utils/core.h +++ b/kernel/inc/utils/core.h @@ -16,7 +16,10 @@ #ifndef NOX_CORE_H #define NOX_CORE_H -#define CORE_HALT_FOREVER while(1) { asm("hlt"); } +#include "utils/logger.h" + #define CORE_HALT_WHILE(a) while(a) { asm("hlt"); } +#define CORE_HALT_FOREVER log(LOG_WARNING, "!=====[ HALTING SYSTEM ]=====!");\ + while(1) { asm("hlt"); } #endif //NOX_CORE_H diff --git a/kernel/inc/utils/panic.h b/kernel/inc/utils/panic.h new file mode 100644 index 0000000..94e0383 --- /dev/null +++ b/kernel/inc/utils/panic.h @@ -0,0 +1,24 @@ +/* Copyright (C) Antifallobst + * + * NoxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#ifndef NOX_PANIC_H +#define NOX_PANIC_H + +#include "platform/cpu.h" +#include "utils/string.h" + +void panic(cpu_state_T* state, string_t message); + +#endif //NOX_PANIC_H diff --git a/kernel/src/kmain.c b/kernel/src/kmain.c index c19ad4d..c363875 100644 --- a/kernel/src/kmain.c +++ b/kernel/src/kmain.c @@ -37,8 +37,8 @@ void kmain(boot_info_T boot_info) { kernel_init(&boot_info); - log(LOG_DEBUG, "FIRING TEST INTERRUPT"); - asm("int $0x01"); + // this should cause a kernel panic + int x = 1312 / 0; CORE_HALT_FOREVER } diff --git a/kernel/src/platform/exceptions.c b/kernel/src/platform/exceptions.c new file mode 100644 index 0000000..3753228 --- /dev/null +++ b/kernel/src/platform/exceptions.c @@ -0,0 +1,55 @@ +/* Copyright (C) Antifallobst + * + * NoxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#include "platform/exceptions.h" +#include "utils/panic.h" + +string_t g_exception_type_strings[EXCEPTIONS_ENUM_END] = { + "Division Error", + "Debug Exception", + "Non Maskable Interrupt", + "Breakpoint Exception", + "Overflow Exception", + "Bound Range Exceeded Exception", + "Invalid Opcode", + "Device Not Available", + "Double Fault", + "[ DEPRECATED ] Coprocessor Segment Overrun", + "Invalid TSS", + "Segment Not Present", + "Stack Segment Fault", + "General Protection Fault", + "Page Fault", + "RESERVED EXCEPTION", + "x87 Floating Point Exception", + "Alignment Check", + "Machine Check", + "SIMD Floating Point Exception", + "Virtualization Exception", + "Control Protection Exception", + "RESERVED EXCEPTION", + "RESERVED EXCEPTION", + "RESERVED EXCEPTION", + "RESERVED EXCEPTION", + "RESERVED EXCEPTION", + "RESERVED EXCEPTION", + "Hypervisor Injection Exception", + "VMM Communication Exception", + "Security Exception" +}; + +cpu_state_T* exception_handle(cpu_state_T* state) { + panic(state, g_exception_type_strings[state->interrupt_id]); +} \ No newline at end of file diff --git a/kernel/src/platform/interrupts.c b/kernel/src/platform/interrupts.c index 8d5ad66..a69c76e 100644 --- a/kernel/src/platform/interrupts.c +++ b/kernel/src/platform/interrupts.c @@ -15,6 +15,7 @@ #include "platform/interrupts.h" #include "platform/cpu.h" +#include "platform/exceptions.h" #include "mm/page_frame.h" #include "utils/logger.h" @@ -118,7 +119,13 @@ void idt_init() { } cpu_state_T* interrupts_handle(cpu_state_T* state) { - log(LOG_DEBUG, "Interrupt"); - while(true)asm("hlt"); + if (state->interrupt_id < EXCEPTIONS_ENUM_END) { + return exception_handle(state); + } + + + log(LOG_WARNING, "Non exception interrupt detected"); + + return state; } diff --git a/kernel/src/utils/panic.c b/kernel/src/utils/panic.c new file mode 100644 index 0000000..51fba39 --- /dev/null +++ b/kernel/src/utils/panic.c @@ -0,0 +1,25 @@ +/* Copyright (C) Antifallobst + * + * NoxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#include "utils/panic.h" +#include "utils/logger.h" +#include "utils/core.h" + +void panic(cpu_state_T* state, string_t message) { + log(LOG_ERROR, "!=====[ KERNEL PANIC ]=====!"); + log(LOG_ERROR, message); + + CORE_HALT_FOREVER +} \ No newline at end of file