2019-04-07 12:16:53 +00:00
|
|
|
#include <kernel/utils.h>
|
2019-04-07 22:43:09 +00:00
|
|
|
#include <kernel/descriptor_tables.h>
|
2019-04-07 12:16:53 +00:00
|
|
|
|
|
|
|
struct idt_entry {
|
|
|
|
unsigned short base_low;
|
|
|
|
unsigned short selector;
|
|
|
|
unsigned char always0;
|
|
|
|
unsigned char flags;
|
|
|
|
unsigned short base_high;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct idt_ptr {
|
|
|
|
unsigned short limit;
|
|
|
|
unsigned int base;
|
2019-04-07 22:43:09 +00:00
|
|
|
} __attribute__((packed));
|
2019-04-07 12:16:53 +00:00
|
|
|
|
|
|
|
struct idt_entry idt[256]; // Interrupt table of 256 entries
|
|
|
|
struct idt_ptr idtp; // Pointer to idt table
|
|
|
|
|
|
|
|
extern void idt_load();
|
|
|
|
|
|
|
|
void idt_set_gate(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags) {
|
|
|
|
idt[num].base_low = base & 0xFFFF;
|
|
|
|
idt[num].base_high = (base >> 16) & 0xFFFF;
|
|
|
|
|
|
|
|
idt[num].selector = sel;
|
|
|
|
idt[num].always0 = 0;
|
|
|
|
|
|
|
|
// Must be uncommented when we get to usermode - it sets the permission
|
|
|
|
// level to 3
|
|
|
|
idt[num].flags = flags; // | 0x60;
|
|
|
|
}
|
|
|
|
|
|
|
|
void idt_install() {
|
|
|
|
idtp.limit = (sizeof (struct idt_entry) * 256) - 1;
|
2019-04-07 22:43:09 +00:00
|
|
|
idtp.base = (uint16_t) &idt;
|
2019-04-07 12:16:53 +00:00
|
|
|
|
|
|
|
memset(&idt, 0, sizeof(struct idt_entry) * 256);
|
|
|
|
|
2019-04-07 22:43:09 +00:00
|
|
|
idt_set_gate(0, (unsigned)isr0, 0x08, 0x8E);
|
2019-06-25 21:31:26 +00:00
|
|
|
idt_set_gate(1, (unsigned)isr1, 0x08, 0x8E);
|
|
|
|
idt_set_gate(2, (unsigned)isr2, 0x08, 0x8E);
|
|
|
|
idt_set_gate(3, (unsigned)isr3, 0x08, 0x8E);
|
|
|
|
idt_set_gate(4, (unsigned)isr4, 0x08, 0x8E);
|
|
|
|
idt_set_gate(5, (unsigned)isr5, 0x08, 0x8E);
|
|
|
|
idt_set_gate(6, (unsigned)isr6, 0x08, 0x8E);
|
|
|
|
idt_set_gate(7, (unsigned)isr7, 0x08, 0x8E);
|
2019-04-07 22:43:09 +00:00
|
|
|
|
2019-06-25 21:31:26 +00:00
|
|
|
idt_set_gate(8, (unsigned)isr8, 0x08, 0x8E);
|
|
|
|
idt_set_gate(9, (unsigned)isr9, 0x08, 0x8E);
|
|
|
|
idt_set_gate(10, (unsigned)isr10, 0x08, 0x8E);
|
|
|
|
idt_set_gate(11, (unsigned)isr11, 0x08, 0x8E);
|
|
|
|
idt_set_gate(12, (unsigned)isr12, 0x08, 0x8E);
|
|
|
|
idt_set_gate(13, (unsigned)isr13, 0x08, 0x8E);
|
|
|
|
idt_set_gate(14, (unsigned)isr14, 0x08, 0x8E);
|
|
|
|
idt_set_gate(15, (unsigned)isr15, 0x08, 0x8E);
|
2019-04-07 22:43:09 +00:00
|
|
|
|
2019-06-25 21:31:26 +00:00
|
|
|
idt_set_gate(16, (unsigned)isr16, 0x08, 0x8E);
|
|
|
|
idt_set_gate(17, (unsigned)isr17, 0x08, 0x8E);
|
|
|
|
idt_set_gate(18, (unsigned)isr18, 0x08, 0x8E);
|
|
|
|
idt_set_gate(19, (unsigned)isr19, 0x08, 0x8E);
|
|
|
|
idt_set_gate(20, (unsigned)isr20, 0x08, 0x8E);
|
|
|
|
idt_set_gate(21, (unsigned)isr21, 0x08, 0x8E);
|
|
|
|
idt_set_gate(22, (unsigned)isr22, 0x08, 0x8E);
|
|
|
|
idt_set_gate(23, (unsigned)isr23, 0x08, 0x8E);
|
2019-04-07 22:43:09 +00:00
|
|
|
|
2019-06-25 21:31:26 +00:00
|
|
|
idt_set_gate(24, (unsigned)isr24, 0x08, 0x8E);
|
|
|
|
idt_set_gate(25, (unsigned)isr25, 0x08, 0x8E);
|
|
|
|
idt_set_gate(26, (unsigned)isr26, 0x08, 0x8E);
|
|
|
|
idt_set_gate(27, (unsigned)isr27, 0x08, 0x8E);
|
|
|
|
idt_set_gate(28, (unsigned)isr28, 0x08, 0x8E);
|
|
|
|
idt_set_gate(29, (unsigned)isr29, 0x08, 0x8E);
|
|
|
|
idt_set_gate(30, (unsigned)isr30, 0x08, 0x8E);
|
|
|
|
idt_set_gate(31, (unsigned)isr31, 0x08, 0x8E);
|
2019-04-07 12:16:53 +00:00
|
|
|
|
|
|
|
idt_load();
|
2019-04-07 22:43:09 +00:00
|
|
|
irq_install();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void irq_install_handler(int irq, void (*handler)(registers_t* r)) {
|
|
|
|
irq_routines[irq] = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void irq_uninstall_handler(int irq) {
|
|
|
|
irq_routines[irq] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void irq_remap() {
|
|
|
|
outb(0x20, 0x11);
|
|
|
|
outb(0xA0, 0x11);
|
|
|
|
outb(0x21, 0x20);
|
|
|
|
outb(0xA1, 0x28);
|
|
|
|
outb(0x21, 0x04);
|
|
|
|
outb(0xA1, 0x02);
|
|
|
|
outb(0x21, 0x01);
|
|
|
|
outb(0xA1, 0x01);
|
|
|
|
outb(0x21, 0x0);
|
|
|
|
outb(0xA1, 0x0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void irq_install() {
|
|
|
|
irq_remap();
|
|
|
|
idt_set_gate(32, (unsigned)irq0, 0x08, 0x8E);
|
|
|
|
idt_set_gate(33, (unsigned)irq1, 0x08, 0x8E);
|
|
|
|
idt_set_gate(34, (unsigned)irq2, 0x08, 0x8E);
|
|
|
|
idt_set_gate(35, (unsigned)irq3, 0x08, 0x8E);
|
|
|
|
idt_set_gate(36, (unsigned)irq4, 0x08, 0x8E);
|
|
|
|
idt_set_gate(37, (unsigned)irq5, 0x08, 0x8E);
|
|
|
|
idt_set_gate(38, (unsigned)irq6, 0x08, 0x8E);
|
|
|
|
idt_set_gate(39, (unsigned)irq7, 0x08, 0x8E);
|
|
|
|
idt_set_gate(40, (unsigned)irq8, 0x08, 0x8E);
|
|
|
|
idt_set_gate(41, (unsigned)irq9, 0x08, 0x8E);
|
|
|
|
idt_set_gate(42, (unsigned)irq10, 0x08, 0x8E);
|
|
|
|
idt_set_gate(43, (unsigned)irq11, 0x08, 0x8E);
|
|
|
|
idt_set_gate(44, (unsigned)irq12, 0x08, 0x8E);
|
|
|
|
idt_set_gate(45, (unsigned)irq13, 0x08, 0x8E);
|
|
|
|
idt_set_gate(46, (unsigned)irq14, 0x08, 0x8E);
|
|
|
|
idt_set_gate(47, (unsigned)irq15, 0x08, 0x8E);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fault_handler(registers_t *r) {
|
|
|
|
if(r->int_no < 32) {
|
|
|
|
serial_print(exception_messages[r->int_no], strlen(exception_messages[r->int_no]));
|
|
|
|
serial_print(" Exception. Halting.\r\n");
|
|
|
|
for(;;);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void irq_handler(registers_t *r) {
|
|
|
|
void (*handler)(registers_t* r);
|
|
|
|
|
|
|
|
serial_print(0x3F8, "[INFO] Received IRQ: " + r->int_no);
|
|
|
|
handler = irq_routines[r->int_no - 32];
|
|
|
|
if(handler)
|
|
|
|
handler(r);
|
|
|
|
|
|
|
|
if(r->int_no > 39)
|
|
|
|
outb(0xA0, 0x20);
|
|
|
|
|
|
|
|
outb(0x20, 0x20);
|
2019-06-25 21:31:26 +00:00
|
|
|
}
|