2023-01-25 19:05:47 +02:00
|
|
|
#include <BAN/Errors.h>
|
|
|
|
#include <kernel/IDT.h>
|
|
|
|
#include <kernel/InterruptController.h>
|
2023-04-14 13:30:21 +03:00
|
|
|
#include <kernel/Memory/kmalloc.h>
|
2023-01-25 19:05:47 +02:00
|
|
|
#include <kernel/Panic.h>
|
2023-05-06 00:10:15 +03:00
|
|
|
#include <kernel/Process.h>
|
2023-04-03 01:51:05 +03:00
|
|
|
#include <kernel/Scheduler.h>
|
2023-01-25 19:05:47 +02:00
|
|
|
|
|
|
|
#define REGISTER_ISR_HANDLER(i) register_interrupt_handler(i, isr ## i)
|
|
|
|
#define REGISTER_IRQ_HANDLER(i) register_interrupt_handler(IRQ_VECTOR_BASE + i, irq ## i)
|
|
|
|
|
|
|
|
namespace IDT
|
|
|
|
{
|
|
|
|
|
2023-01-30 18:52:38 +02:00
|
|
|
struct Registers
|
|
|
|
{
|
|
|
|
uint64_t rsp;
|
|
|
|
uint64_t rip;
|
|
|
|
uint64_t rflags;
|
|
|
|
uint64_t cr4;
|
|
|
|
uint64_t cr3;
|
|
|
|
uint64_t cr2;
|
|
|
|
uint64_t cr0;
|
|
|
|
|
|
|
|
uint64_t r15;
|
|
|
|
uint64_t r14;
|
|
|
|
uint64_t r13;
|
|
|
|
uint64_t r12;
|
|
|
|
uint64_t r11;
|
|
|
|
uint64_t r10;
|
|
|
|
uint64_t r9;
|
|
|
|
uint64_t r8;
|
|
|
|
uint64_t rsi;
|
|
|
|
uint64_t rdi;
|
|
|
|
uint64_t rbp;
|
|
|
|
uint64_t rdx;
|
|
|
|
uint64_t rcx;
|
|
|
|
uint64_t rbx;
|
|
|
|
uint64_t rax;
|
|
|
|
};
|
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
struct GateDescriptor
|
|
|
|
{
|
|
|
|
uint16_t offset1;
|
|
|
|
uint16_t selector;
|
|
|
|
uint8_t IST;
|
|
|
|
uint8_t flags;
|
|
|
|
uint16_t offset2;
|
|
|
|
uint32_t offset3;
|
|
|
|
uint32_t reserved;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct IDTR
|
|
|
|
{
|
|
|
|
uint16_t size;
|
|
|
|
uint64_t offset;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
static IDTR s_idtr;
|
|
|
|
static GateDescriptor* s_idt = nullptr;
|
|
|
|
|
2023-01-25 22:15:32 +02:00
|
|
|
static void(*s_irq_handlers[0x10])() { nullptr };
|
2023-01-25 19:05:47 +02:00
|
|
|
|
|
|
|
static const char* isr_exceptions[] =
|
|
|
|
{
|
|
|
|
"Division Error",
|
|
|
|
"Debug",
|
|
|
|
"Non-maskable Interrupt",
|
|
|
|
"Breakpoint",
|
|
|
|
"Overflow",
|
|
|
|
"Bound Range Exception",
|
|
|
|
"Invalid Opcode",
|
|
|
|
"Device Not Available",
|
|
|
|
"Double Fault",
|
|
|
|
"Coprocessor Segment Overrun",
|
|
|
|
"Invalid TSS",
|
|
|
|
"Segment Not Present",
|
|
|
|
"Stack-Segment Fault",
|
|
|
|
"General Protection Fault",
|
|
|
|
"Page Fault",
|
|
|
|
"Unknown Exception 0x0F",
|
|
|
|
"x87 Floating-Point Exception",
|
|
|
|
"Alignment Check",
|
|
|
|
"Machine Check",
|
|
|
|
"SIMD Floating-Point Exception",
|
|
|
|
"Virtualization Exception",
|
|
|
|
"Control Protection Exception",
|
|
|
|
"Unknown Exception 0x16",
|
|
|
|
"Unknown Exception 0x17",
|
|
|
|
"Unknown Exception 0x18",
|
|
|
|
"Unknown Exception 0x19",
|
|
|
|
"Unknown Exception 0x1A",
|
|
|
|
"Unknown Exception 0x1B",
|
|
|
|
"Hypervisor Injection Exception",
|
|
|
|
"VMM Communication Exception",
|
|
|
|
"Security Exception",
|
|
|
|
"Unkown Exception 0x1F",
|
|
|
|
};
|
|
|
|
|
2023-01-30 18:52:38 +02:00
|
|
|
extern "C" void cpp_isr_handler(uint64_t isr, uint64_t error, const Registers* regs)
|
2023-01-25 19:05:47 +02:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
Kernel::panic(
|
2023-05-06 00:10:15 +03:00
|
|
|
"{} (error code: 0x{16H}), pid {}, tid {}\r\n"
|
2023-01-25 19:05:47 +02:00
|
|
|
"Register dump\r\n"
|
|
|
|
"rax=0x{16H}, rbx=0x{16H}, rcx=0x{16H}, rdx=0x{16H}\r\n"
|
2023-01-30 18:52:38 +02:00
|
|
|
"rsp=0x{16H}, rbp=0x{16H}, rdi=0x{16H}, rsi=0x{16H}\r\n"
|
|
|
|
"rip=0x{16H}, rflags=0x{16H}\r\n"
|
|
|
|
"cr0=0x{16H}, cr2=0x{16H}, cr3=0x{16H}, cr4=0x{16H}\r\n",
|
2023-05-06 00:10:15 +03:00
|
|
|
isr_exceptions[isr], error, Kernel::Process::current().pid(), Kernel::Thread::current().tid(),
|
2023-01-30 18:52:38 +02:00
|
|
|
regs->rax, regs->rbx, regs->rcx, regs->rdx,
|
|
|
|
regs->rsp, regs->rbp, regs->rdi, regs->rsi,
|
|
|
|
regs->rip, regs->rflags,
|
|
|
|
regs->cr0, regs->cr2, regs->cr3, regs->cr4
|
2023-01-25 19:05:47 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void cpp_irq_handler(uint64_t irq)
|
|
|
|
{
|
|
|
|
if (s_irq_handlers[irq])
|
|
|
|
s_irq_handlers[irq]();
|
|
|
|
else
|
2023-01-25 22:15:32 +02:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
if (!InterruptController::get().is_in_service(irq))
|
2023-01-25 22:15:32 +02:00
|
|
|
{
|
|
|
|
dprintln("spurious irq 0x{2H}", irq);
|
|
|
|
return;
|
|
|
|
}
|
2023-01-25 19:05:47 +02:00
|
|
|
dprintln("no handler for irq 0x{2H}\n", irq);
|
2023-01-25 22:15:32 +02:00
|
|
|
}
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-02-19 17:53:29 +02:00
|
|
|
// NOTE: Scheduler sends PIT eoi's
|
|
|
|
if (irq != PIT_IRQ)
|
|
|
|
InterruptController::get().eoi(irq);
|
2023-04-03 01:51:05 +03:00
|
|
|
|
|
|
|
Kernel::Scheduler::get().reschedule_if_idling();
|
2023-01-25 19:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flush_idt()
|
|
|
|
{
|
|
|
|
asm volatile("lidt %0"::"m"(s_idtr));
|
|
|
|
}
|
|
|
|
|
2023-01-30 18:52:38 +02:00
|
|
|
static void register_interrupt_handler(uint8_t index, void(*handler)())
|
2023-01-25 19:05:47 +02:00
|
|
|
{
|
|
|
|
GateDescriptor& descriptor = s_idt[index];
|
2023-01-30 18:52:38 +02:00
|
|
|
descriptor.offset1 = (uint16_t)((uint64_t)handler >> 0);
|
|
|
|
descriptor.offset2 = (uint16_t)((uint64_t)handler >> 16);
|
|
|
|
descriptor.offset3 = (uint32_t)((uint64_t)handler >> 32);
|
2023-01-25 19:05:47 +02:00
|
|
|
|
|
|
|
descriptor.selector = 0x08;
|
|
|
|
descriptor.IST = 0;
|
|
|
|
descriptor.flags = 0x8E;
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:32:46 +02:00
|
|
|
static void register_syscall_handler(uint8_t index, void(*handler)())
|
|
|
|
{
|
|
|
|
register_interrupt_handler(index, handler);
|
|
|
|
s_idt[index].flags = 0xEE;
|
|
|
|
}
|
|
|
|
|
2023-01-30 18:52:38 +02:00
|
|
|
void register_irq_handler(uint8_t irq, void(*handler)())
|
2023-01-25 19:05:47 +02:00
|
|
|
{
|
2023-01-30 18:52:38 +02:00
|
|
|
s_irq_handlers[irq] = handler;
|
2023-01-25 19:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void isr0();
|
|
|
|
extern "C" void isr1();
|
|
|
|
extern "C" void isr2();
|
|
|
|
extern "C" void isr3();
|
|
|
|
extern "C" void isr4();
|
|
|
|
extern "C" void isr5();
|
|
|
|
extern "C" void isr6();
|
|
|
|
extern "C" void isr7();
|
|
|
|
extern "C" void isr8();
|
|
|
|
extern "C" void isr9();
|
|
|
|
extern "C" void isr10();
|
|
|
|
extern "C" void isr11();
|
|
|
|
extern "C" void isr12();
|
|
|
|
extern "C" void isr13();
|
|
|
|
extern "C" void isr14();
|
|
|
|
extern "C" void isr15();
|
|
|
|
extern "C" void isr16();
|
|
|
|
extern "C" void isr17();
|
|
|
|
extern "C" void isr18();
|
|
|
|
extern "C" void isr19();
|
|
|
|
extern "C" void isr20();
|
|
|
|
extern "C" void isr21();
|
|
|
|
extern "C" void isr22();
|
|
|
|
extern "C" void isr23();
|
|
|
|
extern "C" void isr24();
|
|
|
|
extern "C" void isr25();
|
|
|
|
extern "C" void isr26();
|
|
|
|
extern "C" void isr27();
|
|
|
|
extern "C" void isr28();
|
|
|
|
extern "C" void isr29();
|
|
|
|
extern "C" void isr30();
|
|
|
|
extern "C" void isr31();
|
|
|
|
|
|
|
|
extern "C" void irq0();
|
|
|
|
extern "C" void irq1();
|
|
|
|
extern "C" void irq2();
|
|
|
|
extern "C" void irq3();
|
|
|
|
extern "C" void irq4();
|
|
|
|
extern "C" void irq5();
|
|
|
|
extern "C" void irq6();
|
|
|
|
extern "C" void irq7();
|
|
|
|
extern "C" void irq8();
|
|
|
|
extern "C" void irq9();
|
|
|
|
extern "C" void irq10();
|
|
|
|
extern "C" void irq11();
|
|
|
|
extern "C" void irq12();
|
|
|
|
extern "C" void irq13();
|
|
|
|
extern "C" void irq14();
|
|
|
|
extern "C" void irq15();
|
|
|
|
|
2023-03-13 15:32:46 +02:00
|
|
|
extern "C" void syscall_asm();
|
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
void initialize()
|
|
|
|
{
|
2023-02-22 16:18:14 +02:00
|
|
|
s_idt = (GateDescriptor*)kmalloc(0x100 * sizeof(GateDescriptor));
|
2023-02-22 21:48:40 +02:00
|
|
|
ASSERT(s_idt);
|
2023-01-25 19:05:47 +02:00
|
|
|
memset(s_idt, 0x00, 0x100 * sizeof(GateDescriptor));
|
|
|
|
|
|
|
|
s_idtr.offset = (uint64_t)s_idt;
|
|
|
|
s_idtr.size = 0x100 * sizeof(GateDescriptor) - 1;
|
|
|
|
|
|
|
|
REGISTER_ISR_HANDLER(0);
|
|
|
|
REGISTER_ISR_HANDLER(1);
|
|
|
|
REGISTER_ISR_HANDLER(2);
|
|
|
|
REGISTER_ISR_HANDLER(3);
|
|
|
|
REGISTER_ISR_HANDLER(4);
|
|
|
|
REGISTER_ISR_HANDLER(5);
|
|
|
|
REGISTER_ISR_HANDLER(6);
|
|
|
|
REGISTER_ISR_HANDLER(7);
|
|
|
|
REGISTER_ISR_HANDLER(8);
|
|
|
|
REGISTER_ISR_HANDLER(9);
|
|
|
|
REGISTER_ISR_HANDLER(10);
|
|
|
|
REGISTER_ISR_HANDLER(11);
|
|
|
|
REGISTER_ISR_HANDLER(12);
|
|
|
|
REGISTER_ISR_HANDLER(13);
|
|
|
|
REGISTER_ISR_HANDLER(14);
|
|
|
|
REGISTER_ISR_HANDLER(15);
|
|
|
|
REGISTER_ISR_HANDLER(16);
|
|
|
|
REGISTER_ISR_HANDLER(17);
|
|
|
|
REGISTER_ISR_HANDLER(18);
|
|
|
|
REGISTER_ISR_HANDLER(19);
|
|
|
|
REGISTER_ISR_HANDLER(20);
|
|
|
|
REGISTER_ISR_HANDLER(21);
|
|
|
|
REGISTER_ISR_HANDLER(22);
|
|
|
|
REGISTER_ISR_HANDLER(23);
|
|
|
|
REGISTER_ISR_HANDLER(24);
|
|
|
|
REGISTER_ISR_HANDLER(25);
|
|
|
|
REGISTER_ISR_HANDLER(26);
|
|
|
|
REGISTER_ISR_HANDLER(27);
|
|
|
|
REGISTER_ISR_HANDLER(28);
|
|
|
|
REGISTER_ISR_HANDLER(29);
|
|
|
|
REGISTER_ISR_HANDLER(30);
|
|
|
|
REGISTER_ISR_HANDLER(31);
|
|
|
|
|
|
|
|
REGISTER_IRQ_HANDLER(0);
|
|
|
|
REGISTER_IRQ_HANDLER(1);
|
|
|
|
REGISTER_IRQ_HANDLER(2);
|
|
|
|
REGISTER_IRQ_HANDLER(3);
|
|
|
|
REGISTER_IRQ_HANDLER(4);
|
|
|
|
REGISTER_IRQ_HANDLER(5);
|
|
|
|
REGISTER_IRQ_HANDLER(6);
|
|
|
|
REGISTER_IRQ_HANDLER(7);
|
|
|
|
REGISTER_IRQ_HANDLER(8);
|
|
|
|
REGISTER_IRQ_HANDLER(9);
|
|
|
|
REGISTER_IRQ_HANDLER(10);
|
|
|
|
REGISTER_IRQ_HANDLER(11);
|
|
|
|
REGISTER_IRQ_HANDLER(12);
|
|
|
|
REGISTER_IRQ_HANDLER(13);
|
|
|
|
REGISTER_IRQ_HANDLER(14);
|
|
|
|
REGISTER_IRQ_HANDLER(15);
|
|
|
|
|
2023-03-13 15:32:46 +02:00
|
|
|
register_syscall_handler(0x80, syscall_asm);
|
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
flush_idt();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|