/* * This file deals with the Interrupt Descriptor Table. It creates a simple * IDT, with hard-coded handler functions. It is these handler functions that * can run custom handlers, registered at runtime. This file does not deal with * dispatching messages/signals to processes on an interrupt. */ #include #include #include #include #include #include #include #include "desc.h" /* Structure for an IDT Entry */ static struct IDTEntry { uint16_t offsetLower, selector; uint8_t zero, typeAttr; uint16_t offsetHigher; } __attribute__((packed)) *IDT; void (**exceptions)(struct InterruptFrame *); void (**interrupts)(struct InterruptFrame *); /* Install an IDT Entry */ static void install_idt_entry(uint8_t num, void *addr) { uint8_t type = 0x8E; /* Interrupt gate */ if (num < 32) type = 0x8F; /* Trap gate for exceptions */ IDT[num].offsetLower = (uintptr_t) addr & 0xFFFF; IDT[num].selector = 0x08; IDT[num].zero = 0; IDT[num].typeAttr = type | 0x60; /* Allowed from ring 3 */ IDT[num].offsetHigher = (uintptr_t) addr >> 16; } /* Generic ISR handler */ void isr_handler(struct InterruptFrame frame) { if (!exceptions[frame.intnum] && frame.intnum < 32) panic("[CPU#%d] Failed to handle exception %d (%#.8x) @ %#.8x", cpu->id, frame.intnum, frame.err, frame.eip); /* Run registered handler */ exc_handler_t handler = exceptions[frame.intnum]; if (handler) handler(&frame); /* Send EOI */ if (frame.intnum >= 40 && frame.intnum < 48) outb(0xA0, 0x20); if (frame.intnum >= 32 && frame.intnum < 48) outb(0x20, 0x20); /* Send APIC EOI */ if (apic) LAPIC(0xB0) = 0; } /* Register an exception handler */ void register_exception(int num, exc_handler_t addr) { if (num >= 0 && num < 256) exceptions[num] = addr; } /* Register an interrupt handler */ void register_interrupt(int num, int_handler_t addr) { if (num >= 0 && num < 16) interrupts[num] = addr; } /* Exceptions */ extern char exc0[], exc1[], exc2[], exc3[], exc4[], exc5[], exc6[], exc7[], exc8[], exc9[], exc10[], exc11[], exc12[], exc13[], exc14[], exc15[], exc16[], exc17[], exc18[], exc19[], exc20[], exc30[]; /* Interrutps */ extern char exc32[], exc33[], exc34[], exc35[], exc36[], exc37[], exc38[], exc39[], exc40[], exc41[], exc42[], exc43[], exc44[], exc45[], exc46[], exc47[]; /* System Call */ extern char exc128[]; /* Initialise the IDT */ void init_idt(void) { IDT = (void *) alloc_frame(); /* Install exceptions */ install_idt_entry(0, exc0); install_idt_entry(1, exc1); install_idt_entry(2, exc2); install_idt_entry(3, exc3); install_idt_entry(4, exc4); install_idt_entry(5, exc5); install_idt_entry(6, exc6); install_idt_entry(7, exc7); install_idt_entry(8, exc8); install_idt_entry(9, exc9); install_idt_entry(10, exc10); install_idt_entry(11, exc11); install_idt_entry(12, exc12); install_idt_entry(13, exc13); install_idt_entry(14, exc14); install_idt_entry(15, exc15); install_idt_entry(16, exc16); install_idt_entry(17, exc17); install_idt_entry(18, exc18); install_idt_entry(19, exc19); install_idt_entry(20, exc20); install_idt_entry(30, exc30); /* Install interrupts */ install_idt_entry(32, exc32); install_idt_entry(33, exc33); install_idt_entry(34, exc34); install_idt_entry(35, exc35); install_idt_entry(36, exc36); install_idt_entry(37, exc37); install_idt_entry(38, exc38); install_idt_entry(39, exc39); install_idt_entry(40, exc40); install_idt_entry(41, exc41); install_idt_entry(42, exc42); install_idt_entry(43, exc43); install_idt_entry(44, exc44); install_idt_entry(45, exc45); install_idt_entry(46, exc46); install_idt_entry(47, exc47); /* Install system call handler */ install_idt_entry(128, exc128); exceptions = (void *) (IDT + 256); interrupts = (void *) (exceptions + 32); memset(exceptions, 0, 1024); } /* Load the IDT */ void cpu_load_idt(void) { struct DescRecord ptr = { .limit = sizeof(struct IDTEntry) * 256, .base = (uintptr_t) IDT, }; asm volatile("lidt %0" :: "m" (ptr)); }