summaryrefslogtreecommitdiff
path: root/kernel/src/cpu/irq.c
blob: a7288c084b58dc1d72f8058c99340eab8f20ec4a (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
#include <types.h>
#include <irq_handler.h>
#include <pic.h>
#include <io.h>
#include <keyboard.h>
#include <timer.h>
#include <paging.h>
#include <libk/serial_stdio.h>
#include <libk/stdio.h>
#include <panic.h>
#include <ioapic.h>

#define PIT 0

const char *const exception_name[] = {
	"Divide-by-zero Error",
	"Debug",
	"Non-maskable Interrupt",
	"Breakpoint",
	"Overflow",
	"Bound Range Exceeded",
	"Invalid Opcode",
	"Device Not Available",
	"Double Fault",
	"Coprocessor Segment Overrun",
	"Invalid TSS",
	"Segment Not Present",
	"Stack-Segment Fault",
	"General Protection Fault",
	"Page Fault",
	"Reserved",
	"x87 Floating-Point Exception",
	"Alignment Check",
	"Machine Check",
	"SIMD Floating-Point Exception",
	"Virtualization Exception",
	"Control Protection Exception",
	"Reserved",
	"Reserved",
	"Reserved",
	"Reserved",
	"Reserved",
	"Reserved",
	"Hypervisor Injection Exception",
	"VMM Communication Exception",
	"Security Exception",
	"Reserved",
};

void exception(uint64_t number, uint64_t error) {
	switch (number) {
	case 0xe:
		printf("%s, error: 0x%x\n", exception_name[14], error);
		page_fault(error);
		break;
	default:
		panic("%s, error: 0x%x\n", exception_name[number], error);
	}
}

void eoi(uint64_t number)
{
	if (PIT) {
		if (number < 0x28) {
			outb(PIC1_COMMAND, PIC_EOI);
		} else if (number < 0x30) {
			outb(PIC1_COMMAND, PIC_EOI);
			outb(PIC2_COMMAND, PIC_EOI);
		}
	} else {
		ioapic_eoi();
	}
}

void interrupt(uint64_t number)
{
	switch (number) {
	case 0x20:
		timer_handler();
		eoi(number);
		break;
	case 0x21:
		keyboard_handler();
		eoi(number);
		break;
	default:
		printf("spurious interrupt\n");
		break;
	}
}

void isr_def_handler(uint64_t number, uint64_t error)
{
	if (number < 0x20) {
		exception(number, error);
	} else {
		interrupt(number);
	}
}