2023-01-23 20:13:57 +02:00
|
|
|
#include <BAN/Errors.h>
|
2022-11-16 19:49:09 +02:00
|
|
|
#include <kernel/IDT.h>
|
2023-01-23 20:13:57 +02:00
|
|
|
#include <kernel/InterruptController.h>
|
2023-01-22 03:15:48 +02:00
|
|
|
#include <kernel/kmalloc.h>
|
2023-01-09 14:56:20 +02:00
|
|
|
#include <kernel/Panic.h>
|
2022-11-16 19:49:09 +02:00
|
|
|
|
2022-12-30 20:03:09 +02:00
|
|
|
#define INTERRUPT_HANDLER____(i, msg) \
|
|
|
|
static void interrupt ## i () \
|
|
|
|
{ \
|
|
|
|
uint32_t eax, ebx, ecx, edx; \
|
|
|
|
uint32_t esp, ebp; \
|
|
|
|
uint32_t cr0, cr2, cr3, cr4; \
|
|
|
|
asm volatile("":"=a"(eax),"=b"(ebx),"=c"(ecx),"=d"(edx)); \
|
|
|
|
asm volatile("movl %%esp, %%eax":"=a"(esp)); \
|
2023-01-13 14:39:49 +02:00
|
|
|
asm volatile("movl %%ebp, %%eax":"=a"(ebp)); \
|
2022-12-30 20:03:09 +02:00
|
|
|
asm volatile("movl %%cr0, %%eax":"=a"(cr0)); \
|
|
|
|
asm volatile("movl %%cr2, %%eax":"=a"(cr2)); \
|
|
|
|
asm volatile("movl %%cr3, %%eax":"=a"(cr3)); \
|
|
|
|
asm volatile("movl %%cr4, %%eax":"=a"(cr4)); \
|
2023-01-22 01:35:54 +02:00
|
|
|
Kernel::Panic(msg "\r\nRegister dump\r\n" \
|
2023-01-13 14:39:49 +02:00
|
|
|
"eax=0x{8H}, ebx=0x{8H}, ecx=0x{8H}, edx=0x{8H}\r\n" \
|
|
|
|
"esp=0x{8H}, ebp=0x{8H}\r\n" \
|
2023-01-22 01:08:00 +02:00
|
|
|
"CR0=0x{8H}, CR2=0x{8H}, CR3=0x{8H}, CR4=0x{8H}\r\n", \
|
2023-01-13 14:39:49 +02:00
|
|
|
eax, ebx, ecx, edx, esp, ebp, cr0, cr2, cr3, cr4); \
|
2022-11-16 19:49:09 +02:00
|
|
|
}
|
|
|
|
|
2022-12-30 20:03:09 +02:00
|
|
|
#define INTERRUPT_HANDLER_ERR(i, msg) \
|
|
|
|
static void interrupt ## i () \
|
|
|
|
{ \
|
|
|
|
uint32_t eax, ebx, ecx, edx; \
|
|
|
|
uint32_t esp, ebp; \
|
|
|
|
uint32_t cr0, cr2, cr3, cr4; \
|
|
|
|
uint32_t error_code; \
|
|
|
|
asm volatile("":"=a"(eax),"=b"(ebx),"=c"(ecx),"=d"(edx)); \
|
|
|
|
asm volatile("movl %%esp, %%eax":"=a"(esp)); \
|
2023-01-13 14:39:49 +02:00
|
|
|
asm volatile("movl %%ebp, %%eax":"=a"(ebp)); \
|
2022-12-30 20:03:09 +02:00
|
|
|
asm volatile("movl %%cr0, %%eax":"=a"(cr0)); \
|
|
|
|
asm volatile("movl %%cr2, %%eax":"=a"(cr2)); \
|
|
|
|
asm volatile("movl %%cr3, %%eax":"=a"(cr3)); \
|
|
|
|
asm volatile("movl %%cr4, %%eax":"=a"(cr4)); \
|
|
|
|
asm volatile("popl %%eax":"=a"(error_code)); \
|
2023-01-22 01:35:54 +02:00
|
|
|
Kernel::Panic(msg " (error code: 0x{8H})\r\n" \
|
|
|
|
"Register dump\r\n" \
|
2023-01-13 14:39:49 +02:00
|
|
|
"eax=0x{8H}, ebx=0x{8H}, ecx=0x{8H}, edx=0x{8H}\r\n" \
|
|
|
|
"esp=0x{8H}, ebp=0x{8H}\r\n" \
|
2023-01-22 01:35:54 +02:00
|
|
|
"CR0=0x{8H}, CR2=0x{8H}, CR3=0x{8H}, CR4=0x{8H}\r\n", \
|
2023-01-13 14:39:49 +02:00
|
|
|
eax, ebx, ecx, edx, esp, ebp, cr0, cr2, cr3, cr4, error_code); \
|
2022-12-23 15:56:10 +02:00
|
|
|
}
|
2022-12-19 11:34:42 +02:00
|
|
|
|
2022-12-07 02:41:18 +02:00
|
|
|
#define REGISTER_HANDLER(i) register_interrupt_handler(i, interrupt ## i)
|
2022-11-16 19:49:09 +02:00
|
|
|
|
2023-01-22 02:03:43 +02:00
|
|
|
namespace IDT
|
2022-11-16 19:49:09 +02:00
|
|
|
{
|
2022-12-19 11:34:42 +02:00
|
|
|
|
2023-01-22 02:03:43 +02:00
|
|
|
struct GateDescriptor
|
|
|
|
{
|
|
|
|
uint16_t offset1;
|
|
|
|
uint16_t selector;
|
2023-01-25 19:05:23 +02:00
|
|
|
uint8_t reserved : 5;
|
|
|
|
uint8_t zero1 : 3;
|
|
|
|
uint8_t type : 4;
|
|
|
|
uint8_t zero2 : 1;
|
2023-01-22 02:03:43 +02:00
|
|
|
uint8_t DPL : 2;
|
|
|
|
uint8_t present : 1;
|
|
|
|
uint16_t offset2;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct IDTR
|
2022-12-19 11:34:42 +02:00
|
|
|
{
|
2023-01-22 02:03:43 +02:00
|
|
|
uint16_t size;
|
|
|
|
void* offset;
|
|
|
|
} __attribute((packed));
|
|
|
|
|
|
|
|
static IDTR s_idtr;
|
2023-01-23 20:13:57 +02:00
|
|
|
static GateDescriptor* s_idt = nullptr;
|
2023-01-22 02:03:43 +02:00
|
|
|
|
2023-01-25 22:15:32 +02:00
|
|
|
static void(**s_irq_handlers)();
|
2023-01-22 02:03:43 +02:00
|
|
|
|
|
|
|
INTERRUPT_HANDLER____(0x00, "Division Error")
|
|
|
|
INTERRUPT_HANDLER____(0x01, "Debug")
|
|
|
|
INTERRUPT_HANDLER____(0x02, "Non-maskable Interrupt")
|
|
|
|
INTERRUPT_HANDLER____(0x03, "Breakpoint")
|
|
|
|
INTERRUPT_HANDLER____(0x04, "Overflow")
|
|
|
|
INTERRUPT_HANDLER____(0x05, "Bound Range Exception")
|
|
|
|
INTERRUPT_HANDLER____(0x06, "Invalid Opcode")
|
|
|
|
INTERRUPT_HANDLER____(0x07, "Device Not Available")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x08, "Double Fault")
|
|
|
|
INTERRUPT_HANDLER____(0x09, "Coprocessor Segment Overrun")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x0A, "Invalid TSS")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x0B, "Segment Not Present")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x0C, "Stack-Segment Fault")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x0D, "General Protection Fault")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x0E, "Page Fault")
|
|
|
|
INTERRUPT_HANDLER____(0x0F, "Unknown Exception 0x0F")
|
|
|
|
INTERRUPT_HANDLER____(0x10, "x87 Floating-Point Exception")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x11, "Alignment Check")
|
|
|
|
INTERRUPT_HANDLER____(0x12, "Machine Check")
|
|
|
|
INTERRUPT_HANDLER____(0x13, "SIMD Floating-Point Exception")
|
|
|
|
INTERRUPT_HANDLER____(0x14, "Virtualization Exception")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x15, "Control Protection Exception")
|
|
|
|
INTERRUPT_HANDLER____(0x16, "Unknown Exception 0x16")
|
|
|
|
INTERRUPT_HANDLER____(0x17, "Unknown Exception 0x17")
|
|
|
|
INTERRUPT_HANDLER____(0x18, "Unknown Exception 0x18")
|
|
|
|
INTERRUPT_HANDLER____(0x19, "Unknown Exception 0x19")
|
|
|
|
INTERRUPT_HANDLER____(0x1A, "Unknown Exception 0x1A")
|
|
|
|
INTERRUPT_HANDLER____(0x1B, "Unknown Exception 0x1B")
|
|
|
|
INTERRUPT_HANDLER____(0x1C, "Hypervisor Injection Exception")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x1D, "VMM Communication Exception")
|
|
|
|
INTERRUPT_HANDLER_ERR(0x1E, "Security Exception")
|
|
|
|
INTERRUPT_HANDLER____(0x1F, "Unkown Exception 0x1F")
|
|
|
|
|
|
|
|
extern "C" void handle_irq()
|
|
|
|
{
|
|
|
|
uint32_t isr[8];
|
2023-01-23 20:13:57 +02:00
|
|
|
InterruptController::Get().GetISR(isr);
|
2023-01-22 02:03:43 +02:00
|
|
|
|
|
|
|
uint8_t irq = 0;
|
|
|
|
for (uint8_t i = 0; i < 8; i++)
|
2022-12-19 11:34:42 +02:00
|
|
|
{
|
2023-01-22 02:03:43 +02:00
|
|
|
for (uint8_t j = 0; j < 32; j++)
|
2022-12-19 11:34:42 +02:00
|
|
|
{
|
2023-01-22 02:03:43 +02:00
|
|
|
if (isr[i] & ((uint32_t)1 << j))
|
|
|
|
{
|
|
|
|
irq = 32 * i + j;
|
|
|
|
goto found;
|
|
|
|
}
|
2022-12-19 11:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 02:03:43 +02:00
|
|
|
found:
|
|
|
|
if (irq == 0)
|
|
|
|
{
|
|
|
|
dprintln("Spurious irq");
|
|
|
|
return;
|
|
|
|
}
|
2022-11-16 19:49:09 +02:00
|
|
|
|
2023-01-22 02:03:43 +02:00
|
|
|
if (s_irq_handlers[irq])
|
|
|
|
s_irq_handlers[irq]();
|
|
|
|
else
|
2023-01-25 22:15:32 +02:00
|
|
|
{
|
|
|
|
uint32_t isr_byte = irq / 32;
|
|
|
|
uint32_t isr_bit = irq % 32;
|
|
|
|
|
|
|
|
uint32_t isr[8];
|
|
|
|
InterruptController::Get().GetISR(isr);
|
|
|
|
if (!(isr[isr_byte] & (1 << isr_bit)))
|
|
|
|
{
|
|
|
|
dprintln("spurious irq 0x{2H}", irq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dprintln("no handler for irq 0x{2H}\n", irq);
|
|
|
|
}
|
2022-12-19 11:34:42 +02:00
|
|
|
|
2023-01-23 20:13:57 +02:00
|
|
|
InterruptController::Get().EOI(irq);
|
2023-01-22 02:03:43 +02:00
|
|
|
}
|
2022-12-19 11:34:42 +02:00
|
|
|
|
2023-01-22 02:03:43 +02:00
|
|
|
extern "C" void handle_irq_common();
|
|
|
|
asm(
|
|
|
|
".globl handle_irq_common;"
|
|
|
|
"handle_irq_common:"
|
|
|
|
"pusha;"
|
|
|
|
"pushw %ds;"
|
|
|
|
"pushw %es;"
|
|
|
|
"pushw %ss;"
|
|
|
|
"pushw %ss;"
|
|
|
|
"popw %ds;"
|
|
|
|
"popw %es;"
|
|
|
|
"call handle_irq;"
|
|
|
|
"popw %es;"
|
|
|
|
"popw %ds;"
|
|
|
|
"popa;"
|
|
|
|
"iret;"
|
|
|
|
);
|
2022-12-07 02:41:18 +02:00
|
|
|
|
|
|
|
static void flush_idt()
|
|
|
|
{
|
|
|
|
asm volatile("lidt %0"::"m"(s_idtr));
|
|
|
|
}
|
|
|
|
|
2023-01-25 22:15:32 +02:00
|
|
|
static void register_interrupt_handler(uint8_t index, void(*f)())
|
2022-12-07 02:41:18 +02:00
|
|
|
{
|
2023-01-22 02:03:43 +02:00
|
|
|
GateDescriptor& descriptor = s_idt[index];
|
|
|
|
descriptor.offset1 = (uint32_t)f & 0xFFFF;
|
|
|
|
descriptor.selector = 0x08;
|
2023-01-25 19:05:23 +02:00
|
|
|
descriptor.type = 0xE;
|
2023-01-22 02:03:43 +02:00
|
|
|
descriptor.DPL = 0;
|
|
|
|
descriptor.present = 1;
|
|
|
|
descriptor.offset2 = (uint32_t)f >> 16;
|
2022-12-07 02:41:18 +02:00
|
|
|
}
|
|
|
|
|
2023-01-25 22:15:32 +02:00
|
|
|
void register_irq_handler(uint8_t irq, void(*f)())
|
2022-12-07 02:41:18 +02:00
|
|
|
{
|
2022-12-19 11:33:07 +02:00
|
|
|
s_irq_handlers[IRQ_VECTOR_BASE + irq] = f;
|
2022-12-07 02:41:18 +02:00
|
|
|
register_interrupt_handler(IRQ_VECTOR_BASE + irq, handle_irq_common);
|
2023-01-22 01:36:21 +02:00
|
|
|
flush_idt();
|
2022-12-07 02:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void initialize()
|
|
|
|
{
|
2023-01-25 19:05:23 +02:00
|
|
|
constexpr size_t idt_size = 0x100 * sizeof(GateDescriptor);
|
|
|
|
|
|
|
|
s_idt = (GateDescriptor*)kmalloc_eternal(idt_size);
|
|
|
|
memset(s_idt, 0x00, idt_size);
|
2023-01-22 03:15:48 +02:00
|
|
|
|
2023-01-25 19:05:23 +02:00
|
|
|
s_irq_handlers = (void(**)())kmalloc_eternal(0x100 * sizeof(void(*)()));
|
|
|
|
memset(s_irq_handlers, 0x00, 0x100 * sizeof(void(*)()));
|
2023-01-22 03:15:48 +02:00
|
|
|
|
2022-12-07 02:41:18 +02:00
|
|
|
s_idtr.offset = s_idt;
|
2023-01-25 19:05:23 +02:00
|
|
|
s_idtr.size = idt_size - 1;
|
2022-12-07 02:41:18 +02:00
|
|
|
|
2023-01-23 20:13:57 +02:00
|
|
|
for (uint8_t i = 0x00; i <= 0xFF - IRQ_VECTOR_BASE; i++)
|
2022-12-19 11:33:07 +02:00
|
|
|
register_irq_handler(i, nullptr);
|
2022-12-07 02:41:18 +02:00
|
|
|
|
|
|
|
REGISTER_HANDLER(0x00);
|
|
|
|
REGISTER_HANDLER(0x01);
|
|
|
|
REGISTER_HANDLER(0x02);
|
|
|
|
REGISTER_HANDLER(0x03);
|
|
|
|
REGISTER_HANDLER(0x04);
|
|
|
|
REGISTER_HANDLER(0x05);
|
|
|
|
REGISTER_HANDLER(0x06);
|
|
|
|
REGISTER_HANDLER(0x07);
|
|
|
|
REGISTER_HANDLER(0x08);
|
|
|
|
REGISTER_HANDLER(0x09);
|
2022-12-19 11:33:07 +02:00
|
|
|
REGISTER_HANDLER(0x0A);
|
|
|
|
REGISTER_HANDLER(0x0B);
|
|
|
|
REGISTER_HANDLER(0x0C);
|
|
|
|
REGISTER_HANDLER(0x0D);
|
|
|
|
REGISTER_HANDLER(0x0E);
|
|
|
|
REGISTER_HANDLER(0x0F);
|
2022-12-07 02:41:18 +02:00
|
|
|
REGISTER_HANDLER(0x10);
|
2022-12-19 11:33:07 +02:00
|
|
|
REGISTER_HANDLER(0x11);
|
|
|
|
REGISTER_HANDLER(0x12);
|
|
|
|
REGISTER_HANDLER(0x13);
|
|
|
|
REGISTER_HANDLER(0x14);
|
|
|
|
REGISTER_HANDLER(0x15);
|
|
|
|
REGISTER_HANDLER(0x16);
|
|
|
|
REGISTER_HANDLER(0x17);
|
|
|
|
REGISTER_HANDLER(0x18);
|
|
|
|
REGISTER_HANDLER(0x19);
|
|
|
|
REGISTER_HANDLER(0x1A);
|
|
|
|
REGISTER_HANDLER(0x1B);
|
|
|
|
REGISTER_HANDLER(0x1C);
|
|
|
|
REGISTER_HANDLER(0x1D);
|
|
|
|
REGISTER_HANDLER(0x1E);
|
|
|
|
REGISTER_HANDLER(0x1F);
|
2022-12-07 02:41:18 +02:00
|
|
|
|
|
|
|
flush_idt();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|