feat: add IDT

This commit is contained in:
2026-10-03 09:05:40 +02:00
parent 58afc2fc61
commit 86027f21db
10 changed files with 204 additions and 46 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ struct arch {
virtual void init() = 0;
static arch* get();
static arch& get();
};
#endif // ARCH_HPP
+11 -5
View File
@@ -1,15 +1,21 @@
#include "./arch.hpp"
arch* arch::get() {
static x86_64::arch s_arch;
return &s_arch;
arch& arch::get() {
return x86_64::arch::get();
}
namespace x86_64 {
arch& arch::get() {
static x86_64::arch s_arch;
return s_arch;
}
void arch::init() {
load_gdt(m_gdt, sizeof(m_gdt));
reload_segments(0x8, 0x10);
m_gdt.load();
reload_segments(gdt::KERNEL_CS, gdt::KERNEL_DS);
m_idt.load();
}
} // namespace x86_64
+5 -33
View File
@@ -2,49 +2,21 @@
#define ARCH_X86_64_ARCH_HPP
#include "./gdt.hpp"
#include "./idt.hpp"
#include "./task.hpp"
#include "arch/arch.hpp"
namespace x86_64 {
class arch : public ::arch {
public:
static arch& get();
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) },
};
gdt m_gdt{ m_tss };
idt m_idt;
};
} // namespace x86_64
+2 -2
View File
@@ -9,8 +9,8 @@ struct gdt_descriptor {
const void* offset;
} PACKED;
void load_gdt(const gdt_entry* entries, u32 size) {
gdt_descriptor gdtr{ static_cast<u16>((size - 1) & 0xFFFF), entries };
void gdt::load() {
gdt_descriptor gdtr{ (sizeof(entries) - 1), entries };
asm volatile("lgdt %0" : : "m"(gdtr));
}
+47 -2
View File
@@ -1,6 +1,7 @@
#ifndef ARCH_X86_64_GDT_HPP
#define ARCH_X86_64_GDT_HPP
#include "arch/x86_64/task.hpp"
#include "types.hpp"
namespace x86_64 {
@@ -13,7 +14,7 @@ struct gdt_entry {
(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
constexpr gdt_entry(u64 value = 0) noexcept
: value(value) {}
static gdt_entry extend(u32 baseHi) { return { baseHi }; }
@@ -40,7 +41,51 @@ struct gdt_entry {
};
} PACKED;
void load_gdt(const gdt_entry* entries, u32 size);
struct gdt {
gdt_entry entries[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 },
};
static constexpr auto KERNEL_CS = 1 * sizeof(gdt_entry);
static constexpr auto KERNEL_DS = 2 * sizeof(gdt_entry);
static constexpr auto USER_CS = 4 * sizeof(gdt_entry);
static constexpr auto USER_DS = 3 * sizeof(gdt_entry);
static constexpr auto TSS = 5 * sizeof(gdt_entry);
gdt(const tss& tss) {
// Task State Segment
entries[5] = gdt_entry{ static_cast<u32>(reinterpret_cast<u64>(&tss) & 0xFFFFFFFF),
sizeof(tss) - 1,
gdt_entry::ACCESS_P | gdt_entry::ACCESS_E | gdt_entry::ACCESS_A,
0 };
entries[6] = gdt_entry{ static_cast<u32>(reinterpret_cast<u64>(&tss) >> 32) };
}
void load();
};
extern "C" void reload_segments(u64 codeSegment, u64 dataSegment);
+42
View File
@@ -0,0 +1,42 @@
#include "./idt.hpp"
#include "./gdt.hpp"
#include "types.hpp"
namespace x86_64 {
struct idtr {
u16 limit;
const void* base;
} PACKED;
extern "C" void exception_handler(void) {
asm volatile("cli; hlt");
}
extern "C" void* isrStubTable[32];
idt::idt()
: entries() {
for (u8 vector = 0; vector < 32; ++vector) {
entries[vector] = idt_entry{
static_cast<u16>(reinterpret_cast<u64>(isrStubTable[vector]) & 0xFFFF),
gdt::KERNEL_CS,
0,
0x8E,
static_cast<u16>((reinterpret_cast<u64>(isrStubTable[vector]) >> 16) & 0xFFFF),
static_cast<u32>((reinterpret_cast<u64>(isrStubTable[vector]) >> 32) & 0xFFFFFFFF),
0,
};
}
}
void idt::load() {
idtr idtr{ (sizeof(entries) - 1), entries };
asm volatile("cli\n"
"lidt %0" ::"m"(idtr)
: "memory");
}
} // namespace x86_64
+30
View File
@@ -0,0 +1,30 @@
#ifndef ARCH_X86_64_IDT_HPP
#define ARCH_X86_64_IDT_HPP
#include "types.hpp"
namespace x86_64 {
struct idt_entry {
u16 offsetLow;
u16 selector;
u8 ist;
u8 attributes;
u16 offsetMid;
u32 offsetHigh;
u32 rsvd;
} PACKED;
struct idt {
idt_entry entries[256];
idt();
void load();
};
using vector_t = u8;
} // namespace x86_64
#endif // ARCH_X86_64_IDT_HPP
+64
View File
@@ -0,0 +1,64 @@
.altmacro
.section .text
.code64
.extern exception_handler
.macro isr_err_stub i=1
isr_stub_\i:
call exception_handler
iretq
.endm
.macro isr_nerr_stub i
isr_stub_\i:
call exception_handler
iretq
.endm
# From https://wiki.osdev.org/Interrupts_Tutorial
isr_nerr_stub 0
isr_nerr_stub 1
isr_nerr_stub 2
isr_nerr_stub 3
isr_nerr_stub 4
isr_nerr_stub 5
isr_nerr_stub 6
isr_nerr_stub 7
isr_err_stub 8
isr_nerr_stub 9
isr_err_stub 10
isr_err_stub 11
isr_err_stub 12
isr_err_stub 13
isr_err_stub 14
isr_nerr_stub 15
isr_nerr_stub 16
isr_err_stub 17
isr_nerr_stub 18
isr_nerr_stub 19
isr_nerr_stub 20
isr_nerr_stub 21
isr_nerr_stub 22
isr_nerr_stub 23
isr_nerr_stub 24
isr_nerr_stub 25
isr_nerr_stub 26
isr_nerr_stub 27
isr_nerr_stub 28
isr_nerr_stub 29
isr_err_stub 30
isr_nerr_stub 31
.macro isr_label i
.long isr_stub_\i
.endm
.global isrStubTable
isrStubTable:
.set i, 0
.rept 32
isr_label %i
.set i, i+1
.endr