Skip to content

Instantly share code, notes, and snippets.

@josephxanderson
Last active March 27, 2022 18:50
Show Gist options
  • Save josephxanderson/67235aebca8790b50f84048b371af7b5 to your computer and use it in GitHub Desktop.
Save josephxanderson/67235aebca8790b50f84048b371af7b5 to your computer and use it in GitHub Desktop.
Poncho OS Tutorials
[bits 64]
loadGDT:
lgdt [rdi] ; "rdi" register will contain parameter passed to loadGDT when called from C.
; Upload kernelData segment.
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
pop rdi ; Pop return address of loadGDT function.
; Upload kernelCode segment.
mov rax, 0x08
push rax
push rdi
retfq
GLOBAL loadGDT
__attribute__((aligned(0x1000)))
GlobalDescriptorTable defaultGDT = {
{0, 0, 0, 0x00, 0x00, 0}, // null
{0, 0, 0, 0x9a, 0xa0, 0}, // kernelCode
{0, 0, 0, 0x92, 0xa0, 0}, // kernelData
{0, 0, 0, 0x9a, 0xa0, 0}, // userCode
{0, 0, 0, 0x92, 0xa0, 0}, // userData
};
#pragma once
#include <Common/Types.h>
struct GDTDescriptor {
UInt16 size;
UInt64 offset;
} __attribute__((packed));
struct GDTEntry {
UInt16 limit0;
UInt16 base0;
UInt8 base1;
UInt8 accessByte;
UInt8 limit1Flags;
UInt8 base2;
}__attribute__((packed));
struct GlobalDescriptorTable {
GDTEntry null; // 0x00
GDTEntry kernelCode; // 0x08
GDTEntry kernelData; // 0x10
GDTEntry userNull;
GDTEntry userCode;
GDTEntry userData;
}__attribute__((packed)) __attribute__((aligned(0x1000)));
extern GlobalDescriptorTable defaultGDT;
extern "C" void loadGDT(GDTDescriptor *gdtDescriptor);
KernelInformation initializeKernel(BootInformation *bootInformation)
{
GDTDescriptor gdtDescriptor;
gdtDescriptor.size = (sizeof(GlobalDescriptorTable) - 1);
gdtDescriptor.offset = (UInt64)&defaultGDT;
loadGDT(&gdtDescriptor);
prepareMemory(bootInformation);
return kernelInformation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment