refactor: add architecture
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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,18 @@
|
||||
#include "./gdt.hpp"
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
} // namespace x86_64
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef ARCH_X86_64_GDT_HPP
|
||||
#define ARCH_X86_64_GDT_HPP
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
struct gdt_entry {
|
||||
u64 value;
|
||||
|
||||
constexpr gdt_entry(u32 base, u32 limit, u8 access, u8 flags) noexcept
|
||||
: 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) noexcept
|
||||
: 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);
|
||||
|
||||
} // namespace x86_64
|
||||
|
||||
#endif // ARCH_X86_64_GDT_HPP
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef ARCH_X86_64_TSS_HPP
|
||||
#define ARCH_X86_64_TSS_HPP
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
namespace x86_64 {
|
||||
|
||||
struct tss {
|
||||
u32 rsv0;
|
||||
u64 rsp0;
|
||||
u64 rsp1;
|
||||
u64 rsp2;
|
||||
u64 rsv1;
|
||||
u64 ist1;
|
||||
u64 ist2;
|
||||
u64 ist3;
|
||||
u64 ist4;
|
||||
u64 ist5;
|
||||
u64 ist6;
|
||||
u64 ist7;
|
||||
u64 rsv2;
|
||||
u16 rsv3;
|
||||
u16 iopb;
|
||||
} PACKED;
|
||||
|
||||
} // namespace x86_64
|
||||
|
||||
#endif // ARCH_X86_64_TSS_HPP
|
||||
Reference in New Issue
Block a user