matos/core/exception_handler.c

70 lines
3.1 KiB
C
Raw Normal View History

2018-07-20 15:41:58 +02:00
#include "exception.h"
2020-04-23 23:59:57 +02:00
#include "klibc.h"
2018-07-20 15:41:58 +02:00
#include "vga.h"
2020-04-23 23:59:57 +02:00
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
2018-07-20 15:41:58 +02:00
// Need GCC > 6
2020-04-23 23:59:57 +02:00
#define DEFINE_INTERRUPT(int_nb) \
__attribute__((interrupt)) void print_handler_##int_nb(struct interrupt_frame *frame, \
ulong error_code) \
{ \
int intNbInt = int_nb; \
printStringDetails("EXCEPTION ", RED, BLACK, 0, VGA_HEIGHT - 1); \
printIntDetails(intNbInt, RED, BLACK, 11, VGA_HEIGHT - 1); \
printIntDetails(error_code, RED, BLACK, 14, VGA_HEIGHT - 1); \
printf("Exception %d (Err %d) at 0x%x\n", int_nb, error_code, frame->eip); \
2020-04-24 23:39:56 +02:00
asm("hlt"); \
2020-04-23 23:59:57 +02:00
}
2018-11-13 18:02:47 +01:00
2020-04-23 23:59:57 +02:00
DEFINE_INTERRUPT(EXCEPTION_DOUBLE_FAULT)
2020-04-24 23:39:56 +02:00
DEFINE_INTERRUPT(EXCEPTION_DIVIDE_ZERO)
DEFINE_INTERRUPT(EXCEPTION_DEBUG)
DEFINE_INTERRUPT(EXCEPTION_NMI)
DEFINE_INTERRUPT(EXCEPTION_BREAKPOINT)
DEFINE_INTERRUPT(EXCEPTION_OVERFLOW)
DEFINE_INTERRUPT(EXCEPTION_BOUND_RANGE_EXCEEDED)
DEFINE_INTERRUPT(EXCEPTION_INVALID_OPCODE)
DEFINE_INTERRUPT(EXCEPTION_DEVICE_NOT_AVAILABLE)
DEFINE_INTERRUPT(EXCEPTION_COPRO_OVERRUN)
DEFINE_INTERRUPT(EXCEPTION_INVALID_TSS)
DEFINE_INTERRUPT(EXCEPTION_SEGMENT_NOT_PRESENT)
DEFINE_INTERRUPT(EXCEPTION_STACK_SEGMENT_FAULT)
DEFINE_INTERRUPT(EXCEPTION_GENERAL_PROTECTION_FAULT)
DEFINE_INTERRUPT(EXCEPTION_PAGE_FAULT)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_1)
DEFINE_INTERRUPT(EXCEPTION_X87_FP_EXCEPTION)
DEFINE_INTERRUPT(EXCEPTION_ALIGNMENT_CHECK)
DEFINE_INTERRUPT(EXCEPTION_MACHINE_CHECK)
DEFINE_INTERRUPT(EXCEPTION_SIMD_FP)
DEFINE_INTERRUPT(EXCEPTION_VIRTUALIZATION)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_2)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_3)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_4)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_5)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_6)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_7)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_8)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_9)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_10)
DEFINE_INTERRUPT(EXCEPTION_SECURITY)
DEFINE_INTERRUPT(EXCEPTION_RESERVED_11)
2018-11-16 15:00:31 +01:00
// c.f. https://wiki.osdev.org/Paging#Handling
// error_code bit0: Present ?
// bit1: Trying to write ?
// bit2: User page try to access?
2018-11-20 17:43:19 +01:00
__attribute__((interrupt)) void pagefault_handler(struct interrupt_frame *frame,
ulong error_code)
{
// A page fault has occurred.
// The faulting address is stored in the CR2 register.
uint32_t faulting_address;
asm volatile("mov %%cr2, %0" : "=r"(faulting_address));
2018-11-13 18:02:47 +01:00
printStringDetails("PAGE FAULT", RED, BLACK, 0, VGA_HEIGHT - 1);
printIntDetails(error_code, RED, BLACK, 11, VGA_HEIGHT - 1);
2018-11-20 17:43:19 +01:00
(void)faulting_address;
2018-11-13 18:02:47 +01:00
(void)frame;
(void)error_code;
}