IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
idt.cpp
Go to the documentation of this file.
1
11
12
13// C++
14#include <algorithm>
15#include <iterator>
16#include <type_traits>
17// IgrOS-Kernel arch x86
19#include <arch/x86/irq.hpp>
20#include <arch/x86/native.hpp>
21
22#if defined(IGROS_ARCH_i386)
23
24// IgrOS-Kernel arch i386
25#include <arch/i386/gdt.hpp>
26#include <arch/i386/idt.hpp>
27
28#elif defined(IGROS_ARCH_x86_64)
29
30// IgrOS-Kernel arch x86_64
31#include <arch/x86_64/gdt.hpp>
32#include <arch/x86_64/idt.hpp>
33#include <arch/x86_64/tss.hpp>
34
35#endif
36
37
38#ifdef __cplusplus
39
40extern "C" {
41
42#endif // __cplusplus
43
44
47 void idtLoad(const igros::x86::native::idt::pointer_t* const idtPtr) noexcept;
50 void idtStore(igros::x86::native::idt::pointer_t* const idtPtr) noexcept;
51
52
53#ifdef __cplusplus
54}
55
56#endif // __cplusplus
57
58
59// OS namespace (members of native idt are defined in namespace enclosing it)
60namespace igros {
61
62
63 // Set IDT pointer
64 void x86::native::idt::setPointer(const pointer_t &pointer) noexcept {
65 // Load new IDT
66 ::idtLoad(&pointer);
67 }
68
69
70 // Init IDT table
71 void x86::native::idt::init() noexcept {
72
73 // Kernel code segment selector
74 constexpr auto KERNEL_CODE {x86::native::gdt::KERNEL_CODE};
75 // Present, ring 0, interrupt gate
76 constexpr auto INTERRUPT_GATE {0x8E_u8};
77
78 // Every exception and IRQ vector must fit into IDT
79 static_assert(std::size(::irqHandlerTable) == x86::IRQ_LINES);
80 static_assert(std::size(::exHandlerTable) <= x86::IRQ_OFFSET);
81
82 // Exceptions and IRQ descriptors table (IDT)
83 constinit static idt::table_t table {};
84
85 // Kernel interrupt gate for handler
86 const auto gate = [](const std::add_pointer_t<void() noexcept> handler) noexcept {
87 return idt::setEntry(handler, KERNEL_CODE, INTERRUPT_GATE);
88 };
89 // Exceptions setup (vectors from 0)
90 std::ranges::transform(::exHandlerTable, table.begin(), gate);
91 // IRQs setup (vectors from IRQ offset)
92 std::ranges::transform(::irqHandlerTable, table.begin() + x86::irqVector(x86::irq_t::PIT), gate);
93
94 // Exceptions that can't trust current stack
95 using enum x86::except::NUMBER;
96 const auto vector = [](const x86::except::NUMBER number) noexcept {
97 return static_cast<igros_usize_t>(number);
98 };
99
100#if defined(IGROS_ARCH_i386)
101
102 // Double fault: task gate to double fault task (own stack and page directory)
103 constexpr auto TASK_GATE {0x85_u8};
104 table[vector(DOUBLE_FAULT)] = idt::setEntry(nullptr, x86::native::gdt::TSS_DOUBLE_FAULT, TASK_GATE);
105
106#elif defined(IGROS_ARCH_x86_64)
107
108 // Double fault, NMI and machine check: own interrupt stacks (TSS IST)
109 table[vector(DOUBLE_FAULT)].ist = static_cast<igros_byte_t>(x86::native::tss::IST::DOUBLE_FAULT);
110 table[vector(NON_MASKABLE_IRQ)].ist = static_cast<igros_byte_t>(x86::native::tss::IST::NMI);
111 table[vector(MACHINE_CHECK)].ist = static_cast<igros_byte_t>(x86::native::tss::IST::MACHINE_CHECK);
112
113#endif
114
115 // Pointer to IDT
116 constinit static const idt::pointer_t pointer {
117 .size = idt::calcSize(table),
118 .pointer = table.data(),
119 };
120
121 // Set IDT table
122 idt::setPointer(pointer);
123 }
124
125
126} // namespace igros
127
NUMBER
Exceptions number enumeration.
static constexpr auto calcSize(const table_t &table) noexcept -> igros_word_t
Calc IDT size.
Definition idt.hpp:125
std::array< entry_t, IDT_SIZE > table_t
IDT table type.
Definition idt.hpp:75
static auto setEntry(const offset_t handle, const igros_word_t selector, const igros_byte_t type) noexcept -> entry_t
Make IDT entry.
Definition idt.hpp:109
static void setPointer(const pointer_t &pointer) noexcept
Set IDT pointer.
Definition idt.cpp:64
Exceptions low-level operations.
const std::add_pointer_t< void() noexcept > exHandlerTable[32]
Exception handlers addresses, indexed by exception number (exceptions.s).
std::size_t igros_usize_t
Unsigned size.
Definition types.hpp:51
std::uint8_t igros_byte_t
8-bit unsigned integer
Definition types.hpp:41
Global descriptor table low-level operations.
Interrupt descriptor table low-level operations.
void idtStore(igros::x86::native::idt::pointer_t *const idtPtr) noexcept
Store IDT.
void idtLoad(const igros::x86::native::idt::pointer_t *const idtPtr) noexcept
Load IDT.
constexpr auto IRQ_LINES
Number of PIC interrupt lines.
Definition irq.hpp:82
constexpr auto irqVector(const irq_t line) noexcept -> igros_usize_t
Interrupt vector of IRQ line.
Definition irq.hpp:89
constexpr auto IRQ_OFFSET
IRQ offset in ISR list.
Definition isr.hpp:30
IgrOS kernel: common types and literals.
x86 family current (native) architecture selection
IDT table pointer.
Definition idt.hpp:66
Interrupts low-level operations.
const std::add_pointer_t< void() noexcept > irqHandlerTable[16]
IRQ handlers addresses, indexed by IRQ line (irq.s).
Global descriptor table low-level operations.
Interrupt descriptor table low-level operations.
Task state segment (x86_64).