feat: add interrupts handling, PIC and PIT
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
#include "./arch.hpp"
|
||||
|
||||
#include "arch/x86_64/int.hpp"
|
||||
#include "runtime.hpp" // IWYU pragma: keep
|
||||
|
||||
arch& arch::get() {
|
||||
return x86_64::arch::get();
|
||||
}
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
arch::arch() {}
|
||||
|
||||
arch& arch::get() {
|
||||
static x86_64::arch s_arch;
|
||||
return s_arch;
|
||||
@@ -16,6 +21,19 @@ void arch::init() {
|
||||
reload_segments(gdt::KERNEL_CS, gdt::KERNEL_DS);
|
||||
|
||||
m_idt.load();
|
||||
|
||||
m_picController.remap(0x20, 0x28);
|
||||
|
||||
static constexpr hwirq_t PIT_HWIRQ = 0x00;
|
||||
|
||||
m_picController.enable(PIT_HWIRQ);
|
||||
|
||||
irq_t pitIrq = m_irqManager.allocate();
|
||||
m_irqManager.bind(pitIrq, m_picController, PIT_HWIRQ, 0x20);
|
||||
m_irqManager.register_handler(pitIrq, pit::handler, &m_pit);
|
||||
m_irqManager.enable(pitIrq);
|
||||
|
||||
asm volatile("sti");
|
||||
}
|
||||
|
||||
} // namespace x86_64
|
||||
|
||||
@@ -5,11 +5,16 @@
|
||||
#include "./idt.hpp"
|
||||
#include "./task.hpp"
|
||||
#include "arch/arch.hpp"
|
||||
#include "arch/x86_64/int.hpp"
|
||||
#include "arch/x86_64/memory.hpp"
|
||||
#include "arch/x86_64/pic.hpp"
|
||||
#include "arch/x86_64/pit.hpp"
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
class arch : public ::arch {
|
||||
private:
|
||||
arch();
|
||||
public:
|
||||
static arch& get();
|
||||
public:
|
||||
@@ -17,7 +22,11 @@ public:
|
||||
public:
|
||||
pmm& get_pmm() { return m_pmm; }
|
||||
paging& get_paging() { return m_paging; }
|
||||
// vm& get_vm() { return m_vm; }
|
||||
vm& get_vm() { return m_vm; }
|
||||
public:
|
||||
irq_manager& get_irq_manager() { return m_irqManager; }
|
||||
|
||||
pic& get_pic() { return m_picController; }
|
||||
private:
|
||||
tss m_tss{};
|
||||
gdt m_gdt{ m_tss };
|
||||
@@ -26,7 +35,14 @@ private:
|
||||
// Memory
|
||||
pmm m_pmm;
|
||||
paging m_paging{ m_pmm };
|
||||
// vm m_vm;
|
||||
vm m_vm{ m_pmm };
|
||||
|
||||
// Interrupts
|
||||
pic m_picController;
|
||||
|
||||
pit m_pit;
|
||||
|
||||
irq_manager m_irqManager{ m_vm };
|
||||
};
|
||||
|
||||
} // namespace x86_64
|
||||
|
||||
@@ -14,11 +14,11 @@ extern "C" void exception_handler(void) {
|
||||
asm volatile("cli; hlt");
|
||||
}
|
||||
|
||||
extern "C" void* isrStubTable[32];
|
||||
extern "C" void* isrStubTable[256];
|
||||
|
||||
idt::idt()
|
||||
: entries() {
|
||||
for (u8 vector = 0; vector < 32; ++vector) {
|
||||
for (u32 vector = 0; vector < 256; ++vector) {
|
||||
entries[vector] = idt_entry{
|
||||
static_cast<u16>(reinterpret_cast<u64>(isrStubTable[vector]) & 0xFFFF),
|
||||
gdt::KERNEL_CS,
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "int.hpp"
|
||||
|
||||
#include "arch/x86_64/arch.hpp"
|
||||
#include "arch/x86_64/memory.hpp"
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
extern "C" void interrupt_handler(interrupt_frame& frame) {
|
||||
if (frame.vector < 0x20) {
|
||||
} else {
|
||||
arch::get().get_irq_manager().dispatch(frame);
|
||||
}
|
||||
}
|
||||
|
||||
irq_manager::irq_manager(vm& vm)
|
||||
: m_vm(&vm), m_vectorMap() {
|
||||
m_head = m_tail = nullptr;
|
||||
|
||||
for (u64 i = 0; i < 0x100; ++i) {
|
||||
m_vectorMap[i] = INVALID_IRQ;
|
||||
}
|
||||
}
|
||||
|
||||
irq_t irq_manager::allocate() {
|
||||
auto* entry = at(m_count + 1);
|
||||
if (entry == nullptr) {
|
||||
page* newPage = m_vm->allocate<page>();
|
||||
if (newPage == nullptr) return INVALID_IRQ;
|
||||
*newPage = {};
|
||||
|
||||
if (m_head == nullptr)
|
||||
m_head = newPage;
|
||||
else
|
||||
m_tail->next = newPage;
|
||||
m_tail = newPage;
|
||||
|
||||
entry = newPage->entries;
|
||||
}
|
||||
*entry = {};
|
||||
return m_count++;
|
||||
}
|
||||
|
||||
bool irq_manager::bind(irq_t irq, interrupt_controller& controller, hwirq_t hwirq, vector_t vector) {
|
||||
auto* entry = at(irq);
|
||||
if (entry == nullptr) return false;
|
||||
entry->controller = &controller;
|
||||
entry->hwirq = hwirq;
|
||||
|
||||
entry->vector = vector;
|
||||
m_vectorMap[entry->vector] = irq;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool irq_manager::register_handler(irq_t irq, irq_handler handler, void* data) {
|
||||
auto* entry = at(irq);
|
||||
if (entry == nullptr) return false;
|
||||
entry->handler = handler;
|
||||
entry->data = data;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool irq_manager::enable(irq_t irq) {
|
||||
auto* entry = at(irq);
|
||||
if (entry == nullptr) return false;
|
||||
entry->enabled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool irq_manager::disable(irq_t irq) {
|
||||
auto* entry = at(irq);
|
||||
if (entry == nullptr) return false;
|
||||
entry->enabled = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool irq_manager::dispatch(interrupt_frame& frame) {
|
||||
if (frame.vector >= 0x100) return false;
|
||||
irq_t irq = m_vectorMap[frame.vector];
|
||||
if (irq == INVALID_IRQ) return false;
|
||||
auto* entry = at(irq);
|
||||
if (entry == nullptr || !entry->enabled) return false;
|
||||
|
||||
entry->handler(frame, entry->data);
|
||||
entry->controller->eoi(entry->hwirq);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
irq_manager::descriptor* irq_manager::at(irq_t irq) {
|
||||
u64 cntr = irq / page::ENTRIES;
|
||||
|
||||
page* curPage = m_head;
|
||||
while (curPage != nullptr && cntr-- > 0)
|
||||
curPage = curPage->next;
|
||||
|
||||
return (curPage == nullptr) ? nullptr : &curPage->entries[irq % page::ENTRIES];
|
||||
}
|
||||
|
||||
} // namespace x86_64
|
||||
@@ -0,0 +1,97 @@
|
||||
#ifndef ARCH_X86_64_INT_HPP
|
||||
#define ARCH_X86_64_INT_HPP
|
||||
|
||||
#include "arch/x86_64/idt.hpp"
|
||||
#include "arch/x86_64/memory.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
using hwirq_t = u32;
|
||||
static constexpr hwirq_t INVALID_HWIRQ = -1;
|
||||
|
||||
using irq_t = u32;
|
||||
static constexpr irq_t INVALID_IRQ = -1;
|
||||
|
||||
struct interrupt_controller {
|
||||
interrupt_controller() = default;
|
||||
virtual ~interrupt_controller() = default;
|
||||
|
||||
virtual void enable(hwirq_t irq) = 0;
|
||||
virtual void disable(hwirq_t irq) = 0;
|
||||
virtual void eoi(hwirq_t irq) = 0;
|
||||
};
|
||||
|
||||
struct interrupt_frame {
|
||||
u64 r15;
|
||||
u64 r14;
|
||||
u64 r13;
|
||||
u64 r12;
|
||||
u64 r11;
|
||||
u64 r10;
|
||||
u64 r9;
|
||||
u64 r8;
|
||||
u64 rbp;
|
||||
u64 rdi;
|
||||
u64 rsi;
|
||||
u64 rdx;
|
||||
u64 rcx;
|
||||
u64 rbx;
|
||||
u64 rax;
|
||||
|
||||
u64 vector;
|
||||
u64 errorCode;
|
||||
|
||||
u64 rip;
|
||||
u64 cs;
|
||||
u64 rflags;
|
||||
u64 rsp;
|
||||
u64 ss;
|
||||
};
|
||||
|
||||
using irq_handler = void (*)(interrupt_frame&, void*);
|
||||
|
||||
class irq_manager {
|
||||
public:
|
||||
irq_manager(vm& vm);
|
||||
|
||||
irq_t allocate();
|
||||
bool bind(irq_t irq, interrupt_controller& controller, hwirq_t hwirq, vector_t vector);
|
||||
bool register_handler(irq_t irq, irq_handler handler, void* data);
|
||||
bool enable(irq_t irq);
|
||||
bool disable(irq_t irq);
|
||||
|
||||
bool dispatch(interrupt_frame& frame);
|
||||
private:
|
||||
struct descriptor {
|
||||
interrupt_controller* controller = nullptr;
|
||||
irq_handler handler = nullptr;
|
||||
void* data = nullptr;
|
||||
|
||||
hwirq_t hwirq = INVALID_HWIRQ;
|
||||
vector_t vector = 0;
|
||||
bool enabled = false;
|
||||
};
|
||||
|
||||
struct page {
|
||||
static constexpr u64 ENTRIES = (4096 - sizeof(page*)) / sizeof(descriptor);
|
||||
|
||||
page* next = nullptr;
|
||||
descriptor entries[ENTRIES];
|
||||
};
|
||||
|
||||
static_assert(sizeof(page) < 4096, "page struct is too big!");
|
||||
private:
|
||||
descriptor* at(irq_t irq);
|
||||
private:
|
||||
vm* m_vm;
|
||||
irq_t m_count = 0;
|
||||
page* m_head;
|
||||
page* m_tail;
|
||||
|
||||
irq_t m_vectorMap[0x100];
|
||||
};
|
||||
|
||||
} // namespace x86_64
|
||||
|
||||
#endif // ARCH_X86_64_INT_HPP
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef ARCH_X86_64_IO_HPP
|
||||
#define ARCH_X86_64_IO_HPP
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
struct io {
|
||||
static u8 inb(u16 port) {
|
||||
u8 value{};
|
||||
asm volatile("inb %1, %0" : "=a"(value) : "Nd"(port));
|
||||
return value;
|
||||
}
|
||||
|
||||
static void outb(u16 port, u8 value) { asm volatile("outb %0, %1" : : "a"(value), "Nd"(port)); }
|
||||
};
|
||||
|
||||
} // namespace x86_64
|
||||
|
||||
#endif // ARCH_X86_64_IO_HPP
|
||||
@@ -3,18 +3,62 @@
|
||||
.section .text
|
||||
.code64
|
||||
|
||||
.extern exception_handler
|
||||
.extern interrupt_handler
|
||||
|
||||
common_isr_stub:
|
||||
push %rax
|
||||
push %rbx
|
||||
push %rcx
|
||||
push %rdx
|
||||
push %rsi
|
||||
push %rdi
|
||||
push %rbp
|
||||
push %r8
|
||||
push %r9
|
||||
push %r10
|
||||
push %r11
|
||||
push %r12
|
||||
push %r13
|
||||
push %r14
|
||||
push %r15
|
||||
|
||||
cld
|
||||
mov %rsp, %rdi
|
||||
call interrupt_handler
|
||||
|
||||
pop %r15
|
||||
pop %r14
|
||||
pop %r13
|
||||
pop %r12
|
||||
pop %r11
|
||||
pop %r10
|
||||
pop %r9
|
||||
pop %r8
|
||||
pop %rbp
|
||||
pop %rdi
|
||||
pop %rsi
|
||||
pop %rdx
|
||||
pop %rcx
|
||||
pop %rbx
|
||||
pop %rax
|
||||
|
||||
add $16, %rsp
|
||||
|
||||
iretq
|
||||
|
||||
.macro isr_err_stub i=1
|
||||
isr_stub_\i:
|
||||
call exception_handler
|
||||
iretq
|
||||
pushq $\i
|
||||
|
||||
jmp common_isr_stub
|
||||
.endm
|
||||
|
||||
.macro isr_nerr_stub i
|
||||
isr_stub_\i:
|
||||
call exception_handler
|
||||
iretq
|
||||
pushq $0
|
||||
pushq $\i
|
||||
|
||||
jmp common_isr_stub
|
||||
.endm
|
||||
|
||||
# From https://wiki.osdev.org/Interrupts_Tutorial
|
||||
@@ -39,7 +83,7 @@ isr_err_stub 17
|
||||
isr_nerr_stub 18
|
||||
isr_nerr_stub 19
|
||||
isr_nerr_stub 20
|
||||
isr_nerr_stub 21
|
||||
isr_err_stub 21
|
||||
isr_nerr_stub 22
|
||||
isr_nerr_stub 23
|
||||
isr_nerr_stub 24
|
||||
@@ -47,10 +91,16 @@ isr_nerr_stub 25
|
||||
isr_nerr_stub 26
|
||||
isr_nerr_stub 27
|
||||
isr_nerr_stub 28
|
||||
isr_nerr_stub 29
|
||||
isr_err_stub 29
|
||||
isr_err_stub 30
|
||||
isr_nerr_stub 31
|
||||
|
||||
.set i, 32
|
||||
.rept 256 - 32
|
||||
isr_nerr_stub %i
|
||||
.set i, i+1
|
||||
.endr
|
||||
|
||||
.macro isr_label i
|
||||
.quad isr_stub_\i
|
||||
.endm
|
||||
@@ -58,7 +108,7 @@ isr_nerr_stub 31
|
||||
.global isrStubTable
|
||||
isrStubTable:
|
||||
.set i, 0
|
||||
.rept 32
|
||||
.rept 256
|
||||
isr_label %i
|
||||
.set i, i+1
|
||||
.endr
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
#ifndef ARCH_X86_64_PIC_HPP
|
||||
#define ARCH_X86_64_PIC_HPP
|
||||
|
||||
#include "./io.hpp"
|
||||
#include "arch/x86_64/int.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
struct pic : interrupt_controller {
|
||||
static constexpr u16 PIC1_BASE = 0x20;
|
||||
static constexpr u16 PIC2_BASE = 0xA0;
|
||||
|
||||
static constexpr u16 PIC1_CMD = PIC1_BASE + 0;
|
||||
static constexpr u16 PIC1_DATA = PIC1_BASE + 1;
|
||||
static constexpr u16 PIC2_CMD = PIC2_BASE + 0;
|
||||
static constexpr u16 PIC2_DATA = PIC2_BASE + 1;
|
||||
|
||||
void remap(u8 off1, u8 off2) {
|
||||
static constexpr u8 ICW1_ICW4 = 0x01;
|
||||
static constexpr u8 ICW1_INIT = 0x10;
|
||||
|
||||
static constexpr u8 CASCADE_IRQ = 2;
|
||||
|
||||
static constexpr u8 ICW4_8086 = 0x01;
|
||||
|
||||
io::outb(PIC1_CMD, ICW1_INIT | ICW1_ICW4);
|
||||
io::outb(PIC2_CMD, ICW1_INIT | ICW1_ICW4);
|
||||
io::outb(PIC1_DATA, off1);
|
||||
io::outb(PIC2_DATA, off2);
|
||||
io::outb(PIC1_DATA, 1 << CASCADE_IRQ);
|
||||
io::outb(PIC2_DATA, CASCADE_IRQ);
|
||||
io::outb(PIC1_DATA, ICW4_8086);
|
||||
io::outb(PIC2_DATA, ICW4_8086);
|
||||
io::outb(PIC1_DATA, 0xFB);
|
||||
io::outb(PIC2_DATA, 0xFF);
|
||||
}
|
||||
|
||||
static void disable_pic(void) {
|
||||
io::outb(PIC1_DATA, 0xFF);
|
||||
io::outb(PIC2_DATA, 0xFF);
|
||||
}
|
||||
|
||||
void enable(hwirq_t irq) override {
|
||||
u16 port = (irq < 8) ? PIC1_DATA : PIC2_DATA;
|
||||
io::outb(port, io::inb(port) & ~(1 << (irq & 7)));
|
||||
}
|
||||
|
||||
void disable(hwirq_t irq) override {
|
||||
u16 port = (irq < 8) ? PIC1_DATA : PIC2_DATA;
|
||||
io::outb(port, io::inb(port) | (1 << (irq & 7)));
|
||||
}
|
||||
|
||||
void eoi(hwirq_t irq) override {
|
||||
static constexpr u8 EOI = 0x20;
|
||||
if (irq >= 8) io::outb(PIC2_CMD, EOI);
|
||||
io::outb(PIC1_CMD, EOI);
|
||||
}
|
||||
|
||||
u16 get_irr(void) {
|
||||
static constexpr u8 READ_IRR = 0x0A;
|
||||
io::outb(PIC1_CMD, READ_IRR);
|
||||
io::outb(PIC2_CMD, READ_IRR);
|
||||
return (io::inb(PIC2_CMD) << 8) | io::inb(PIC1_CMD);
|
||||
}
|
||||
|
||||
u16 get_isr(void) {
|
||||
static constexpr u8 READ_ISR = 0x0B;
|
||||
io::outb(PIC1_CMD, READ_ISR);
|
||||
io::outb(PIC2_CMD, READ_ISR);
|
||||
return (io::inb(PIC2_CMD) << 8) | io::inb(PIC1_CMD);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace x86_64
|
||||
|
||||
#endif // ARCH_X86_64_PIC_HPP
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef ARCH_X86_64_PIT_HPP
|
||||
#define ARCH_X86_64_PIT_HPP
|
||||
|
||||
#include "arch/x86_64/int.hpp"
|
||||
#include "arch/x86_64/io.hpp"
|
||||
#include "types.hpp"
|
||||
#include "vga.hpp"
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
struct pit {
|
||||
pit(u32 targetHz = 100) {
|
||||
constexpr u32 PIT_INPUT_HZ = 1193182;
|
||||
|
||||
const u16 divisor = static_cast<u16>(PIT_INPUT_HZ / targetHz);
|
||||
|
||||
io::outb(0x43, 0x36);
|
||||
|
||||
io::outb(0x40, static_cast<u8>(divisor & 0xFF));
|
||||
io::outb(0x40, static_cast<u8>(divisor >> 8));
|
||||
}
|
||||
|
||||
static void handler(interrupt_frame& frame, void* data) {
|
||||
(void)frame;
|
||||
|
||||
pit* pit = reinterpret_cast<decltype(pit)>(data);
|
||||
++pit->ticks;
|
||||
|
||||
vga::puts("PIT called!", 0, 5);
|
||||
}
|
||||
|
||||
volatile u64 ticks = 0;
|
||||
};
|
||||
|
||||
} // namespace x86_64
|
||||
|
||||
#endif // ARCH_X86_64_PIT_HPP
|
||||
Reference in New Issue
Block a user