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 <algorithm>
15#include <bit>
16// IgrOS-Kernel arch x86_64
17#include <arch/x86_64/gdt.hpp>
18#include <arch/x86_64/tss.hpp>
19
20
21#ifdef __cplusplus
22
23extern "C" {
24
25#endif // __cplusplus
26
27
29 void gdtResetSegments() noexcept;
30
33 void gdtLoad(const igros::x86_64::gdt::pointer_t* gdtPtr) noexcept;
36 void gdtStore(igros::x86_64::gdt::pointer_t* const gdtPtr) noexcept;
37
40 void tssLoad(const igros::igros_word_t selector) noexcept;
41
42
43#ifdef __cplusplus
44}
45
46#endif // __cplusplus
47
48
49// x86_64 namespace
50namespace igros::x86_64 {
51
52
53 // Set GDT pointer
54 void gdt::setPointer(const pointer_t &pointer) noexcept {
55 // Load new GDT
56 ::gdtLoad(&pointer);
57 }
58
59
60 // Init GDT table
61 void gdt::init() noexcept {
62
63 // Global descriptors table (GDT)
64 constinit static gdt::table_t table {
65 // Empty entry (should be there!)
66 gdt::setEntry(0x00000000, 0x00000000, GDT_ENTRY_EMPTY),
67 // Kernel code
68 gdt::setEntry(0x00000000, 0xFFFFFFFF, GDT_ENTRY_CODE_RING0),
69 // Kernel data
70 gdt::setEntry(0x00000000, 0xFFFFFFFF, GDT_ENTRY_DATA_RING0),
71 // User code
72 gdt::setEntry(0x00000000, 0xFFFFFFFF, GDT_ENTRY_CODE_RING3),
73 // User data
74 gdt::setEntry(0x00000000, 0xFFFFFFFF, GDT_ENTRY_DATA_RING3),
75 // Task state segment (16-byte descriptor, filled below: base is known at run time only)
76 gdt::setEntry(0x00000000, 0x00000000, GDT_ENTRY_EMPTY),
77 gdt::setEntry(0x00000000, 0x00000000, GDT_ENTRY_EMPTY),
78 };
79
80 // Pointer to GDT
81 constinit static const gdt::pointer_t pointer {
82 .size = gdt::calcSize(table),
83 .pointer = table.data(),
84 };
85
86 // Task state segment (interrupt stacks)
87 const auto &segment {tss::init()};
88 const auto descriptor {gdt::tssEntry(static_cast<igros_quad_t>(std::bit_cast<igros_usize_t>(&segment)), static_cast<igros_dword_t>(sizeof(segment) - 1_usize))};
89 std::ranges::copy(descriptor, table.begin() + (TSS / sizeof(entry_t)));
90
91 // Load new GDT
92 gdt::setPointer(pointer);
93 // Load task register (CPU marks TSS descriptor busy: GDT must stay writable)
95 }
96
97
98} // namespace igros::x86_64
99
static constexpr auto TSS
16-byte descriptor: entries 5 and 6
Definition gdt.hpp:109
static constexpr auto GDT_ENTRY_CODE_RING3
User code segment (ring 3, code).
Definition gdt.hpp:61
static constexpr auto GDT_ENTRY_DATA_RING0
Kernel data segment (ring 0, data).
Definition gdt.hpp:59
static constexpr auto tssEntry(const igros_quad_t base, const igros_dword_t limit) noexcept -> std::array< entry_t, 2 >
64-bit available TSS descriptor (two GDT entries: base is 64-bit)
Definition gdt.hpp:162
static void setPointer(const pointer_t &pointer) noexcept
Set GDT pointer.
Definition gdt.cpp:54
std::array< gdt::entry_t, gdt::GDT_SIZE > table_t
GDT table.
Definition gdt.hpp:104
static constexpr auto GDT_ENTRY_CODE_RING0
Kernel code segment (ring 0, code).
Definition gdt.hpp:57
static constexpr auto GDT_ENTRY_DATA_RING3
User data segment (ring 3, data).
Definition gdt.hpp:63
static constexpr auto calcSize(const table_t &table) noexcept -> igros_word_t
Calc GDT size.
Definition gdt.hpp:187
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:147
static constexpr auto GDT_ENTRY_EMPTY
Empty GDT entry (the first one).
Definition gdt.hpp:55
static void init() noexcept
Init GDT table.
Definition gdt.cpp:61
static auto init() noexcept -> const segment_t &
Set up task state segment interrupt stacks (returns static segment for GDT).
Definition tss.cpp:26
std::uint32_t igros_dword_t
32-bit unsigned integer
Definition types.hpp:43
std::uint64_t igros_quad_t
64-bit unsigned integer
Definition types.hpp:44
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.
x86_64 (long mode): GDT, IDT, TSS, CPUID, MSR, CPU registers
IgrOS kernel: common types and literals.
GDT table entry.
Definition gdt.hpp:84
GDT table pointer.
Definition gdt.hpp:95
Global descriptor table low-level operations.
Task state segment (x86_64).