Mercurial > lcfOS
annotate cos/kernel/handlers.c @ 33:d8185ddb6c7b
Added more interrupt handlers
author | windel |
---|---|
date | Sun, 15 Jan 2012 13:39:49 +0100 |
parents | 3a6a9b929db0 |
children | 91f91ff07ea8 |
rev | line source |
---|---|
14 | 1 #include "kernel.h" |
2 | |
3 // Assembler wrapper prototypes: | |
31 | 4 void INT255(void); |
14 | 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 INT16(void); | |
21 void INT17(void); | |
22 void INT18(void); | |
23 void INT19(void); | |
33 | 24 // Remapped irq assembler wrappers: |
14 | 25 void INT32(void); |
26 void INT33(void); | |
27 void INT34(void); | |
33 | 28 void INT35(void); |
29 void INT36(void); | |
30 void INT37(void); | |
31 void INT38(void); | |
32 void INT39(void); | |
33 void INT40(void); | |
34 void INT41(void); | |
35 void INT42(void); | |
36 void INT43(void); | |
37 void INT44(void); | |
38 void INT45(void); | |
39 void INT46(void); | |
40 void INT47(void); | |
14 | 41 |
42 // THE interrupt descriptor table: | |
24 | 43 IDT_entry *idt = (IDT_entry*)0x5000; |
17 | 44 volatile idtPointer idtP; |
14 | 45 |
17 | 46 void setIDTentry(int num, void (*handler)(), uint16_t selector, uint8_t flags) |
14 | 47 { |
48 // Fill one entry with IDT info: | |
49 uint64_t offset; | |
50 | |
17 | 51 // Typecast the function pointer to a number: |
52 offset = (uint64_t)handler; | |
14 | 53 |
17 | 54 // Set offset: |
55 idt[num].baseLow = offset & 0xFFFF; | |
56 idt[num].baseMid = (offset >> 16) & 0xFFFF; | |
57 idt[num].baseHigh = (offset >> 32) & 0xFFFFFFFF; | |
58 | |
59 // Set flags and selector: | |
60 idt[num].selector = selector; | |
61 idt[num].flags = flags; | |
62 | |
63 // Set reserved fields: | |
64 idt[num].reserved1 = 0; | |
65 idt[num].reserved2 = 0; | |
14 | 66 } |
67 | |
68 void setupIDT(void) { | |
69 int i; | |
26 | 70 |
14 | 71 // Fill all vectors with the default handler: |
72 for (i=0; i<256; i++) { | |
31 | 73 setIDTentry(i, INT255, 0x08, 0x8E); |
14 | 74 } |
75 | |
76 // Now set other then default handler: | |
17 | 77 setIDTentry(0, INT0, 0x08, 0x8E); |
78 setIDTentry(1, INT1, 0x08, 0x8E); | |
79 setIDTentry(2, INT2, 0x08, 0x8E); | |
80 setIDTentry(3, INT3, 0x08, 0x8E); | |
81 setIDTentry(4, INT4, 0x08, 0x8E); | |
82 setIDTentry(5, INT5, 0x08, 0x8E); | |
83 setIDTentry(6, INT6, 0x08, 0x8E); | |
84 setIDTentry(7, INT7, 0x08, 0x8E); | |
85 setIDTentry(8, INT8, 0x08, 0x8E); | |
86 setIDTentry(9, INT9, 0x08, 0x8E); | |
87 setIDTentry(10, INT10, 0x08, 0x8E); | |
88 setIDTentry(11, INT11, 0x08, 0x8E); | |
89 setIDTentry(12, INT12, 0x08, 0x8E); | |
90 setIDTentry(13, INT13, 0x08, 0x8E); | |
91 setIDTentry(14, INT14, 0x08, 0x8E); | |
31 | 92 //setIDTentry(15, INT15, 0x08, 0x8E); |
17 | 93 setIDTentry(16, INT16, 0x08, 0x8E); |
94 setIDTentry(17, INT17, 0x08, 0x8E); | |
95 setIDTentry(18, INT18, 0x08, 0x8E); | |
96 setIDTentry(19, INT19, 0x08, 0x8E); | |
14 | 97 /* reserved interrupts: */ |
98 // From int20 - int31 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
17
diff
changeset
|
99 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
|
100 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
|
101 setIDTentry(34, INT34, 0x08, 0x8F); |
33 | 102 setIDTentry(35, INT35, 0x08, 0x8F); |
103 setIDTentry(36, INT36, 0x08, 0x8F); | |
104 setIDTentry(37, INT37, 0x08, 0x8F); | |
105 setIDTentry(38, INT38, 0x08, 0x8F); | |
106 setIDTentry(39, INT39, 0x08, 0x8F); | |
107 setIDTentry(40, INT40, 0x08, 0x8F); | |
108 setIDTentry(41, INT41, 0x08, 0x8F); | |
109 setIDTentry(42, INT42, 0x08, 0x8F); | |
110 setIDTentry(43, INT43, 0x08, 0x8F); | |
111 setIDTentry(44, INT44, 0x08, 0x8F); | |
112 setIDTentry(45, INT45, 0x08, 0x8F); | |
113 setIDTentry(46, INT46, 0x08, 0x8F); | |
114 setIDTentry(47, INT47, 0x08, 0x8F); | |
14 | 115 |
17 | 116 // Set the correct values in the IDT pointer: |
21 | 117 idtP.base = (uint64_t)idt; |
17 | 118 idtP.limit = (sizeof(IDT_entry) * 256) - 1; |
14 | 119 // call load IDT asm function: |
17 | 120 loadIDT(); |
14 | 121 |
122 PICremap(); | |
17 | 123 asm("sti"); |
14 | 124 } |
125 | |
126 // PIC functions: | |
127 void PICremap() { | |
128 unsigned char maskmaster, maskslave, pic1, pic2; | |
129 pic1 = 0x20; // remapping location master | |
130 pic2 = 0x28; | |
131 maskmaster = inb(0x21); // master cmd=0x20, data=0x21 | |
132 maskslave = inb(0xA1); // slave command=0xA0, data=0xA1 | |
133 | |
134 outb(0x20, 0x20); // end of init | |
135 | |
136 outb(0x20, 0x11); // init + ICW1_ICW2 | |
137 outb(0xA0, 0x11); // init + ICW1_ICW2 | |
138 | |
139 outb(0x21, pic1); // ICW2, write offset | |
140 outb(0xA1, pic2); // ICW2 | |
141 | |
142 outb(0x21, 4); // ICW3, write a 4! | |
143 outb(0xA1, 2); // ICW3, write a 2! | |
144 | |
145 outb(0x21, 1); // ICW4, 8086 mode | |
146 outb(0xA1, 1); // ICW4 | |
147 | |
148 outb(0x21, maskmaster); | |
149 outb(0xA1, maskslave); | |
150 } | |
151 | |
33 | 152 void gp_fault(uint64_t *regs) |
153 { | |
154 printf("GP fault\n"); | |
155 printf("Error code: %x\n", regs[8]); | |
156 printf("RIP: %x\n", regs[9]); | |
157 panic("No resolution to general protection fault!"); | |
158 } | |
159 | |
160 void page_fault(uint64_t *regs) | |
161 { | |
162 printf("Page fault\n"); | |
163 uint64_t faulting_address; | |
164 asm volatile("mov %%cr2, %0" : "=r" (faulting_address)); | |
165 printf("Error code: %x\n", regs[8]); | |
166 printf("RIP: %x\n", regs[9]); | |
167 printf("Faulting address: %x\n", faulting_address); | |
168 panic("No resolution to page fault!"); | |
169 | |
170 } | |
31 | 171 // Global isr handler: |
14 | 172 |
31 | 173 // Hopefully, this function get called with the correct registers. |
174 void isr_handler(uint64_t* registers) | |
14 | 175 { |
31 | 176 uint64_t intnum = registers[7]; |
14 | 177 |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
178 // TODO: make a list with handler pointers: |
31 | 179 if (intnum == 32) |
180 { | |
181 timerDriverUpdate(); | |
182 } | |
183 else if (intnum == 33) | |
184 { | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
185 keyboardDriverUpdate(); |
31 | 186 } |
33 | 187 else if (intnum == 13) |
188 { | |
189 gp_fault(registers); | |
190 } | |
191 else if (intnum == 14) | |
192 { | |
193 page_fault(registers); | |
194 } | |
195 else if (intnum == 39) | |
196 { | |
197 printf("Spurious interrupt\n"); | |
198 } | |
31 | 199 else |
200 { | |
33 | 201 printf("Interrupt %d called, registers at: %x\n", registers[7], registers); |
202 panic("Paniek! Unhandled interrupt!"); | |
31 | 203 } |
14 | 204 |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
205 // TODO: EOI to slave? |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
206 // TODO: replace this with APIC code? |
31 | 207 outb(0x20, 0x20); // EOI to master |
14 | 208 } |
209 | |
31 | 210 // Interrupt service routines: |
14 | 211 |
20 | 212 void INT14handler() |
14 | 213 { |
20 | 214 uint64_t faulting_address; |
215 | |
29 | 216 printf("Segfault!\n"); |
217 | |
20 | 218 // Retrieve failed page from CR2: |
219 asm volatile("mov %%cr2, %0" : "=r" (faulting_address)); | |
220 | |
221 printf("INT14 called! Page fault for address 0x%X!\n", faulting_address); | |
14 | 222 |
223 panic("Unhandled exception!"); | |
224 } | |
225 | |
226 void INT15handler() | |
227 { | |
228 printf("INT15 called!\n"); | |
229 panic("Unhandled exception!"); | |
230 } | |
231 | |
232 void INTDEF_handler() | |
233 { | |
29 | 234 panic("Default int handler called\n"); |
14 | 235 } |
236 |