IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
gdt.cpp
Go to the documentation of this file.
1
11
12
13// C++
14#include <bit>
15// IgrOS-Kernel arch i386
16#include <arch/i386/gdt.hpp>
17#include <arch/i386/tss.hpp>
18
19
20#ifdef __cplusplus
21
22extern "C" {
23
24#endif // __cplusplus
25
26
28 void gdtResetSegments() noexcept;
29
32 void gdtLoad(const igros::i386::gdt::pointer_t* const gdtPtr) noexcept;
35 void gdtStore(igros::i386::gdt::pointer_t* const gdtPtr) noexcept;
36
39 void tssLoad(const igros::igros_word_t selector) noexcept;
40
41
42#ifdef __cplusplus
43}
44
45#endif // __cplusplus
46
47
48// i386 namespace
49namespace igros::i386 {
50
51
52 // Set GDT pointer
53 void gdt::setPointer(const pointer_t &pointer) noexcept {
54 // Load new GDT
55 ::gdtLoad(&pointer);
56 }
57
58
59 // Init GDT table
60 void gdt::init() noexcept {
61
62 // Global descriptors table (GDT)
63 constinit static gdt::table_t table {
64 // Empty entry (should be there!)
65 gdt::setEntry(0x00000000, 0x00000000, GDT_ENTRY_EMPTY),
66 // Kernel code
67 gdt::setEntry(0x00000000, 0xFFFFFFFF, GDT_ENTRY_CODE_RING0),
68 // Kernel data
69 gdt::setEntry(0x00000000, 0xFFFFFFFF, GDT_ENTRY_DATA_RING0),
70 // User code
71 gdt::setEntry(0x00000000, 0xFFFFFFFF, GDT_ENTRY_CODE_RING3),
72 // User data
73 gdt::setEntry(0x00000000, 0xFFFFFFFF, GDT_ENTRY_DATA_RING3),
74 // Task state segments (filled below: bases are known at run time only)
75 gdt::setEntry(0x00000000, 0x00000000, GDT_ENTRY_EMPTY),
76 gdt::setEntry(0x00000000, 0x00000000, GDT_ENTRY_EMPTY),
77 };
78
79 // Pointer to GDT
80 constinit static const gdt::pointer_t pointer {
81 .size = gdt::calcSize(table),
82 .pointer = table.data(),
83 };
84
85 // Task state segments: kernel task and double fault task
87 const auto entry = [](const tss::segment_t &segment) noexcept {
88 return gdt::tssEntry(static_cast<igros_dword_t>(std::bit_cast<igros_usize_t>(&segment)), static_cast<igros_dword_t>(sizeof(segment) - 1_usize));
89 };
90 table[TSS / sizeof(entry_t)] = entry(tss::kernel());
91 table[TSS_DOUBLE_FAULT / sizeof(entry_t)] = entry(tss::doubleFault());
92
93 // Load new GDT
94 gdt::setPointer(pointer);
95 // Load task register (CPU marks TSS descriptor busy: GDT must stay writable)
97 }
98
99
100} // namespace igros::i386
101
static constexpr auto setEntry(const igros_dword_t base, const igros_dword_t limit, const klib::kFlags< FLAGS > flags) noexcept -> entry_t
Set GDT entry.
Definition gdt.hpp:148
static constexpr auto KERNEL_CODE
Kernel code segment.
Definition gdt.hpp:107
static constexpr auto tssEntry(const igros_dword_t base, const igros_dword_t limit) noexcept -> entry_t
Available 32-bit TSS descriptor (base is known at run time only).
Definition gdt.hpp:163
static constexpr auto TSS_DOUBLE_FAULT
Double fault task.
Definition gdt.hpp:110
static constexpr auto GDT_ENTRY_DATA_RING0
Kernel data segment (ring 0, data).
Definition gdt.hpp:59
static constexpr auto GDT_ENTRY_CODE_RING0
Kernel code segment (ring 0, code).
Definition gdt.hpp:57
static constexpr auto GDT_ENTRY_EMPTY
Empty GDT entry (the first one).
Definition gdt.hpp:55
static constexpr auto calcSize(const table_t &table) noexcept -> igros_word_t
Calc GDT size.
Definition gdt.hpp:178
static void setPointer(const pointer_t &pointer) noexcept
Set GDT pointer.
Definition gdt.cpp:53
static void init() noexcept
Init GDT table.
Definition gdt.cpp:60
static constexpr auto TSS
Kernel task.
Definition gdt.hpp:109
static constexpr auto GDT_ENTRY_DATA_RING3
User data segment (ring 3, data).
Definition gdt.hpp:63
static constexpr auto GDT_ENTRY_CODE_RING3
User code segment (ring 3, code).
Definition gdt.hpp:61
std::array< gdt::entry_t, gdt::GDT_SIZE > table_t
GDT table.
Definition gdt.hpp:104
static constexpr auto KERNEL_DATA
Kernel data segment.
Definition gdt.hpp:108
static void init(const igros_word_t codeSelector, const igros_word_t dataSelector) noexcept
Set up task state segments (double fault task uses current page directory).
Definition tss.cpp:96
static auto doubleFault() noexcept -> segment_t &
Double fault task state segment.
Definition tss.cpp:122
static auto kernel() noexcept -> segment_t &
Kernel task state segment.
Definition tss.cpp:116
std::uint32_t igros_dword_t
32-bit unsigned integer
Definition types.hpp:43
void gdtLoad(const igros::i386::gdt::pointer_t *const gdtPtr) noexcept
Load GDT.
void gdtStore(igros::i386::gdt::pointer_t *const gdtPtr) noexcept
Store GDT.
void gdtResetSegments() noexcept
Reset segments to apply new GDT.
void tssLoad(const igros::igros_word_t selector) noexcept
Load task register.
Global descriptor table low-level operations.
Task state segment (i386).
i386 (32-bit protected mode): GDT, IDT, TSS, CPU registers
IgrOS kernel: common types and literals.
GDT table entry.
Definition gdt.hpp:84
GDT table pointer.
Definition gdt.hpp:95
Task state segment layout (segment registers in low 16 bits).
Definition tss.hpp:47