forked from CHatingPython/towos
feat: add IDT
This commit is contained in:
@@ -33,6 +33,7 @@ Checks: >
|
|||||||
-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access,
|
-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access,
|
||||||
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
|
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
|
||||||
-cppcoreguidelines-use-enum-class,
|
-cppcoreguidelines-use-enum-class,
|
||||||
|
-cppcoreguidelines-special-member-functions,
|
||||||
-bugprone-forward-declaration-namespace,
|
-bugprone-forward-declaration-namespace,
|
||||||
-bugprone-tagged-union-member-count,
|
-bugprone-tagged-union-member-count,
|
||||||
-bugprone-unchecked-optional-access
|
-bugprone-unchecked-optional-access
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ struct arch {
|
|||||||
|
|
||||||
virtual void init() = 0;
|
virtual void init() = 0;
|
||||||
|
|
||||||
static arch* get();
|
static arch& get();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ARCH_HPP
|
#endif // ARCH_HPP
|
||||||
|
|||||||
@@ -1,15 +1,21 @@
|
|||||||
#include "./arch.hpp"
|
#include "./arch.hpp"
|
||||||
|
|
||||||
arch* arch::get() {
|
arch& arch::get() {
|
||||||
static x86_64::arch s_arch;
|
return x86_64::arch::get();
|
||||||
return &s_arch;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace x86_64 {
|
namespace x86_64 {
|
||||||
|
|
||||||
|
arch& arch::get() {
|
||||||
|
static x86_64::arch s_arch;
|
||||||
|
return s_arch;
|
||||||
|
}
|
||||||
|
|
||||||
void arch::init() {
|
void arch::init() {
|
||||||
load_gdt(m_gdt, sizeof(m_gdt));
|
m_gdt.load();
|
||||||
reload_segments(0x8, 0x10);
|
reload_segments(gdt::KERNEL_CS, gdt::KERNEL_DS);
|
||||||
|
|
||||||
|
m_idt.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace x86_64
|
} // namespace x86_64
|
||||||
|
|||||||
@@ -2,49 +2,21 @@
|
|||||||
#define ARCH_X86_64_ARCH_HPP
|
#define ARCH_X86_64_ARCH_HPP
|
||||||
|
|
||||||
#include "./gdt.hpp"
|
#include "./gdt.hpp"
|
||||||
|
#include "./idt.hpp"
|
||||||
#include "./task.hpp"
|
#include "./task.hpp"
|
||||||
#include "arch/arch.hpp"
|
#include "arch/arch.hpp"
|
||||||
|
|
||||||
namespace x86_64 {
|
namespace x86_64 {
|
||||||
|
|
||||||
class arch : public ::arch {
|
class arch : public ::arch {
|
||||||
|
public:
|
||||||
|
static arch& get();
|
||||||
public:
|
public:
|
||||||
void init() override;
|
void init() override;
|
||||||
private:
|
private:
|
||||||
tss m_tss{};
|
tss m_tss{};
|
||||||
|
gdt m_gdt{ m_tss };
|
||||||
gdt_entry m_gdt[7] = {
|
idt m_idt;
|
||||||
// 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
|
} // namespace x86_64
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ struct gdt_descriptor {
|
|||||||
const void* offset;
|
const void* offset;
|
||||||
} PACKED;
|
} PACKED;
|
||||||
|
|
||||||
void load_gdt(const gdt_entry* entries, u32 size) {
|
void gdt::load() {
|
||||||
gdt_descriptor gdtr{ static_cast<u16>((size - 1) & 0xFFFF), entries };
|
gdt_descriptor gdtr{ (sizeof(entries) - 1), entries };
|
||||||
|
|
||||||
asm volatile("lgdt %0" : : "m"(gdtr));
|
asm volatile("lgdt %0" : : "m"(gdtr));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef ARCH_X86_64_GDT_HPP
|
#ifndef ARCH_X86_64_GDT_HPP
|
||||||
#define ARCH_X86_64_GDT_HPP
|
#define ARCH_X86_64_GDT_HPP
|
||||||
|
|
||||||
|
#include "arch/x86_64/task.hpp"
|
||||||
#include "types.hpp"
|
#include "types.hpp"
|
||||||
|
|
||||||
namespace x86_64 {
|
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>(access & 0xFF) << 40) | (static_cast<u64>(limit & 0x0F) << 48) |
|
||||||
(static_cast<u64>(flags & 0x0F) << 52) | (static_cast<u64>((base >> 24) & 0xFF) << 56)) {}
|
(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) {}
|
: value(value) {}
|
||||||
|
|
||||||
static gdt_entry extend(u32 baseHi) { return { baseHi }; }
|
static gdt_entry extend(u32 baseHi) { return { baseHi }; }
|
||||||
@@ -40,7 +41,51 @@ struct gdt_entry {
|
|||||||
};
|
};
|
||||||
} PACKED;
|
} 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);
|
extern "C" void reload_segments(u64 codeSegment, u64 dataSegment);
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
+1
-3
@@ -1,12 +1,10 @@
|
|||||||
#include "arch/arch.hpp"
|
#include "arch/arch.hpp"
|
||||||
#include "types.hpp"
|
|
||||||
#include "vga.hpp"
|
#include "vga.hpp"
|
||||||
|
|
||||||
extern "C" void kmain(void) {
|
extern "C" void kmain(void) {
|
||||||
vga::clear();
|
vga::clear();
|
||||||
|
|
||||||
arch* arch = arch::get();
|
arch::get().init();
|
||||||
arch->init();
|
|
||||||
|
|
||||||
vga::puts("TowOS initialized!", 0, 0);
|
vga::puts("TowOS initialized!", 0, 0);
|
||||||
vga::puts("Zdravstvuyte, tovarisch!", 0, 1);
|
vga::puts("Zdravstvuyte, tovarisch!", 0, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user