comparison cos/kernel/handlers.c @ 14:a58904747019

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