49 lines
1.3 KiB
C
49 lines
1.3 KiB
C
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
#include "apic.h"
|
|
#include "gdt.h"
|
|
#include "idt.h"
|
|
#include "kprintf.h"
|
|
|
|
#define PIC1 0x20 /* IO base address for master PIC */
|
|
#define PIC2 0xA0 /* IO base address for slave PIC */
|
|
#define PIC1_COMMAND PIC1
|
|
#define PIC1_DATA (PIC1 + 1)
|
|
#define PIC2_COMMAND PIC2
|
|
#define PIC2_DATA (PIC2 + 1)
|
|
|
|
extern void *isr_stub_table[];
|
|
extern void *irq_stub_table[];
|
|
|
|
__attribute__((aligned(0x10))) static struct idt_entry idt_entries[IDT_SIZE];
|
|
static struct idt_descriptor idtr;
|
|
|
|
void load_idt(struct idt_descriptor *idtr);
|
|
|
|
void idt_set_descriptor(uint8_t index, void *isr, uint8_t flags)
|
|
{
|
|
struct idt_entry *descriptor = &idt_entries[index];
|
|
|
|
descriptor->isr_low = (uint32_t)isr & 0xFFFF;
|
|
descriptor->kernel_cs = GDT_OFFSET_KERNEL_CODE;
|
|
descriptor->attributes = flags;
|
|
descriptor->isr_high = (uint32_t)isr >> 16;
|
|
descriptor->reserved = 0;
|
|
}
|
|
|
|
void init_idt(void)
|
|
{
|
|
idtr.offset = (uintptr_t)&idt_entries[0];
|
|
idtr.size = (uint16_t)sizeof(struct idt_entry) * IDT_SIZE - 1;
|
|
|
|
uint8_t i;
|
|
for (i = 0; i < 32; i++)
|
|
idt_set_descriptor(i, isr_stub_table[i], 0x8E);
|
|
pic_remap(0x20, 0x28);
|
|
for (uint8_t j = 0; j < 16; j++)
|
|
idt_set_descriptor(i + j, irq_stub_table[j], 0x8E);
|
|
load_idt(&idtr);
|
|
__asm__ volatile("sti");
|
|
}
|