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