diff --git a/.clang-tidy b/.clang-tidy index 4303f61..06d18e3 100755 --- a/.clang-tidy +++ b/.clang-tidy @@ -100,6 +100,6 @@ CheckOptions: - key: readability-identifier-naming.StaticConstantCase value: aNy_CasE - key: readability-identifier-naming.EnumConstantCase - value: CamelCase + value: UPPER_CASE - key: readability-identifier-naming.ScopedEnumConstantCase value: CamelCase diff --git a/Makefile b/Makefile index 2f85a91..672a8d2 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ BUILD_DIR := build CXXSOURCES := $(shell find $(SRC_DIR) -type f -name '*.cpp') CXXOBJECTS := $(patsubst $(SRC_DIR)/%.cpp,$(BIN_DIR)/%.o,$(CXXSOURCES)) 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) @@ -49,7 +49,7 @@ $(BIN_DIR)/%.o: $(SRC_DIR)/%.cpp @mkdir -p $(dir $@) $(CXX) -x c++ $(CXXFLAGS) -c $< -o $@ -$(BIN_DIR)/%.o: $(SRC_DIR)/%.S +$(BIN_DIR)/%.S.o: $(SRC_DIR)/%.S @mkdir -p $(dir $@) $(AS) $(ASFLAGS) -c $< -o $@ diff --git a/kernel/gdt.S b/kernel/gdt.S new file mode 100644 index 0000000..3913a98 --- /dev/null +++ b/kernel/gdt.S @@ -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 diff --git a/kernel/gdt.cpp b/kernel/gdt.cpp new file mode 100644 index 0000000..95f3cf7 --- /dev/null +++ b/kernel/gdt.cpp @@ -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((size - 1) & 0xFFFF), entries }; + + asm volatile("lgdt %0" : : "m"(gdtr)); +} diff --git a/kernel/gdt.hpp b/kernel/gdt.hpp new file mode 100644 index 0000000..ea22487 --- /dev/null +++ b/kernel/gdt.hpp @@ -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(limit & 0xFFFF) << 0) | (static_cast(base & 0xFFFFFF) << 16) | + (static_cast(access & 0xFF) << 40) | (static_cast(limit & 0x0F) << 48) | + (static_cast(flags & 0x0F) << 52) | (static_cast((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 diff --git a/kernel/kmain.cpp b/kernel/kmain.cpp index e9980a6..fd7b197 100644 --- a/kernel/kmain.cpp +++ b/kernel/kmain.cpp @@ -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(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"); diff --git a/kernel/types.hpp b/kernel/types.hpp new file mode 100644 index 0000000..1eb6653 --- /dev/null +++ b/kernel/types.hpp @@ -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