218 lines
6.0 KiB
C
218 lines
6.0 KiB
C
#define IRQ0 32
|
|
#define IRQ1 33
|
|
#define IRQ2 34
|
|
#define IRQ3 35
|
|
#define IRQ4 36
|
|
#define IRQ5 37
|
|
#define IRQ6 38
|
|
#define IRQ7 39
|
|
#define IRQ8 40
|
|
#define IRQ9 41
|
|
#define IRQ10 42
|
|
#define IRQ11 43
|
|
#define IRQ12 44
|
|
#define IRQ13 45
|
|
#define IRQ14 46
|
|
#define IRQ15 47
|
|
|
|
//#include "asm_helper.h"
|
|
|
|
struct idt_entry_struct
|
|
{
|
|
uint16_t base_lo; // The lower 16 bits of the ISR's address
|
|
uint16_t sel; // The GDT segment selector that the CPU will load into CS before calling the ISR
|
|
uint8_t always0; // Always set to zero
|
|
uint8_t flags; // Type and attributes
|
|
uint16_t base_hi; // The higher 16 bits of the ISR's address
|
|
} __attribute__((packed));
|
|
typedef struct idt_entry_struct idt_entry_t;
|
|
|
|
struct idt_ptr_struct {
|
|
uint16_t limit;
|
|
uint32_t base;
|
|
} __attribute__((packed));
|
|
typedef struct idt_ptr_struct idt_ptr_t;
|
|
|
|
idt_entry_t idt_entries[256];
|
|
idt_ptr_t idt_ptr;
|
|
|
|
void idt_set_gate(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags) {
|
|
idt_entries[num].base_lo = base & 0xFFFF;
|
|
idt_entries[num].base_hi = (base >> 16) & 0xFFFF;
|
|
|
|
idt_entries[num].sel = sel;
|
|
idt_entries[num].always0 = 0;
|
|
|
|
idt_entries[num].flags = flags;
|
|
}
|
|
|
|
void idt_init(void);
|
|
void idt_init() {
|
|
idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1;
|
|
idt_ptr.base = (uint32_t)&idt_entries;
|
|
|
|
memset(&idt_entries, 0, sizeof(idt_entry_t)*256);
|
|
|
|
// Remap the irq table.
|
|
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);
|
|
|
|
extern int isr0;
|
|
extern int isr1;
|
|
extern int isr2;
|
|
extern int isr3;
|
|
extern int isr4;
|
|
extern int isr5;
|
|
extern int isr6;
|
|
extern int isr7;
|
|
extern int isr8;
|
|
extern int isr9;
|
|
extern int isr10;
|
|
extern int isr11;
|
|
extern int isr12;
|
|
extern int isr13;
|
|
extern int isr14;
|
|
extern int isr15;
|
|
extern int isr16;
|
|
extern int isr17;
|
|
extern int isr18;
|
|
extern int isr19;
|
|
extern int isr20;
|
|
extern int isr21;
|
|
extern int isr22;
|
|
extern int isr23;
|
|
extern int isr24;
|
|
extern int isr25;
|
|
extern int isr26;
|
|
extern int isr27;
|
|
extern int isr28;
|
|
extern int isr29;
|
|
extern int isr30;
|
|
extern int isr31;
|
|
|
|
extern int irq0;
|
|
extern int irq1;
|
|
extern int irq2;
|
|
extern int irq3;
|
|
extern int irq4;
|
|
extern int irq5;
|
|
extern int irq6;
|
|
extern int irq7;
|
|
extern int irq8;
|
|
extern int irq9;
|
|
extern int irq10;
|
|
extern int irq11;
|
|
extern int irq12;
|
|
extern int irq13;
|
|
extern int irq14;
|
|
extern int irq15;
|
|
|
|
idt_set_gate(0, (uint32_t)isr0, 0x08, 0x8E);
|
|
idt_set_gate(1, (uint32_t)isr1, 0x08, 0x8E);
|
|
idt_set_gate(2, (uint32_t)isr2, 0x08, 0x8E);
|
|
idt_set_gate(3, (uint32_t)isr3, 0x08, 0x8E);
|
|
idt_set_gate(4, (uint32_t)isr4, 0x08, 0x8E);
|
|
idt_set_gate(5, (uint32_t)isr5, 0x08, 0x8E);
|
|
idt_set_gate(6, (uint32_t)isr6, 0x08, 0x8E);
|
|
idt_set_gate(7, (uint32_t)isr7, 0x08, 0x8E);
|
|
idt_set_gate(8, (uint32_t)isr8, 0x08, 0x8E);
|
|
idt_set_gate(9, (uint32_t)isr9, 0x08, 0x8E);
|
|
idt_set_gate(10, (uint32_t)isr10, 0x08, 0x8E);
|
|
idt_set_gate(11, (uint32_t)isr11, 0x08, 0x8E);
|
|
idt_set_gate(12, (uint32_t)isr12, 0x08, 0x8E);
|
|
idt_set_gate(13, (uint32_t)isr13, 0x08, 0x8E);
|
|
idt_set_gate(14, (uint32_t)isr14, 0x08, 0x8E);
|
|
idt_set_gate(15, (uint32_t)isr15, 0x08, 0x8E);
|
|
idt_set_gate(16, (uint32_t)isr16, 0x08, 0x8E);
|
|
idt_set_gate(17, (uint32_t)isr17, 0x08, 0x8E);
|
|
idt_set_gate(18, (uint32_t)isr18, 0x08, 0x8E);
|
|
idt_set_gate(19, (uint32_t)isr19, 0x08, 0x8E);
|
|
idt_set_gate(20, (uint32_t)isr20, 0x08, 0x8E);
|
|
idt_set_gate(21, (uint32_t)isr21, 0x08, 0x8E);
|
|
idt_set_gate(22, (uint32_t)isr22, 0x08, 0x8E);
|
|
idt_set_gate(23, (uint32_t)isr23, 0x08, 0x8E);
|
|
idt_set_gate(24, (uint32_t)isr24, 0x08, 0x8E);
|
|
idt_set_gate(25, (uint32_t)isr25, 0x08, 0x8E);
|
|
idt_set_gate(26, (uint32_t)isr26, 0x08, 0x8E);
|
|
idt_set_gate(27, (uint32_t)isr27, 0x08, 0x8E);
|
|
idt_set_gate(28, (uint32_t)isr28, 0x08, 0x8E);
|
|
idt_set_gate(29, (uint32_t)isr29, 0x08, 0x8E);
|
|
idt_set_gate(30, (uint32_t)isr30, 0x08, 0x8E);
|
|
idt_set_gate(31, (uint32_t)isr31, 0x08, 0x8E);
|
|
|
|
idt_set_gate(32, (uint32_t)irq0, 0x08, 0x8E);
|
|
idt_set_gate(33, (uint32_t)irq1, 0x08, 0x8E);
|
|
idt_set_gate(34, (uint32_t)irq2, 0x08, 0x8E);
|
|
idt_set_gate(35, (uint32_t)irq3, 0x08, 0x8E);
|
|
idt_set_gate(36, (uint32_t)irq4, 0x08, 0x8E);
|
|
idt_set_gate(37, (uint32_t)irq5, 0x08, 0x8E);
|
|
idt_set_gate(38, (uint32_t)irq6, 0x08, 0x8E);
|
|
idt_set_gate(39, (uint32_t)irq7, 0x08, 0x8E);
|
|
idt_set_gate(40, (uint32_t)irq8, 0x08, 0x8E);
|
|
idt_set_gate(41, (uint32_t)irq9, 0x08, 0x8E);
|
|
idt_set_gate(42, (uint32_t)irq10, 0x08, 0x8E);
|
|
idt_set_gate(43, (uint32_t)irq11, 0x08, 0x8E);
|
|
idt_set_gate(44, (uint32_t)irq12, 0x08, 0x8E);
|
|
idt_set_gate(45, (uint32_t)irq13, 0x08, 0x8E);
|
|
idt_set_gate(46, (uint32_t)irq14, 0x08, 0x8E);
|
|
idt_set_gate(47, (uint32_t)irq15, 0x08, 0x8E);
|
|
|
|
extern void idt_flush(uint32_t);
|
|
idt_flush((uint32_t)&idt_ptr);
|
|
}
|
|
|
|
typedef struct registers
|
|
{
|
|
uint32_t ds; // Data segment selector
|
|
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; // Pushed by pusha.
|
|
uint32_t int_no, err_code; // Interrupt number and error code (if applicable)
|
|
uint32_t eip, cs, eflags, useresp, ss; // Pushed by the processor automatically.
|
|
} registers_t;
|
|
|
|
typedef void (*isr_t)(registers_t);
|
|
isr_t interrupt_handlers[256];
|
|
|
|
void isr_handler(registers_t regs)
|
|
{
|
|
printf("Received Interrupr\n");
|
|
serial_printf("Received interrupt: %s\n", regs.int_no);
|
|
|
|
if (interrupt_handlers[regs.int_no] != 0)
|
|
{
|
|
isr_t handler = interrupt_handlers[regs.int_no];
|
|
handler(regs);
|
|
}
|
|
}
|
|
|
|
void irq_handler(registers_t regs)
|
|
{
|
|
// Send an EOI (end of interrupt) signal to the PICs.
|
|
// If this interrupt involved the slave.
|
|
if (regs.int_no >= 40)
|
|
{
|
|
// Send reset signal to slave.
|
|
outb(0xA0, 0x20);
|
|
}
|
|
// Send reset signal to master. (As well as slave, if necessary).
|
|
outb(0x20, 0x20);
|
|
|
|
if (interrupt_handlers[regs.int_no] != 0)
|
|
{
|
|
isr_t handler = interrupt_handlers[regs.int_no];
|
|
handler(regs);
|
|
}
|
|
}
|
|
|
|
void register_interrupt_handler(uint8_t n, isr_t handler)
|
|
{
|
|
interrupt_handlers[n] = handler;
|
|
}
|