-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterrupts.cpp
210 lines (151 loc) · 8.75 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "interrupts.h"
void printf(char* str);
InterruptHandler::InterruptHandler(InterruptManager* interruptManager, uint8_t InterruptNumber)
{
this->InterruptNumber = InterruptNumber;
this->interruptManager = interruptManager;
interruptManager->handlers[InterruptNumber] = this;
}
InterruptHandler::~InterruptHandler()
{
if(interruptManager->handlers[InterruptNumber] == this)
interruptManager->handlers[InterruptNumber] = 0;
}
uint32_t InterruptHandler::HandleInterrupt(uint32_t esp)
{
return esp;
}
InterruptManager::GateDescriptor InterruptManager::interruptDescriptorTable[256];
InterruptManager* InterruptManager::ActiveInterruptManager = 0;
void InterruptManager::SetInterruptDescriptorTableEntry(uint8_t interrupt,
uint16_t CodeSegment, void (*handler)(), uint8_t DescriptorPrivilegeLevel, uint8_t DescriptorType)
{
// address of pointer to code segment (relative to global descriptor table)
// and address of the handler (relative to segment)
interruptDescriptorTable[interrupt].handlerAddressLowBits = ((uint32_t) handler) & 0xFFFF;
interruptDescriptorTable[interrupt].handlerAddressHighBits = (((uint32_t) handler) >> 16) & 0xFFFF;
interruptDescriptorTable[interrupt].gdt_codeSegmentSelector = CodeSegment;
const uint8_t IDT_DESC_PRESENT = 0x80;
interruptDescriptorTable[interrupt].access = IDT_DESC_PRESENT | ((DescriptorPrivilegeLevel & 3) << 5) | DescriptorType;
interruptDescriptorTable[interrupt].reserved = 0;
}
InterruptManager::InterruptManager(uint16_t hardwareInterruptOffset, GlobalDescriptorTable* globalDescriptorTable)
: programmableInterruptControllerMasterCommandPort(0x20),
programmableInterruptControllerMasterDataPort(0x21),
programmableInterruptControllerSlaveCommandPort(0xA0),
programmableInterruptControllerSlaveDataPort(0xA1)
{
this->hardwareInterruptOffset = hardwareInterruptOffset;
uint32_t CodeSegment = globalDescriptorTable->CodeSegmentSelector();
const uint8_t IDT_INTERRUPT_GATE = 0xE;
for(uint8_t i = 255; i > 0; --i)
{
SetInterruptDescriptorTableEntry(i, CodeSegment, &InterruptIgnore, 0, IDT_INTERRUPT_GATE);
handlers[i] = 0;
}
SetInterruptDescriptorTableEntry(0, CodeSegment, &InterruptIgnore, 0, IDT_INTERRUPT_GATE);
handlers[0] = 0;
SetInterruptDescriptorTableEntry(0x00, CodeSegment, &HandleException0x00, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x01, CodeSegment, &HandleException0x01, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x02, CodeSegment, &HandleException0x02, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x03, CodeSegment, &HandleException0x03, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x04, CodeSegment, &HandleException0x04, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x05, CodeSegment, &HandleException0x05, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x06, CodeSegment, &HandleException0x06, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x07, CodeSegment, &HandleException0x07, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x08, CodeSegment, &HandleException0x08, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x09, CodeSegment, &HandleException0x09, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x0A, CodeSegment, &HandleException0x0A, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x0B, CodeSegment, &HandleException0x0B, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x0C, CodeSegment, &HandleException0x0C, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x0D, CodeSegment, &HandleException0x0D, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x0E, CodeSegment, &HandleException0x0E, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x0F, CodeSegment, &HandleException0x0F, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x10, CodeSegment, &HandleException0x10, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x11, CodeSegment, &HandleException0x11, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x12, CodeSegment, &HandleException0x12, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x13, CodeSegment, &HandleException0x13, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x00, CodeSegment, &HandleInterruptRequest0x00, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x01, CodeSegment, &HandleInterruptRequest0x01, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x02, CodeSegment, &HandleInterruptRequest0x02, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x03, CodeSegment, &HandleInterruptRequest0x03, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x04, CodeSegment, &HandleInterruptRequest0x04, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x05, CodeSegment, &HandleInterruptRequest0x05, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x06, CodeSegment, &HandleInterruptRequest0x06, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x07, CodeSegment, &HandleInterruptRequest0x07, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x08, CodeSegment, &HandleInterruptRequest0x08, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x09, CodeSegment, &HandleInterruptRequest0x09, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0A, CodeSegment, &HandleInterruptRequest0x0A, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0B, CodeSegment, &HandleInterruptRequest0x0B, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0C, CodeSegment, &HandleInterruptRequest0x0C, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0D, CodeSegment, &HandleInterruptRequest0x0D, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0E, CodeSegment, &HandleInterruptRequest0x0E, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0F, CodeSegment, &HandleInterruptRequest0x0F, 0, IDT_INTERRUPT_GATE);
programmableInterruptControllerMasterCommandPort.Write(0x11);
programmableInterruptControllerSlaveCommandPort.Write(0x11);
// remap
programmableInterruptControllerMasterDataPort.Write(hardwareInterruptOffset);
programmableInterruptControllerSlaveDataPort.Write(hardwareInterruptOffset+8);
programmableInterruptControllerMasterDataPort.Write(0x04);
programmableInterruptControllerSlaveDataPort.Write(0x02);
programmableInterruptControllerMasterDataPort.Write(0x01);
programmableInterruptControllerSlaveDataPort.Write(0x01);
programmableInterruptControllerMasterDataPort.Write(0x00);
programmableInterruptControllerSlaveDataPort.Write(0x00);
InterruptDescriptorTablePointer idt_pointer;
idt_pointer.size = 256*sizeof(GateDescriptor) - 1;
idt_pointer.base = (uint32_t)interruptDescriptorTable;
asm volatile("lidt %0" : : "m" (idt_pointer));
}
InterruptManager::~InterruptManager()
{
Deactivate();
}
uint16_t InterruptManager::HardwareInterruptOffset()
{
return hardwareInterruptOffset;
}
void InterruptManager::Activate()
{
if(ActiveInterruptManager != 0)
ActiveInterruptManager->Deactivate();
ActiveInterruptManager = this;
asm("sti");
}
void InterruptManager::Deactivate()
{
if(ActiveInterruptManager == this)
{
ActiveInterruptManager = 0;
asm("cli");
}
}
uint32_t InterruptManager::HandleInterrupt(uint8_t interrupt, uint32_t esp)
{
if(ActiveInterruptManager != 0)
return ActiveInterruptManager->DoHandleInterrupt(interrupt, esp);
return esp;
}
uint32_t InterruptManager::DoHandleInterrupt(uint8_t interrupt, uint32_t esp)
{
if(handlers[interrupt] != 0)
{
esp = handlers[interrupt]->HandleInterrupt(esp);
}
else if(interrupt != hardwareInterruptOffset)
{
char* foo = "UNHANDLED INTERRUPT 0x00";
char* hex = "0123456789ABCDEF";
foo[22] = hex[(interrupt >> 4) & 0xF];
foo[23] = hex[interrupt & 0xF];
printf(foo);
}
// hardware interrupts must be acknowledged
if(hardwareInterruptOffset <= interrupt && interrupt < hardwareInterruptOffset+16)
{
programmableInterruptControllerMasterCommandPort.Write(0x20);
if(hardwareInterruptOffset + 8 <= interrupt)
programmableInterruptControllerSlaveCommandPort.Write(0x20);
}
return esp;
}