Add: ISR now handling common exceptions

This commit is contained in:
xamidev
2024-07-14 21:41:04 +02:00
parent db77556cfa
commit c45c9b3b32
11 changed files with 443 additions and 3 deletions

50
< Normal file
View File

@@ -0,0 +1,50 @@
#include "stdio.h"
#include "serial.h"
#include "gdt.h"
#include "idt.h"
#include "system.h"
int kmain(int retvalue)
{
gdt_install();
idt_install();
isr_install();
// serial testing
init_serial();
log("serial connection established", 3);
log("initialized GDT entries", 2);
log("kernel started", 2);
log("initialized IDT", 2);
log("initialized ISRs", 3);
clear();
// printf testing
// TODO: Framebuffer upgrade: color output
int age = 34;
int problems = 124;
char* name = "xamidev";
printf("Hello %s, you are %d years old and have %d problems. wow %%\n", name, age, problems);
long suchwow = 2934342;
char character = 65;
printf("such number %u\nsuch character %c", suchwow, character);
printf("wow negative %d\n", -3742);
printf("such hex %x %X\n", 0xcafe, 0xdeadbeef);
printf("such pointer %p\n", (void*)0xcafe1234);
// Div by zero exception
printf("Lalala, what a beautiful day! %d", 4/0);
return retvalue;
}

BIN
com1.out

Binary file not shown.

Binary file not shown.

117
isr.c Normal file
View File

@@ -0,0 +1,117 @@
#include "system.h"
#include "stdio.h"
#include "idt.h"
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();
void isr_install()
{
idt_set_gate(0, (unsigned)isr0, 0x08, 0x8E);
idt_set_gate(1, (unsigned)isr1, 0x08, 0x8E);
idt_set_gate(2, (unsigned)isr2, 0x08, 0x8E);
idt_set_gate(3, (unsigned)isr3, 0x08, 0x8E);
idt_set_gate(4, (unsigned)isr4, 0x08, 0x8E);
idt_set_gate(5, (unsigned)isr5, 0x08, 0x8E);
idt_set_gate(6, (unsigned)isr6, 0x08, 0x8E);
idt_set_gate(7, (unsigned)isr7, 0x08, 0x8E);
idt_set_gate(8, (unsigned)isr8, 0x08, 0x8E);
idt_set_gate(9, (unsigned)isr9, 0x08, 0x8E);
idt_set_gate(10, (unsigned)isr10, 0x08, 0x8E);
idt_set_gate(11, (unsigned)isr11, 0x08, 0x8E);
idt_set_gate(12, (unsigned)isr12, 0x08, 0x8E);
idt_set_gate(13, (unsigned)isr13, 0x08, 0x8E);
idt_set_gate(14, (unsigned)isr14, 0x08, 0x8E);
idt_set_gate(15, (unsigned)isr15, 0x08, 0x8E);
idt_set_gate(16, (unsigned)isr16, 0x08, 0x8E);
idt_set_gate(17, (unsigned)isr17, 0x08, 0x8E);
idt_set_gate(18, (unsigned)isr18, 0x08, 0x8E);
idt_set_gate(19, (unsigned)isr19, 0x08, 0x8E);
idt_set_gate(20, (unsigned)isr20, 0x08, 0x8E);
idt_set_gate(21, (unsigned)isr21, 0x08, 0x8E);
idt_set_gate(22, (unsigned)isr22, 0x08, 0x8E);
idt_set_gate(23, (unsigned)isr23, 0x08, 0x8E);
idt_set_gate(24, (unsigned)isr24, 0x08, 0x8E);
idt_set_gate(25, (unsigned)isr25, 0x08, 0x8E);
idt_set_gate(26, (unsigned)isr26, 0x08, 0x8E);
idt_set_gate(27, (unsigned)isr27, 0x08, 0x8E);
idt_set_gate(28, (unsigned)isr28, 0x08, 0x8E);
idt_set_gate(29, (unsigned)isr29, 0x08, 0x8E);
idt_set_gate(30, (unsigned)isr30, 0x08, 0x8E);
idt_set_gate(31, (unsigned)isr31, 0x08, 0x8E);
}
char *exception_messages[] =
{
"Division by Zero",
"Debug",
"Non Maskable Interrupt",
"Breakpoint",
"Into Detected Overflow",
"Out of Bounds",
"Invalid Opcode",
"No Coprocessor",
"Double Fault",
"Coprocessor Segment Overrun",
"Bad TSS",
"Segment Not Present",
"Stack Fault",
"General Protection Fault",
"Page Fault",
"Unknown Interrupt",
"Coprocessor Fault",
"Alignment Check",
"Machine Check",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved"
};
void fault_handler(struct regs *r)
{
if (r->int_no < 32)
{
printf("%s Exception. Halting!\n", exception_messages[r->int_no]);
for (;;);
}
}

Binary file not shown.

10
kmain.c
View File

@@ -2,12 +2,14 @@
#include "serial.h" #include "serial.h"
#include "gdt.h" #include "gdt.h"
#include "idt.h" #include "idt.h"
#include "system.h"
int kmain(int retvalue) int kmain(int retvalue)
{ {
gdt_install(); gdt_install();
idt_install(); idt_install();
isr_install();
// serial testing // serial testing
@@ -16,11 +18,15 @@ int kmain(int retvalue)
log("serial connection established", 3); log("serial connection established", 3);
log("initialized GDT entries", 2); log("initialized GDT entries", 2);
log("kernel started", 2); log("kernel started", 2);
log("initialized IDT", 2);
log("initialized ISRs", 3);
clear(); clear();
// printf testing // printf testing
// TODO: Framebuffer upgrade: color output
int age = 34; int age = 34;
int problems = 124; int problems = 124;
char* name = "xamidev"; char* name = "xamidev";
@@ -36,5 +42,9 @@ int kmain(int retvalue)
printf("such pointer %p\n", (void*)0xcafe1234); printf("such pointer %p\n", (void*)0xcafe1234);
// Div by zero exception
printf("Lalala, what a beautiful day! %d", 4/0);
return retvalue; return retvalue;
} }

