forked from CHatingPython/towos
feat: load GDT
This commit is contained in:
+1
-1
@@ -100,6 +100,6 @@ CheckOptions:
|
|||||||
- key: readability-identifier-naming.StaticConstantCase
|
- key: readability-identifier-naming.StaticConstantCase
|
||||||
value: aNy_CasE
|
value: aNy_CasE
|
||||||
- key: readability-identifier-naming.EnumConstantCase
|
- key: readability-identifier-naming.EnumConstantCase
|
||||||
value: CamelCase
|
value: UPPER_CASE
|
||||||
- key: readability-identifier-naming.ScopedEnumConstantCase
|
- key: readability-identifier-naming.ScopedEnumConstantCase
|
||||||
value: CamelCase
|
value: CamelCase
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ BUILD_DIR := build
|
|||||||
CXXSOURCES := $(shell find $(SRC_DIR) -type f -name '*.cpp')
|
CXXSOURCES := $(shell find $(SRC_DIR) -type f -name '*.cpp')
|
||||||
CXXOBJECTS := $(patsubst $(SRC_DIR)/%.cpp,$(BIN_DIR)/%.o,$(CXXSOURCES))
|
CXXOBJECTS := $(patsubst $(SRC_DIR)/%.cpp,$(BIN_DIR)/%.o,$(CXXSOURCES))
|
||||||
SSOURCES := $(shell find $(SRC_DIR) -type f -name '*.S')
|
SSOURCES := $(shell find $(SRC_DIR) -type f -name '*.S')
|
||||||
SOBJECTS := $(patsubst $(SRC_DIR)/%.S,$(BIN_DIR)/%.o,$(SSOURCES))
|
SOBJECTS := $(patsubst $(SRC_DIR)/%.S,$(BIN_DIR)/%.S.o,$(SSOURCES))
|
||||||
|
|
||||||
OBJECTS := $(CXXOBJECTS) $(SOBJECTS)
|
OBJECTS := $(CXXOBJECTS) $(SOBJECTS)
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ $(BIN_DIR)/%.o: $(SRC_DIR)/%.cpp
|
|||||||
@mkdir -p $(dir $@)
|
@mkdir -p $(dir $@)
|
||||||
$(CXX) -x c++ $(CXXFLAGS) -c $< -o $@
|
$(CXX) -x c++ $(CXXFLAGS) -c $< -o $@
|
||||||
|
|
||||||
$(BIN_DIR)/%.o: $(SRC_DIR)/%.S
|
$(BIN_DIR)/%.S.o: $(SRC_DIR)/%.S
|
||||||
@mkdir -p $(dir $@)
|
@mkdir -p $(dir $@)
|
||||||
$(AS) $(ASFLAGS) -c $< -o $@
|
$(AS) $(ASFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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));
|
||||||
|
}
|
||||||
@@ -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
@@ -1,15 +1,57 @@
|
|||||||
using u8 = unsigned char;
|
#include "gdt.hpp"
|
||||||
using u16 = unsigned short;
|
#include "types.hpp"
|
||||||
using u32 = unsigned int;
|
|
||||||
using u64 = unsigned long long;
|
|
||||||
|
|
||||||
static volatile u16* const s_vga = reinterpret_cast<volatile u16*>(0xB8000);
|
static volatile u16* const s_vga = reinterpret_cast<volatile u16*>(0xB8000);
|
||||||
|
|
||||||
extern "C" void kmain() {
|
static constexpr u8 VGA_ROWS = 25;
|
||||||
const char* msg = "Zdravstvuyte, tovorisch!";
|
static constexpr u8 VGA_COLS = 80;
|
||||||
|
|
||||||
for (int i = 0; msg[i] != '\0'; i++)
|
static inline void clrsc(void) {
|
||||||
s_vga[i] = 0x0700 | msg[i];
|
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 (;;)
|
for (;;)
|
||||||
__asm__ volatile("hlt");
|
__asm__ volatile("hlt");
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in New Issue
Block a user