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