Kernel: IDT cleanup GateDesctiptor usage

And move everything to IDT namespace
This commit is contained in:
Bananymous 2023-01-22 02:03:43 +02:00
parent 558374a47c
commit fbfb3d6b70
1 changed files with 114 additions and 120 deletions

View File

@ -4,39 +4,6 @@
#include <kernel/kprint.h> #include <kernel/kprint.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>
union GateDescriptor
{
struct
{
uint16_t offset_lo;
uint16_t selector;
uint8_t reserved;
uint8_t type : 4;
uint8_t zero : 1;
uint8_t dpl : 2;
uint8_t present : 1;
uint16_t offset_hi;
};
struct
{
uint32_t low;
uint32_t high;
};
} __attribute__((packed));
struct IDTR
{
uint16_t size;
void* offset;
} __attribute((packed));
static IDTR s_idtr;
static GateDescriptor s_idt[0x100];
static void (*s_irq_handlers[0x100])() { nullptr };
#define INTERRUPT_HANDLER____(i, msg) \ #define INTERRUPT_HANDLER____(i, msg) \
static void interrupt ## i () \ static void interrupt ## i () \
{ \ { \
@ -80,6 +47,34 @@ static void (*s_irq_handlers[0x100])() { nullptr };
eax, ebx, ecx, edx, esp, ebp, cr0, cr2, cr3, cr4, error_code); \ eax, ebx, ecx, edx, esp, ebp, cr0, cr2, cr3, cr4, error_code); \
} }
#define REGISTER_HANDLER(i) register_interrupt_handler(i, interrupt ## i)
namespace IDT
{
struct GateDescriptor
{
uint16_t offset1;
uint16_t selector;
uint8_t reserved;
uint8_t gate_type : 4;
uint8_t zero : 1;
uint8_t DPL : 2;
uint8_t present : 1;
uint16_t offset2;
} __attribute__((packed));
struct IDTR
{
uint16_t size;
void* offset;
} __attribute((packed));
static IDTR s_idtr;
static GateDescriptor s_idt[0x100];
static void (*s_irq_handlers[0x100])() { nullptr };
INTERRUPT_HANDLER____(0x00, "Division Error") INTERRUPT_HANDLER____(0x00, "Division Error")
INTERRUPT_HANDLER____(0x01, "Debug") INTERRUPT_HANDLER____(0x01, "Debug")
INTERRUPT_HANDLER____(0x02, "Non-maskable Interrupt") INTERRUPT_HANDLER____(0x02, "Non-maskable Interrupt")
@ -113,8 +108,6 @@ INTERRUPT_HANDLER_ERR(0x1D, "VMM Communication Exception")
INTERRUPT_HANDLER_ERR(0x1E, "Security Exception") INTERRUPT_HANDLER_ERR(0x1E, "Security Exception")
INTERRUPT_HANDLER____(0x1F, "Unkown Exception 0x1F") INTERRUPT_HANDLER____(0x1F, "Unkown Exception 0x1F")
#define REGISTER_HANDLER(i) register_interrupt_handler(i, interrupt ## i)
extern "C" void handle_irq() extern "C" void handle_irq()
{ {
uint32_t isr[8]; uint32_t isr[8];
@ -166,10 +159,6 @@ asm(
"iret;" "iret;"
); );
namespace IDT
{
static void flush_idt() static void flush_idt()
{ {
asm volatile("lidt %0"::"m"(s_idtr)); asm volatile("lidt %0"::"m"(s_idtr));
@ -177,8 +166,13 @@ namespace IDT
static void register_interrupt_handler(uint8_t index, void (*f)()) static void register_interrupt_handler(uint8_t index, void (*f)())
{ {
s_idt[index].low = 0x00080000 | ((uint32_t)(f) & 0x0000ffff); GateDescriptor& descriptor = s_idt[index];
s_idt[index].high = ((uint32_t)(f) & 0xffff0000) | 0x8e00; descriptor.offset1 = (uint32_t)f & 0xFFFF;
descriptor.selector = 0x08;
descriptor.gate_type = 0xE;
descriptor.DPL = 0;
descriptor.present = 1;
descriptor.offset2 = (uint32_t)f >> 16;
} }
void register_irq_handler(uint8_t irq, void (*f)()) void register_irq_handler(uint8_t irq, void (*f)())