View File

@@ -5,6 +5,10 @@ SECTIONS {
. = 0x00100000; . = 0x00100000;
.__mbHeader : {
*(.__mbHeader)
}
/* Align relevant sections at 4KB */ /* Align relevant sections at 4KB */
.text ALIGN (0x1000) : .text ALIGN (0x1000) :

253
loader.s
View File

@@ -1,12 +1,15 @@
global loader global loader
section .__mbHeader
align 0x4
section .text:
MAGIC_NUMBER equ 0x1BADB002 ; multiboot magic MAGIC_NUMBER equ 0x1BADB002 ; multiboot magic
FLAGS equ 0x0 FLAGS equ 0x0
CHECKSUM equ -MAGIC_NUMBER CHECKSUM equ -MAGIC_NUMBER
KERNEL_STACK_SIZE equ 4096 KERNEL_STACK_SIZE equ 4096
section .text:
align 4
dd MAGIC_NUMBER dd MAGIC_NUMBER
dd FLAGS dd FLAGS
dd CHECKSUM dd CHECKSUM
@@ -45,6 +48,252 @@ idt_load:
lidt [idtp] lidt [idtp]
ret ret
global isr0
global isr1
global isr2
global isr3
global isr4
global isr5
global isr6
global isr7
global isr8
global isr9
global isr10
global isr11
global isr12
global isr13
global isr14
global isr15
global isr16
global isr17
global isr18
global isr19
global isr20
global isr21
global isr22
global isr23
global isr24
global isr25
global isr26
global isr27
global isr28
global isr29
global isr30
global isr31
; Interrupt service routine exceptions
isr0:
cli
push byte 0
push byte 0
jmp isr_common_stub
isr1:
cli
push byte 0
push byte 1
jmp isr_common_stub
isr2:
cli
push byte 0
push byte 2
jmp isr_common_stub
isr3:
cli
push byte 0
push byte 3
jmp isr_common_stub
isr4:
cli
push byte 0
push byte 4
jmp isr_common_stub
isr5:
cli
push byte 0
push byte 5
jmp isr_common_stub
isr6:
cli
push byte 0
push byte 6
jmp isr_common_stub
isr7:
cli
push byte 0
push byte 7
jmp isr_common_stub
isr8:
cli
push byte 8
jmp isr_common_stub
isr9:
cli
push byte 0
push byte 9
jmp isr_common_stub
isr10:
cli
push byte 10
jmp isr_common_stub
isr11:
cli
push byte 11
jmp isr_common_stub
isr12:
cli
push byte 12
jmp isr_common_stub
isr13:
cli
push byte 13
jmp isr_common_stub
isr14:
cli
push byte 14
jmp isr_common_stub
isr15:
cli
push byte 0
push byte 15
jmp isr_common_stub
isr16:
cli
push byte 0
push byte 16
jmp isr_common_stub
isr17:
cli
push byte 0
push byte 17
jmp isr_common_stub
isr18:
cli
push byte 0
push byte 18
jmp isr_common_stub
isr19:
cli
push byte 0
push byte 19
jmp isr_common_stub
isr20:
cli
push byte 0
push byte 20
jmp isr_common_stub
isr21:
cli
push byte 0
push byte 21
jmp isr_common_stub
isr22:
cli
push byte 0
push byte 22
jmp isr_common_stub
isr23:
cli
push byte 0
push byte 23
jmp isr_common_stub
isr24:
cli
push byte 0
push byte 24
jmp isr_common_stub
isr25:
cli
push byte 0
push byte 25
jmp isr_common_stub
isr26:
cli
push byte 0
push byte 26
jmp isr_common_stub
isr27:
cli
push byte 0
push byte 27
jmp isr_common_stub
isr28:
cli
push byte 0
push byte 28
jmp isr_common_stub
isr29:
cli
push byte 0
push byte 29
jmp isr_common_stub
isr30:
cli
push byte 0
push byte 30
jmp isr_common_stub
isr31:
cli
push byte 0
push byte 31
jmp isr_common_stub
extern fault_handler
isr_common_stub:
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, fault_handler
call eax
pop eax
pop gs
pop fs
pop es
pop ds
popa
add esp, 8
iret
section .bss section .bss
align 4 align 4
kernel_stack: kernel_stack:

View File

@@ -1,4 +1,4 @@
OBJECTS = loader.o kmain.o stdio.o io.o string.o serial.o gdt.o idt.o system.o OBJECTS = loader.o kmain.o stdio.o io.o string.o serial.o gdt.o idt.o system.o isr.o
CC = gcc CC = gcc
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -Wextra -c CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -Wextra -c
LDFLAGS = -T link.ld -melf_i386 LDFLAGS = -T link.ld -melf_i386

BIN
os.iso

Binary file not shown.

View File

@@ -5,4 +5,14 @@ typedef int size_t;
void *memset(void *dest, char val, size_t count); void *memset(void *dest, char val, size_t count);
struct regs
{
unsigned int gs, fs, es, ds;
unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
unsigned int int_no, err_code;
unsigned int eip, cs, eflags, useresp, ss;
};
void isr_install();
#endif #endif