From 5fc5a3be0e499dc259e2073bb39a13577ef20b35 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 10 Sep 2024 11:19:23 +0200 Subject: [PATCH] fix: cringe gdt and and stack gdt --- headers/gdt.h | 4 ++++ src/gdt/gdt.c | 47 ++++++++++++++++++++++++++++++++++++----------- src/gdt/sgdt.s | 12 ------------ 3 files changed, 40 insertions(+), 23 deletions(-) delete mode 100644 src/gdt/sgdt.s diff --git a/headers/gdt.h b/headers/gdt.h index 53b61e3..9cc0aa1 100644 --- a/headers/gdt.h +++ b/headers/gdt.h @@ -2,6 +2,10 @@ #include +#define GDT_BASE 0x00000800 +// sizeof(Segment Descriptor) * nb(Segment Descriptor) +#define GDT_SIZE 8 * 7 + struct gdt_entry { uint32_t limit; uint32_t base; diff --git a/src/gdt/gdt.c b/src/gdt/gdt.c index d0d0c2c..21c7b5e 100644 --- a/src/gdt/gdt.c +++ b/src/gdt/gdt.c @@ -1,5 +1,6 @@ #include "gdt.h" #include "kprintf.h" +#include "string.h" extern void setGdt(void *); @@ -27,44 +28,68 @@ void encodeGdtEntry(uint8_t *target, struct gdt_entry source) // Encode the flags target[6] |= (source.flags << 4); } -// sizeof(Segment Descriptor) * nb(Segment Descriptor) -uint8_t *gdt_entries = (uint8_t *) 0x00000800; -struct gdt_descriptor gdtr; + +uint8_t gdt_entries[8][7]; +struct gdt_descriptor gdtr; void initGdt() { - gdtr.size = 8 * 5 - 1; - gdtr.base = (uint32_t) &gdt_entries; + gdtr.size = GDT_SIZE - 1; + gdtr.base = GDT_BASE; struct gdt_entry gdt_entry_null_descriptor = { .base = 0, .limit = 0x00000000, .access_byte = 0x00, .flags = 0x0}; - encodeGdtEntry(gdt_entries + 0x0000, gdt_entry_null_descriptor); + encodeGdtEntry(gdt_entries[0], gdt_entry_null_descriptor); struct gdt_entry gdt_entry_kernel_mode_code_segment = { .base = 0, .limit = 0xFFFFF, .access_byte = 0x9A, .flags = 0xC }; - encodeGdtEntry(gdt_entries + 0x0008, gdt_entry_kernel_mode_code_segment); + encodeGdtEntry(gdt_entries[1], gdt_entry_kernel_mode_code_segment); struct gdt_entry gdt_entry_kernel_mode_data_segment = { .base = 0, .limit = 0xFFFFF, .access_byte = 0x92, .flags = 0xC }; - encodeGdtEntry(gdt_entries + 0x0010, gdt_entry_kernel_mode_data_segment); + encodeGdtEntry(gdt_entries[2], gdt_entry_kernel_mode_data_segment); + + struct gdt_entry gdt_entry_kernel_mode_stack_segment = { .base = 0x0, + .limit = 0x0, + .access_byte = 0x97, + .flags = 0x0D }; + encodeGdtEntry(gdt_entries[3], gdt_entry_kernel_mode_data_segment); struct gdt_entry gdt_entry_user_mode_code_segment = { .base = 0, .limit = 0xFFFFF, .access_byte = 0xFA, .flags = 0xC }; - encodeGdtEntry(gdt_entries + 0x0018, gdt_entry_user_mode_code_segment); + encodeGdtEntry(gdt_entries[4], gdt_entry_user_mode_code_segment); struct gdt_entry gdt_entry_user_mode_data_segment = { .base = 0, .limit = 0xFFFFF, .access_byte = 0xF2, .flags = 0xC }; - encodeGdtEntry(gdt_entries + 0x0020, gdt_entry_user_mode_data_segment); + encodeGdtEntry(gdt_entries[5], gdt_entry_user_mode_data_segment); - setGdt(&gdtr); + struct gdt_entry gdt_entry_user_mode_stack_segment = { .base = 0x0, + .limit = 0x0, + .access_byte = 0xF7, + .flags = 0x0D }; + encodeGdtEntry(gdt_entries[6], gdt_entry_user_mode_data_segment); + + memcpy((void *) gdtr.base, (void *) gdt_entries, (size_t) GDT_SIZE); + + /* load the gdtr registry */ + asm("lgdtl (gdtr)"); + + /* initiliaz the segments */ + asm(" movw $0x10, %ax \n \ + movw %ax, %ds \n \ + movw %ax, %es \n \ + movw %ax, %fs \n \ + movw %ax, %gs \n \ + ljmp $0x08, $next \n \ + next: \n"); } \ No newline at end of file diff --git a/src/gdt/sgdt.s b/src/gdt/sgdt.s deleted file mode 100644 index 87829d9..0000000 --- a/src/gdt/sgdt.s +++ /dev/null @@ -1,12 +0,0 @@ -.globl setGdt - -gdtr: .word 0 # For limit storage - .long 0 # For base storage - -setGdt: - movw 4(%esp), %ax # Move the word at [esp + 4] into AX - movw %ax, gdtr # Move AX into gdtr - movl 8(%esp), %eax # Move the double word at [esp + 8] into EAX - movl %eax, gdtr + 2 # Move EAX into gdtr + 2 - lgdt gdtr # Load the GDT - ret \ No newline at end of file