From 387db2dcf715bb6dd9aee2ae6e6c47ecb7938a5d Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Fri, 2 Oct 2026 13:32:40 +0200 Subject: [PATCH] refactor: add architecture --- Makefile | 28 +++++-- compile_commands.json | 122 ++++++++++++++++++++++++++++++ kernel/arch/arch.hpp | 18 +++++ kernel/arch/x86_64/arch.cpp | 15 ++++ kernel/arch/x86_64/arch.hpp | 52 +++++++++++++ kernel/{ => arch/x86_64}/gdt.S | 0 kernel/{ => arch/x86_64}/gdt.cpp | 6 +- kernel/{ => arch/x86_64}/gdt.hpp | 10 ++- kernel/{ => arch/x86_64}/task.hpp | 10 ++- kernel/kmain.cpp | 42 +--------- kernel/runtime.cpp | 33 ++++++++ 11 files changed, 283 insertions(+), 53 deletions(-) create mode 100644 compile_commands.json create mode 100644 kernel/arch/arch.hpp create mode 100644 kernel/arch/x86_64/arch.cpp create mode 100644 kernel/arch/x86_64/arch.hpp rename kernel/{ => arch/x86_64}/gdt.S (100%) rename kernel/{ => arch/x86_64}/gdt.cpp (81%) rename kernel/{ => arch/x86_64}/gdt.hpp (91%) rename kernel/{ => arch/x86_64}/task.hpp (66%) create mode 100644 kernel/runtime.cpp diff --git a/Makefile b/Makefile index 672a8d2..2c3a5c2 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,23 @@ TARGET := towos.elf ISO := towos.iso -CC := x86_64-elf-gcc -CXX := x86_64-elf-g++ -LD := x86_64-elf-ld -AS := x86_64-elf-as +# Possible values: x86_64 +TARGET_ARCH ?= $(shell uname -m) + +CC := $(TARGET_ARCH)-elf-gcc +CXX := $(TARGET_ARCH)-elf-g++ +LD := $(TARGET_ARCH)-elf-ld +AS := $(TARGET_ARCH)-elf-as CXXFLAGS := \ -std=c++17 \ + -Ikernel \ -ffreestanding \ -fno-exceptions \ -fno-rtti \ -fno-stack-protector \ -fno-stack-check \ + -fno-threadsafe-statics \ -fno-pic \ -fno-pie \ -fno-use-cxa-atexit \ @@ -20,7 +25,11 @@ CXXFLAGS := \ -Wall \ -Wextra \ -Wpedantic \ - -O2 + -O2 \ + -mno-sse \ + -mno-sse2 \ + -mno-mmx \ + -mno-80387 ASFLAGS := \ --64 @@ -29,19 +38,22 @@ LDFLAGS := \ -T linker.ld \ -nostdlib \ -no-pie \ - --build-id=none + --build-id=none \ + --no-warn-rwx-segments SRC_DIR := kernel BIN_DIR := bin BUILD_DIR := build -CXXSOURCES := $(shell find $(SRC_DIR) -type f -name '*.cpp') +CXXSOURCES := $(shell find $(SRC_DIR) -type f -name '*.cpp' -not -path "*/arch/*" -o -type f -name '*.cpp' -path "*/arch/$(TARGET_ARCH)/*") +SSOURCES := $(shell find $(SRC_DIR) -type f -name '*.S' -not -path "*/arch/*" -o -type f -name '*.S' -path "*/arch/$(TARGET_ARCH)/*") 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)/%.S.o,$(SSOURCES)) OBJECTS := $(CXXOBJECTS) $(SOBJECTS) + + .PHONY: all run debug check clean all: $(BUILD_DIR)/$(ISO) diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..7533287 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,122 @@ +[ + { + "file": "kernel/runtime.cpp", + "arguments": [ + "/usr/bin/x86_64-elf-g++", + "-x", + "c++", + "-std=c++17", + "-Ikernel", + "-ffreestanding", + "-fno-exceptions", + "-fno-rtti", + "-fno-stack-protector", + "-fno-stack-check", + "-fno-threadsafe-statics", + "-fno-pic", + "-fno-pie", + "-fno-use-cxa-atexit", + "-mno-red-zone", + "-Wall", + "-Wextra", + "-Wpedantic", + "-O2", + "-c", + "kernel/runtime.cpp", + "-o", + "bin/runtime.o" + ], + "directory": "/home/CeHaP/Dev/towos", + "output": "bin/runtime.o" + }, + { + "file": "kernel/arch/x86_64/arch.cpp", + "arguments": [ + "/usr/bin/x86_64-elf-g++", + "-x", + "c++", + "-std=c++17", + "-Ikernel", + "-ffreestanding", + "-fno-exceptions", + "-fno-rtti", + "-fno-stack-protector", + "-fno-stack-check", + "-fno-threadsafe-statics", + "-fno-pic", + "-fno-pie", + "-fno-use-cxa-atexit", + "-mno-red-zone", + "-Wall", + "-Wextra", + "-Wpedantic", + "-O2", + "-c", + "kernel/arch/x86_64/arch.cpp", + "-o", + "bin/arch/x86_64/arch.o" + ], + "directory": "/home/CeHaP/Dev/towos", + "output": "bin/arch/x86_64/arch.o" + }, + { + "file": "kernel/arch/x86_64/gdt.cpp", + "arguments": [ + "/usr/bin/x86_64-elf-g++", + "-x", + "c++", + "-std=c++17", + "-Ikernel", + "-ffreestanding", + "-fno-exceptions", + "-fno-rtti", + "-fno-stack-protector", + "-fno-stack-check", + "-fno-threadsafe-statics", + "-fno-pic", + "-fno-pie", + "-fno-use-cxa-atexit", + "-mno-red-zone", + "-Wall", + "-Wextra", + "-Wpedantic", + "-O2", + "-c", + "kernel/arch/x86_64/gdt.cpp", + "-o", + "bin/arch/x86_64/gdt.o" + ], + "directory": "/home/CeHaP/Dev/towos", + "output": "bin/arch/x86_64/gdt.o" + }, + { + "file": "kernel/kmain.cpp", + "arguments": [ + "/usr/bin/x86_64-elf-g++", + "-x", + "c++", + "-std=c++17", + "-Ikernel", + "-ffreestanding", + "-fno-exceptions", + "-fno-rtti", + "-fno-stack-protector", + "-fno-stack-check", + "-fno-threadsafe-statics", + "-fno-pic", + "-fno-pie", + "-fno-use-cxa-atexit", + "-mno-red-zone", + "-Wall", + "-Wextra", + "-Wpedantic", + "-O2", + "-c", + "kernel/kmain.cpp", + "-o", + "bin/kmain.o" + ], + "directory": "/home/CeHaP/Dev/towos", + "output": "bin/kmain.o" + } +] \ No newline at end of file diff --git a/kernel/arch/arch.hpp b/kernel/arch/arch.hpp new file mode 100644 index 0000000..06e112c --- /dev/null +++ b/kernel/arch/arch.hpp @@ -0,0 +1,18 @@ +#ifndef ARCH_HPP +#define ARCH_HPP + +struct arch { + arch() = default; + virtual ~arch() = default; + + arch(arch&&) noexcept = default; + arch& operator=(arch&&) noexcept = default; + arch(const arch&) = default; + arch& operator=(const arch&) = default; + + virtual void init() = 0; + + static arch* get(); +}; + +#endif // ARCH_HPP diff --git a/kernel/arch/x86_64/arch.cpp b/kernel/arch/x86_64/arch.cpp new file mode 100644 index 0000000..d8b9b1f --- /dev/null +++ b/kernel/arch/x86_64/arch.cpp @@ -0,0 +1,15 @@ +#include "./arch.hpp" + +arch* arch::get() { + static x86_64::arch s_arch; + return &s_arch; +} + +namespace x86_64 { + +void arch::init() { + load_gdt(m_gdt, sizeof(m_gdt)); + reload_segments(0x8, 0x10); +} + +} // namespace x86_64 diff --git a/kernel/arch/x86_64/arch.hpp b/kernel/arch/x86_64/arch.hpp new file mode 100644 index 0000000..b9f38da --- /dev/null +++ b/kernel/arch/x86_64/arch.hpp @@ -0,0 +1,52 @@ +#ifndef ARCH_X86_64_ARCH_HPP +#define ARCH_X86_64_ARCH_HPP + +#include "./gdt.hpp" +#include "./task.hpp" +#include "arch/arch.hpp" + +namespace x86_64 { + +class arch : public ::arch { +public: + void init() override; +private: + tss m_tss{}; + + gdt_entry m_gdt[7] = { + // 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{ static_cast(reinterpret_cast(&m_tss) & 0xFFFFFFFF), + sizeof(m_tss) - 1, + gdt_entry::ACCESS_P | gdt_entry::ACCESS_E | gdt_entry::ACCESS_A, + 0 }, + gdt_entry{ static_cast(reinterpret_cast(&m_tss) >> 32) }, + }; +}; + +} // namespace x86_64 + +#endif // ARCH_X86_64_ARCH_HPP diff --git a/kernel/gdt.S b/kernel/arch/x86_64/gdt.S similarity index 100% rename from kernel/gdt.S rename to kernel/arch/x86_64/gdt.S diff --git a/kernel/gdt.cpp b/kernel/arch/x86_64/gdt.cpp similarity index 81% rename from kernel/gdt.cpp rename to kernel/arch/x86_64/gdt.cpp index 95f3cf7..07e86b8 100644 --- a/kernel/gdt.cpp +++ b/kernel/arch/x86_64/gdt.cpp @@ -1,7 +1,9 @@ -#include "gdt.hpp" +#include "./gdt.hpp" #include "types.hpp" +namespace x86_64 { + struct gdt_descriptor { u16 size; const void* offset; @@ -12,3 +14,5 @@ void load_gdt(const gdt_entry* entries, u32 size) { asm volatile("lgdt %0" : : "m"(gdtr)); } + +} // namespace x86_64 diff --git a/kernel/gdt.hpp b/kernel/arch/x86_64/gdt.hpp similarity index 91% rename from kernel/gdt.hpp rename to kernel/arch/x86_64/gdt.hpp index 243ddf6..fc8269d 100644 --- a/kernel/gdt.hpp +++ b/kernel/arch/x86_64/gdt.hpp @@ -1,8 +1,10 @@ -#ifndef GDT_HPP -#define GDT_HPP +#ifndef ARCH_X86_64_GDT_HPP +#define ARCH_X86_64_GDT_HPP #include "types.hpp" +namespace x86_64 { + struct gdt_entry { u64 value; @@ -42,4 +44,6 @@ void load_gdt(const gdt_entry* entries, u32 size); extern "C" void reload_segments(u64 codeSegment, u64 dataSegment); -#endif // GDT_HPP +} // namespace x86_64 + +#endif // ARCH_X86_64_GDT_HPP diff --git a/kernel/task.hpp b/kernel/arch/x86_64/task.hpp similarity index 66% rename from kernel/task.hpp rename to kernel/arch/x86_64/task.hpp index 14b9789..6db4540 100644 --- a/kernel/task.hpp +++ b/kernel/arch/x86_64/task.hpp @@ -1,8 +1,10 @@ -#ifndef TSS_HPP -#define TSS_HPP +#ifndef ARCH_X86_64_TSS_HPP +#define ARCH_X86_64_TSS_HPP #include "types.hpp" +namespace x86_64 { + struct tss { u32 rsv0; u64 rsp0; @@ -21,4 +23,6 @@ struct tss { u16 iopb; } PACKED; -#endif // TSS_HPP +} // namespace x86_64 + +#endif // ARCH_X86_64_TSS_HPP diff --git a/kernel/kmain.cpp b/kernel/kmain.cpp index e0a739d..f5fac18 100644 --- a/kernel/kmain.cpp +++ b/kernel/kmain.cpp @@ -1,5 +1,4 @@ -#include "gdt.hpp" -#include "task.hpp" +#include "arch/arch.hpp" #include "types.hpp" static volatile u16* const s_vga = reinterpret_cast(0xB8000); @@ -17,46 +16,13 @@ static inline void puts(const char* str, u8 row, u8 col) { s_vga[col + off + (VGA_COLS * row)] = 0x0700 | str[off]; } -static constexpr tss s_tss{}; - -static gdt_entry s_gdt[] = { - // 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{ static_cast(reinterpret_cast(&s_tss) & 0xFFFFFFFF), - sizeof(s_tss) - 1, - gdt_entry::ACCESS_P | gdt_entry::ACCESS_E | gdt_entry::ACCESS_A, - 0 }, - gdt_entry{ static_cast(reinterpret_cast(&s_tss) >> 32) } -}; - extern "C" void kmain(void) { clrsc(); - load_gdt(s_gdt, sizeof(s_gdt)); - reload_segments(0x8, 0x10); + arch* arch = arch::get(); + arch->init(); - puts("Loaded GDT!", 0, 0); + puts("TowOS initialized!", 0, 0); puts("Zdravstvuyte, tovarisch!", 1, 0); for (;;) diff --git a/kernel/runtime.cpp b/kernel/runtime.cpp new file mode 100644 index 0000000..62461ba --- /dev/null +++ b/kernel/runtime.cpp @@ -0,0 +1,33 @@ +// NOLINTBEGIN + +extern "C" int atexit(void (*func)(void)) { + (void)func; + return 0; +} + +extern "C" int __cxa_atexit(void (*func)(void*), void* ptr, void* dtr) { + (void)func; + (void)ptr; + (void)dtr; + return 0; +} + +void operator delete(void* ptr) noexcept { + (void)ptr; +} + +void operator delete(void* ptr, unsigned long size) noexcept { + (void)ptr; + (void)size; +} + +void operator delete[](void* ptr) noexcept { + (void)ptr; +} + +void operator delete[](void* ptr, unsigned long size) noexcept { + (void)ptr; + (void)size; +} + +// NOLINTEND