feat: load GDT

This commit is contained in:
2026-10-02 11:59:14 +02:00
parent b463527025
commit fb71af6d41
7 changed files with 142 additions and 11 deletions
+19
View File
@@ -0,0 +1,19 @@
.section .text
.code64
.global reload_segments
.type reload_segments, @function
reload_segments:
pushq %rdi
leaq .reload_cs(%rip), %rax
pushq %rax
lretq
.reload_cs:
movw %si, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
ret
+14
View File
@@ -0,0 +1,14 @@
#include "gdt.hpp"
#include "types.hpp"
struct gdt_descriptor {
u16 size;
const void* offset;
} PACKED;
void load_gdt(const gdt_entry* entries, u32 size) {
gdt_descriptor gdtr{ static_cast<u16>((size - 1) & 0xFFFF), entries };
asm volatile("lgdt %0" : : "m"(gdtr));
}
+45
View File
@@ -0,0 +1,45 @@
#ifndef GDT_HPP
#define GDT_HPP
#include "types.hpp"
struct gdt_entry {
u64 value;
constexpr gdt_entry(u32 base, u32 limit, u8 access, u8 flags)
: value((static_cast<u64>(limit & 0xFFFF) << 0) | (static_cast<u64>(base & 0xFFFFFF) << 16) |
(static_cast<u64>(access & 0xFF) << 40) | (static_cast<u64>(limit & 0x0F) << 48) |
(static_cast<u64>(flags & 0x0F) << 52) | (static_cast<u64>((base >> 24) & 0xFF) << 56)) {}
constexpr gdt_entry(u64 value)
: value(value) {}
static gdt_entry extend(u32 baseHi) { return { baseHi }; }
enum {
ACCESS_P = 1 << 7, /**< Present */
ACCESS_DPL0 = 0, /**< Highest privilege */
ACCESS_DPL3 = 3 << 5, /**< Lowest privilege */
ACCESS_S = 1 << 4, /**< Code/data segment */
ACCESS_E = 1 << 3, /**< Executable */
ACCESS_DC = 1 << 2, /**< Direction/Conforming */
ACCESS_RW = 1 << 1, /**< Readable/Writeable */
ACCESS_A = 1 << 0, /**< Accessed */
TYPE_LDT = 0x2,
TYPE_AVAIL = 0x9,
TYPE_BUSY = 0xB,
};
enum {
FLAG_G = 1 << 3, /**< Granularity */
FLAG_DB = 1 << 2, /**< Size */
FLAG_L = 1 << 1, /**< Long mode */
};
} PACKED;
void load_gdt(const gdt_entry* entries, u32 size);
extern "C" void reload_segments(u64 codeSegment, u64 dataSegment);
#endif // GDT_HPP
+50 -8
View File
@@ -1,15 +1,57 @@
using u8 = unsigned char;
using u16 = unsigned short;
using u32 = unsigned int;
using u64 = unsigned long long;
#include "gdt.hpp"
#include "types.hpp"
static volatile u16* const s_vga = reinterpret_cast<volatile u16*>(0xB8000);
extern "C" void kmain() {
const char* msg = "Zdravstvuyte, tovorisch!";
static constexpr u8 VGA_ROWS = 25;
static constexpr u8 VGA_COLS = 80;
for (int i = 0; msg[i] != '\0'; i++)
s_vga[i] = 0x0700 | msg[i];
static inline void clrsc(void) {
for (u32 i = 0; i < VGA_ROWS * VGA_COLS; ++i)
s_vga[i] = 0;
}
static inline void puts(const char* str, u8 row, u8 col) {
for (u32 off = 0; str[off] != '\0'; ++off)
s_vga[col + off + (VGA_COLS * row)] = 0x0700 | str[off];
}
static constexpr gdt_entry s_gdt[5] = {
// Null Descriptor
gdt_entry{ 0, 0, 0, 0 },
// Kernel Mode Code Segment
gdt_entry{ 0,
0xFFFFF,
gdt_entry::ACCESS_P | gdt_entry::ACCESS_DPL0 | gdt_entry::ACCESS_S | gdt_entry::ACCESS_E | gdt_entry::ACCESS_RW,
gdt_entry::FLAG_G | gdt_entry::FLAG_L },
// Kernel Mode Data Segment
gdt_entry{ 0,
0xFFFFF,
gdt_entry::ACCESS_P | gdt_entry::ACCESS_DPL0 | gdt_entry::ACCESS_S | gdt_entry::ACCESS_RW,
gdt_entry::FLAG_G | gdt_entry::FLAG_DB },
// User Mode Data Segment
gdt_entry{ 0,
0xFFFFF,
gdt_entry::ACCESS_P | gdt_entry::ACCESS_DPL3 | gdt_entry::ACCESS_S | gdt_entry::ACCESS_RW,
gdt_entry::FLAG_G | gdt_entry::FLAG_DB },
// User Mode Code Segment
gdt_entry{ 0,
0xFFFFF,
gdt_entry::ACCESS_P | gdt_entry::ACCESS_DPL3 | gdt_entry::ACCESS_S | gdt_entry::ACCESS_E | gdt_entry::ACCESS_RW,
gdt_entry::FLAG_G | gdt_entry::FLAG_L },
// Task State Segment
// gdt_entry{ 0, 0, gdt_entry::ACCESS_P | gdt_entry::ACCESS_E | gdt_entry::ACCESS_A, 0 },
// gdt_entry{ 0 }
};
extern "C" void kmain(void) {
clrsc();
load_gdt(s_gdt, sizeof(s_gdt));
reload_segments(0x8, 0x10);
puts("Loaded GDT!", 0, 0);
puts("Zdravstvuyte, tovarisch!", 1, 0);
for (;;)
__asm__ volatile("hlt");
+11
View File
@@ -0,0 +1,11 @@
#ifndef TYPES_HPP
#define TYPES_HPP
using u8 = unsigned char;
using u16 = unsigned short;
using u32 = unsigned int;
using u64 = unsigned long long;
#define PACKED __attribute__((packed))
#endif // TYPES_HPP