From a417a747310df36b7d13976ace1e8ae2e30be6b5 Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 14 Oct 2019 02:06:50 -0700 Subject: [PATCH] base working kernel with a gdt that doesn't restart emulator(no idea why this works yet) --- gdt.c | 2 +- gdt.h | 2 +- gdt_seg.s | 6 +++--- kernel.c | 15 +++++++++++++++ kernel.h | 4 ++++ loader.s | 12 ++++-------- makefile | 2 +- 7 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 kernel.c create mode 100644 kernel.h diff --git a/gdt.c b/gdt.c index 2c8785f..d3d35ef 100644 --- a/gdt.c +++ b/gdt.c @@ -39,5 +39,5 @@ void gdt_configure() { gdt_configure_entry(4, 0, 0xffffffff, 0xf2, 0xcf); // Load in the new changes to the gdt - load_gdt(&gdt_ptr); + load_gdt(); } diff --git a/gdt.h b/gdt.h index 19858f5..6e151c3 100644 --- a/gdt.h +++ b/gdt.h @@ -22,7 +22,7 @@ struct GDT_Entry gdt_entries[NO_GDT_ENTRIES]; struct GDT_PTR gdt_ptr; // this func is actually taken care of in gdt_seg.s -extern void load_gdt(struct GDT_PTR* ptr); +extern void load_gdt(); void gdt_configure_entry(u32 entry, u32 base, u32 limit, u8 access, u8 gran); diff --git a/gdt_seg.s b/gdt_seg.s index 2767b45..338115a 100644 --- a/gdt_seg.s +++ b/gdt_seg.s @@ -2,16 +2,16 @@ global load_gdt extern gdt_ptr load_gdt: - lgdt [esp+4] mov ax, 0x10 ; offset in the gdt to our data segment mov ds, ax mov es, ax mov fs, ax mov gs, ax mov ss, ax - jmp 0x08:.flush_cs ; far jump to the code segment + jmp 0x08:flush_cs ; far jump to the code segment -.flush_cs: +flush_cs: + lgdt [gdt_ptr] ret diff --git a/kernel.c b/kernel.c new file mode 100644 index 0000000..48acec8 --- /dev/null +++ b/kernel.c @@ -0,0 +1,15 @@ +#include "types.h" +#include "tests.h" +#include "gdt.h" +#include "kernel.h" + +void kinit() { + gdt_configure(); + test_dispatcher(); +} + + +void kmain() { + kinit(); + for(;;); // the most amazing loop +} diff --git a/kernel.h b/kernel.h new file mode 100644 index 0000000..0cf4dc0 --- /dev/null +++ b/kernel.h @@ -0,0 +1,4 @@ +#include "types.h" + +void kinit(); +void kmain(); diff --git a/loader.s b/loader.s index 85ea6da..c40efca 100644 --- a/loader.s +++ b/loader.s @@ -1,16 +1,13 @@ ; since we have no stack we have to create one for the OS global loader -; this section is partly handled by nasm tolerating some minor bs + +; NOTE: these symbols are what we use to represent the "magic bytes" for the whole os MAGIC_NUMBER equ 0x1BADB002 FLAGS equ 0x0 CHECKSUM equ -MAGIC_NUMBER -KERNEL_STACK_SIZE equ 4096 - -; size in bytes of stack -extern gdt_configure -extern test_dispatcher +extern kmain section .text ; align all instructions to 4 byte boundary by the x86 instruction set law @@ -23,8 +20,7 @@ align 4 ; sets up our gdt and segment selectors for later use loader: - call gdt_configure - call test_dispatcher + call kmain .loop: jmp .loop diff --git a/makefile b/makefile index c9a38b6..e4296c0 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,7 @@ ASM=nasm LINK=ld ISO=genisoimage -OBJECTS=gdt_seg.o gdt.o loader.o serial.o framebuffer.o ports.o stlio.o tests.o +OBJECTS=kernel.o gdt_seg.o gdt.o loader.o serial.o framebuffer.o ports.o stlio.o tests.o AFLAGS=-f elf32 CFLAGS=-m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles \ -nodefaultlibs -Wall -Wextra -Werror -c