summaryrefslogtreecommitdiff
path: root/src/kernel/arch/i386/interrupts/idt.c
blob: 0b327c80f7ef6b0322aee6a46d52e0b6fd802305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <kernel/arch/i386/gdt.h>
#include <kernel/arch/i386/interrupts/idt.h>
#include <kernel/arch/i386/interrupts/isr.h>
#include <kernel/panic.h>
#include <stdbool.h>
#include <stdint.h>

struct idt_entry {
	uint16_t offset_low   ;
	uint16_t code_seg     ;
	uint8_t  zero         ; // unused, has to be 0
	uint8_t  type      : 4; // 16/32 bit, task/interrupt/task gate
	uint8_t  storage   : 1; // 0 for interrupt/trap gates
	uint8_t  ring      : 2;
	uint8_t  present   : 1;
	uint16_t offset_high  ;
} __attribute__((packed));

// is exactly the same as lgdt_arg, i should combine them into a single struct
// later
struct lidt_arg {
	uint16_t limit;
	uint32_t base;
} __attribute__((packed));


static struct idt_entry IDT[256];
static struct lidt_arg lidt_arg;

static void idt_prepare();
static void idt_load();
static void idt_test();


static void idt_prepare() {
	uintptr_t offset;
	for (int i = 0; i < 256; i++) {
		offset = (uintptr_t) &_isr_stubs + i * 8;

		IDT[i] = (struct idt_entry) {
			.offset_low  = offset,
			.offset_high = offset >> 16,
			.code_seg    = SEG_r0code << 3,
			.zero        = 0,
			.present     = 1,
			.ring        = 0,
			.storage     = 0,
			.type        = 0xE, // 32-bit interrupt gate
		};
	}
}

static void idt_load() {
	lidt_arg.limit = sizeof(IDT) - 1;
	lidt_arg.base = (uintptr_t) &IDT;
	asm("lidt (%0)" : : "r" (&lidt_arg) : "memory");
}

static void idt_test() {
	asm("int $0x34" : : : "memory");
	if (!isr_test_interrupt_called) panic();
}

void idt_init() {
	idt_prepare();
	idt_load();
	idt_test();
}