14
|
1 #include "kernel.h"
|
|
2
|
|
3 void panic(char *msg) {
|
|
4 printf("Kernel panic: ");
|
|
5 printf(msg);
|
|
6 halt();
|
|
7 }
|
|
8
|
|
9 // Assembler wrapper prototypes:
|
|
10 void INTDEF(void);
|
|
11 void INT0(void);
|
|
12 void INT1(void);
|
|
13 void INT2(void);
|
|
14 void INT3(void);
|
|
15 void INT4(void);
|
|
16 void INT5(void);
|
|
17 void INT6(void);
|
|
18 void INT7(void);
|
|
19 void INT8(void);
|
|
20 void INT9(void);
|
|
21 void INT10(void);
|
|
22 void INT11(void);
|
|
23 void INT12(void);
|
|
24 void INT13(void);
|
|
25 void INT14(void);
|
|
26 void INT15(void);
|
|
27 void INT16(void);
|
|
28 void INT17(void);
|
|
29 void INT18(void);
|
|
30 void INT19(void);
|
|
31 // Remapped handlers:
|
|
32 void INT32(void);
|
|
33 void INT33(void);
|
|
34 void INT34(void);
|
|
35
|
|
36 // THE interrupt descriptor table:
|
17
|
37 static IDT_entry idt[256];
|
|
38 volatile idtPointer idtP;
|
14
|
39
|
17
|
40 void setIDTentry(int num, void (*handler)(), uint16_t selector, uint8_t flags)
|
14
|
41 {
|
|
42 // Fill one entry with IDT info:
|
|
43 uint64_t offset;
|
|
44
|
17
|
45 // Typecast the function pointer to a number:
|
|
46 offset = (uint64_t)handler;
|
14
|
47
|
17
|
48 // Set offset:
|
|
49 idt[num].baseLow = offset & 0xFFFF;
|
|
50 idt[num].baseMid = (offset >> 16) & 0xFFFF;
|
|
51 idt[num].baseHigh = (offset >> 32) & 0xFFFFFFFF;
|
|
52
|
|
53 // Set flags and selector:
|
|
54 idt[num].selector = selector;
|
|
55 idt[num].flags = flags;
|
|
56
|
|
57 // Set reserved fields:
|
|
58 idt[num].reserved1 = 0;
|
|
59 idt[num].reserved2 = 0;
|
14
|
60 }
|
|
61
|
|
62 void setupIDT(void) {
|
|
63 int i;
|
|
64 // Fill all vectors with the default handler:
|
|
65 for (i=0; i<256; i++) {
|
17
|
66 setIDTentry(i, &INTDEF, 0x08, 0x8E);
|
14
|
67 }
|
|
68
|
|
69 // Now set other then default handler:
|
17
|
70 setIDTentry(0, INT0, 0x08, 0x8E);
|
|
71 setIDTentry(1, INT1, 0x08, 0x8E);
|
|
72 setIDTentry(2, INT2, 0x08, 0x8E);
|
|
73 setIDTentry(3, INT3, 0x08, 0x8E);
|
|
74 setIDTentry(4, INT4, 0x08, 0x8E);
|
|
75 setIDTentry(5, INT5, 0x08, 0x8E);
|
|
76 setIDTentry(6, INT6, 0x08, 0x8E);
|
|
77 setIDTentry(7, INT7, 0x08, 0x8E);
|
|
78 setIDTentry(8, INT8, 0x08, 0x8E);
|
|
79 setIDTentry(9, INT9, 0x08, 0x8E);
|
|
80 setIDTentry(10, INT10, 0x08, 0x8E);
|
|
81 setIDTentry(11, INT11, 0x08, 0x8E);
|
|
82 setIDTentry(12, INT12, 0x08, 0x8E);
|
|
83 setIDTentry(13, INT13, 0x08, 0x8E);
|
|
84 setIDTentry(14, INT14, 0x08, 0x8E);
|
|
85 setIDTentry(15, INT15, 0x08, 0x8E);
|
|
86 setIDTentry(16, INT16, 0x08, 0x8E);
|
|
87 setIDTentry(17, INT17, 0x08, 0x8E);
|
|
88 setIDTentry(18, INT18, 0x08, 0x8E);
|
|
89 setIDTentry(19, INT19, 0x08, 0x8E);
|
14
|
90 /* reserved interrupts: */
|
|
91 // From int20 - int31
|
17
|
92 setIDTentry(32, INT32, 0x08, 0x8E);
|
|
93 setIDTentry(33, INT33, 0x08, 0x8E);
|
|
94 setIDTentry(34, INT34, 0x08, 0x8E);
|
14
|
95
|
17
|
96 // Set the correct values in the IDT pointer:
|
|
97 idtP.base = (uint64_t)&idt;
|
|
98 idtP.limit = (sizeof(IDT_entry) * 256) - 1;
|
14
|
99 // call load IDT asm function:
|
17
|
100 loadIDT();
|
14
|
101
|
|
102 PICremap();
|
17
|
103 printf("enable ints\n");
|
|
104 asm("sti");
|
|
105 printf("Done!\n");
|
14
|
106 }
|
|
107
|
|
108 // PIC functions:
|
|
109 void PICremap() {
|
|
110 unsigned char maskmaster, maskslave, pic1, pic2;
|
|
111 pic1 = 0x20; // remapping location master
|
|
112 pic2 = 0x28;
|
|
113 maskmaster = inb(0x21); // master cmd=0x20, data=0x21
|
|
114 maskslave = inb(0xA1); // slave command=0xA0, data=0xA1
|
|
115
|
|
116 outb(0x20, 0x20); // end of init
|
|
117
|
|
118 outb(0x20, 0x11); // init + ICW1_ICW2
|
|
119 outb(0xA0, 0x11); // init + ICW1_ICW2
|
|
120
|
|
121 outb(0x21, pic1); // ICW2, write offset
|
|
122 outb(0xA1, pic2); // ICW2
|
|
123
|
|
124 outb(0x21, 4); // ICW3, write a 4!
|
|
125 outb(0xA1, 2); // ICW3, write a 2!
|
|
126
|
|
127 outb(0x21, 1); // ICW4, 8086 mode
|
|
128 outb(0xA1, 1); // ICW4
|
|
129
|
|
130 outb(0x21, maskmaster);
|
|
131 outb(0xA1, maskslave);
|
|
132 }
|
|
133
|
|
134 // Interrupt service routines:
|
|
135
|
|
136 void INT0handler()
|
|
137 {
|
|
138 printf("INT0 called!\n");
|
|
139 panic("Unhandled exception!");
|
|
140 }
|
|
141
|
|
142 void INT1handler()
|
|
143 {
|
|
144 printf("INT1 called!\n");
|
|
145 panic("Unhandled exception!");
|
|
146 }
|
|
147
|
|
148 void INT2handler()
|
|
149 {
|
|
150 printf("INT2 called!\n");
|
|
151 panic("Unhandled exception!");
|
|
152 }
|
|
153
|
|
154 void INT3handler()
|
|
155 {
|
|
156 printf("INT3 called!\n");
|
|
157 panic("Unhandled exception!");
|
|
158 }
|
|
159
|
|
160 void INT4handler()
|
|
161 {
|
|
162 printf("INT4 called!\n");
|
|
163 panic("Unhandled exception!");
|
|
164 }
|
|
165
|
|
166 void INT5handler()
|
|
167 {
|
|
168 printf("INT5 called!\n");
|
|
169 panic("Unhandled exception!");
|
|
170 }
|
|
171
|
|
172 void INT6handler()
|
|
173 {
|
|
174 printf("INT6 called!\n");
|
|
175 panic("Unhandled exception!");
|
|
176 }
|
|
177
|
|
178 void INT7handler()
|
|
179 {
|
|
180 printf("INT7 called!\n");
|
|
181 panic("Unhandled exception!");
|
|
182 }
|
|
183
|
|
184 void INT8handler()
|
|
185 {
|
|
186 printf("INT8 called!\n");
|
|
187 panic("Unhandled exception!");
|
|
188 }
|
|
189
|
|
190 void INT9handler()
|
|
191 {
|
|
192 printf("INT9 called!\n");
|
|
193 panic("Unhandled exception!");
|
|
194 }
|
|
195
|
|
196 void INT10handler()
|
|
197 {
|
|
198 printf("INT10 called!\n");
|
|
199 panic("Unhandled exception!");
|
|
200 }
|
|
201
|
|
202 void INT11handler()
|
|
203 {
|
|
204 printf("INT11 called!\n");
|
|
205 panic("Unhandled exception!");
|
|
206 }
|
|
207
|
|
208 void INT12handler()
|
|
209 {
|
|
210 printf("INT12 called!\n");
|
|
211 panic("Unhandled exception!");
|
|
212 }
|
|
213
|
|
214 void INT13handler()
|
|
215 {
|
|
216 printf("INT13 called!\n");
|
|
217 panic("Unhandled exception!");
|
|
218 }
|
|
219
|
17
|
220 void* getUnusedPage()
|
|
221 {
|
14
|
222 return 0;
|
|
223 }
|
|
224
|
17
|
225 void mappage(uint64_t address) {
|
14
|
226 uint32_t pageDirIndex = address >> 22;
|
|
227 uint32_t pageTableIndex = (address >> 12) & 0x3FF;
|
|
228
|
|
229 printf("Allocating page at %d, tableindex=%d\n", pageDirIndex, pageTableIndex);
|
|
230 //uint32_t *pageTable = (uint32_t*)(pageDirectory[pageDirIndex] & 0xFFFFFC00);
|
|
231 }
|
|
232
|
|
233 void INT14handler(unsigned int address)
|
|
234 {
|
|
235 printf("INT14 called! Page fault for address 0x%x!\n", address);
|
|
236 if ( (address & 0xF0000000) == 0xD0000000 ) {
|
|
237 mappage(address & 0xFFFFF000);
|
|
238 return;
|
|
239 }
|
|
240
|
|
241 panic("Unhandled exception!");
|
|
242 }
|
|
243
|
|
244 void INT15handler()
|
|
245 {
|
|
246 printf("INT15 called!\n");
|
|
247 panic("Unhandled exception!");
|
|
248 }
|
|
249
|
|
250 void INT16handler()
|
|
251 {
|
|
252 printf("INT16 called!\n");
|
|
253 panic("Unhandled exception!");
|
|
254 }
|
|
255
|
|
256 void INT17handler()
|
|
257 {
|
|
258 printf("INT17 called!\n");
|
|
259 panic("Unhandled exception!");
|
|
260 }
|
|
261
|
|
262 void INT18handler()
|
|
263 {
|
|
264 printf("INT18 called!\n");
|
|
265 panic("Unhandled exception!");
|
|
266 }
|
|
267
|
|
268 void INT19handler()
|
|
269 {
|
|
270 printf("INT19 called!\n");
|
|
271 panic("Unhandled exception!");
|
|
272 }
|
|
273
|
|
274 // remapped IRQ from master PIC:
|
|
275 void INT32handler()
|
|
276 {
|
|
277 // System timer.
|
|
278 //printf("INT32 called!\n");
|
|
279 // called very frequent, what is this?
|
|
280 timerDriverUpdate();
|
|
281 outb(0x20, 0x20); // EOI to master
|
|
282 }
|
|
283
|
|
284 void INT33handler()
|
|
285 {
|
|
286 //printf("INT33 called, key pressed????\n");
|
|
287 unsigned char scancode = inb(0x60);
|
|
288 //printf("Scancode = 0x%x\n", scancode);
|
|
289 keyboardDriverUpdate(scancode);
|
|
290 outb(0x20, 0x20); // EOI to master
|
|
291 }
|
|
292
|
|
293 void INT34handler()
|
|
294 {
|
|
295 printf("INT34 called!\n");
|
|
296 }
|
|
297
|
|
298 void INTDEF_handler()
|
|
299 {
|
|
300 printf("Default int handler called\n");
|
|
301 }
|
|
302
|
|
303
|