summaryrefslogtreecommitdiff
path: root/stage3/interrupts.c
blob: 2713cfa2929862d135cb3eb79fca774919a4febf (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "def.h"
#include "halt.h"
#include "font.h"
#include "heap.h"
#include "pic.h"
#include "thread.h"
#include "io.h"

extern u64 idt_entries[256]; // isr.asm

typedef struct __attribute__((packed)) {
	u64 rax, rdx, rcx, rsi, rdi, r8, r9, r10, r11;
	u64 which, error_code;
	u64 rip, cs, rflags, rsp, ss;
} interrupt_frame;

static str exception[32] = {
	S("Division Error"),
	S("Debug"),
	S("Non-maskable Interrupt"),
	S("Breakpoint"),
	S("Overflow"),
	S("Bound Range Exceeded"),
	S("Invalid Opcode"),
	S("Device Not Available"),
	S("Double Fault"),
	S("Coprocessor Segment Overrun"),
	S("Invalid TSS"),
	S("Segment Not Present"),
	S("Stack-Segment Fault"),
	S("General Protection Fault"),
	S("Page Fault"),
	NILS,
	S("x87 Floating-Point Exception"),
	S("Alignment Check"),
	S("Machine Check"),
	S("SIMD Floating-Point Exception"),
	S("Virtualization Exception"),
	S("Control Protection Exception"),
	NILS, NILS, NILS, NILS, NILS, NILS,
	S("Hypervisor Injection Exception"),
	S("VMM Communication Exception"),
	S("Security Exception"),
	NILS,
};

static void dump_frame(interrupt_frame *frame)
{
	print(S("rip = ")); print_hex(frame->rip); print_char('\n');
	print(S("cs = ")); print_hex(frame->cs); print_char('\n');
	print(S("rflags = ")); print_hex(frame->rflags); print_char('\n');
	print(S("rsp = ")); print_hex(frame->rsp); print_char('\n');
	print(S("ss = ")); print_hex(frame->ss); print_char('\n');
}

void interrupt_handler(interrupt_frame *frame)
{
	if (frame->which < 32) {
		// TODO: possible race condition due to printing here
		// when exception happens in printing code itself

		if (exception[frame->which].data == nil) {
			print(S("Unknown Exception "));
			print_dec(frame->which);
		} else {
			print(exception[frame->which]);
		}
		print_char('\n');

		if (frame->which == 13) {
			str bits[8] = {
				S("present"),
				S("write"),
				S("user"),
				S("reserved_write"),
				S("instruction_fetch"),
				S("protection_key"),
				S("shadow_stack"),
				S("software_guard_extensions"),
			};

			u8 err = frame->error_code;

			for (int i = 0; i < 8; i++) {
				print(bits[i]); print(S(" = ")); print((err & 1) ? S("true\n") : S("false\n"));
				err >>= 1;
			}
		} else {
			print(S("error_code = ")); print_dec(frame->error_code); print_char('\n');
		}

		dump_frame(frame);
		freeze();
	} else if (frame->which-32 < 16) {
		if (queue_write.len == queue_write.cap) {
			panic(S("queue exceeded\n"));
			/*
			// TODO: malloc would cause a race condition
			queue_write.cap = queue_write.cap == 0 ? 1 : queue_write.cap * 2;
			queue_write.data = realloc(queue_write.data, queue_write.cap);
			*/
		}

		event *e = &queue_write.data[queue_write.len++];
		e->irq = frame->which-32;

		if (e->irq == 1) {
			e->data.scancode = inb(IO_PS2_DATA);
		}

		ack_irq(e->irq);
	} else {
		// print("Spurious Interrupt "); print_num(frame->which, 10, 0); print("\n");
		// dump_frame(frame);
	}
}

typedef struct {
	u16 size;
	u64 addr;
} __attribute__((packed)) idt_descriptor;

idt_descriptor idtr;

void interrupts_init()
{
	typedef struct {
	   u16 offset_1;
	   u16 selector;
	   u8 ist;
	   u8 type_attrs;
	   u16 offset_2;
	   u32 offset_3;
	   u32 zero;
	} __attribute__((packed)) interrupt_descriptor;

	interrupt_descriptor *idt = malloc(256 * sizeof *idt);

	for (int i = 0; i < 255; i++) {
		union {
			struct {
				u16 offset_1;
				u16 offset_2;
				u32 offset_3;
			} __attribute__((packed)) off;
			u64 addr;
		} __attribute__((packed)) isr = {
			.addr = idt_entries[i],
		};

		idt[i] = (interrupt_descriptor) {
			.offset_1 = isr.off.offset_1,
			.selector = 0x8,
			.ist = 0,
			.type_attrs = 0x8E,
			.offset_2 = isr.off.offset_2,
			.offset_3 = isr.off.offset_3,
			.zero = 0,
		};
	}

	idtr = (idt_descriptor) {
		.size = 256 * sizeof *idt - 1,
		.addr = (u64) &idt[0],
	};

	asm("lidt %0" : : "m"(idtr));
}