diff --git a/< b/< new file mode 100644 index 0000000..37cc297 --- /dev/null +++ b/< @@ -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; +} diff --git a/com1.out b/com1.out index a4fd5da..c02424c 100644 Binary files a/com1.out and b/com1.out differ diff --git a/iso/boot/kernel.elf b/iso/boot/kernel.elf index 37b9308..7ee5345 100755 Binary files a/iso/boot/kernel.elf and b/iso/boot/kernel.elf differ diff --git a/isr.c b/isr.c new file mode 100644 index 0000000..7ec9781 --- /dev/null +++ b/isr.c @@ -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 (;;); + } +} diff --git a/kernel.elf b/kernel.elf index 37b9308..7ee5345 100755 Binary files a/kernel.elf and b/kernel.elf differ diff --git a/kmain.c b/kmain.c index a9712ba..37cc297 100644 --- a/kmain.c +++ b/kmain.c @@ -2,12 +2,14 @@ #include "serial.h" #include "gdt.h" #include "idt.h" +#include "system.h" int kmain(int retvalue) { gdt_install(); idt_install(); + isr_install(); // serial testing @@ -16,11 +18,15 @@ int kmain(int retvalue) 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"; @@ -35,6 +41,10 @@ int kmain(int retvalue) 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; } diff --git a/link.ld b/link.ld index 29147a9..bc9c8dd 100644 --- a/link.ld +++ b/link.ld @@ -5,6 +5,10 @@ SECTIONS { . = 0x00100000; + .__mbHeader : { + *(.__mbHeader) + } + /* Align relevant sections at 4KB */ .text ALIGN (0x1000) : diff --git a/loader.s b/loader.s index 25aa675..1286130 100644 --- a/loader.s +++ b/loader.s @@ -1,12 +1,15 @@ global loader +section .__mbHeader + +align 0x4 +section .text: + MAGIC_NUMBER equ 0x1BADB002 ; multiboot magic FLAGS equ 0x0 CHECKSUM equ -MAGIC_NUMBER KERNEL_STACK_SIZE equ 4096 -section .text: -align 4 dd MAGIC_NUMBER dd FLAGS dd CHECKSUM @@ -45,6 +48,252 @@ idt_load: lidt [idtp] 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 align 4 kernel_stack: diff --git a/makefile b/makefile index f663857..cfcec12 100644 --- a/makefile +++ b/makefile @@ -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 CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -Wextra -c LDFLAGS = -T link.ld -melf_i386 diff --git a/os.iso b/os.iso index b19cf32..83ed1fd 100644 Binary files a/os.iso and b/os.iso differ diff --git a/system.h b/system.h index 5a9118e..16f117a 100644 --- a/system.h +++ b/system.h @@ -5,4 +5,14 @@ typedef int size_t; 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