refactor: add architecture

This commit is contained in:
2026-10-02 13:32:40 +02:00
parent c4fb939e48
commit 387db2dcf7
11 changed files with 283 additions and 53 deletions
+20 -8
View File
@@ -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)
+122
View File
@@ -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"
}
]
+18
View File
@@ -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
+15
View File
@@ -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
+52
View File
@@ -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<u32>(reinterpret_cast<u64>(&m_tss) & 0xFFFFFFFF),
sizeof(m_tss) - 1,
gdt_entry::ACCESS_P | gdt_entry::ACCESS_E | gdt_entry::ACCESS_A,
0 },
gdt_entry{ static_cast<u32>(reinterpret_cast<u64>(&m_tss) >> 32) },
};
};
} // namespace x86_64
#endif // ARCH_X86_64_ARCH_HPP
@@ -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
@@ -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
@@ -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
+4 -38
View File
@@ -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<volatile u16*>(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<u32>(reinterpret_cast<u64>(&s_tss) & 0xFFFFFFFF),
sizeof(s_tss) - 1,
gdt_entry::ACCESS_P | gdt_entry::ACCESS_E | gdt_entry::ACCESS_A,
0 },
gdt_entry{ static_cast<u32>(reinterpret_cast<u64>(&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 (;;)
+33
View File
@@ -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