IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
gdt.hpp
Go to the documentation of this file.
1
11
12
13#pragma once
14
15
16// C++
17#include <array>
18// IgrOS-Kernel arch
19#include <arch/types.hpp>
20// IgrOS-Kernel library
21#include <klib/kFlags.hpp>
22
23
24// x86_64 namespace
25namespace igros::x86_64 {
26
27
29 class gdt final {
30
32 enum class FLAGS : igros_word_t {
33 EMPTY = 0x0000_u16,
34 TYPE_ACCESS = 0x0001_u16,
35 TYPE_RODATA = 0x0000_u16,
36 TYPE_DATA = 0x0002_u16,
37 TYPE_ROSTACK = 0x0004_u16,
38 TYPE_STACK = 0x0006_u16,
39 TYPE_EXCODE = 0x0008_u16,
40 TYPE_CODE = 0x000A_u16,
41 TYPE_EXCONF = 0x000C_u16,
42 TYPE_CONF = 0x000E_u16,
43 RING0 = 0x0010_u16,
44 RING1 = 0x0030_u16,
45 RING2 = 0x0050_u16,
46 RING3 = 0x0070_u16,
47 RING_USE = 0x0080_u16,
48 L = 0x0200_u16,
49 SZ = 0x0400_u16,
50 G = 0x0800_u16,
51 };
52
53
55 constexpr static auto GDT_ENTRY_EMPTY {klib::kFlags<FLAGS> {}};
64
66 constexpr static auto GDT_SIZE {7_usize};
67
68
69 public:
70
72 gdt(const gdt &other) = delete;
74 auto operator=(const gdt &other) -> gdt & = delete;
76 gdt(gdt &&other) = delete;
78 auto operator=(gdt &&other) -> gdt & = delete;
79
80
81#pragma pack(push, 1)
82
92
94 // cppcheck-suppress-begin unusedStructMember ; fixed layout read by the CPU, not by C++ code
99 // cppcheck-suppress-end unusedStructMember
100
101#pragma pack(pop)
102
104 using table_t = std::array<gdt::entry_t, gdt::GDT_SIZE>;
105
106 // Segment selectors (index * 8, see init)
107 constexpr static auto KERNEL_CODE {igros_word_t {0x08}};
108 constexpr static auto KERNEL_DATA {igros_word_t {0x10}};
109 constexpr static auto TSS {igros_word_t {0x28}};
110
112 gdt() noexcept = default;
114 ~gdt() noexcept = default;
115
120 [[nodiscard]]
121 constexpr static auto tssEntry(const igros_quad_t base, const igros_dword_t limit) noexcept -> std::array<entry_t, 2>;
122
128 [[nodiscard]]
129 constexpr static auto setEntry(const igros_dword_t base, const igros_dword_t limit, const klib::kFlags<FLAGS> flags) noexcept -> entry_t;
133 [[nodiscard]]
134 constexpr static auto calcSize(const table_t &table) noexcept -> igros_word_t;
135
138 static void setPointer(const pointer_t &pointer) noexcept;
139
141 static void init() noexcept;
142 };
143
144
146 [[nodiscard]]
147 constexpr auto gdt::setEntry(const igros_dword_t base, const igros_dword_t limit, const klib::kFlags<FLAGS> flags) noexcept -> entry_t {
148 // Flags in 32-bit arithmetic (16-bit operands would be promoted to int)
149 const auto bits {static_cast<igros_dword_t>(flags.value())};
150 return entry_t {
151 .limitLow = static_cast<igros_word_t>(limit & 0xFFFF_u32),
152 .baseLow = static_cast<igros_word_t>(base & 0xFFFF_u32),
153 .baseMid = static_cast<igros_byte_t>((base & 0xFF0000_u32) >> 16),
154 .access = static_cast<igros_byte_t>(bits & 0x00FF_u32),
155 .limitFlags = static_cast<igros_byte_t>(((limit & 0xF0000_u32) >> 16) | ((bits & 0x0F00_u32) >> 4)),
156 .baseHigh = static_cast<igros_byte_t>((base & 0xFF000000_u32) >> 24),
157 };
158 }
159
161 [[nodiscard]]
162 constexpr auto gdt::tssEntry(const igros_quad_t base, const igros_dword_t limit) noexcept -> std::array<entry_t, 2> {
163 // Present, ring 0, type 9 (available 64-bit TSS), byte granular limit
164 constexpr auto TSS_AVAILABLE {0x89_u8};
165 return {
166 entry_t {
167 .limitLow = static_cast<igros_word_t>(limit & 0xFFFF_u32),
168 .baseLow = static_cast<igros_word_t>(base & 0xFFFF_u64),
169 .baseMid = static_cast<igros_byte_t>((base >> 16) & 0xFF_u64),
170 .access = TSS_AVAILABLE,
171 .limitFlags = static_cast<igros_byte_t>((limit >> 16) & 0x0F_u32),
172 .baseHigh = static_cast<igros_byte_t>((base >> 24) & 0xFF_u64),
173 },
174 // Upper half: base bits 32..63, rest reserved
175 entry_t {
176 .limitLow = static_cast<igros_word_t>((base >> 32) & 0xFFFF_u64),
177 .baseLow = static_cast<igros_word_t>((base >> 48) & 0xFFFF_u64),
178 .baseMid = 0_u8,
179 .access = 0_u8,
180 .limitFlags = 0_u8,
181 .baseHigh = 0_u8,
182 },
183 };
184 }
185
187 constexpr auto gdt::calcSize(const table_t &table) noexcept -> igros_word_t {
188 // Size equals to (Num of entries * Entry size) - 1
189 return static_cast<igros_word_t>(table.size() * sizeof(entry_t)) - 1_u16;
190 }
191
192
193} // namespace igros::x86_64
194
Bit flags of enum type.
Definition kFlags.hpp:29
static constexpr auto KERNEL_DATA
Kernel data segment.
Definition gdt.hpp:108
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
auto operator=(gdt &&other) -> gdt &=delete
Move assignment.
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
gdt(gdt &&other)=delete
Move c-tor.
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_SIZE
Number of GDT entries.
Definition gdt.hpp:66
static constexpr auto KERNEL_CODE
Kernel code segment.
Definition gdt.hpp:107
FLAGS
GDT entry flags: access byte (bits 0..7) and flags nibble (bits 8..11).
Definition gdt.hpp:32
@ TYPE_ROSTACK
Read-only expand-down data.
Definition gdt.hpp:37
@ RING3
Code/data segment, privilege level 3.
Definition gdt.hpp:46
@ TYPE_CODE
Execute/read code.
Definition gdt.hpp:40
@ TYPE_RODATA
Read-only data.
Definition gdt.hpp:35
@ TYPE_DATA
Read/write data.
Definition gdt.hpp:36
@ SZ
32-bit segment
Definition gdt.hpp:49
@ RING0
Code/data segment, privilege level 0.
Definition gdt.hpp:43
@ TYPE_STACK
Read/write expand-down data.
Definition gdt.hpp:38
@ RING2
Code/data segment, privilege level 2.
Definition gdt.hpp:45
@ RING1
Code/data segment, privilege level 1.
Definition gdt.hpp:44
@ RING_USE
Segment present.
Definition gdt.hpp:47
@ TYPE_EXCONF
Execute-only conforming code.
Definition gdt.hpp:41
@ EMPTY
No flags (null descriptor).
Definition gdt.hpp:33
@ L
64-bit code segment
Definition gdt.hpp:48
@ TYPE_EXCODE
Execute-only code.
Definition gdt.hpp:39
@ TYPE_CONF
Execute/read conforming code.
Definition gdt.hpp:42
@ G
Limit in 4 KB units.
Definition gdt.hpp:50
@ TYPE_ACCESS
Segment accessed.
Definition gdt.hpp:34
gdt(const gdt &other)=delete
Copy c-tor.
gdt() noexcept=default
Default c-tor.
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
auto operator=(const gdt &other) -> gdt &=delete
Copy assignment.
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
std::uint16_t igros_word_t
16-bit unsigned integer
Definition types.hpp:42
std::uint8_t igros_byte_t
8-bit unsigned integer
Definition types.hpp:41
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
Bit flags template datatype.
Freestanding kernel library: printing, math, alignment, flags, strings.
constexpr auto make_kflags(const Args... flags) noexcept -> kFlags< T >
Flags creation helper function from set flags.
Definition kFlags.hpp:94
x86_64 (long mode): GDT, IDT, TSS, CPUID, MSR, CPU registers
GDT table entry.
Definition gdt.hpp:84
igros_byte_t access
Access byte (present, privilege, type).
Definition gdt.hpp:88
igros_word_t baseLow
Base bits 0..15.
Definition gdt.hpp:86
igros_byte_t limitFlags
Limit bits 16..19 and flags (granularity, size, long mode).
Definition gdt.hpp:89
igros_word_t limitLow
Limit bits 0..15.
Definition gdt.hpp:85
igros_byte_t baseHigh
Base bits 24..31.
Definition gdt.hpp:90
igros_byte_t baseMid
Base bits 16..23.
Definition gdt.hpp:87
GDT table pointer.
Definition gdt.hpp:95
const entry_t * pointer
GDT pointer.
Definition gdt.hpp:97
igros_word_t size
GDT size.
Definition gdt.hpp:96
Common C++ types redefinition.