implemented exception handling

This commit is contained in:
antifallobst 2023-02-05 15:29:26 +01:00
parent 60cece9afb
commit d751d35a71
7 changed files with 176 additions and 5 deletions

View File

@ -0,0 +1,57 @@
/* Copyright (C) Antifallobst <antifallobst@systemausfall.org>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#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

View File

@ -16,7 +16,10 @@
#ifndef NOX_CORE_H #ifndef NOX_CORE_H
#define 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_WHILE(a) while(a) { asm("hlt"); }
#define CORE_HALT_FOREVER log(LOG_WARNING, "!=====[ HALTING SYSTEM ]=====!");\
while(1) { asm("hlt"); }
#endif //NOX_CORE_H #endif //NOX_CORE_H

24
kernel/inc/utils/panic.h Normal file
View File

@ -0,0 +1,24 @@
/* Copyright (C) Antifallobst <antifallobst@systemausfall.org>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#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

View File

@ -37,8 +37,8 @@ void kmain(boot_info_T boot_info) {
kernel_init(&boot_info); kernel_init(&boot_info);
log(LOG_DEBUG, "FIRING TEST INTERRUPT"); // this should cause a kernel panic
asm("int $0x01"); int x = 1312 / 0;
CORE_HALT_FOREVER CORE_HALT_FOREVER
} }

View File

@ -0,0 +1,55 @@
/* Copyright (C) Antifallobst <antifallobst@systemausfall.org>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#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]);
}

View File

@ -15,6 +15,7 @@
#include "platform/interrupts.h" #include "platform/interrupts.h"
#include "platform/cpu.h" #include "platform/cpu.h"
#include "platform/exceptions.h"
#include "mm/page_frame.h" #include "mm/page_frame.h"
#include "utils/logger.h" #include "utils/logger.h"
@ -118,7 +119,13 @@ void idt_init() {
} }
cpu_state_T* interrupts_handle(cpu_state_T* state) { cpu_state_T* interrupts_handle(cpu_state_T* state) {
log(LOG_DEBUG, "Interrupt"); if (state->interrupt_id < EXCEPTIONS_ENUM_END) {
while(true)asm("hlt"); return exception_handle(state);
}
log(LOG_WARNING, "Non exception interrupt detected");
return state;
} }

25
kernel/src/utils/panic.c Normal file
View File

@ -0,0 +1,25 @@
/* Copyright (C) Antifallobst <antifallobst@systemausfall.org>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#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
}