Files
Espresso/drivers/idt.c

196 lines
5.4 KiB
C
Raw Normal View History

2025-06-13 18:03:39 -05:00
#include <stdio.h>
2025-05-28 14:41:02 -05:00
#include <port_io.h>
2026-03-20 16:57:08 -05:00
#include <scheduler.h>
#include <kernel/syscall.h>
#include <drivers/irq.h>
2025-06-13 18:03:39 -05:00
#include <drivers/idt.h>
2025-05-28 14:41:02 -05:00
2025-06-13 18:03:39 -05:00
#define IDT_MAX_DESCRIPTORS 256
2025-05-28 14:41:02 -05:00
2025-06-13 18:03:39 -05:00
#define PIC1_COMMAND 0x20
#define PIC1_DATA 0x21
#define PIC2_COMMAND 0xA0
#define PIC2_DATA 0xA1
2025-05-28 14:41:02 -05:00
2025-06-13 18:03:39 -05:00
/*
Most of the code is this file (and idt.h) are taken from the osdev wiki: https://wiki.osdev.org/Interrupts_Tutorial, though not all of it.
*/
typedef struct {
2025-07-01 20:39:38 -05:00
uint16_t isr_low; // The lower 16 bits of the ISR's address
uint16_t kernel_cs; // The GDT segment selector that the CPU will load into CS before calling the ISR
uint8_t reserved; // Set to zero
uint8_t attributes; // Type and attributes; see the IDT page
uint16_t isr_high; // The higher 16 bits of the ISR's address
2025-06-13 18:03:39 -05:00
} __attribute__((packed)) idt_entry_t;
typedef struct {
2025-07-01 20:39:38 -05:00
uint16_t limit;
uint32_t base;
2025-06-13 18:03:39 -05:00
} __attribute__((packed)) idtr_t;
__attribute__((aligned(0x10)))
2026-03-20 16:57:08 -05:00
static idt_entry_t idt[256]; /* create an array of IDT entries; aligned for performance */
2025-06-13 18:03:39 -05:00
static idtr_t idtr;
static bool vectors[IDT_MAX_DESCRIPTORS];
extern void* isr_stub_table[];
2026-03-20 16:57:08 -05:00
void idt_init(void)
{
idtr.base = (uintptr_t)&idt[0];
idtr.limit = (uint16_t)sizeof(idt_entry_t) * IDT_MAX_DESCRIPTORS - 1;
for (uint8_t vector = 0; vector < 32; vector++)
{
idt_set_descriptor(vector, isr_stub_table[vector], 0x8E);
vectors[vector] = true;
}
extern void* irq_stub_table[];
for (uint8_t i = 0; i < 20; i++)
{
idt_set_descriptor(32 + i, irq_stub_table[i], 0x8E);
}
asm volatile ("lidt %0" : : "m"(idtr)); /* load the new IDT */
//asm volatile ("sti"); /* set the interrupt flag */
}
registers_t* interrupt_dispatcher(registers_t* regs)
{
if (regs->int_no < 32)
{
printf("external: %s, IDT/GDT: %s, ", ((regs->err_code & 0x0001) ? "true" : "false"), (regs->err_code & 0x0006) ? "IDT" : "GDT");
printf("LDT: %s, selector: %x (%i)\n", ((regs->err_code & 0x0006) == 0b10) ? "true" : "false", regs->err_code & 0xFFF8, regs->err_code & 0xFFF8);
printf("int: %i (%x), err: %i (%x)\n", regs->int_no, regs->int_no, regs->err_code, regs->err_code);
exception_handler(regs);
}
else if (regs->int_no < 52)
{
uint32_t irq = regs->int_no - 32;
regs = irq_handler(irq, regs);
if (irq >= 8)
{
outb(0xA0, 0x20); /* acknowledge the IRQ to slave PIC */
}
outb(0x20, 0x20); /* acknowledge the IRQ to master PIC */
}
return regs;
}
2025-06-13 18:03:39 -05:00
2026-03-20 16:57:08 -05:00
__noreturn
void exception_handler(registers_t* regs)
2025-05-28 14:41:02 -05:00
{
2026-03-20 16:57:08 -05:00
uint32_t int_no = regs->int_no;
uint32_t err_code = regs->err_code;
2025-06-13 18:03:39 -05:00
switch (int_no)
{
case 0:
2026-03-20 16:57:08 -05:00
printf("Divide by zero exception (or other division error)\n");
break;
case 2:
printf("NMI encountered\n");
break;
case 6: /* XXX: NOTE: this can be used to emulate instructions that do not exist on the current CPU :NOTE :XXX */
printf("Invalid opcode encountered at %p\n", regs->eip);
break;
case 7: /* XXX: NOTE: use this for FPU emulation and for saving/restoring FPU registers in a multiprocessing enviroment :NOTE :XXX */
printf("FPU instructions used, but FPU is nonexistant/disabled\n");
break;
case 8: /* double fault */
printf("Double fault at %p, err %i\n", regs->eip, regs->err_code);
2025-06-13 18:03:39 -05:00
break;
case 13:
2026-03-20 16:57:08 -05:00
printf("General Protection Fault: err=0x%x at %p\n", err_code, regs->eip);
2025-06-13 18:03:39 -05:00
break;
case 14:
{
uint32_t cr2;
2025-06-27 14:48:06 -05:00
asm volatile ("mov %%cr2, %0" : "=r"(cr2));
2026-03-20 16:57:08 -05:00
printf("PF addr=%p eip=%p err=%x\n", cr2, regs->eip, err_code);
//printf("Page Fault at address: 0x%x, err=0x%x\n", cr2, err_code);
2025-06-13 18:03:39 -05:00
break;
}
default:
2026-03-20 16:57:08 -05:00
printf("Unhandled exception #%u, err=0x%x at %p\n", int_no, err_code, regs->eip);
2025-06-13 18:03:39 -05:00
break;
}
uint16_t cs, ds, es, ss;
asm volatile ("mov %%cs, %0" : "=r"(cs));
asm volatile ("mov %%ds, %0" : "=r"(ds));
asm volatile ("mov %%es, %0" : "=r"(es));
asm volatile ("mov %%ss, %0" : "=r"(ss));
2026-03-20 16:57:08 -05:00
if (((uint16_t) regs->cs != cs) || ((uint16_t) regs->ds != ds) || ((uint16_t) regs->es != es))
{
printf("segment register mismatch!\n");
}
printf("cs: 0x%x, ds: 0x%x, es: 0x%x,\nss: 0x%x, fs: 0x%x, gs: 0x%x\n", regs->cs, regs->ds, regs->es, ss, regs->fs, regs->gs);
2025-06-13 18:03:39 -05:00
2025-06-27 14:48:06 -05:00
asm volatile ("cli; hlt");
/* Will never be reached */
while (true)
{
asm volatile ("hlt" ::: "memory");
}
2025-06-13 18:03:39 -05:00
}
2025-07-01 20:39:38 -05:00
void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags)
{
2025-06-13 18:03:39 -05:00
idt_entry_t* descriptor = &idt[vector];
2026-03-20 16:57:08 -05:00
descriptor->isr_low = (uint32_t) isr & 0xFFFF;
descriptor->kernel_cs = 0x08;
descriptor->attributes = flags;
descriptor->isr_high = (uint32_t) isr >> 16;
descriptor->reserved = 0;
2025-05-28 14:41:02 -05:00
}
void pic_remap(void)
{
2025-06-13 18:03:39 -05:00
uint8_t a1, a2;
2025-05-28 14:41:02 -05:00
2026-03-20 16:57:08 -05:00
/* save masks */
2025-06-13 18:03:39 -05:00
a1 = inb(PIC1_DATA);
a2 = inb(PIC2_DATA);
2026-03-20 16:57:08 -05:00
/* start initialization sequence (in cascade mode) */
2025-06-13 18:03:39 -05:00
outb(PIC1_COMMAND, 0x11);
outb(PIC2_COMMAND, 0x11);
2026-03-20 16:57:08 -05:00
/* set vector offset */
2025-06-27 14:48:06 -05:00
outb(PIC1_DATA, 0x20); /* IRQs 0-7 mapped to IDT entries 0x20-0x27 (3239) */
outb(PIC2_DATA, 0x28); /* IRQs 8-15 mapped to IDT entries 0x28-0x2F (4047) */
2025-06-13 18:03:39 -05:00
2026-03-20 16:57:08 -05:00
/* tell the master PIC about Slave PIC at IRQ2 (0000 0100) */
2025-06-13 18:03:39 -05:00
outb(PIC1_DATA, 0x04);
2026-03-20 16:57:08 -05:00
/* tell the slave PIC its cascade identity (0000 0010) */
2025-06-13 18:03:39 -05:00
outb(PIC2_DATA, 0x02);
2025-05-28 14:41:02 -05:00
2026-03-20 16:57:08 -05:00
/* set 8086/88 mode */
2025-06-13 18:03:39 -05:00
outb(PIC1_DATA, 0x01);
outb(PIC2_DATA, 0x01);
2026-03-20 16:57:08 -05:00
/* restore saved masks */
2025-06-13 18:03:39 -05:00
outb(PIC1_DATA, a1);
outb(PIC2_DATA, a2);
2025-05-28 14:41:02 -05:00
}