Added ISRs for exceptions (taken from old-kern)

This commit is contained in:
2024-03-09 20:52:22 +01:00
parent 71e80f73bb
commit 6121884d76
8 changed files with 379 additions and 7 deletions
+213
View File
@@ -3,6 +3,7 @@
[extern main]
[extern idtp]
[extern faultHandler]
_start:
call main
@@ -13,3 +14,215 @@ _start:
idtLoad:
lidt [idtp]
ret
[global isr0]
isr0:
cli
push 0
push 0
jmp isrCommon
[global isr1]
isr1:
cli
push 0
push 1
jmp isrCommon
[global isr2]
isr2:
cli
push 0
push 2
jmp isrCommon
[global isr3]
isr3:
cli
push 0
push 3
jmp isrCommon
[global isr4]
isr4:
cli
push 0
push 4
jmp isrCommon
[global isr5]
isr5:
cli
push 0
push 5
jmp isrCommon
[global isr6]
isr6:
cli
push 0
push 6
jmp isrCommon
[global isr7]
isr7:
cli
push 0
push 7
jmp isrCommon
[global isr8]
isr8:
cli
push 8
jmp isrCommon
[global isr9]
isr9:
cli
push 0
push 9
jmp isrCommon
[global isr10]
isr10:
cli
push 10
jmp isrCommon
[global isr11]
isr11:
cli
push 11
jmp isrCommon
[global isr12]
isr12:
cli
push 12
jmp isrCommon
[global isr13]
isr13:
cli
push 13
jmp isrCommon
[global isr14]
isr14:
cli
push 14
jmp isrCommon
[global isr15]
isr15:
cli
push 0
push 15
jmp isrCommon
[global isr16]
isr16:
cli
push 0
push 16
jmp isrCommon
[global isr17]
isr17:
cli
push 0
push 17
jmp isrCommon
[global isr18]
isr18:
cli
push 0
push 18
jmp isrCommon
[global isr19]
isr19:
cli
push 0
push 19
jmp isrCommon
[global isr20]
isr20:
cli
push 0
push 20
jmp isrCommon
[global isr21]
isr21:
cli
push 0
push 21
jmp isrCommon
[global isr22]
isr22:
cli
push 0
push 22
jmp isrCommon
[global isr23]
isr23:
cli
push 0
push 23
jmp isrCommon
[global isr24]
isr24:
cli
push 0
push 24
jmp isrCommon
[global isr25]
isr25:
cli
push 0
push 25
jmp isrCommon
[global isr26]
isr26:
cli
push 0
push 26
jmp isrCommon
[global isr27]
isr27:
cli
push 0
push 27
jmp isrCommon
[global isr28]
isr28:
cli
push 0
push 28
jmp isrCommon
[global isr29]
isr29:
cli
push 0
push 29
jmp isrCommon
[global isr30]
isr30:
cli
push 0
push 30
jmp isrCommon
[global isr31]
isr31:
cli
push 0
push 31
jmp isrCommon
isrCommon: ; No clue how this works, i'm pretty sure it calls the C function and has to push the registers or something idk
pusha
push ds
push es
push fs
push gs
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov eax, esp
push eax
mov eax, faultHandler
call eax
pop eax
pop gs
pop fs
pop es
pop ds
popa
add esp, 8
iret
+89
View File
@@ -0,0 +1,89 @@
#include "isrs.h"
#include "idt.h"
#include "screen.h"
#include "system.h"
char* exceptionMessages[] = {
"Divide By Zero Error",
"Debug Exception",
"NMI Interrupt",
"Breakpoint",
"Overflow",
"BOUND Range Exceeded",
"Invalid Opcode",
"Device Not Available",
"Double Fault",
"Coprocessor Segment Overrun",
"Invalid TSS",
"Segment Not Present",
"Stack-Segment Fault",
"General Protection Fault",
"Page Fault",
"Reserved",
"x87 FPU Floating-Point Error",
"Alignment Check",
"Machine Check",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
};
void setupIsrs() {
idtSetGate(0, (unsigned)isr0, 0x08, 0x8e);
idtSetGate(1, (unsigned)isr1, 0x08, 0x8e);
idtSetGate(2, (unsigned)isr2, 0x08, 0x8e);
idtSetGate(3, (unsigned)isr3, 0x08, 0x8e);
idtSetGate(4, (unsigned)isr4, 0x08, 0x8e);
idtSetGate(5, (unsigned)isr5, 0x08, 0x8e);
idtSetGate(6, (unsigned)isr6, 0x08, 0x8e);
idtSetGate(7, (unsigned)isr7, 0x08, 0x8e);
idtSetGate(8, (unsigned)isr8, 0x08, 0x8e);
idtSetGate(9, (unsigned)isr9, 0x08, 0x8e);
idtSetGate(10, (unsigned)isr10, 0x08, 0x8e);
idtSetGate(11, (unsigned)isr11, 0x08, 0x8e);
idtSetGate(12, (unsigned)isr12, 0x08, 0x8e);
idtSetGate(13, (unsigned)isr13, 0x08, 0x8e);
idtSetGate(14, (unsigned)isr14, 0x08, 0x8e);
idtSetGate(15, (unsigned)isr15, 0x08, 0x8e);
idtSetGate(16, (unsigned)isr16, 0x08, 0x8e);
idtSetGate(17, (unsigned)isr17, 0x08, 0x8e);
idtSetGate(18, (unsigned)isr18, 0x08, 0x8e);
idtSetGate(19, (unsigned)isr19, 0x08, 0x8e);
idtSetGate(20, (unsigned)isr20, 0x08, 0x8e);
idtSetGate(21, (unsigned)isr21, 0x08, 0x8e);
idtSetGate(22, (unsigned)isr22, 0x08, 0x8e);
idtSetGate(23, (unsigned)isr23, 0x08, 0x8e);
idtSetGate(24, (unsigned)isr24, 0x08, 0x8e);
idtSetGate(25, (unsigned)isr25, 0x08, 0x8e);
idtSetGate(26, (unsigned)isr26, 0x08, 0x8e);
idtSetGate(27, (unsigned)isr27, 0x08, 0x8e);
idtSetGate(28, (unsigned)isr28, 0x08, 0x8e);
idtSetGate(29, (unsigned)isr29, 0x08, 0x8e);
idtSetGate(30, (unsigned)isr30, 0x08, 0x8e);
idtSetGate(31, (unsigned)isr31, 0x08, 0x8e);
}
void faultHandler(struct regs* r) {
if (r->intNo < 32) {
print(exceptionMessages[r->intNo], 0x04);
if (r->errCode) {
print(": 0x", 0x04);
printHex(r->errCode, 0x04);
}
cli();
hlt();
}
}
+48
View File
@@ -0,0 +1,48 @@
#ifndef ISRS_H
#define ISRS_H
void setupIsrs();
// Contains values of registers and exception info from isrs
struct regs {
unsigned int gs, fs, es, ds;
unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
unsigned int intNo, errCode;
unsigned int eip, cs, eflags, useresp, ss;
};
// Repetative isr definitions
extern void isr0();
extern void isr1();
extern void isr2();
extern void isr3();
extern void isr4();
extern void isr5();
extern void isr6();
extern void isr7();
extern void isr8();
extern void isr9();
extern void isr10();
extern void isr11();
extern void isr12();
extern void isr13();
extern void isr14();
extern void isr15();
extern void isr16();
extern void isr17();
extern void isr18();
extern void isr19();
extern void isr20();
extern void isr21();
extern void isr22();
extern void isr23();
extern void isr24();
extern void isr25();
extern void isr26();
extern void isr27();
extern void isr28();
extern void isr29();
extern void isr30();
extern void isr31();
#endif
+2 -1
View File
@@ -1,11 +1,12 @@
#include "screen.h"
#include "idt.h"
#include "isrs.h"
void main() {
setupIdt();
setupIsrs();
clear(0x0f);
print("kern.\n\n", 0x0f);
print("> ", 0x0e);
}
+10
View File
@@ -50,6 +50,16 @@ void clear(uint8_t attr) {
}
}
void printHex(uint8_t d, uint8_t attr) {
char digits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
uint8_t low = d & 0x0f;
uint8_t high = (d & 0xf0) >> 4;
putc(digits[low], attr);
putc(digits[high], attr);
}
void setCursorPos(uint16_t offset) {
cursor = offset;
}
+1
View File
@@ -21,6 +21,7 @@ void print(char* s, uint8_t attr);
void putcAt(char c, uint8_t attr, uint8_t row, uint8_t col);
void printAt(char* s, uint8_t attr, uint8_t row, uint8_t col);
void clear(uint8_t attr);
void printHex(uint8_t d, uint8_t attr);
void setCursorPos(uint16_t offset);
uint16_t getCursorPos();
+12
View File
@@ -25,3 +25,15 @@ char* memset(char* dst, char value, uint16_t length) {
return dst;
}
void cli() {
asm volatile("cli");
}
void sti() {
asm volatile("sti");
}
void hlt() {
asm volatile("hlt");
}
+4 -6
View File
@@ -11,11 +11,9 @@ void outb(uint16_t port, uint8_t data);
char* memcopy(char* src, char* dst, uint16_t length);
char* memset(char* dst, char data, uint16_t length);
struct regs {
unsigned int gs, fs, es, ds;
unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
unsigned int intNo, errCode;
unsigned int eip, cs, eflags, useresp, ss;
};
// Assembly stuff
void cli();
void sti();
void hlt();
#endif