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
+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