diff --git a/kernel/interupt.c b/kernel/interupt.c index 99990a7..07e5584 100644 --- a/kernel/interupt.c +++ b/kernel/interupt.c @@ -13,14 +13,10 @@ typedef struct { uint base; } __attribute__((packed)) idtr; -void init_IDT(uint type_attr) { - __attribute__((aligned(4))) - static idt_entry IDT[256]; - static idtr IDTR; -} +__attribute__((aligned(4))) +static idt_entry IDT[IDT_MAX_DESCS]; +static idtr IDTR; -__attribute__((noreturn)) -void exception_handler(void); void exception_handler() { __asm__ __volatile__("cli; hlt"); } @@ -30,3 +26,25 @@ void interupt_handler() { // handle stuff __asm__("popa; leave; iret"); } + +void idt_set_desc(uint8 idx, void* isr, uint8 flags) { + idt_entry* desc = &IDT[idx]; // get descriptor + + desc->offset_1 = (uint) isr & 0xffff; + desc->selector = 0x05; // kernel code selector for the GDT (null is 0x0 - 0x4 => code is 0x05 to something...) + desc->reserved = 0; + desc->type_attr = flags; + desc->offset_2 = (uint) isr >> 16; +} + +extern void* isr_stub_table[]; +void idt_init() { + IDTR.base = (uintptr) &IDT[0]; // base offset + IDTR.limit = (uint16) sizeof(idt_entry) * IDT_MAX_DESCS - 1; + + for (uint8 idx = 0; idx < 32; idx++) + idt_set_desc(idx, isr_stub_table[idx], 0x8e); + + __asm__ __volatile__("lidt %0" : : "m"(IDTR)); + __asm__ __volatile__("sti"); +} diff --git a/kernel/interupt.h b/kernel/interupt.h index 189b9f6..3655d42 100644 --- a/kernel/interupt.h +++ b/kernel/interupt.h @@ -1,7 +1,12 @@ #include "../lib/types.h" +#define IDT_MAX_DESCS 256 + __attribute__((noreturn)) void exception_handler(); __attribute__((noreturn)) void interupt_handler(); + +void idt_set_desc(uint8, void*, uint8); +void idt_init(); diff --git a/kernel/interupt.asm b/kernel/isr.asm similarity index 100% rename from kernel/interupt.asm rename to kernel/isr.asm diff --git a/kernel/kernel.c b/kernel/kernel.c index a29af1d..44e8ff6 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -60,6 +60,8 @@ void print_kernel_stats() { } void kernel_init() { + idt_init(); // Enable interupts + vga_init(); // Initialize the screen first // i.e. clear the screen et cetera. diff --git a/kernel/kernel.h b/kernel/kernel.h index 7c2e537..5ccce6a 100644 --- a/kernel/kernel.h +++ b/kernel/kernel.h @@ -1,3 +1,4 @@ +#include "interupt.h" #include "memory.h" #include "paging.h" #include "../drivers/vga.h" diff --git a/lib/types.h b/lib/types.h index 47e2c4f..75d2baa 100644 --- a/lib/types.h +++ b/lib/types.h @@ -10,8 +10,9 @@ typedef unsigned char uint8; typedef short int int16; typedef unsigned short int uint16; -// MISC +// POINTERS typedef char* pointer; +typedef unsigned long int uintptr; // BOOL typedef int bool;