summaryrefslogtreecommitdiff
path: root/kernel/idt.c
blob: 55c1baead7adcc53f76176ea29c53f52ff35b0e2 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "idt.h"

idt_entry idt[] = {
	{.base_low = 1}, // Keep IDT in .data.
	[1 ... 255] = idt_entry_null
};

idt_ptr idtr = {
	.base = idt,
	.limit = sizeof(idt) - 1
};

void idt_set_gate(uint8_t num, uint32_t base, uint16_t segment, uint16_t flags) {
	idt[num].base_low = base & 0xffff;
	idt[num].base_high = base >> 16 & 0xffff;
	idt[num].segment = segment;
	idt[num].flags = flags;
}

#define isr_panic(num) void isr_ ## num(); \
asm( \
	".globl isr_" #num "\n" \
	"isr_" #num ":\n" \
	"push $0\n" \
	"push $" #num "\n" \
	"pusha\n" \
	"push %ds\n" \
	"push %ss\n" \
	"call panic\n" \
)

#define isr_panic_e(num) void isr_ ## num(); \
asm( \
	".globl isr_" #num "\n" \
	"isr_" #num ":\n" \
	"push $" #num "\n" \
	"pusha\n" \
	"push %ds\n" \
	"push %ss\n" \
	"call panic\n" \
)

isr_panic(0);
isr_panic(1);
isr_panic(2);
isr_panic(3);
isr_panic(4);
isr_panic(5);
isr_panic(6);
isr_panic(7);
isr_panic_e(8);
isr_panic(9);
isr_panic_e(10);
isr_panic_e(11);
isr_panic_e(12);
isr_panic_e(13);
isr_panic_e(14);
isr_panic(15);
isr_panic(16);
isr_panic_e(17);
isr_panic(18);
isr_panic(19);
isr_panic(20);
isr_panic(21);
isr_panic(22);
isr_panic(23);
isr_panic(24);
isr_panic(25);
isr_panic(26);
isr_panic(27);
isr_panic(28);
isr_panic(29);
isr_panic(30);
isr_panic(31);

#include "irq.h"

void tick() {
	//*(uint16_t*)(0xb8890) += 1;
	irq_reset();
}


asm(
	".globl isr_32\n"
	"isr_32:\n"
	"pusha\n"
	"call tick\n"
	"popa\n"
	"iret\n"
);

void idt_init() {
	idt_set_gate(0, (uint32_t)isr_0, 0x08, 0x8e00);
	idt_set_gate(1, (uint32_t)isr_1, 0x08, 0x8e00);
	idt_set_gate(2, (uint32_t)isr_2, 0x08, 0x8e00);
	idt_set_gate(3, (uint32_t)isr_3, 0x08, 0x8e00);
	idt_set_gate(4, (uint32_t)isr_4, 0x08, 0x8e00);
	idt_set_gate(5, (uint32_t)isr_5, 0x08, 0x8e00);
	idt_set_gate(6, (uint32_t)isr_6, 0x08, 0x8e00);
	idt_set_gate(7, (uint32_t)isr_7, 0x08, 0x8e00);
	idt_set_gate(8, (uint32_t)isr_8, 0x08, 0x8e00);
	idt_set_gate(9, (uint32_t)isr_9, 0x08, 0x8e00);
	idt_set_gate(10, (uint32_t)isr_10, 0x08, 0x8e00);
	idt_set_gate(11, (uint32_t)isr_11, 0x08, 0x8e00);
	idt_set_gate(12, (uint32_t)isr_12, 0x08, 0x8e00);
	idt_set_gate(13, (uint32_t)isr_13, 0x08, 0x8e00);
	idt_set_gate(14, (uint32_t)isr_14, 0x08, 0x8e00);
	idt_set_gate(15, (uint32_t)isr_15, 0x08, 0x8e00);
	idt_set_gate(16, (uint32_t)isr_16, 0x08, 0x8e00);
	idt_set_gate(17, (uint32_t)isr_17, 0x08, 0x8e00);
	idt_set_gate(18, (uint32_t)isr_18, 0x08, 0x8e00);
	idt_set_gate(19, (uint32_t)isr_19, 0x08, 0x8e00);
	idt_set_gate(10, (uint32_t)isr_20, 0x08, 0x8e00);
	idt_set_gate(21, (uint32_t)isr_21, 0x08, 0x8e00);
	idt_set_gate(22, (uint32_t)isr_22, 0x08, 0x8e00);
	idt_set_gate(23, (uint32_t)isr_23, 0x08, 0x8e00);
	idt_set_gate(24, (uint32_t)isr_24, 0x08, 0x8e00);
	idt_set_gate(25, (uint32_t)isr_25, 0x08, 0x8e00);
	idt_set_gate(26, (uint32_t)isr_26, 0x08, 0x8e00);
	idt_set_gate(27, (uint32_t)isr_27, 0x08, 0x8e00);
	idt_set_gate(28, (uint32_t)isr_28, 0x08, 0x8e00);
	idt_set_gate(29, (uint32_t)isr_29, 0x08, 0x8e00);
	idt_set_gate(30, (uint32_t)isr_30, 0x08, 0x8e00);
	idt_set_gate(31, (uint32_t)isr_31, 0x08, 0x8e00);
	
	void isr_32();
	idt_set_gate(32, (uint32_t)isr_32, 0x08, 0x8e00);
	
	asm volatile(
		"lidt (%0)\n"
		: : "r" (&idtr)
	);
}