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

Binary file not shown.

View File

@@ -1,23 +1,14 @@
OSNAME = CustomOS
GNUEFI = ../gnu-efi
OVMFDIR = ../OVMFbin
LDS = kernel.ld
CC = gcc
LD = ld
CFLAGS = -ffreestanding -fshort-wchar
LDFLAGS = -T $(LDS) -static -Bsymbolic -nostdlib
OSNAME = CustomOS OSNAME = CustomOS
GNUEFI = ../gnu-efi GNUEFI = ../gnu-efi
OVMFDIR = ../OVMFbin OVMFDIR = ../OVMFbin
LDS = kernel.ld LDS = kernel.ld
CC = gcc CC = gcc
ASMC = nasm
LD = ld LD = ld
CFLAGS = -ffreestanding -fshort-wchar CFLAGS = -ffreestanding -fshort-wchar
ASMFLAGS =
LDFLAGS = -T $(LDS) -static -Bsymbolic -nostdlib LDFLAGS = -T $(LDS) -static -Bsymbolic -nostdlib
SRCDIR := src SRCDIR := src
@@ -28,7 +19,9 @@ BOOTEFI := $(GNUEFI)/x86_64/bootloader/main.efi
rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d)) rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))
SRC = $(call rwildcard,$(SRCDIR),*.cpp) SRC = $(call rwildcard,$(SRCDIR),*.cpp)
ASMSRC = $(call rwildcard,$(SRCDIR),*.asm)
OBJS = $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(SRC)) OBJS = $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(SRC))
OBJS += $(patsubst $(SRCDIR)/%.asm, $(OBJDIR)/%_asm.o, $(ASMSRC))
DIRS = $(wildcard $(SRCDIR)/*) DIRS = $(wildcard $(SRCDIR)/*)
kernel: $(OBJS) link kernel: $(OBJS) link
@@ -38,9 +31,13 @@ $(OBJDIR)/%.o: $(SRCDIR)/%.cpp
@ mkdir -p $(@D) @ mkdir -p $(@D)
$(CC) $(CFLAGS) -c $^ -o $@ $(CC) $(CFLAGS) -c $^ -o $@
$(OBJDIR)/%_asm.o: $(SRCDIR)/%.asm
@ echo !==== ASSEMBLING $^
@ mkdir -p $(@D)
$(ASMC) $(ASMFLAGS) $^ -f elf64 -o $@
link: link:
@ echo !==== LINKING @ echo !==== LINKING
@echo $(OBJS)
$(LD) $(LDFLAGS) -o $(BUILDDIR)/kernel.elf $(OBJS) $(LD) $(LDFLAGS) -o $(BUILDDIR)/kernel.elf $(OBJS)
setup: setup:

Binary file not shown.

Binary file not shown.

BIN
kernel/lib/gdt/gdt.o Normal file

Binary file not shown.

BIN
kernel/lib/gdt/gdt_asm.o Normal file

Binary file not shown.

Binary file not shown.

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