98 lines
2.0 KiB
C++
98 lines
2.0 KiB
C++
#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
|