Newer
Older
tree-os / src / kernel / drivers / interrupts / interrupts.c
@lukas lukas on 24 Sep 2021 861 bytes add capital typing
#include <_stdio.h>
#include <alloc.h>
#include <exceptions.h>
#include <interrupts.h>
#include <irqs.h>
#include <ports.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 = (uint32_t)callback & 0xFFFF;
    descriptor->codeSegment = 0x08;
    descriptor->attributes = 0x8E;
    descriptor->isr_high = (uint32_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)entries;
    setupExceptions();
    setupIRQs();
    __asm__ volatile("lidt %0" : : "memory"(idtr));
    __asm__ volatile("sti");
}