From a3bbffcfd9a88712d4e420036bb35143bab870c6 Mon Sep 17 00:00:00 2001 From: Starnakin Date: Wed, 2 Oct 2024 14:15:49 +0200 Subject: [PATCH] core: use define for gdt offset --- headers/gdt.h | 9 +++++++++ src/gdt/gdt.c | 12 ++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/headers/gdt.h b/headers/gdt.h index 5659557..d4d7654 100644 --- a/headers/gdt.h +++ b/headers/gdt.h @@ -30,3 +30,12 @@ void init_gdt(); #define GDT_ACCESS_RW_READABLE_FOR_CODE_WRITABLE_FOR_DATA 0b00000010 #define GDT_ACCESS_A_ACCESSED 0b00000001 #define GDT_ACCESS_A_NOT_ACCESSED 0b00000000 + +extern uint8_t gdt_entries[GDT_SIZE * 8]; + +#define GDT_OFFSET_KERNEL_CODE 0x08 +#define GDT_OFFSET_KERNEL_DATA 0x10 +#define GDT_OFFSET_KERNEL_STACK 0x18 +#define GDT_OFFSET_USER_CODE 0x20 +#define GDT_OFFSET_USER_DATA 0x28 +#define GDT_OFFSET_USER_STACK 0x30 diff --git a/src/gdt/gdt.c b/src/gdt/gdt.c index d765283..b571f60 100644 --- a/src/gdt/gdt.c +++ b/src/gdt/gdt.c @@ -42,7 +42,7 @@ void init_gdt(void) set_gdt_entry_value(gdt_entries + 0x00, 0, 0, 0, 0); // Null segment set_gdt_entry_value( - gdt_entries + 0x08, 0, 0xFFFFF, + gdt_entries + GDT_OFFSET_KERNEL_CODE, 0, 0xFFFFF, GDT_ACCESS_P_VALID | GDT_ACCESS_DPL_KERNEL_MODE | GDT_ACCESS_S_CODE_OR_DATA | GDT_ACCESS_E_EXECUTABLE | GDT_ACCESS_DC_NOT_CONFORM | @@ -50,7 +50,7 @@ void init_gdt(void) GDT_ACCESS_A_ACCESSED, GDT_FLAG_32BIT_MODE | GDT_FLAG_PAGE_MODE); // Kernel code set_gdt_entry_value( - gdt_entries + 0x10, 0, 0xFFFFF, + gdt_entries + GDT_OFFSET_KERNEL_DATA, 0, 0xFFFFF, GDT_ACCESS_P_VALID | GDT_ACCESS_DPL_KERNEL_MODE | GDT_ACCESS_S_CODE_OR_DATA | GDT_ACCESS_E_NOT_EXECUTABLE | GDT_ACCESS_DC_NOT_CONFORM | @@ -58,7 +58,7 @@ void init_gdt(void) GDT_ACCESS_A_ACCESSED, GDT_FLAG_32BIT_MODE | GDT_FLAG_PAGE_MODE); // Kernel data set_gdt_entry_value( - gdt_entries + 0x18, 0, 0xFFFFF, + gdt_entries + GDT_OFFSET_KERNEL_STACK, 0, 0xFFFFF, GDT_ACCESS_P_VALID | GDT_ACCESS_DPL_KERNEL_MODE | GDT_ACCESS_S_CODE_OR_DATA | GDT_ACCESS_E_NOT_EXECUTABLE | GDT_ACCESS_DC_CONFORM | @@ -67,7 +67,7 @@ void init_gdt(void) GDT_FLAG_32BIT_MODE | GDT_FLAG_PAGE_MODE); // Kernel stack set_gdt_entry_value( - gdt_entries + 0x20, 0, 0xFFFFF, + gdt_entries + GDT_OFFSET_USER_CODE, 0, 0xFFFFF, GDT_ACCESS_P_VALID | GDT_ACCESS_DPL_USER_MODE | GDT_ACCESS_S_CODE_OR_DATA | GDT_ACCESS_E_EXECUTABLE | GDT_ACCESS_DC_CONFORM | @@ -75,7 +75,7 @@ void init_gdt(void) GDT_ACCESS_A_ACCESSED, GDT_FLAG_32BIT_MODE | GDT_FLAG_PAGE_MODE); // User code set_gdt_entry_value( - gdt_entries + 0x28, 0, 0xFFFFF, + gdt_entries + GDT_OFFSET_USER_DATA, 0, 0xFFFFF, GDT_ACCESS_P_VALID | GDT_ACCESS_DPL_USER_MODE | GDT_ACCESS_S_CODE_OR_DATA | GDT_ACCESS_E_NOT_EXECUTABLE | GDT_ACCESS_DC_NOT_CONFORM | @@ -83,7 +83,7 @@ void init_gdt(void) GDT_ACCESS_A_ACCESSED, GDT_FLAG_32BIT_MODE | GDT_FLAG_PAGE_MODE); // User data set_gdt_entry_value( - gdt_entries + 0x30, 0, 0xFFFFF, + gdt_entries + GDT_OFFSET_USER_STACK, 0, 0xFFFFF, GDT_ACCESS_P_VALID | GDT_ACCESS_DPL_USER_MODE | GDT_ACCESS_S_CODE_OR_DATA | GDT_ACCESS_E_NOT_EXECUTABLE | GDT_ACCESS_DC_CONFORM |