gdt working

This commit is contained in:
2026-01-29 08:04:16 +11:00
parent 68df6c7ebb
commit 7d869a3cb4
11 changed files with 79 additions and 13 deletions

23
kernel/src/gdt/gdt.asm Normal file
View File

@@ -0,0 +1,23 @@
[bits 64]
LoadGDT:
; rdi is the first argument passed to the LoadGDT function by C
lgdt [rdi]
; update the data segment registers
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
; update the code segment
pop rdi ; store return address in rdi
mov rax, 0x08 ; code segment offset
push rax ; we push 0x08 because it will be used by the far return instruction
push rdi ; push return address because it will be used by far return
; far return
retfq
GLOBAL LoadGDT

11
kernel/src/gdt/gdt.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include "gdt.h"
__attribute__((aligned(0x1000)))
GDT DefaultGDT = {
{0, 0, 0, 0x00, 0x00, 0}, // null
{0, 0, 0, 0x9A, 0xA0, 0}, // kernel code
{0, 0, 0, 0x92, 0xA0, 0}, // kernel data
{0, 0, 0, 0x00, 0x00, 0}, // user null
{0, 0, 0, 0x9A, 0xA0, 0}, // user code
{0, 0, 0, 0x92, 0xA0, 0}, // user data
};

29
kernel/src/gdt/gdt.h Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <stdint.h>
struct GDTDescriptor {
uint16_t Size;
uint64_t Offset;
}__attribute__((packed));
struct GDTEntry {
uint16_t Limit0;
uint16_t Base0;
uint8_t Base1;
uint8_t AccessByte;
uint8_t Limit1_Flags;
uint8_t Base2;
}__attribute__((packed));
struct GDT {
GDTEntry Null; // 0x00
GDTEntry KernelCode; // 0x08
GDTEntry KernelData; // 0x10
GDTEntry UserNull; // 0x18
GDTEntry UserCode; // 0x20
GDTEntry UserData; // 0x28
}__attribute__((packed)) __attribute__((aligned(0x1000)));
extern GDT DefaultGDT;
extern "C" void LoadGDT(GDTDescriptor* gdtDescriptor);

View File

@@ -5,6 +5,7 @@ extern "C" void __stack_chk_fail(void) {
}
#include "kernelUtil.h"
#include "gdt/gdt.h"
KernelInfo kernelInfo;
PageTableManager pageTableManager = NULL;
@@ -43,6 +44,11 @@ void PrepareMemory(BootInfo* bootInfo){
KernelInfo InitializeKernel(BootInfo* bootInfo){
GDTDescriptor gdtDescriptor;
gdtDescriptor.Size = sizeof(GDT) - 1;
gdtDescriptor.Offset = (uint64_t)&DefaultGDT;
LoadGDT(&gdtDescriptor);
PrepareMemory(bootInfo);
memset(bootInfo->framebuffer->BaseAddress, 0, bootInfo->framebuffer->BufferSize);