From 2859788682f7ec6f4a6e3fe0e6f9f8ff8597410f Mon Sep 17 00:00:00 2001 From: Furkan Mudanyali Date: Mon, 18 Jul 2022 18:14:20 +0300 Subject: [PATCH] Initial implementation of GDT --- kernel/Makefile | 8 ++--- kernel/arch/i386/{ => boot}/boot.S | 5 ++-- kernel/arch/i386/{ => boot}/crti.S | 0 kernel/arch/i386/{ => boot}/crtn.S | 0 kernel/arch/i386/boot/gdt.c | 48 ++++++++++++++++++++++++++++++ kernel/arch/i386/boot/gdt_load.S | 20 +++++++++++++ kernel/arch/i386/make.config | 6 ++-- kernel/arch/i386/{ => vga}/tty.c | 0 kernel/arch/i386/{ => vga}/vga.h | 0 kernel/include/kernel/gdt.h | 42 ++++++++++++++++++++++++++ kernel/kernel/kernel.c | 1 + 11 files changed, 122 insertions(+), 8 deletions(-) rename kernel/arch/i386/{ => boot}/boot.S (93%) rename kernel/arch/i386/{ => boot}/crti.S (100%) rename kernel/arch/i386/{ => boot}/crtn.S (100%) create mode 100644 kernel/arch/i386/boot/gdt.c create mode 100644 kernel/arch/i386/boot/gdt_load.S rename kernel/arch/i386/{ => vga}/tty.c (100%) rename kernel/arch/i386/{ => vga}/vga.h (100%) create mode 100644 kernel/include/kernel/gdt.h diff --git a/kernel/Makefile b/kernel/Makefile index 448c0b6..ada5e86 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -32,20 +32,20 @@ $(KERNEL_ARCH_OBJS) \ kernel/kernel.o \ OBJS=\ -$(ARCHDIR)/crti.o \ +$(ARCHDIR)/boot/crti.o \ $(ARCHDIR)/crtbegin.o \ $(KERNEL_OBJS) \ $(ARCHDIR)/crtend.o \ -$(ARCHDIR)/crtn.o \ +$(ARCHDIR)/boot/crtn.o \ LINK_LIST=\ $(LDFLAGS) \ -$(ARCHDIR)/crti.o \ +$(ARCHDIR)/boot/crti.o \ $(ARCHDIR)/crtbegin.o \ $(KERNEL_OBJS) \ $(LIBS) \ $(ARCHDIR)/crtend.o \ -$(ARCHDIR)/crtn.o \ +$(ARCHDIR)/boot/crtn.o \ .PHONY: all clean install install-headers install-kernel .SUFFIXES: .o .c .S diff --git a/kernel/arch/i386/boot.S b/kernel/arch/i386/boot/boot.S similarity index 93% rename from kernel/arch/i386/boot.S rename to kernel/arch/i386/boot/boot.S index dca9994..65e3e0e 100644 --- a/kernel/arch/i386/boot.S +++ b/kernel/arch/i386/boot/boot.S @@ -25,10 +25,11 @@ stack_top: .type _start, @function _start: movl $stack_top, %esp - call _init # call global constructors + cli + call gdt_init # call global constructors call kernel_main # transfer control to main kernel # Hang if kernel_main unexpectedly returns - cli 1: hlt jmp 1b + .size _start, . - _start diff --git a/kernel/arch/i386/crti.S b/kernel/arch/i386/boot/crti.S similarity index 100% rename from kernel/arch/i386/crti.S rename to kernel/arch/i386/boot/crti.S diff --git a/kernel/arch/i386/crtn.S b/kernel/arch/i386/boot/crtn.S similarity index 100% rename from kernel/arch/i386/crtn.S rename to kernel/arch/i386/boot/crtn.S diff --git a/kernel/arch/i386/boot/gdt.c b/kernel/arch/i386/boot/gdt.c new file mode 100644 index 0000000..e274ca9 --- /dev/null +++ b/kernel/arch/i386/boot/gdt.c @@ -0,0 +1,48 @@ +/** + * Referenced from https://github.com/tendstofortytwo/clay + * kernel/kernel.c + */ + +#include +#include +#include + +uint32_t TSS[32]; +gdt_entry GDT[GDT_ENTRIES]; +gdt_ptr gp; + +void gdt_fill(gdt_entry* target, gdt* source) { + if(source->limit > 0xFFFFF) { + printf("Invalid GDT limit %d\n", source->limit); + for(;;); + } + + target->limit_lo = source->limit & 0xFFFF; + target->limit_hi_flags = (source->limit >> 16) & 0x0F; + target->base_lo = source->base & 0xFFFF; + target->base_mid = (source->base >> 16) & 0xFF; + target->base_hi = (source->base >> 24) & 0xFF; + target->access = source->access; + target->limit_hi_flags |= (source->flags << 4); +} + +void gdt_init(void) { + uint32_t tssRef = (uint32_t) TSS; + gdt rows[GDT_ENTRIES] = { + {0, 0x00000, 0x00, 0x0}, // 0x0000 Null Descriptor + {0, 0xFFFFF, 0x9A, 0xC}, // 0x0008 Kernel Mode Code Segment + {0, 0xFFFFF, 0x92, 0xC}, // 0x0010 Kernel Mode Data Segment + {0, 0xFFFFF, 0xFA, 0xC}, // 0x0018 User Mode Code Segment + {0, 0xFFFFF, 0xF2, 0xC}, // 0x0020 User Mode Data Segment + {tssRef, sizeof(TSS), 0x89, 0x0}, // 0x0028 Task State Segment + }; + + for(uint32_t i = 0; i < GDT_ENTRIES; ++i) { + gdt_fill(GDT+i, rows+i); + } + + gp.base = (void*) GDT; + gp.limit = (GDT_ENTRIES * sizeof(gdt_entry)) - 1; + + gdt_load(); +} diff --git a/kernel/arch/i386/boot/gdt_load.S b/kernel/arch/i386/boot/gdt_load.S new file mode 100644 index 0000000..70eac4d --- /dev/null +++ b/kernel/arch/i386/boot/gdt_load.S @@ -0,0 +1,20 @@ +# Referenced from https://github.com/tendstofortytwo/clay +# kernel/boot/gdt.s + +.global gdt_load +.type gdt_load, @function +gdt_load: + lgdt (gp) + mov %cr0, %eax + or $0x1, %eax + mov %eax, %cr0 + jmp $0x08, $reload + +reload: + mov $0x10, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %fs + mov %ax, %gs + mov %ax, %ss + ret diff --git a/kernel/arch/i386/make.config b/kernel/arch/i386/make.config index c4fb501..8441da1 100644 --- a/kernel/arch/i386/make.config +++ b/kernel/arch/i386/make.config @@ -4,5 +4,7 @@ KERNEL_ARCH_LDFLAGS= KERNEL_ARCH_LIBS= KERNEL_ARCH_OBJS=\ -$(ARCHDIR)/boot.o \ -$(ARCHDIR)/tty.o \ +$(ARCHDIR)/boot/boot.o \ +$(ARCHDIR)/vga/tty.o \ +$(ARCHDIR)/boot/gdt.o \ +$(ARCHDIR)/boot/gdt_load.o diff --git a/kernel/arch/i386/tty.c b/kernel/arch/i386/vga/tty.c similarity index 100% rename from kernel/arch/i386/tty.c rename to kernel/arch/i386/vga/tty.c diff --git a/kernel/arch/i386/vga.h b/kernel/arch/i386/vga/vga.h similarity index 100% rename from kernel/arch/i386/vga.h rename to kernel/arch/i386/vga/vga.h diff --git a/kernel/include/kernel/gdt.h b/kernel/include/kernel/gdt.h new file mode 100644 index 0000000..612684f --- /dev/null +++ b/kernel/include/kernel/gdt.h @@ -0,0 +1,42 @@ +/** + * Referenced from https://tendstofortytwo/clay + * kernel/kernel.c + */ + +#ifndef _KERNEL_GDT_H +#define _KERNEL_GDT_H + +#include + +#define GDT_ENTRIES 0x6 + +typedef struct { + uint16_t limit; + uint32_t base; +}__attribute__((packed)) gdt_ptr; + +typedef struct { + uint16_t limit_lo; + uint16_t base_lo; + uint8_t base_mid; + uint8_t access; + uint8_t limit_hi_flags; + uint8_t base_hi; +}__attribute__((packed)) gdt_entry; + +typedef struct { + uint32_t base; + uint32_t limit; + uint8_t access; + uint8_t flags; +}__attribute__((packed)) gdt; + +void gdt_fill(gdt_entry*, gdt*); +void gdt_init(void); +extern void gdt_load(); + +extern uint32_t TSS[32]; +extern gdt_entry GDT[GDT_ENTRIES]; +extern gdt_ptr gp; + +#endif diff --git a/kernel/kernel/kernel.c b/kernel/kernel/kernel.c index 6ef89b7..83e08cb 100644 --- a/kernel/kernel/kernel.c +++ b/kernel/kernel/kernel.c @@ -1,6 +1,7 @@ #include #include +#include void kernel_main(void) { terminal_initialize();