Newer
Older
tree-os / src / kernel / drivers / interrupts / interrupts.c
#include <alloc.h>
#include <exceptions.h>
#include <interrupts.h>
#include <irqs.h>
#include <ports.h>
#include <stdio.h>
#include <task.h>
#include <timer.h>

IDTR idtr;
IDTEntry *entries;
void *buffer;

void setInterrupt(uint8_t vector, void *callback) {
    IDTEntry *descriptor = &entries[vector];
    descriptor->isr_low = (uintptr_t)callback & 0xFFFF;
    descriptor->codeSegment = 0x08;
    descriptor->attributes = 0x8E;
    descriptor->isr_high = (uintptr_t)callback >> 16;
    descriptor->reserved = 0;
}

void setupInterrupts() {
    buffer = malloc(sizeof(IDTEntry) * 257);
    entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3);
    idtr.limit = sizeof(IDTEntry) * 256 - 1;
    idtr.base = (uint32_t)(uintptr_t)entries;
    setupExceptions();
    setupIRQs();
    __asm__ volatile("lidt %0" : : "memory"(idtr));
    __asm__ volatile("sti");
}