-
Notifications
You must be signed in to change notification settings - Fork 0
/
interrupts.cpp
124 lines (107 loc) · 3.72 KB
/
interrupts.cpp
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
#include "interrupts.h"
void print(char* str);
InterruptManager::GateDescriptor InterruptManager::interruptDescriptorTable[0x100];
InterruptManager* InterruptManager::activeInterruptManager = 0;
void InterruptManager::setInterruptDescriptorTableEntry(
uint8_t interruptNumber,
uint16_t codeSegmentSelector,
void (*handler)(),
uint8_t desciptorPrivilegeLevel,
uint8_t desciptorType){
const uint8_t IDT_DESC_PRESENT = 0x80;
interruptDescriptorTable[interruptNumber].handlerAdress_lwr = (uint32_t) handler & 0xffff;
interruptDescriptorTable[interruptNumber].handlerAdress_upr = (((uint32_t) handler) >> 16) & 0xffff;
interruptDescriptorTable[interruptNumber].gdt_codeSegmentSelector = codeSegmentSelector;
interruptDescriptorTable[interruptNumber].reserved = 0;
interruptDescriptorTable[interruptNumber].access = IDT_DESC_PRESENT | desciptorType | ((desciptorPrivilegeLevel & 3) << 5);
}
InterruptManager::InterruptManager(GlobalDescriptorTable* gdt)
:picMasterCommand(0x20),
picMasterData(0x21),
picSlaveCommand(0xa0),
picSlaveData(0xa1)
{
uint16_t codeSegment = gdt -> getCodeSegmentSelector();
const uint8_t IDT_INTERRUPT_GATE = 0xe;
for(int i = 0; i < 0xff; i++) {
handlers[i] = 0;
setInterruptDescriptorTableEntry(i, codeSegment, &ignoreInterruptRequest, 0, IDT_INTERRUPT_GATE);
}
setInterruptDescriptorTableEntry(0x20, codeSegment, &handleInterruptRequest0x00, 0, IDT_INTERRUPT_GATE);
setInterruptDescriptorTableEntry(0x21, codeSegment, &handleInterruptRequest0x01, 0, IDT_INTERRUPT_GATE);
picMasterCommand.write(0x11);
picSlaveCommand.write(0x11);
// add offset to interrupts
picMasterData.write(0x20);
picSlaveData.write(0x28);
//tells who is master/slave
picMasterData.write(0x04);
picSlaveData.write(0x02);
// ??
picMasterData.write(0x01);
picSlaveData.write(0x01);
// ??
picMasterData.write(0x00);
picSlaveData.write(0x00);
InterruptDescriptorTablePointer idt;
idt.size = 0x100 * sizeof(GateDescriptor) - 1;
idt.base = (uint32_t)interruptDescriptorTable;
asm volatile("lidt %0" : : "m" (idt));
}
InterruptManager::~InterruptManager() {
deactivate();
}
void InterruptManager::activate() {
if(activeInterruptManager != 0) activeInterruptManager->deactivate();
activeInterruptManager = this;
asm("sti");
}
void InterruptManager::deactivate() {
if(activeInterruptManager == this) {
asm("cli");
activeInterruptManager = 0;
}
}
uint32_t InterruptManager::handleInterrupt(uint8_t interrupt, uint32_t esp) {
//asm("jmp 0");
if(activeInterruptManager != 0) {
return activeInterruptManager->doHandleInterrupt(interrupt, esp);
}
char* foo = "INTERRUPT 0x00";
char* hex = "0123456789ABCDEF";
foo[12] = hex[(interrupt >> 4) & 0xF];
foo[13] = hex[interrupt & 0xF];
print(foo);
return esp;
}
uint32_t InterruptManager::doHandleInterrupt(uint8_t interrupt, uint32_t esp) {
if(handlers[interrupt] != 0) {
esp = handlers[interrupt]->handleInterrupt(esp);
} else {
if(interrupt == 0x20) {
//print("ti "); // timer interrupt
} else {
char* txt = "UNHANDLED INTERRUPT 0x?? ";
char* hex = "0123456789abcdef";
txt[22] = hex[(interrupt>>4) & 0x0f];
txt[23] = hex[interrupt & 0x0f];
print(txt);
}
}
if(0x20 <= interrupt && interrupt < 0x30) { //HW-INT
picMasterCommand.write(0x20);
if(0x28 <= interrupt) picSlaveCommand.write(0x20);
}
return esp;
}
InterruptHandler::InterruptHandler(uint8_t interruptNumber, InterruptManager* manager) {
this->interruptNumber = interruptNumber;
this->interruptManager = manager;
manager->handlers[interruptNumber] = this;
}
InterruptHandler::~InterruptHandler() {
if(interruptManager->handlers[interruptNumber] == this) interruptManager->handlers[interruptNumber] = 0;
}
uint32_t InterruptHandler::handleInterrupt(uint32_t esp) {
return esp;
}