diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index f0fdbae..df975de 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -29,7 +29,7 @@ _start: mov [ebxStart], ebx mov esp, stackEnd - lgdt [gdt32.end] + lgdt [earlyGDT.end] mov ax, 16 mov ds, ax mov ss, ax @@ -91,10 +91,9 @@ pop eax ret -ALIGN 4 -gdt32: +earlyGDT: +.start: dq 0 - .code: dw 0xffff dw 0 @@ -102,7 +101,6 @@ db 10011010b db 11001111b db 0 - .data: dw 0xffff dw 0 @@ -110,7 +108,31 @@ db 10010010b db 11001111b db 0 +.end: + dw .end - .start - 1 + dd .start +section .sharedFunctions +tss: + resb 0x68 +ALIGN 4 +newGDT: +.start: + dq 0 +.code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 +.data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 .userCode: dw 0xffff dw 0 @@ -125,16 +147,18 @@ db 10010010b db 11001111b db 0 +.tss: + resb 0x8 .end: - dw .end - gdt32 - 1 - dd gdt32 + dw .end - .start - 1 + dd .start + +global newGDT +global tss section .text -newGDT: - dw gdt32.end - gdt32 - 1 - dd gdt32 + 0xFFB00000 higherKernelEntry: - lgdt [newGDT] + lgdt [newGDT.end] mov ebx, [ebxStart] push ebx .cleanOriginalEntryCode: diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index f0fdbae..df975de 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -29,7 +29,7 @@ _start: mov [ebxStart], ebx mov esp, stackEnd - lgdt [gdt32.end] + lgdt [earlyGDT.end] mov ax, 16 mov ds, ax mov ss, ax @@ -91,10 +91,9 @@ pop eax ret -ALIGN 4 -gdt32: +earlyGDT: +.start: dq 0 - .code: dw 0xffff dw 0 @@ -102,7 +101,6 @@ db 10011010b db 11001111b db 0 - .data: dw 0xffff dw 0 @@ -110,7 +108,31 @@ db 10010010b db 11001111b db 0 +.end: + dw .end - .start - 1 + dd .start +section .sharedFunctions +tss: + resb 0x68 +ALIGN 4 +newGDT: +.start: + dq 0 +.code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 +.data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 .userCode: dw 0xffff dw 0 @@ -125,16 +147,18 @@ db 10010010b db 11001111b db 0 +.tss: + resb 0x8 .end: - dw .end - gdt32 - 1 - dd gdt32 + dw .end - .start - 1 + dd .start + +global newGDT +global tss section .text -newGDT: - dw gdt32.end - gdt32 - 1 - dd gdt32 + 0xFFB00000 higherKernelEntry: - lgdt [newGDT] + lgdt [newGDT.end] mov ebx, [ebxStart] push ebx .cleanOriginalEntryCode: diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 6ec652c..ae7c56f 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -1,15 +1,26 @@ section .sharedFunctions +extern onInterrupt + +global interruptStack +interruptStack: resb 1024 + handleInterrupt: - mov eax, 8 - jmp $ + mov eax, cr3 + push eax + mov eax, 0x500000 + mov cr3, eax + call onInterrupt + pop eax + mov cr3, eax + iret %macro interruptHandler 1 ALIGN 4 global idtHandler%1 idtHandler%1: - mov ebx, %1 - xor ecx, ecx + push 0 + push %1 jmp handleInterrupt %endmacro @@ -17,8 +28,7 @@ align 4 global idtHandler%1 idtHandler%1: - pop ecx - mov ebx, %1 + push %1 jmp handleInterrupt %endmacro @@ -54,3 +64,227 @@ interruptHandler 29 interruptHandlerError 30 interruptHandler 31 +interruptHandler 32 +interruptHandler 33 +interruptHandler 34 +interruptHandler 35 +interruptHandler 36 +interruptHandler 37 +interruptHandler 38 +interruptHandler 39 +interruptHandler 40 +interruptHandler 41 +interruptHandler 42 +interruptHandler 43 +interruptHandler 44 +interruptHandler 45 +interruptHandler 46 +interruptHandler 47 +interruptHandler 48 +interruptHandler 49 +interruptHandler 50 +interruptHandler 51 +interruptHandler 52 +interruptHandler 53 +interruptHandler 54 +interruptHandler 55 +interruptHandler 56 +interruptHandler 57 +interruptHandler 58 +interruptHandler 59 +interruptHandler 60 +interruptHandler 61 +interruptHandler 62 +interruptHandler 63 +interruptHandler 64 +interruptHandler 65 +interruptHandler 66 +interruptHandler 67 +interruptHandler 68 +interruptHandler 69 +interruptHandler 70 +interruptHandler 71 +interruptHandler 72 +interruptHandler 73 +interruptHandler 74 +interruptHandler 75 +interruptHandler 76 +interruptHandler 77 +interruptHandler 78 +interruptHandler 79 +interruptHandler 80 +interruptHandler 81 +interruptHandler 82 +interruptHandler 83 +interruptHandler 84 +interruptHandler 85 +interruptHandler 86 +interruptHandler 87 +interruptHandler 88 +interruptHandler 89 +interruptHandler 90 +interruptHandler 91 +interruptHandler 92 +interruptHandler 93 +interruptHandler 94 +interruptHandler 95 +interruptHandler 96 +interruptHandler 97 +interruptHandler 98 +interruptHandler 99 +interruptHandler 100 +interruptHandler 101 +interruptHandler 102 +interruptHandler 103 +interruptHandler 104 +interruptHandler 105 +interruptHandler 106 +interruptHandler 107 +interruptHandler 108 +interruptHandler 109 +interruptHandler 110 +interruptHandler 111 +interruptHandler 112 +interruptHandler 113 +interruptHandler 114 +interruptHandler 115 +interruptHandler 116 +interruptHandler 117 +interruptHandler 118 +interruptHandler 119 +interruptHandler 120 +interruptHandler 121 +interruptHandler 122 +interruptHandler 123 +interruptHandler 124 +interruptHandler 125 +interruptHandler 126 +interruptHandler 127 +interruptHandler 128 +interruptHandler 129 +interruptHandler 130 +interruptHandler 131 +interruptHandler 132 +interruptHandler 133 +interruptHandler 134 +interruptHandler 135 +interruptHandler 136 +interruptHandler 137 +interruptHandler 138 +interruptHandler 139 +interruptHandler 140 +interruptHandler 141 +interruptHandler 142 +interruptHandler 143 +interruptHandler 144 +interruptHandler 145 +interruptHandler 146 +interruptHandler 147 +interruptHandler 148 +interruptHandler 149 +interruptHandler 150 +interruptHandler 151 +interruptHandler 152 +interruptHandler 153 +interruptHandler 154 +interruptHandler 155 +interruptHandler 156 +interruptHandler 157 +interruptHandler 158 +interruptHandler 159 +interruptHandler 160 +interruptHandler 161 +interruptHandler 162 +interruptHandler 163 +interruptHandler 164 +interruptHandler 165 +interruptHandler 166 +interruptHandler 167 +interruptHandler 168 +interruptHandler 169 +interruptHandler 170 +interruptHandler 171 +interruptHandler 172 +interruptHandler 173 +interruptHandler 174 +interruptHandler 175 +interruptHandler 176 +interruptHandler 177 +interruptHandler 178 +interruptHandler 179 +interruptHandler 180 +interruptHandler 181 +interruptHandler 182 +interruptHandler 183 +interruptHandler 184 +interruptHandler 185 +interruptHandler 186 +interruptHandler 187 +interruptHandler 188 +interruptHandler 189 +interruptHandler 190 +interruptHandler 191 +interruptHandler 192 +interruptHandler 193 +interruptHandler 194 +interruptHandler 195 +interruptHandler 196 +interruptHandler 197 +interruptHandler 198 +interruptHandler 199 +interruptHandler 200 +interruptHandler 201 +interruptHandler 202 +interruptHandler 203 +interruptHandler 204 +interruptHandler 205 +interruptHandler 206 +interruptHandler 207 +interruptHandler 208 +interruptHandler 209 +interruptHandler 210 +interruptHandler 211 +interruptHandler 212 +interruptHandler 213 +interruptHandler 214 +interruptHandler 215 +interruptHandler 216 +interruptHandler 217 +interruptHandler 218 +interruptHandler 219 +interruptHandler 220 +interruptHandler 221 +interruptHandler 222 +interruptHandler 223 +interruptHandler 224 +interruptHandler 225 +interruptHandler 226 +interruptHandler 227 +interruptHandler 228 +interruptHandler 229 +interruptHandler 230 +interruptHandler 231 +interruptHandler 232 +interruptHandler 233 +interruptHandler 234 +interruptHandler 235 +interruptHandler 236 +interruptHandler 237 +interruptHandler 238 +interruptHandler 239 +interruptHandler 240 +interruptHandler 241 +interruptHandler 242 +interruptHandler 243 +interruptHandler 244 +interruptHandler 245 +interruptHandler 246 +interruptHandler 247 +interruptHandler 248 +interruptHandler 249 +interruptHandler 250 +interruptHandler 251 +interruptHandler 252 +interruptHandler 253 +interruptHandler 254 +interruptHandler 255 diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index f0fdbae..df975de 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -29,7 +29,7 @@ _start: mov [ebxStart], ebx mov esp, stackEnd - lgdt [gdt32.end] + lgdt [earlyGDT.end] mov ax, 16 mov ds, ax mov ss, ax @@ -91,10 +91,9 @@ pop eax ret -ALIGN 4 -gdt32: +earlyGDT: +.start: dq 0 - .code: dw 0xffff dw 0 @@ -102,7 +101,6 @@ db 10011010b db 11001111b db 0 - .data: dw 0xffff dw 0 @@ -110,7 +108,31 @@ db 10010010b db 11001111b db 0 +.end: + dw .end - .start - 1 + dd .start +section .sharedFunctions +tss: + resb 0x68 +ALIGN 4 +newGDT: +.start: + dq 0 +.code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 +.data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 .userCode: dw 0xffff dw 0 @@ -125,16 +147,18 @@ db 10010010b db 11001111b db 0 +.tss: + resb 0x8 .end: - dw .end - gdt32 - 1 - dd gdt32 + dw .end - .start - 1 + dd .start + +global newGDT +global tss section .text -newGDT: - dw gdt32.end - gdt32 - 1 - dd gdt32 + 0xFFB00000 higherKernelEntry: - lgdt [newGDT] + lgdt [newGDT.end] mov ebx, [ebxStart] push ebx .cleanOriginalEntryCode: diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 6ec652c..ae7c56f 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -1,15 +1,26 @@ section .sharedFunctions +extern onInterrupt + +global interruptStack +interruptStack: resb 1024 + handleInterrupt: - mov eax, 8 - jmp $ + mov eax, cr3 + push eax + mov eax, 0x500000 + mov cr3, eax + call onInterrupt + pop eax + mov cr3, eax + iret %macro interruptHandler 1 ALIGN 4 global idtHandler%1 idtHandler%1: - mov ebx, %1 - xor ecx, ecx + push 0 + push %1 jmp handleInterrupt %endmacro @@ -17,8 +28,7 @@ align 4 global idtHandler%1 idtHandler%1: - pop ecx - mov ebx, %1 + push %1 jmp handleInterrupt %endmacro @@ -54,3 +64,227 @@ interruptHandler 29 interruptHandlerError 30 interruptHandler 31 +interruptHandler 32 +interruptHandler 33 +interruptHandler 34 +interruptHandler 35 +interruptHandler 36 +interruptHandler 37 +interruptHandler 38 +interruptHandler 39 +interruptHandler 40 +interruptHandler 41 +interruptHandler 42 +interruptHandler 43 +interruptHandler 44 +interruptHandler 45 +interruptHandler 46 +interruptHandler 47 +interruptHandler 48 +interruptHandler 49 +interruptHandler 50 +interruptHandler 51 +interruptHandler 52 +interruptHandler 53 +interruptHandler 54 +interruptHandler 55 +interruptHandler 56 +interruptHandler 57 +interruptHandler 58 +interruptHandler 59 +interruptHandler 60 +interruptHandler 61 +interruptHandler 62 +interruptHandler 63 +interruptHandler 64 +interruptHandler 65 +interruptHandler 66 +interruptHandler 67 +interruptHandler 68 +interruptHandler 69 +interruptHandler 70 +interruptHandler 71 +interruptHandler 72 +interruptHandler 73 +interruptHandler 74 +interruptHandler 75 +interruptHandler 76 +interruptHandler 77 +interruptHandler 78 +interruptHandler 79 +interruptHandler 80 +interruptHandler 81 +interruptHandler 82 +interruptHandler 83 +interruptHandler 84 +interruptHandler 85 +interruptHandler 86 +interruptHandler 87 +interruptHandler 88 +interruptHandler 89 +interruptHandler 90 +interruptHandler 91 +interruptHandler 92 +interruptHandler 93 +interruptHandler 94 +interruptHandler 95 +interruptHandler 96 +interruptHandler 97 +interruptHandler 98 +interruptHandler 99 +interruptHandler 100 +interruptHandler 101 +interruptHandler 102 +interruptHandler 103 +interruptHandler 104 +interruptHandler 105 +interruptHandler 106 +interruptHandler 107 +interruptHandler 108 +interruptHandler 109 +interruptHandler 110 +interruptHandler 111 +interruptHandler 112 +interruptHandler 113 +interruptHandler 114 +interruptHandler 115 +interruptHandler 116 +interruptHandler 117 +interruptHandler 118 +interruptHandler 119 +interruptHandler 120 +interruptHandler 121 +interruptHandler 122 +interruptHandler 123 +interruptHandler 124 +interruptHandler 125 +interruptHandler 126 +interruptHandler 127 +interruptHandler 128 +interruptHandler 129 +interruptHandler 130 +interruptHandler 131 +interruptHandler 132 +interruptHandler 133 +interruptHandler 134 +interruptHandler 135 +interruptHandler 136 +interruptHandler 137 +interruptHandler 138 +interruptHandler 139 +interruptHandler 140 +interruptHandler 141 +interruptHandler 142 +interruptHandler 143 +interruptHandler 144 +interruptHandler 145 +interruptHandler 146 +interruptHandler 147 +interruptHandler 148 +interruptHandler 149 +interruptHandler 150 +interruptHandler 151 +interruptHandler 152 +interruptHandler 153 +interruptHandler 154 +interruptHandler 155 +interruptHandler 156 +interruptHandler 157 +interruptHandler 158 +interruptHandler 159 +interruptHandler 160 +interruptHandler 161 +interruptHandler 162 +interruptHandler 163 +interruptHandler 164 +interruptHandler 165 +interruptHandler 166 +interruptHandler 167 +interruptHandler 168 +interruptHandler 169 +interruptHandler 170 +interruptHandler 171 +interruptHandler 172 +interruptHandler 173 +interruptHandler 174 +interruptHandler 175 +interruptHandler 176 +interruptHandler 177 +interruptHandler 178 +interruptHandler 179 +interruptHandler 180 +interruptHandler 181 +interruptHandler 182 +interruptHandler 183 +interruptHandler 184 +interruptHandler 185 +interruptHandler 186 +interruptHandler 187 +interruptHandler 188 +interruptHandler 189 +interruptHandler 190 +interruptHandler 191 +interruptHandler 192 +interruptHandler 193 +interruptHandler 194 +interruptHandler 195 +interruptHandler 196 +interruptHandler 197 +interruptHandler 198 +interruptHandler 199 +interruptHandler 200 +interruptHandler 201 +interruptHandler 202 +interruptHandler 203 +interruptHandler 204 +interruptHandler 205 +interruptHandler 206 +interruptHandler 207 +interruptHandler 208 +interruptHandler 209 +interruptHandler 210 +interruptHandler 211 +interruptHandler 212 +interruptHandler 213 +interruptHandler 214 +interruptHandler 215 +interruptHandler 216 +interruptHandler 217 +interruptHandler 218 +interruptHandler 219 +interruptHandler 220 +interruptHandler 221 +interruptHandler 222 +interruptHandler 223 +interruptHandler 224 +interruptHandler 225 +interruptHandler 226 +interruptHandler 227 +interruptHandler 228 +interruptHandler 229 +interruptHandler 230 +interruptHandler 231 +interruptHandler 232 +interruptHandler 233 +interruptHandler 234 +interruptHandler 235 +interruptHandler 236 +interruptHandler 237 +interruptHandler 238 +interruptHandler 239 +interruptHandler 240 +interruptHandler 241 +interruptHandler 242 +interruptHandler 243 +interruptHandler 244 +interruptHandler 245 +interruptHandler 246 +interruptHandler 247 +interruptHandler 248 +interruptHandler 249 +interruptHandler 250 +interruptHandler 251 +interruptHandler 252 +interruptHandler 253 +interruptHandler 254 +interruptHandler 255 diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 4976243..98c18ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -1,5 +1,6 @@ #include "interrupts.h" #include +#include #include #define IDT_ENTRY(i) \ @@ -8,11 +9,44 @@ extern void *idt; +extern GDTEntry newGDT; +extern TSS tss; + __attribute__((section(".sharedFunction"))) __attribute__((aligned(0x10))) IdtEntry idtEntries[256] = {}; +void onInterrupt(uint32_t cr3, uint32_t intNo, uint32_t errorCode) { + if (intNo > 31) { + // an external interrupt was triggered + while (1) + ; + } + if (intNo <= 31 && cr3 != 0x500000) { + // a task encountered an exception + asm(".intel_syntax noprefix\n" + "mov eax, [temporaryESP]\n" + "mov esp, eax\n" + "pop ebp\n" + "ret\n" + ".att_syntax"); + } +} + +extern void *interruptStack; + void registerInterrupts() { - for (uint16_t i = 0; i < 16; i++) { + GDTEntry *currentGdt = &newGDT; + currentGdt[5].limit = sizeof(TSS); + currentGdt[5].baseLow = U32(&tss); + currentGdt[5].baseMid = U32(&tss) >> 16; + currentGdt[5].baseHigh = U32(&tss) >> 24; + currentGdt[5].access = 0xE9; + currentGdt[5].granularity = 0; + tss.ss0 = tss.ss = 0x10; + tss.esp0 = tss.esp = U32(&interruptStack) + 1024; + asm("mov $40, %%ax" ::); + asm("ltr %%ax" ::); + for (uint16_t i = 0; i < 256; i++) { idtEntries[i].reserved = 0; idtEntries[i].type = 0x8E; idtEntries[i].segment = 0x8; diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index f0fdbae..df975de 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -29,7 +29,7 @@ _start: mov [ebxStart], ebx mov esp, stackEnd - lgdt [gdt32.end] + lgdt [earlyGDT.end] mov ax, 16 mov ds, ax mov ss, ax @@ -91,10 +91,9 @@ pop eax ret -ALIGN 4 -gdt32: +earlyGDT: +.start: dq 0 - .code: dw 0xffff dw 0 @@ -102,7 +101,6 @@ db 10011010b db 11001111b db 0 - .data: dw 0xffff dw 0 @@ -110,7 +108,31 @@ db 10010010b db 11001111b db 0 +.end: + dw .end - .start - 1 + dd .start +section .sharedFunctions +tss: + resb 0x68 +ALIGN 4 +newGDT: +.start: + dq 0 +.code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 +.data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 .userCode: dw 0xffff dw 0 @@ -125,16 +147,18 @@ db 10010010b db 11001111b db 0 +.tss: + resb 0x8 .end: - dw .end - gdt32 - 1 - dd gdt32 + dw .end - .start - 1 + dd .start + +global newGDT +global tss section .text -newGDT: - dw gdt32.end - gdt32 - 1 - dd gdt32 + 0xFFB00000 higherKernelEntry: - lgdt [newGDT] + lgdt [newGDT.end] mov ebx, [ebxStart] push ebx .cleanOriginalEntryCode: diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 6ec652c..ae7c56f 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -1,15 +1,26 @@ section .sharedFunctions +extern onInterrupt + +global interruptStack +interruptStack: resb 1024 + handleInterrupt: - mov eax, 8 - jmp $ + mov eax, cr3 + push eax + mov eax, 0x500000 + mov cr3, eax + call onInterrupt + pop eax + mov cr3, eax + iret %macro interruptHandler 1 ALIGN 4 global idtHandler%1 idtHandler%1: - mov ebx, %1 - xor ecx, ecx + push 0 + push %1 jmp handleInterrupt %endmacro @@ -17,8 +28,7 @@ align 4 global idtHandler%1 idtHandler%1: - pop ecx - mov ebx, %1 + push %1 jmp handleInterrupt %endmacro @@ -54,3 +64,227 @@ interruptHandler 29 interruptHandlerError 30 interruptHandler 31 +interruptHandler 32 +interruptHandler 33 +interruptHandler 34 +interruptHandler 35 +interruptHandler 36 +interruptHandler 37 +interruptHandler 38 +interruptHandler 39 +interruptHandler 40 +interruptHandler 41 +interruptHandler 42 +interruptHandler 43 +interruptHandler 44 +interruptHandler 45 +interruptHandler 46 +interruptHandler 47 +interruptHandler 48 +interruptHandler 49 +interruptHandler 50 +interruptHandler 51 +interruptHandler 52 +interruptHandler 53 +interruptHandler 54 +interruptHandler 55 +interruptHandler 56 +interruptHandler 57 +interruptHandler 58 +interruptHandler 59 +interruptHandler 60 +interruptHandler 61 +interruptHandler 62 +interruptHandler 63 +interruptHandler 64 +interruptHandler 65 +interruptHandler 66 +interruptHandler 67 +interruptHandler 68 +interruptHandler 69 +interruptHandler 70 +interruptHandler 71 +interruptHandler 72 +interruptHandler 73 +interruptHandler 74 +interruptHandler 75 +interruptHandler 76 +interruptHandler 77 +interruptHandler 78 +interruptHandler 79 +interruptHandler 80 +interruptHandler 81 +interruptHandler 82 +interruptHandler 83 +interruptHandler 84 +interruptHandler 85 +interruptHandler 86 +interruptHandler 87 +interruptHandler 88 +interruptHandler 89 +interruptHandler 90 +interruptHandler 91 +interruptHandler 92 +interruptHandler 93 +interruptHandler 94 +interruptHandler 95 +interruptHandler 96 +interruptHandler 97 +interruptHandler 98 +interruptHandler 99 +interruptHandler 100 +interruptHandler 101 +interruptHandler 102 +interruptHandler 103 +interruptHandler 104 +interruptHandler 105 +interruptHandler 106 +interruptHandler 107 +interruptHandler 108 +interruptHandler 109 +interruptHandler 110 +interruptHandler 111 +interruptHandler 112 +interruptHandler 113 +interruptHandler 114 +interruptHandler 115 +interruptHandler 116 +interruptHandler 117 +interruptHandler 118 +interruptHandler 119 +interruptHandler 120 +interruptHandler 121 +interruptHandler 122 +interruptHandler 123 +interruptHandler 124 +interruptHandler 125 +interruptHandler 126 +interruptHandler 127 +interruptHandler 128 +interruptHandler 129 +interruptHandler 130 +interruptHandler 131 +interruptHandler 132 +interruptHandler 133 +interruptHandler 134 +interruptHandler 135 +interruptHandler 136 +interruptHandler 137 +interruptHandler 138 +interruptHandler 139 +interruptHandler 140 +interruptHandler 141 +interruptHandler 142 +interruptHandler 143 +interruptHandler 144 +interruptHandler 145 +interruptHandler 146 +interruptHandler 147 +interruptHandler 148 +interruptHandler 149 +interruptHandler 150 +interruptHandler 151 +interruptHandler 152 +interruptHandler 153 +interruptHandler 154 +interruptHandler 155 +interruptHandler 156 +interruptHandler 157 +interruptHandler 158 +interruptHandler 159 +interruptHandler 160 +interruptHandler 161 +interruptHandler 162 +interruptHandler 163 +interruptHandler 164 +interruptHandler 165 +interruptHandler 166 +interruptHandler 167 +interruptHandler 168 +interruptHandler 169 +interruptHandler 170 +interruptHandler 171 +interruptHandler 172 +interruptHandler 173 +interruptHandler 174 +interruptHandler 175 +interruptHandler 176 +interruptHandler 177 +interruptHandler 178 +interruptHandler 179 +interruptHandler 180 +interruptHandler 181 +interruptHandler 182 +interruptHandler 183 +interruptHandler 184 +interruptHandler 185 +interruptHandler 186 +interruptHandler 187 +interruptHandler 188 +interruptHandler 189 +interruptHandler 190 +interruptHandler 191 +interruptHandler 192 +interruptHandler 193 +interruptHandler 194 +interruptHandler 195 +interruptHandler 196 +interruptHandler 197 +interruptHandler 198 +interruptHandler 199 +interruptHandler 200 +interruptHandler 201 +interruptHandler 202 +interruptHandler 203 +interruptHandler 204 +interruptHandler 205 +interruptHandler 206 +interruptHandler 207 +interruptHandler 208 +interruptHandler 209 +interruptHandler 210 +interruptHandler 211 +interruptHandler 212 +interruptHandler 213 +interruptHandler 214 +interruptHandler 215 +interruptHandler 216 +interruptHandler 217 +interruptHandler 218 +interruptHandler 219 +interruptHandler 220 +interruptHandler 221 +interruptHandler 222 +interruptHandler 223 +interruptHandler 224 +interruptHandler 225 +interruptHandler 226 +interruptHandler 227 +interruptHandler 228 +interruptHandler 229 +interruptHandler 230 +interruptHandler 231 +interruptHandler 232 +interruptHandler 233 +interruptHandler 234 +interruptHandler 235 +interruptHandler 236 +interruptHandler 237 +interruptHandler 238 +interruptHandler 239 +interruptHandler 240 +interruptHandler 241 +interruptHandler 242 +interruptHandler 243 +interruptHandler 244 +interruptHandler 245 +interruptHandler 246 +interruptHandler 247 +interruptHandler 248 +interruptHandler 249 +interruptHandler 250 +interruptHandler 251 +interruptHandler 252 +interruptHandler 253 +interruptHandler 254 +interruptHandler 255 diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 4976243..98c18ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -1,5 +1,6 @@ #include "interrupts.h" #include +#include #include #define IDT_ENTRY(i) \ @@ -8,11 +9,44 @@ extern void *idt; +extern GDTEntry newGDT; +extern TSS tss; + __attribute__((section(".sharedFunction"))) __attribute__((aligned(0x10))) IdtEntry idtEntries[256] = {}; +void onInterrupt(uint32_t cr3, uint32_t intNo, uint32_t errorCode) { + if (intNo > 31) { + // an external interrupt was triggered + while (1) + ; + } + if (intNo <= 31 && cr3 != 0x500000) { + // a task encountered an exception + asm(".intel_syntax noprefix\n" + "mov eax, [temporaryESP]\n" + "mov esp, eax\n" + "pop ebp\n" + "ret\n" + ".att_syntax"); + } +} + +extern void *interruptStack; + void registerInterrupts() { - for (uint16_t i = 0; i < 16; i++) { + GDTEntry *currentGdt = &newGDT; + currentGdt[5].limit = sizeof(TSS); + currentGdt[5].baseLow = U32(&tss); + currentGdt[5].baseMid = U32(&tss) >> 16; + currentGdt[5].baseHigh = U32(&tss) >> 24; + currentGdt[5].access = 0xE9; + currentGdt[5].granularity = 0; + tss.ss0 = tss.ss = 0x10; + tss.esp0 = tss.esp = U32(&interruptStack) + 1024; + asm("mov $40, %%ax" ::); + asm("ltr %%ax" ::); + for (uint16_t i = 0; i < 256; i++) { idtEntries[i].reserved = 0; idtEntries[i].type = 0x8E; idtEntries[i].segment = 0x8; diff --git a/src/kernel/interrupts/interrupts.h b/src/kernel/interrupts/interrupts.h index dbac9d8..48817c0 100644 --- a/src/kernel/interrupts/interrupts.h +++ b/src/kernel/interrupts/interrupts.h @@ -16,6 +16,45 @@ uint32_t base; } __attribute__((packed)) InterruptTablePointer; +typedef struct { + uint16_t limit; + uint16_t baseLow; + uint8_t baseMid; + uint8_t access; + uint8_t granularity; + uint8_t baseHigh; +} __attribute__((packed)) GDTEntry; + +typedef struct { + uint32_t previous; + uint32_t esp0; + uint32_t ss0; + uint32_t esp1; + uint32_t ss1; + uint32_t esp2; + uint32_t ss2; + uint32_t cr3; + uint32_t eip; + uint32_t eflags; + uint32_t eax; + uint32_t ecx; + uint32_t edx; + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; + uint32_t es; + uint32_t cs; + uint32_t ss; + uint32_t ds; + uint32_t fs; + uint32_t gs; + uint32_t ldt; + uint16_t trap; + uint16_t iomap; +} __attribute__((packed)) TSS; + // sorry #define TIMES(x) \ x(0); \ diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index f0fdbae..df975de 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -29,7 +29,7 @@ _start: mov [ebxStart], ebx mov esp, stackEnd - lgdt [gdt32.end] + lgdt [earlyGDT.end] mov ax, 16 mov ds, ax mov ss, ax @@ -91,10 +91,9 @@ pop eax ret -ALIGN 4 -gdt32: +earlyGDT: +.start: dq 0 - .code: dw 0xffff dw 0 @@ -102,7 +101,6 @@ db 10011010b db 11001111b db 0 - .data: dw 0xffff dw 0 @@ -110,7 +108,31 @@ db 10010010b db 11001111b db 0 +.end: + dw .end - .start - 1 + dd .start +section .sharedFunctions +tss: + resb 0x68 +ALIGN 4 +newGDT: +.start: + dq 0 +.code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 +.data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 .userCode: dw 0xffff dw 0 @@ -125,16 +147,18 @@ db 10010010b db 11001111b db 0 +.tss: + resb 0x8 .end: - dw .end - gdt32 - 1 - dd gdt32 + dw .end - .start - 1 + dd .start + +global newGDT +global tss section .text -newGDT: - dw gdt32.end - gdt32 - 1 - dd gdt32 + 0xFFB00000 higherKernelEntry: - lgdt [newGDT] + lgdt [newGDT.end] mov ebx, [ebxStart] push ebx .cleanOriginalEntryCode: diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 6ec652c..ae7c56f 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -1,15 +1,26 @@ section .sharedFunctions +extern onInterrupt + +global interruptStack +interruptStack: resb 1024 + handleInterrupt: - mov eax, 8 - jmp $ + mov eax, cr3 + push eax + mov eax, 0x500000 + mov cr3, eax + call onInterrupt + pop eax + mov cr3, eax + iret %macro interruptHandler 1 ALIGN 4 global idtHandler%1 idtHandler%1: - mov ebx, %1 - xor ecx, ecx + push 0 + push %1 jmp handleInterrupt %endmacro @@ -17,8 +28,7 @@ align 4 global idtHandler%1 idtHandler%1: - pop ecx - mov ebx, %1 + push %1 jmp handleInterrupt %endmacro @@ -54,3 +64,227 @@ interruptHandler 29 interruptHandlerError 30 interruptHandler 31 +interruptHandler 32 +interruptHandler 33 +interruptHandler 34 +interruptHandler 35 +interruptHandler 36 +interruptHandler 37 +interruptHandler 38 +interruptHandler 39 +interruptHandler 40 +interruptHandler 41 +interruptHandler 42 +interruptHandler 43 +interruptHandler 44 +interruptHandler 45 +interruptHandler 46 +interruptHandler 47 +interruptHandler 48 +interruptHandler 49 +interruptHandler 50 +interruptHandler 51 +interruptHandler 52 +interruptHandler 53 +interruptHandler 54 +interruptHandler 55 +interruptHandler 56 +interruptHandler 57 +interruptHandler 58 +interruptHandler 59 +interruptHandler 60 +interruptHandler 61 +interruptHandler 62 +interruptHandler 63 +interruptHandler 64 +interruptHandler 65 +interruptHandler 66 +interruptHandler 67 +interruptHandler 68 +interruptHandler 69 +interruptHandler 70 +interruptHandler 71 +interruptHandler 72 +interruptHandler 73 +interruptHandler 74 +interruptHandler 75 +interruptHandler 76 +interruptHandler 77 +interruptHandler 78 +interruptHandler 79 +interruptHandler 80 +interruptHandler 81 +interruptHandler 82 +interruptHandler 83 +interruptHandler 84 +interruptHandler 85 +interruptHandler 86 +interruptHandler 87 +interruptHandler 88 +interruptHandler 89 +interruptHandler 90 +interruptHandler 91 +interruptHandler 92 +interruptHandler 93 +interruptHandler 94 +interruptHandler 95 +interruptHandler 96 +interruptHandler 97 +interruptHandler 98 +interruptHandler 99 +interruptHandler 100 +interruptHandler 101 +interruptHandler 102 +interruptHandler 103 +interruptHandler 104 +interruptHandler 105 +interruptHandler 106 +interruptHandler 107 +interruptHandler 108 +interruptHandler 109 +interruptHandler 110 +interruptHandler 111 +interruptHandler 112 +interruptHandler 113 +interruptHandler 114 +interruptHandler 115 +interruptHandler 116 +interruptHandler 117 +interruptHandler 118 +interruptHandler 119 +interruptHandler 120 +interruptHandler 121 +interruptHandler 122 +interruptHandler 123 +interruptHandler 124 +interruptHandler 125 +interruptHandler 126 +interruptHandler 127 +interruptHandler 128 +interruptHandler 129 +interruptHandler 130 +interruptHandler 131 +interruptHandler 132 +interruptHandler 133 +interruptHandler 134 +interruptHandler 135 +interruptHandler 136 +interruptHandler 137 +interruptHandler 138 +interruptHandler 139 +interruptHandler 140 +interruptHandler 141 +interruptHandler 142 +interruptHandler 143 +interruptHandler 144 +interruptHandler 145 +interruptHandler 146 +interruptHandler 147 +interruptHandler 148 +interruptHandler 149 +interruptHandler 150 +interruptHandler 151 +interruptHandler 152 +interruptHandler 153 +interruptHandler 154 +interruptHandler 155 +interruptHandler 156 +interruptHandler 157 +interruptHandler 158 +interruptHandler 159 +interruptHandler 160 +interruptHandler 161 +interruptHandler 162 +interruptHandler 163 +interruptHandler 164 +interruptHandler 165 +interruptHandler 166 +interruptHandler 167 +interruptHandler 168 +interruptHandler 169 +interruptHandler 170 +interruptHandler 171 +interruptHandler 172 +interruptHandler 173 +interruptHandler 174 +interruptHandler 175 +interruptHandler 176 +interruptHandler 177 +interruptHandler 178 +interruptHandler 179 +interruptHandler 180 +interruptHandler 181 +interruptHandler 182 +interruptHandler 183 +interruptHandler 184 +interruptHandler 185 +interruptHandler 186 +interruptHandler 187 +interruptHandler 188 +interruptHandler 189 +interruptHandler 190 +interruptHandler 191 +interruptHandler 192 +interruptHandler 193 +interruptHandler 194 +interruptHandler 195 +interruptHandler 196 +interruptHandler 197 +interruptHandler 198 +interruptHandler 199 +interruptHandler 200 +interruptHandler 201 +interruptHandler 202 +interruptHandler 203 +interruptHandler 204 +interruptHandler 205 +interruptHandler 206 +interruptHandler 207 +interruptHandler 208 +interruptHandler 209 +interruptHandler 210 +interruptHandler 211 +interruptHandler 212 +interruptHandler 213 +interruptHandler 214 +interruptHandler 215 +interruptHandler 216 +interruptHandler 217 +interruptHandler 218 +interruptHandler 219 +interruptHandler 220 +interruptHandler 221 +interruptHandler 222 +interruptHandler 223 +interruptHandler 224 +interruptHandler 225 +interruptHandler 226 +interruptHandler 227 +interruptHandler 228 +interruptHandler 229 +interruptHandler 230 +interruptHandler 231 +interruptHandler 232 +interruptHandler 233 +interruptHandler 234 +interruptHandler 235 +interruptHandler 236 +interruptHandler 237 +interruptHandler 238 +interruptHandler 239 +interruptHandler 240 +interruptHandler 241 +interruptHandler 242 +interruptHandler 243 +interruptHandler 244 +interruptHandler 245 +interruptHandler 246 +interruptHandler 247 +interruptHandler 248 +interruptHandler 249 +interruptHandler 250 +interruptHandler 251 +interruptHandler 252 +interruptHandler 253 +interruptHandler 254 +interruptHandler 255 diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 4976243..98c18ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -1,5 +1,6 @@ #include "interrupts.h" #include +#include #include #define IDT_ENTRY(i) \ @@ -8,11 +9,44 @@ extern void *idt; +extern GDTEntry newGDT; +extern TSS tss; + __attribute__((section(".sharedFunction"))) __attribute__((aligned(0x10))) IdtEntry idtEntries[256] = {}; +void onInterrupt(uint32_t cr3, uint32_t intNo, uint32_t errorCode) { + if (intNo > 31) { + // an external interrupt was triggered + while (1) + ; + } + if (intNo <= 31 && cr3 != 0x500000) { + // a task encountered an exception + asm(".intel_syntax noprefix\n" + "mov eax, [temporaryESP]\n" + "mov esp, eax\n" + "pop ebp\n" + "ret\n" + ".att_syntax"); + } +} + +extern void *interruptStack; + void registerInterrupts() { - for (uint16_t i = 0; i < 16; i++) { + GDTEntry *currentGdt = &newGDT; + currentGdt[5].limit = sizeof(TSS); + currentGdt[5].baseLow = U32(&tss); + currentGdt[5].baseMid = U32(&tss) >> 16; + currentGdt[5].baseHigh = U32(&tss) >> 24; + currentGdt[5].access = 0xE9; + currentGdt[5].granularity = 0; + tss.ss0 = tss.ss = 0x10; + tss.esp0 = tss.esp = U32(&interruptStack) + 1024; + asm("mov $40, %%ax" ::); + asm("ltr %%ax" ::); + for (uint16_t i = 0; i < 256; i++) { idtEntries[i].reserved = 0; idtEntries[i].type = 0x8E; idtEntries[i].segment = 0x8; diff --git a/src/kernel/interrupts/interrupts.h b/src/kernel/interrupts/interrupts.h index dbac9d8..48817c0 100644 --- a/src/kernel/interrupts/interrupts.h +++ b/src/kernel/interrupts/interrupts.h @@ -16,6 +16,45 @@ uint32_t base; } __attribute__((packed)) InterruptTablePointer; +typedef struct { + uint16_t limit; + uint16_t baseLow; + uint8_t baseMid; + uint8_t access; + uint8_t granularity; + uint8_t baseHigh; +} __attribute__((packed)) GDTEntry; + +typedef struct { + uint32_t previous; + uint32_t esp0; + uint32_t ss0; + uint32_t esp1; + uint32_t ss1; + uint32_t esp2; + uint32_t ss2; + uint32_t cr3; + uint32_t eip; + uint32_t eflags; + uint32_t eax; + uint32_t ecx; + uint32_t edx; + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; + uint32_t es; + uint32_t cs; + uint32_t ss; + uint32_t ds; + uint32_t fs; + uint32_t gs; + uint32_t ldt; + uint16_t trap; + uint16_t iomap; +} __attribute__((packed)) TSS; + // sorry #define TIMES(x) \ x(0); \ diff --git a/src/kernel/main.c b/src/kernel/main.c index 40bb13c..49b3f59 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -53,7 +53,6 @@ while (1) { Syscall *call = listPopFirst(&callsToProcess); if (!call) { - int x = 1 / 0; asm("hlt"); continue; } diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index f0fdbae..df975de 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -29,7 +29,7 @@ _start: mov [ebxStart], ebx mov esp, stackEnd - lgdt [gdt32.end] + lgdt [earlyGDT.end] mov ax, 16 mov ds, ax mov ss, ax @@ -91,10 +91,9 @@ pop eax ret -ALIGN 4 -gdt32: +earlyGDT: +.start: dq 0 - .code: dw 0xffff dw 0 @@ -102,7 +101,6 @@ db 10011010b db 11001111b db 0 - .data: dw 0xffff dw 0 @@ -110,7 +108,31 @@ db 10010010b db 11001111b db 0 +.end: + dw .end - .start - 1 + dd .start +section .sharedFunctions +tss: + resb 0x68 +ALIGN 4 +newGDT: +.start: + dq 0 +.code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 +.data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 .userCode: dw 0xffff dw 0 @@ -125,16 +147,18 @@ db 10010010b db 11001111b db 0 +.tss: + resb 0x8 .end: - dw .end - gdt32 - 1 - dd gdt32 + dw .end - .start - 1 + dd .start + +global newGDT +global tss section .text -newGDT: - dw gdt32.end - gdt32 - 1 - dd gdt32 + 0xFFB00000 higherKernelEntry: - lgdt [newGDT] + lgdt [newGDT.end] mov ebx, [ebxStart] push ebx .cleanOriginalEntryCode: diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 6ec652c..ae7c56f 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -1,15 +1,26 @@ section .sharedFunctions +extern onInterrupt + +global interruptStack +interruptStack: resb 1024 + handleInterrupt: - mov eax, 8 - jmp $ + mov eax, cr3 + push eax + mov eax, 0x500000 + mov cr3, eax + call onInterrupt + pop eax + mov cr3, eax + iret %macro interruptHandler 1 ALIGN 4 global idtHandler%1 idtHandler%1: - mov ebx, %1 - xor ecx, ecx + push 0 + push %1 jmp handleInterrupt %endmacro @@ -17,8 +28,7 @@ align 4 global idtHandler%1 idtHandler%1: - pop ecx - mov ebx, %1 + push %1 jmp handleInterrupt %endmacro @@ -54,3 +64,227 @@ interruptHandler 29 interruptHandlerError 30 interruptHandler 31 +interruptHandler 32 +interruptHandler 33 +interruptHandler 34 +interruptHandler 35 +interruptHandler 36 +interruptHandler 37 +interruptHandler 38 +interruptHandler 39 +interruptHandler 40 +interruptHandler 41 +interruptHandler 42 +interruptHandler 43 +interruptHandler 44 +interruptHandler 45 +interruptHandler 46 +interruptHandler 47 +interruptHandler 48 +interruptHandler 49 +interruptHandler 50 +interruptHandler 51 +interruptHandler 52 +interruptHandler 53 +interruptHandler 54 +interruptHandler 55 +interruptHandler 56 +interruptHandler 57 +interruptHandler 58 +interruptHandler 59 +interruptHandler 60 +interruptHandler 61 +interruptHandler 62 +interruptHandler 63 +interruptHandler 64 +interruptHandler 65 +interruptHandler 66 +interruptHandler 67 +interruptHandler 68 +interruptHandler 69 +interruptHandler 70 +interruptHandler 71 +interruptHandler 72 +interruptHandler 73 +interruptHandler 74 +interruptHandler 75 +interruptHandler 76 +interruptHandler 77 +interruptHandler 78 +interruptHandler 79 +interruptHandler 80 +interruptHandler 81 +interruptHandler 82 +interruptHandler 83 +interruptHandler 84 +interruptHandler 85 +interruptHandler 86 +interruptHandler 87 +interruptHandler 88 +interruptHandler 89 +interruptHandler 90 +interruptHandler 91 +interruptHandler 92 +interruptHandler 93 +interruptHandler 94 +interruptHandler 95 +interruptHandler 96 +interruptHandler 97 +interruptHandler 98 +interruptHandler 99 +interruptHandler 100 +interruptHandler 101 +interruptHandler 102 +interruptHandler 103 +interruptHandler 104 +interruptHandler 105 +interruptHandler 106 +interruptHandler 107 +interruptHandler 108 +interruptHandler 109 +interruptHandler 110 +interruptHandler 111 +interruptHandler 112 +interruptHandler 113 +interruptHandler 114 +interruptHandler 115 +interruptHandler 116 +interruptHandler 117 +interruptHandler 118 +interruptHandler 119 +interruptHandler 120 +interruptHandler 121 +interruptHandler 122 +interruptHandler 123 +interruptHandler 124 +interruptHandler 125 +interruptHandler 126 +interruptHandler 127 +interruptHandler 128 +interruptHandler 129 +interruptHandler 130 +interruptHandler 131 +interruptHandler 132 +interruptHandler 133 +interruptHandler 134 +interruptHandler 135 +interruptHandler 136 +interruptHandler 137 +interruptHandler 138 +interruptHandler 139 +interruptHandler 140 +interruptHandler 141 +interruptHandler 142 +interruptHandler 143 +interruptHandler 144 +interruptHandler 145 +interruptHandler 146 +interruptHandler 147 +interruptHandler 148 +interruptHandler 149 +interruptHandler 150 +interruptHandler 151 +interruptHandler 152 +interruptHandler 153 +interruptHandler 154 +interruptHandler 155 +interruptHandler 156 +interruptHandler 157 +interruptHandler 158 +interruptHandler 159 +interruptHandler 160 +interruptHandler 161 +interruptHandler 162 +interruptHandler 163 +interruptHandler 164 +interruptHandler 165 +interruptHandler 166 +interruptHandler 167 +interruptHandler 168 +interruptHandler 169 +interruptHandler 170 +interruptHandler 171 +interruptHandler 172 +interruptHandler 173 +interruptHandler 174 +interruptHandler 175 +interruptHandler 176 +interruptHandler 177 +interruptHandler 178 +interruptHandler 179 +interruptHandler 180 +interruptHandler 181 +interruptHandler 182 +interruptHandler 183 +interruptHandler 184 +interruptHandler 185 +interruptHandler 186 +interruptHandler 187 +interruptHandler 188 +interruptHandler 189 +interruptHandler 190 +interruptHandler 191 +interruptHandler 192 +interruptHandler 193 +interruptHandler 194 +interruptHandler 195 +interruptHandler 196 +interruptHandler 197 +interruptHandler 198 +interruptHandler 199 +interruptHandler 200 +interruptHandler 201 +interruptHandler 202 +interruptHandler 203 +interruptHandler 204 +interruptHandler 205 +interruptHandler 206 +interruptHandler 207 +interruptHandler 208 +interruptHandler 209 +interruptHandler 210 +interruptHandler 211 +interruptHandler 212 +interruptHandler 213 +interruptHandler 214 +interruptHandler 215 +interruptHandler 216 +interruptHandler 217 +interruptHandler 218 +interruptHandler 219 +interruptHandler 220 +interruptHandler 221 +interruptHandler 222 +interruptHandler 223 +interruptHandler 224 +interruptHandler 225 +interruptHandler 226 +interruptHandler 227 +interruptHandler 228 +interruptHandler 229 +interruptHandler 230 +interruptHandler 231 +interruptHandler 232 +interruptHandler 233 +interruptHandler 234 +interruptHandler 235 +interruptHandler 236 +interruptHandler 237 +interruptHandler 238 +interruptHandler 239 +interruptHandler 240 +interruptHandler 241 +interruptHandler 242 +interruptHandler 243 +interruptHandler 244 +interruptHandler 245 +interruptHandler 246 +interruptHandler 247 +interruptHandler 248 +interruptHandler 249 +interruptHandler 250 +interruptHandler 251 +interruptHandler 252 +interruptHandler 253 +interruptHandler 254 +interruptHandler 255 diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 4976243..98c18ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -1,5 +1,6 @@ #include "interrupts.h" #include +#include #include #define IDT_ENTRY(i) \ @@ -8,11 +9,44 @@ extern void *idt; +extern GDTEntry newGDT; +extern TSS tss; + __attribute__((section(".sharedFunction"))) __attribute__((aligned(0x10))) IdtEntry idtEntries[256] = {}; +void onInterrupt(uint32_t cr3, uint32_t intNo, uint32_t errorCode) { + if (intNo > 31) { + // an external interrupt was triggered + while (1) + ; + } + if (intNo <= 31 && cr3 != 0x500000) { + // a task encountered an exception + asm(".intel_syntax noprefix\n" + "mov eax, [temporaryESP]\n" + "mov esp, eax\n" + "pop ebp\n" + "ret\n" + ".att_syntax"); + } +} + +extern void *interruptStack; + void registerInterrupts() { - for (uint16_t i = 0; i < 16; i++) { + GDTEntry *currentGdt = &newGDT; + currentGdt[5].limit = sizeof(TSS); + currentGdt[5].baseLow = U32(&tss); + currentGdt[5].baseMid = U32(&tss) >> 16; + currentGdt[5].baseHigh = U32(&tss) >> 24; + currentGdt[5].access = 0xE9; + currentGdt[5].granularity = 0; + tss.ss0 = tss.ss = 0x10; + tss.esp0 = tss.esp = U32(&interruptStack) + 1024; + asm("mov $40, %%ax" ::); + asm("ltr %%ax" ::); + for (uint16_t i = 0; i < 256; i++) { idtEntries[i].reserved = 0; idtEntries[i].type = 0x8E; idtEntries[i].segment = 0x8; diff --git a/src/kernel/interrupts/interrupts.h b/src/kernel/interrupts/interrupts.h index dbac9d8..48817c0 100644 --- a/src/kernel/interrupts/interrupts.h +++ b/src/kernel/interrupts/interrupts.h @@ -16,6 +16,45 @@ uint32_t base; } __attribute__((packed)) InterruptTablePointer; +typedef struct { + uint16_t limit; + uint16_t baseLow; + uint8_t baseMid; + uint8_t access; + uint8_t granularity; + uint8_t baseHigh; +} __attribute__((packed)) GDTEntry; + +typedef struct { + uint32_t previous; + uint32_t esp0; + uint32_t ss0; + uint32_t esp1; + uint32_t ss1; + uint32_t esp2; + uint32_t ss2; + uint32_t cr3; + uint32_t eip; + uint32_t eflags; + uint32_t eax; + uint32_t ecx; + uint32_t edx; + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; + uint32_t es; + uint32_t cs; + uint32_t ss; + uint32_t ds; + uint32_t fs; + uint32_t gs; + uint32_t ldt; + uint16_t trap; + uint16_t iomap; +} __attribute__((packed)) TSS; + // sorry #define TIMES(x) \ x(0); \ diff --git a/src/kernel/main.c b/src/kernel/main.c index 40bb13c..49b3f59 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -53,7 +53,6 @@ while (1) { Syscall *call = listPopFirst(&callsToProcess); if (!call) { - int x = 1 / 0; asm("hlt"); continue; } diff --git a/src/kernel/service/services.c b/src/kernel/service/services.c index f15c6fd..d13ca0f 100644 --- a/src/kernel/service/services.c +++ b/src/kernel/service/services.c @@ -31,7 +31,7 @@ service->pagingInfo.pageDirectory = malloc(0x1000); service->name = serviceName; void *current = &functionsStart; - while (current <= (void *)&functionsEnd) { + for (uint32_t i = 0; i < 3; i++) { // todo: make this unwritable! sharePage(&(service->pagingInfo), current, current); current += 0x1000; diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index f0fdbae..df975de 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -29,7 +29,7 @@ _start: mov [ebxStart], ebx mov esp, stackEnd - lgdt [gdt32.end] + lgdt [earlyGDT.end] mov ax, 16 mov ds, ax mov ss, ax @@ -91,10 +91,9 @@ pop eax ret -ALIGN 4 -gdt32: +earlyGDT: +.start: dq 0 - .code: dw 0xffff dw 0 @@ -102,7 +101,6 @@ db 10011010b db 11001111b db 0 - .data: dw 0xffff dw 0 @@ -110,7 +108,31 @@ db 10010010b db 11001111b db 0 +.end: + dw .end - .start - 1 + dd .start +section .sharedFunctions +tss: + resb 0x68 +ALIGN 4 +newGDT: +.start: + dq 0 +.code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 +.data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 .userCode: dw 0xffff dw 0 @@ -125,16 +147,18 @@ db 10010010b db 11001111b db 0 +.tss: + resb 0x8 .end: - dw .end - gdt32 - 1 - dd gdt32 + dw .end - .start - 1 + dd .start + +global newGDT +global tss section .text -newGDT: - dw gdt32.end - gdt32 - 1 - dd gdt32 + 0xFFB00000 higherKernelEntry: - lgdt [newGDT] + lgdt [newGDT.end] mov ebx, [ebxStart] push ebx .cleanOriginalEntryCode: diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 6ec652c..ae7c56f 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -1,15 +1,26 @@ section .sharedFunctions +extern onInterrupt + +global interruptStack +interruptStack: resb 1024 + handleInterrupt: - mov eax, 8 - jmp $ + mov eax, cr3 + push eax + mov eax, 0x500000 + mov cr3, eax + call onInterrupt + pop eax + mov cr3, eax + iret %macro interruptHandler 1 ALIGN 4 global idtHandler%1 idtHandler%1: - mov ebx, %1 - xor ecx, ecx + push 0 + push %1 jmp handleInterrupt %endmacro @@ -17,8 +28,7 @@ align 4 global idtHandler%1 idtHandler%1: - pop ecx - mov ebx, %1 + push %1 jmp handleInterrupt %endmacro @@ -54,3 +64,227 @@ interruptHandler 29 interruptHandlerError 30 interruptHandler 31 +interruptHandler 32 +interruptHandler 33 +interruptHandler 34 +interruptHandler 35 +interruptHandler 36 +interruptHandler 37 +interruptHandler 38 +interruptHandler 39 +interruptHandler 40 +interruptHandler 41 +interruptHandler 42 +interruptHandler 43 +interruptHandler 44 +interruptHandler 45 +interruptHandler 46 +interruptHandler 47 +interruptHandler 48 +interruptHandler 49 +interruptHandler 50 +interruptHandler 51 +interruptHandler 52 +interruptHandler 53 +interruptHandler 54 +interruptHandler 55 +interruptHandler 56 +interruptHandler 57 +interruptHandler 58 +interruptHandler 59 +interruptHandler 60 +interruptHandler 61 +interruptHandler 62 +interruptHandler 63 +interruptHandler 64 +interruptHandler 65 +interruptHandler 66 +interruptHandler 67 +interruptHandler 68 +interruptHandler 69 +interruptHandler 70 +interruptHandler 71 +interruptHandler 72 +interruptHandler 73 +interruptHandler 74 +interruptHandler 75 +interruptHandler 76 +interruptHandler 77 +interruptHandler 78 +interruptHandler 79 +interruptHandler 80 +interruptHandler 81 +interruptHandler 82 +interruptHandler 83 +interruptHandler 84 +interruptHandler 85 +interruptHandler 86 +interruptHandler 87 +interruptHandler 88 +interruptHandler 89 +interruptHandler 90 +interruptHandler 91 +interruptHandler 92 +interruptHandler 93 +interruptHandler 94 +interruptHandler 95 +interruptHandler 96 +interruptHandler 97 +interruptHandler 98 +interruptHandler 99 +interruptHandler 100 +interruptHandler 101 +interruptHandler 102 +interruptHandler 103 +interruptHandler 104 +interruptHandler 105 +interruptHandler 106 +interruptHandler 107 +interruptHandler 108 +interruptHandler 109 +interruptHandler 110 +interruptHandler 111 +interruptHandler 112 +interruptHandler 113 +interruptHandler 114 +interruptHandler 115 +interruptHandler 116 +interruptHandler 117 +interruptHandler 118 +interruptHandler 119 +interruptHandler 120 +interruptHandler 121 +interruptHandler 122 +interruptHandler 123 +interruptHandler 124 +interruptHandler 125 +interruptHandler 126 +interruptHandler 127 +interruptHandler 128 +interruptHandler 129 +interruptHandler 130 +interruptHandler 131 +interruptHandler 132 +interruptHandler 133 +interruptHandler 134 +interruptHandler 135 +interruptHandler 136 +interruptHandler 137 +interruptHandler 138 +interruptHandler 139 +interruptHandler 140 +interruptHandler 141 +interruptHandler 142 +interruptHandler 143 +interruptHandler 144 +interruptHandler 145 +interruptHandler 146 +interruptHandler 147 +interruptHandler 148 +interruptHandler 149 +interruptHandler 150 +interruptHandler 151 +interruptHandler 152 +interruptHandler 153 +interruptHandler 154 +interruptHandler 155 +interruptHandler 156 +interruptHandler 157 +interruptHandler 158 +interruptHandler 159 +interruptHandler 160 +interruptHandler 161 +interruptHandler 162 +interruptHandler 163 +interruptHandler 164 +interruptHandler 165 +interruptHandler 166 +interruptHandler 167 +interruptHandler 168 +interruptHandler 169 +interruptHandler 170 +interruptHandler 171 +interruptHandler 172 +interruptHandler 173 +interruptHandler 174 +interruptHandler 175 +interruptHandler 176 +interruptHandler 177 +interruptHandler 178 +interruptHandler 179 +interruptHandler 180 +interruptHandler 181 +interruptHandler 182 +interruptHandler 183 +interruptHandler 184 +interruptHandler 185 +interruptHandler 186 +interruptHandler 187 +interruptHandler 188 +interruptHandler 189 +interruptHandler 190 +interruptHandler 191 +interruptHandler 192 +interruptHandler 193 +interruptHandler 194 +interruptHandler 195 +interruptHandler 196 +interruptHandler 197 +interruptHandler 198 +interruptHandler 199 +interruptHandler 200 +interruptHandler 201 +interruptHandler 202 +interruptHandler 203 +interruptHandler 204 +interruptHandler 205 +interruptHandler 206 +interruptHandler 207 +interruptHandler 208 +interruptHandler 209 +interruptHandler 210 +interruptHandler 211 +interruptHandler 212 +interruptHandler 213 +interruptHandler 214 +interruptHandler 215 +interruptHandler 216 +interruptHandler 217 +interruptHandler 218 +interruptHandler 219 +interruptHandler 220 +interruptHandler 221 +interruptHandler 222 +interruptHandler 223 +interruptHandler 224 +interruptHandler 225 +interruptHandler 226 +interruptHandler 227 +interruptHandler 228 +interruptHandler 229 +interruptHandler 230 +interruptHandler 231 +interruptHandler 232 +interruptHandler 233 +interruptHandler 234 +interruptHandler 235 +interruptHandler 236 +interruptHandler 237 +interruptHandler 238 +interruptHandler 239 +interruptHandler 240 +interruptHandler 241 +interruptHandler 242 +interruptHandler 243 +interruptHandler 244 +interruptHandler 245 +interruptHandler 246 +interruptHandler 247 +interruptHandler 248 +interruptHandler 249 +interruptHandler 250 +interruptHandler 251 +interruptHandler 252 +interruptHandler 253 +interruptHandler 254 +interruptHandler 255 diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 4976243..98c18ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -1,5 +1,6 @@ #include "interrupts.h" #include +#include #include #define IDT_ENTRY(i) \ @@ -8,11 +9,44 @@ extern void *idt; +extern GDTEntry newGDT; +extern TSS tss; + __attribute__((section(".sharedFunction"))) __attribute__((aligned(0x10))) IdtEntry idtEntries[256] = {}; +void onInterrupt(uint32_t cr3, uint32_t intNo, uint32_t errorCode) { + if (intNo > 31) { + // an external interrupt was triggered + while (1) + ; + } + if (intNo <= 31 && cr3 != 0x500000) { + // a task encountered an exception + asm(".intel_syntax noprefix\n" + "mov eax, [temporaryESP]\n" + "mov esp, eax\n" + "pop ebp\n" + "ret\n" + ".att_syntax"); + } +} + +extern void *interruptStack; + void registerInterrupts() { - for (uint16_t i = 0; i < 16; i++) { + GDTEntry *currentGdt = &newGDT; + currentGdt[5].limit = sizeof(TSS); + currentGdt[5].baseLow = U32(&tss); + currentGdt[5].baseMid = U32(&tss) >> 16; + currentGdt[5].baseHigh = U32(&tss) >> 24; + currentGdt[5].access = 0xE9; + currentGdt[5].granularity = 0; + tss.ss0 = tss.ss = 0x10; + tss.esp0 = tss.esp = U32(&interruptStack) + 1024; + asm("mov $40, %%ax" ::); + asm("ltr %%ax" ::); + for (uint16_t i = 0; i < 256; i++) { idtEntries[i].reserved = 0; idtEntries[i].type = 0x8E; idtEntries[i].segment = 0x8; diff --git a/src/kernel/interrupts/interrupts.h b/src/kernel/interrupts/interrupts.h index dbac9d8..48817c0 100644 --- a/src/kernel/interrupts/interrupts.h +++ b/src/kernel/interrupts/interrupts.h @@ -16,6 +16,45 @@ uint32_t base; } __attribute__((packed)) InterruptTablePointer; +typedef struct { + uint16_t limit; + uint16_t baseLow; + uint8_t baseMid; + uint8_t access; + uint8_t granularity; + uint8_t baseHigh; +} __attribute__((packed)) GDTEntry; + +typedef struct { + uint32_t previous; + uint32_t esp0; + uint32_t ss0; + uint32_t esp1; + uint32_t ss1; + uint32_t esp2; + uint32_t ss2; + uint32_t cr3; + uint32_t eip; + uint32_t eflags; + uint32_t eax; + uint32_t ecx; + uint32_t edx; + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; + uint32_t es; + uint32_t cs; + uint32_t ss; + uint32_t ds; + uint32_t fs; + uint32_t gs; + uint32_t ldt; + uint16_t trap; + uint16_t iomap; +} __attribute__((packed)) TSS; + // sorry #define TIMES(x) \ x(0); \ diff --git a/src/kernel/main.c b/src/kernel/main.c index 40bb13c..49b3f59 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -53,7 +53,6 @@ while (1) { Syscall *call = listPopFirst(&callsToProcess); if (!call) { - int x = 1 / 0; asm("hlt"); continue; } diff --git a/src/kernel/service/services.c b/src/kernel/service/services.c index f15c6fd..d13ca0f 100644 --- a/src/kernel/service/services.c +++ b/src/kernel/service/services.c @@ -31,7 +31,7 @@ service->pagingInfo.pageDirectory = malloc(0x1000); service->name = serviceName; void *current = &functionsStart; - while (current <= (void *)&functionsEnd) { + for (uint32_t i = 0; i < 3; i++) { // todo: make this unwritable! sharePage(&(service->pagingInfo), current, current); current += 0x1000; diff --git a/src/kernel/syscalls/syscall.c b/src/kernel/syscalls/syscall.c index 21ef2fe..8e0a966 100644 --- a/src/kernel/syscalls/syscall.c +++ b/src/kernel/syscalls/syscall.c @@ -38,13 +38,6 @@ call->service = currentSyscall->service; call->esp = esp; call->respondingTo = currentSyscall->respondingTo; - if (call->respondingTo && - call->respondingTo->service == currentSyscall->service) { - asm("nop" ::"a"(currentSyscall), "b"(call), - "c"(currentSyscall->respondingTo), "d"(0xB105F00D)); - while (1) - ; - } Service *currentService = currentSyscall->service; call->cr3 = getPhysicalAddressKernel(currentService->pagingInfo.pageDirectory); diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index f0fdbae..df975de 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -29,7 +29,7 @@ _start: mov [ebxStart], ebx mov esp, stackEnd - lgdt [gdt32.end] + lgdt [earlyGDT.end] mov ax, 16 mov ds, ax mov ss, ax @@ -91,10 +91,9 @@ pop eax ret -ALIGN 4 -gdt32: +earlyGDT: +.start: dq 0 - .code: dw 0xffff dw 0 @@ -102,7 +101,6 @@ db 10011010b db 11001111b db 0 - .data: dw 0xffff dw 0 @@ -110,7 +108,31 @@ db 10010010b db 11001111b db 0 +.end: + dw .end - .start - 1 + dd .start +section .sharedFunctions +tss: + resb 0x68 +ALIGN 4 +newGDT: +.start: + dq 0 +.code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 +.data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 .userCode: dw 0xffff dw 0 @@ -125,16 +147,18 @@ db 10010010b db 11001111b db 0 +.tss: + resb 0x8 .end: - dw .end - gdt32 - 1 - dd gdt32 + dw .end - .start - 1 + dd .start + +global newGDT +global tss section .text -newGDT: - dw gdt32.end - gdt32 - 1 - dd gdt32 + 0xFFB00000 higherKernelEntry: - lgdt [newGDT] + lgdt [newGDT.end] mov ebx, [ebxStart] push ebx .cleanOriginalEntryCode: diff --git a/src/kernel/interrupts/interruptDescriptors.asm b/src/kernel/interrupts/interruptDescriptors.asm index 6ec652c..ae7c56f 100644 --- a/src/kernel/interrupts/interruptDescriptors.asm +++ b/src/kernel/interrupts/interruptDescriptors.asm @@ -1,15 +1,26 @@ section .sharedFunctions +extern onInterrupt + +global interruptStack +interruptStack: resb 1024 + handleInterrupt: - mov eax, 8 - jmp $ + mov eax, cr3 + push eax + mov eax, 0x500000 + mov cr3, eax + call onInterrupt + pop eax + mov cr3, eax + iret %macro interruptHandler 1 ALIGN 4 global idtHandler%1 idtHandler%1: - mov ebx, %1 - xor ecx, ecx + push 0 + push %1 jmp handleInterrupt %endmacro @@ -17,8 +28,7 @@ align 4 global idtHandler%1 idtHandler%1: - pop ecx - mov ebx, %1 + push %1 jmp handleInterrupt %endmacro @@ -54,3 +64,227 @@ interruptHandler 29 interruptHandlerError 30 interruptHandler 31 +interruptHandler 32 +interruptHandler 33 +interruptHandler 34 +interruptHandler 35 +interruptHandler 36 +interruptHandler 37 +interruptHandler 38 +interruptHandler 39 +interruptHandler 40 +interruptHandler 41 +interruptHandler 42 +interruptHandler 43 +interruptHandler 44 +interruptHandler 45 +interruptHandler 46 +interruptHandler 47 +interruptHandler 48 +interruptHandler 49 +interruptHandler 50 +interruptHandler 51 +interruptHandler 52 +interruptHandler 53 +interruptHandler 54 +interruptHandler 55 +interruptHandler 56 +interruptHandler 57 +interruptHandler 58 +interruptHandler 59 +interruptHandler 60 +interruptHandler 61 +interruptHandler 62 +interruptHandler 63 +interruptHandler 64 +interruptHandler 65 +interruptHandler 66 +interruptHandler 67 +interruptHandler 68 +interruptHandler 69 +interruptHandler 70 +interruptHandler 71 +interruptHandler 72 +interruptHandler 73 +interruptHandler 74 +interruptHandler 75 +interruptHandler 76 +interruptHandler 77 +interruptHandler 78 +interruptHandler 79 +interruptHandler 80 +interruptHandler 81 +interruptHandler 82 +interruptHandler 83 +interruptHandler 84 +interruptHandler 85 +interruptHandler 86 +interruptHandler 87 +interruptHandler 88 +interruptHandler 89 +interruptHandler 90 +interruptHandler 91 +interruptHandler 92 +interruptHandler 93 +interruptHandler 94 +interruptHandler 95 +interruptHandler 96 +interruptHandler 97 +interruptHandler 98 +interruptHandler 99 +interruptHandler 100 +interruptHandler 101 +interruptHandler 102 +interruptHandler 103 +interruptHandler 104 +interruptHandler 105 +interruptHandler 106 +interruptHandler 107 +interruptHandler 108 +interruptHandler 109 +interruptHandler 110 +interruptHandler 111 +interruptHandler 112 +interruptHandler 113 +interruptHandler 114 +interruptHandler 115 +interruptHandler 116 +interruptHandler 117 +interruptHandler 118 +interruptHandler 119 +interruptHandler 120 +interruptHandler 121 +interruptHandler 122 +interruptHandler 123 +interruptHandler 124 +interruptHandler 125 +interruptHandler 126 +interruptHandler 127 +interruptHandler 128 +interruptHandler 129 +interruptHandler 130 +interruptHandler 131 +interruptHandler 132 +interruptHandler 133 +interruptHandler 134 +interruptHandler 135 +interruptHandler 136 +interruptHandler 137 +interruptHandler 138 +interruptHandler 139 +interruptHandler 140 +interruptHandler 141 +interruptHandler 142 +interruptHandler 143 +interruptHandler 144 +interruptHandler 145 +interruptHandler 146 +interruptHandler 147 +interruptHandler 148 +interruptHandler 149 +interruptHandler 150 +interruptHandler 151 +interruptHandler 152 +interruptHandler 153 +interruptHandler 154 +interruptHandler 155 +interruptHandler 156 +interruptHandler 157 +interruptHandler 158 +interruptHandler 159 +interruptHandler 160 +interruptHandler 161 +interruptHandler 162 +interruptHandler 163 +interruptHandler 164 +interruptHandler 165 +interruptHandler 166 +interruptHandler 167 +interruptHandler 168 +interruptHandler 169 +interruptHandler 170 +interruptHandler 171 +interruptHandler 172 +interruptHandler 173 +interruptHandler 174 +interruptHandler 175 +interruptHandler 176 +interruptHandler 177 +interruptHandler 178 +interruptHandler 179 +interruptHandler 180 +interruptHandler 181 +interruptHandler 182 +interruptHandler 183 +interruptHandler 184 +interruptHandler 185 +interruptHandler 186 +interruptHandler 187 +interruptHandler 188 +interruptHandler 189 +interruptHandler 190 +interruptHandler 191 +interruptHandler 192 +interruptHandler 193 +interruptHandler 194 +interruptHandler 195 +interruptHandler 196 +interruptHandler 197 +interruptHandler 198 +interruptHandler 199 +interruptHandler 200 +interruptHandler 201 +interruptHandler 202 +interruptHandler 203 +interruptHandler 204 +interruptHandler 205 +interruptHandler 206 +interruptHandler 207 +interruptHandler 208 +interruptHandler 209 +interruptHandler 210 +interruptHandler 211 +interruptHandler 212 +interruptHandler 213 +interruptHandler 214 +interruptHandler 215 +interruptHandler 216 +interruptHandler 217 +interruptHandler 218 +interruptHandler 219 +interruptHandler 220 +interruptHandler 221 +interruptHandler 222 +interruptHandler 223 +interruptHandler 224 +interruptHandler 225 +interruptHandler 226 +interruptHandler 227 +interruptHandler 228 +interruptHandler 229 +interruptHandler 230 +interruptHandler 231 +interruptHandler 232 +interruptHandler 233 +interruptHandler 234 +interruptHandler 235 +interruptHandler 236 +interruptHandler 237 +interruptHandler 238 +interruptHandler 239 +interruptHandler 240 +interruptHandler 241 +interruptHandler 242 +interruptHandler 243 +interruptHandler 244 +interruptHandler 245 +interruptHandler 246 +interruptHandler 247 +interruptHandler 248 +interruptHandler 249 +interruptHandler 250 +interruptHandler 251 +interruptHandler 252 +interruptHandler 253 +interruptHandler 254 +interruptHandler 255 diff --git a/src/kernel/interrupts/interrupts.c b/src/kernel/interrupts/interrupts.c index 4976243..98c18ba 100644 --- a/src/kernel/interrupts/interrupts.c +++ b/src/kernel/interrupts/interrupts.c @@ -1,5 +1,6 @@ #include "interrupts.h" #include +#include #include #define IDT_ENTRY(i) \ @@ -8,11 +9,44 @@ extern void *idt; +extern GDTEntry newGDT; +extern TSS tss; + __attribute__((section(".sharedFunction"))) __attribute__((aligned(0x10))) IdtEntry idtEntries[256] = {}; +void onInterrupt(uint32_t cr3, uint32_t intNo, uint32_t errorCode) { + if (intNo > 31) { + // an external interrupt was triggered + while (1) + ; + } + if (intNo <= 31 && cr3 != 0x500000) { + // a task encountered an exception + asm(".intel_syntax noprefix\n" + "mov eax, [temporaryESP]\n" + "mov esp, eax\n" + "pop ebp\n" + "ret\n" + ".att_syntax"); + } +} + +extern void *interruptStack; + void registerInterrupts() { - for (uint16_t i = 0; i < 16; i++) { + GDTEntry *currentGdt = &newGDT; + currentGdt[5].limit = sizeof(TSS); + currentGdt[5].baseLow = U32(&tss); + currentGdt[5].baseMid = U32(&tss) >> 16; + currentGdt[5].baseHigh = U32(&tss) >> 24; + currentGdt[5].access = 0xE9; + currentGdt[5].granularity = 0; + tss.ss0 = tss.ss = 0x10; + tss.esp0 = tss.esp = U32(&interruptStack) + 1024; + asm("mov $40, %%ax" ::); + asm("ltr %%ax" ::); + for (uint16_t i = 0; i < 256; i++) { idtEntries[i].reserved = 0; idtEntries[i].type = 0x8E; idtEntries[i].segment = 0x8; diff --git a/src/kernel/interrupts/interrupts.h b/src/kernel/interrupts/interrupts.h index dbac9d8..48817c0 100644 --- a/src/kernel/interrupts/interrupts.h +++ b/src/kernel/interrupts/interrupts.h @@ -16,6 +16,45 @@ uint32_t base; } __attribute__((packed)) InterruptTablePointer; +typedef struct { + uint16_t limit; + uint16_t baseLow; + uint8_t baseMid; + uint8_t access; + uint8_t granularity; + uint8_t baseHigh; +} __attribute__((packed)) GDTEntry; + +typedef struct { + uint32_t previous; + uint32_t esp0; + uint32_t ss0; + uint32_t esp1; + uint32_t ss1; + uint32_t esp2; + uint32_t ss2; + uint32_t cr3; + uint32_t eip; + uint32_t eflags; + uint32_t eax; + uint32_t ecx; + uint32_t edx; + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; + uint32_t es; + uint32_t cs; + uint32_t ss; + uint32_t ds; + uint32_t fs; + uint32_t gs; + uint32_t ldt; + uint16_t trap; + uint16_t iomap; +} __attribute__((packed)) TSS; + // sorry #define TIMES(x) \ x(0); \ diff --git a/src/kernel/main.c b/src/kernel/main.c index 40bb13c..49b3f59 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -53,7 +53,6 @@ while (1) { Syscall *call = listPopFirst(&callsToProcess); if (!call) { - int x = 1 / 0; asm("hlt"); continue; } diff --git a/src/kernel/service/services.c b/src/kernel/service/services.c index f15c6fd..d13ca0f 100644 --- a/src/kernel/service/services.c +++ b/src/kernel/service/services.c @@ -31,7 +31,7 @@ service->pagingInfo.pageDirectory = malloc(0x1000); service->name = serviceName; void *current = &functionsStart; - while (current <= (void *)&functionsEnd) { + for (uint32_t i = 0; i < 3; i++) { // todo: make this unwritable! sharePage(&(service->pagingInfo), current, current); current += 0x1000; diff --git a/src/kernel/syscalls/syscall.c b/src/kernel/syscalls/syscall.c index 21ef2fe..8e0a966 100644 --- a/src/kernel/syscalls/syscall.c +++ b/src/kernel/syscalls/syscall.c @@ -38,13 +38,6 @@ call->service = currentSyscall->service; call->esp = esp; call->respondingTo = currentSyscall->respondingTo; - if (call->respondingTo && - call->respondingTo->service == currentSyscall->service) { - asm("nop" ::"a"(currentSyscall), "b"(call), - "c"(currentSyscall->respondingTo), "d"(0xB105F00D)); - while (1) - ; - } Service *currentService = currentSyscall->service; call->cr3 = getPhysicalAddressKernel(currentService->pagingInfo.pageDirectory); diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index 45d48ef..c3dcdba 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -63,5 +63,8 @@ loadFromInitrd("log"); log("hello world"); log("honey os is alive :)"); + log("doing wrong code..."); + uint32_t x = 1 / 0; + log("something went wrong?"); return 0; }