diff --git a/headers/idt.h b/headers/idt.h new file mode 100644 index 0000000..396558b --- /dev/null +++ b/headers/idt.h @@ -0,0 +1,9 @@ +#pragma once + +// doc : https://wiki.osdev.org/Interrupt_Descriptor_Table#IDTR +struct { + uint16_t size; + uint32_t offset; +} __attribute__((packed)) idt_descriptor; + +#define IDT_SIZE 256 \ No newline at end of file diff --git a/src/interrupt/idt.c b/src/interrupt/idt.c new file mode 100644 index 0000000..7145a81 --- /dev/null +++ b/src/interrupt/idt.c @@ -0,0 +1,36 @@ +#include +#include + +#include "idt.h" + +uint16_t idt_entries[IDT_SIZE * 4]; +struct idt_descriptor *idtr = (struct gdt_descriptor *)GDT_ADDRESS; + +static void set_idt_entry_value(uint16_t *target, uint32_t offset, + uint16_t selector, uint8_t dpl, + uint8_t gate_type) +{ + // Encode the offset + target[0] = offset & 0xFFFF; + target[3] = (offset >> 16) & 0xFFFF; + + // Encode the presence + target[1] |= 1 << 16; + + // Encode the CPU Privilege Levels + target[1] = (0b11 << 14) & dpl; + + target[1] &= ~(1 << 13); + + // Encode Gate Type + target[1] |= gate_type & 0x0F00; + + // Encode selector + target[2] = selector; +} + +void init_idt(void) +{ + idtr->size = 8 * IDT_SIZE - 1; + idtr->offset = (uint32_t)&idt_entries; +} \ No newline at end of file