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