fix: cringe gdt and and stack gdt

This commit is contained in:
starnakin 2024-09-10 11:19:23 +02:00
parent 0787b1a1fc
commit 5fc5a3be0e
3 changed files with 40 additions and 23 deletions

View File

@ -2,6 +2,10 @@
#include <stdint.h> #include <stdint.h>
#define GDT_BASE 0x00000800
// sizeof(Segment Descriptor) * nb(Segment Descriptor)
#define GDT_SIZE 8 * 7
struct gdt_entry { struct gdt_entry {
uint32_t limit; uint32_t limit;
uint32_t base; uint32_t base;

View File

@ -1,5 +1,6 @@
#include "gdt.h" #include "gdt.h"
#include "kprintf.h" #include "kprintf.h"
#include "string.h"
extern void setGdt(void *); extern void setGdt(void *);
@ -27,44 +28,68 @@ void encodeGdtEntry(uint8_t *target, struct gdt_entry source)
// Encode the flags // Encode the flags
target[6] |= (source.flags << 4); target[6] |= (source.flags << 4);
} }
// sizeof(Segment Descriptor) * nb(Segment Descriptor)
uint8_t *gdt_entries = (uint8_t *) 0x00000800; uint8_t gdt_entries[8][7];
struct gdt_descriptor gdtr; struct gdt_descriptor gdtr;
void initGdt() void initGdt()
{ {
gdtr.size = 8 * 5 - 1; gdtr.size = GDT_SIZE - 1;
gdtr.base = (uint32_t) &gdt_entries; gdtr.base = GDT_BASE;
struct gdt_entry gdt_entry_null_descriptor = { .base = 0, struct gdt_entry gdt_entry_null_descriptor = { .base = 0,
.limit = 0x00000000, .limit = 0x00000000,
.access_byte = 0x00, .access_byte = 0x00,
.flags = 0x0}; .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, struct gdt_entry gdt_entry_kernel_mode_code_segment = { .base = 0,
.limit = 0xFFFFF, .limit = 0xFFFFF,
.access_byte = 0x9A, .access_byte = 0x9A,
.flags = 0xC }; .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, struct gdt_entry gdt_entry_kernel_mode_data_segment = { .base = 0,
.limit = 0xFFFFF, .limit = 0xFFFFF,
.access_byte = 0x92, .access_byte = 0x92,
.flags = 0xC }; .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, struct gdt_entry gdt_entry_user_mode_code_segment = { .base = 0,
.limit = 0xFFFFF, .limit = 0xFFFFF,
.access_byte = 0xFA, .access_byte = 0xFA,
.flags = 0xC }; .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, struct gdt_entry gdt_entry_user_mode_data_segment = { .base = 0,
.limit = 0xFFFFF, .limit = 0xFFFFF,
.access_byte = 0xF2, .access_byte = 0xF2,
.flags = 0xC }; .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");
} }

View File

@ -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