A poorly written OS for the x86 arch. (WIP)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
eOS/kernel/idt.c

59 lines
1.3 KiB

#include "idt.h"
3 years ago
3 years ago
typedef struct {
3 years ago
uint16 offset_1; // offset 0 to 15 bits
uint16 selector; // code segment sel
3 years ago
uint8 reserved; // should be zero
3 years ago
uint8 type_attr; // type & attr stuff
uint16 offset_2; // offset 16 to 31
3 years ago
} __attribute__((packed)) idt_entry;
typedef struct {
uint16 limit;
uint base;
} __attribute__((packed)) idtr;
3 years ago
3 years ago
__attribute__((aligned(16)))
3 years ago
static idt_entry IDT[IDT_MAX_DESCS];
static idtr IDTR;
3 years ago
void interupt_handler(uint interupt) {
pic_send_eoi(interupt);
3 years ago
char* buf;
3 years ago
set_cursor_pos(0, 0);
3 years ago
print("[int] ", EXC_COLOR);
buf = itoa(interupt, buf, 10);
3 years ago
print(buf, 0x0c);
3 years ago
}
3 years ago
void idt_set_desc(uint8 idx, void* isr, uint8 flags) {
idt_entry* desc = &IDT[idx]; // get descriptor
3 years ago
uint* gdt_code_ptr = 0xee88;
uint8 gdt_code = *gdt_code_ptr;
3 years ago
desc->offset_1 = (uint) isr & 0xffff;
3 years ago
desc->selector = gdt_code; // kernel code selector for the GDT
3 years ago
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);
3 years ago
/*
3 years ago
outb_w(PIC1, 0xfd);
outb_w(PIC2, 0xfd);
*/
3 years ago
__asm__ __volatile__("lidt %0" : : "m"(IDTR));
__asm__ __volatile__("sti");
3 years ago
}