From a6c1174b3fb598cd59c1668cfc4d4746ab688079 Mon Sep 17 00:00:00 2001 From: aqua Date: Fri, 1 Apr 2022 11:38:00 +0300 Subject: lidt --- arch/i686/lidt.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 arch/i686/lidt.c (limited to 'arch/i686/lidt.c') diff --git a/arch/i686/lidt.c b/arch/i686/lidt.c new file mode 100644 index 0000000..efbade3 --- /dev/null +++ b/arch/i686/lidt.c @@ -0,0 +1,46 @@ +#include +#include + +struct __attribute__((packed)) Pointer { + uint16_t limit; + uint32_t base; +}; + +enum Type { + Null = 0, + Intr = 0b10001110, // 32-bit interrupt +}; + +struct __attribute__((packed)) Gate_t { + uint16_t offset_15_0; // segment offset low + uint16_t selector; // code segment selector + uint8_t __unused; // unused in protected mode + uint8_t type; // interrupt type + uint16_t offset_31_16; // segment offset high +}; +_Static_assert(sizeof(struct Gate_t) == 8); + +void +Gate(struct Gate_t *entry, void *f, uint16_t selector) +{ + uint32_t f_addr = (uint32_t)f; + entry->offset_15_0 = f_addr & 0xffff; + entry->offset_31_16 = (f_addr >> 16) & 0xffff; + entry->selector = selector; + entry->__unused = 0; + entry->type = Intr; +} + +static struct Gate_t interrupt_table[256] __attribute((aligned(4096))); + +void +idt_install() +{ + for (int i = 0; i <= 0x2f; ++i) Gate(&interrupt_table[i], &interrupt_handler, 0x10); + Gate(&interrupt_table[0x80], &abort_handler, 0x10); + + const struct Pointer ptr = {.limit = sizeof(interrupt_table) - 1, .base = (unsigned)&interrupt_table}; + asm volatile("lidt (%0)" : : "a"(&ptr)); + + asm volatile("sti"); +} -- cgit v1.2.1