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