Mercurial > lcfOS
annotate cos/kernel/handlers.c @ 290:7b38782ed496
File moves
author | Windel Bouwman |
---|---|
date | Sun, 24 Nov 2013 11:24:15 +0100 |
parents | 24ce177e01e8 |
children |
rev | line source |
---|---|
14 | 1 #include "kernel.h" |
2 | |
3 // Assembler wrapper prototypes: | |
40 | 4 extern void INT255(void); |
5 extern void INT0(void); | |
6 extern void INT1(void); | |
7 extern void INT2(void); | |
8 extern void INT3(void); | |
9 extern void INT4(void); | |
10 extern void INT5(void); | |
11 extern void INT6(void); | |
12 extern void INT7(void); | |
13 extern void INT8(void); | |
14 extern void INT9(void); | |
15 extern void INT10(void); | |
16 extern void INT11(void); | |
17 extern void INT12(void); | |
18 extern void INT13(void); | |
19 extern void INT14(void); | |
20 extern void INT16(void); | |
21 extern void INT17(void); | |
22 extern void INT18(void); | |
23 extern void INT19(void); | |
33 | 24 // Remapped irq assembler wrappers: |
40 | 25 extern void INT32(void); |
26 extern void INT33(void); | |
27 extern void INT34(void); | |
28 extern void INT35(void); | |
29 extern void INT36(void); | |
30 extern void INT37(void); | |
31 extern void INT38(void); | |
32 extern void INT39(void); | |
33 extern void INT40(void); | |
34 extern void INT41(void); | |
35 extern void INT42(void); | |
36 extern void INT43(void); | |
37 extern void INT44(void); | |
38 extern void INT45(void); | |
39 extern void INT46(void); | |
40 extern 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 { |
36 | 48 // Fill one entry with IDT info: |
49 uint64_t offset; | |
14 | 50 |
36 | 51 // Typecast the function pointer to a number: |
52 offset = (uint64_t)handler; | |
14 | 53 |
36 | 54 // Set offset: |
55 idt[num].baseLow = offset & 0xFFFF; | |
56 idt[num].baseMid = (offset >> 16) & 0xFFFF; | |
57 idt[num].baseHigh = (offset >> 32) & 0xFFFFFFFF; | |
17 | 58 |
36 | 59 // Set flags and selector: |
60 idt[num].selector = selector; | |
61 idt[num].flags = flags; | |
17 | 62 |
36 | 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 | |
36 | 152 /* Function that prints the contents of all registers |
153 on the interrupted stack. */ | |
154 void display_registers(uint64_t *regs) | |
155 { | |
156 // Pushed by interrupt handler assembler: | |
157 printf("rax: %x\n", regs[6]); | |
158 printf("rbx: %x\n", regs[3]); | |
159 printf("rcx: %x\n", regs[5]); | |
160 printf("rdx: %x\n", regs[4]); | |
161 printf("rsi: %x\n", regs[1]); | |
162 printf("rdi: %x\n", regs[0]); | |
163 printf("rbp: %x\n", regs[2]); | |
164 // Pushed by CPU: | |
165 printf("rip: %x\n", regs[9]); | |
166 } | |
167 | |
33 | 168 void gp_fault(uint64_t *regs) |
169 { | |
170 printf("GP fault\n"); | |
171 printf("Error code: %x\n", regs[8]); | |
36 | 172 display_registers(regs); |
33 | 173 panic("No resolution to general protection fault!"); |
174 } | |
175 | |
176 void page_fault(uint64_t *regs) | |
177 { | |
178 printf("Page fault\n"); | |
179 uint64_t faulting_address; | |
180 asm volatile("mov %%cr2, %0" : "=r" (faulting_address)); | |
181 printf("Error code: %x\n", regs[8]); | |
36 | 182 display_registers(regs); |
33 | 183 printf("Faulting address: %x\n", faulting_address); |
184 panic("No resolution to page fault!"); | |
185 | |
186 } | |
40 | 187 |
31 | 188 // Global isr handler: |
189 // Hopefully, this function get called with the correct registers. | |
190 void isr_handler(uint64_t* registers) | |
14 | 191 { |
31 | 192 uint64_t intnum = registers[7]; |
14 | 193 |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
194 // TODO: make a list with handler pointers: |
31 | 195 if (intnum == 32) |
196 { | |
197 timerDriverUpdate(); | |
198 } | |
199 else if (intnum == 33) | |
200 { | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
201 keyboardDriverUpdate(); |
31 | 202 } |
33 | 203 else if (intnum == 13) |
204 { | |
205 gp_fault(registers); | |
206 } | |
207 else if (intnum == 14) | |
208 { | |
209 page_fault(registers); | |
210 } | |
211 else if (intnum == 39) | |
212 { | |
213 printf("Spurious interrupt\n"); | |
214 } | |
31 | 215 else |
216 { | |
33 | 217 printf("Interrupt %d called, registers at: %x\n", registers[7], registers); |
218 panic("Paniek! Unhandled interrupt!"); | |
31 | 219 } |
14 | 220 |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
221 // TODO: EOI to slave? |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
222 // TODO: replace this with APIC code? |
31 | 223 outb(0x20, 0x20); // EOI to master |
14 | 224 } |
225 |