gdt working
This commit is contained in:
@@ -1,23 +1,14 @@
|
||||
|
||||
OSNAME = CustomOS
|
||||
|
||||
GNUEFI = ../gnu-efi
|
||||
OVMFDIR = ../OVMFbin
|
||||
LDS = kernel.ld
|
||||
CC = gcc
|
||||
ASMC = nasm
|
||||
LD = ld
|
||||
|
||||
CFLAGS = -ffreestanding -fshort-wchar
|
||||
LDFLAGS = -T $(LDS) -static -Bsymbolic -nostdlib
|
||||
OSNAME = CustomOS
|
||||
|
||||
GNUEFI = ../gnu-efi
|
||||
OVMFDIR = ../OVMFbin
|
||||
LDS = kernel.ld
|
||||
CC = gcc
|
||||
LD = ld
|
||||
|
||||
CFLAGS = -ffreestanding -fshort-wchar
|
||||
ASMFLAGS =
|
||||
LDFLAGS = -T $(LDS) -static -Bsymbolic -nostdlib
|
||||
|
||||
SRCDIR := src
|
||||
@@ -27,8 +18,10 @@ BOOTEFI := $(GNUEFI)/x86_64/bootloader/main.efi
|
||||
|
||||
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)/%.asm, $(OBJDIR)/%_asm.o, $(ASMSRC))
|
||||
DIRS = $(wildcard $(SRCDIR)/*)
|
||||
|
||||
kernel: $(OBJS) link
|
||||
@@ -38,9 +31,13 @@ $(OBJDIR)/%.o: $(SRCDIR)/%.cpp
|
||||
@ mkdir -p $(@D)
|
||||
$(CC) $(CFLAGS) -c $^ -o $@
|
||||
|
||||
$(OBJDIR)/%_asm.o: $(SRCDIR)/%.asm
|
||||
@ echo !==== ASSEMBLING $^
|
||||
@ mkdir -p $(@D)
|
||||
$(ASMC) $(ASMFLAGS) $^ -f elf64 -o $@
|
||||
|
||||
link:
|
||||
@ echo !==== LINKING
|
||||
@echo $(OBJS)
|
||||
$(LD) $(LDFLAGS) -o $(BUILDDIR)/kernel.elf $(OBJS)
|
||||
|
||||
setup:
|
||||
|
||||
Binary file not shown.
Binary file not shown.
BIN
kernel/lib/gdt/gdt.o
Normal file
BIN
kernel/lib/gdt/gdt.o
Normal file
Binary file not shown.
BIN
kernel/lib/gdt/gdt_asm.o
Normal file
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
23
kernel/src/gdt/gdt.asm
Normal 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
11
kernel/src/gdt/gdt.cpp
Normal 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
29
kernel/src/gdt/gdt.h
Normal 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);
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user