diff --git a/.clang-tidy b/.clang-tidy index 06d18e3..c0a9487 100755 --- a/.clang-tidy +++ b/.clang-tidy @@ -33,6 +33,7 @@ Checks: > -cppcoreguidelines-pro-bounds-avoid-unchecked-container-access, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, -cppcoreguidelines-use-enum-class, + -cppcoreguidelines-special-member-functions, -bugprone-forward-declaration-namespace, -bugprone-tagged-union-member-count, -bugprone-unchecked-optional-access diff --git a/kernel/arch/arch.hpp b/kernel/arch/arch.hpp index 06e112c..76945de 100644 --- a/kernel/arch/arch.hpp +++ b/kernel/arch/arch.hpp @@ -12,7 +12,7 @@ struct arch { virtual void init() = 0; - static arch* get(); + static arch& get(); }; #endif // ARCH_HPP diff --git a/kernel/arch/x86_64/arch.cpp b/kernel/arch/x86_64/arch.cpp index d8b9b1f..1366071 100644 --- a/kernel/arch/x86_64/arch.cpp +++ b/kernel/arch/x86_64/arch.cpp @@ -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 diff --git a/kernel/arch/x86_64/arch.hpp b/kernel/arch/x86_64/arch.hpp index b9f38da..f4cc4a3 100644 --- a/kernel/arch/x86_64/arch.hpp +++ b/kernel/arch/x86_64/arch.hpp @@ -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(reinterpret_cast(&m_tss) & 0xFFFFFFFF), - sizeof(m_tss) - 1, - gdt_entry::ACCESS_P | gdt_entry::ACCESS_E | gdt_entry::ACCESS_A, - 0 }, - gdt_entry{ static_cast(reinterpret_cast(&m_tss) >> 32) }, - }; + gdt m_gdt{ m_tss }; + idt m_idt; }; } // namespace x86_64 diff --git a/kernel/arch/x86_64/gdt.cpp b/kernel/arch/x86_64/gdt.cpp index 07e86b8..71dae08 100644 --- a/kernel/arch/x86_64/gdt.cpp +++ b/kernel/arch/x86_64/gdt.cpp @@ -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((size - 1) & 0xFFFF), entries }; +void gdt::load() { + gdt_descriptor gdtr{ (sizeof(entries) - 1), entries }; asm volatile("lgdt %0" : : "m"(gdtr)); } diff --git a/kernel/arch/x86_64/gdt.hpp b/kernel/arch/x86_64/gdt.hpp index fc8269d..5bc3fb9 100644 --- a/kernel/arch/x86_64/gdt.hpp +++ b/kernel/arch/x86_64/gdt.hpp @@ -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(access & 0xFF) << 40) | (static_cast(limit & 0x0F) << 48) | (static_cast(flags & 0x0F) << 52) | (static_cast((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(reinterpret_cast(&tss) & 0xFFFFFFFF), + sizeof(tss) - 1, + gdt_entry::ACCESS_P | gdt_entry::ACCESS_E | gdt_entry::ACCESS_A, + 0 }; + entries[6] = gdt_entry{ static_cast(reinterpret_cast(&tss) >> 32) }; + } + + void load(); +}; extern "C" void reload_segments(u64 codeSegment, u64 dataSegment); diff --git a/kernel/arch/x86_64/idt.cpp b/kernel/arch/x86_64/idt.cpp new file mode 100644 index 0000000..12d7005 --- /dev/null +++ b/kernel/arch/x86_64/idt.cpp @@ -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(reinterpret_cast(isrStubTable[vector]) & 0xFFFF), + gdt::KERNEL_CS, + 0, + 0x8E, + static_cast((reinterpret_cast(isrStubTable[vector]) >> 16) & 0xFFFF), + static_cast((reinterpret_cast(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 diff --git a/kernel/arch/x86_64/idt.hpp b/kernel/arch/x86_64/idt.hpp new file mode 100644 index 0000000..e249a23 --- /dev/null +++ b/kernel/arch/x86_64/idt.hpp @@ -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 diff --git a/kernel/arch/x86_64/isr.S b/kernel/arch/x86_64/isr.S new file mode 100644 index 0000000..4e82168 --- /dev/null +++ b/kernel/arch/x86_64/isr.S @@ -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 diff --git a/kernel/kmain.cpp b/kernel/kmain.cpp index 91947c2..10127ec 100644 --- a/kernel/kmain.cpp +++ b/kernel/kmain.cpp @@ -1,12 +1,10 @@ #include "arch/arch.hpp" -#include "types.hpp" #include "vga.hpp" extern "C" void kmain(void) { vga::clear(); - arch* arch = arch::get(); - arch->init(); + arch::get().init(); vga::puts("TowOS initialized!", 0, 0); vga::puts("Zdravstvuyte, tovarisch!", 0, 1);