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// i386 namespace
25namespace igros::i386 {
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 constexpr static auto TSS_DOUBLE_FAULT {igros_word_t {0x30}};
111
113 gdt() noexcept = default;
115 ~gdt() noexcept = default;
116
121 [[nodiscard]]
122 constexpr static auto tssEntry(const igros_dword_t base, const igros_dword_t limit) noexcept -> entry_t;
123
129 [[nodiscard]]
130 constexpr static auto setEntry(const igros_dword_t base, const igros_dword_t limit, const klib::kFlags<FLAGS> flags) noexcept -> entry_t;
134 [[nodiscard]]
135 constexpr static auto calcSize(const table_t &table) noexcept -> igros_word_t;
136
139 static void setPointer(const pointer_t &pointer) noexcept;
140
142 static void init() noexcept;
143 };
144
145
147 [[nodiscard]]
148 constexpr auto gdt::setEntry(const igros_dword_t base, const igros_dword_t limit, const klib::kFlags<FLAGS> flags) noexcept -> entry_t {
149 // Flags in 32-bit arithmetic (16-bit operands would be promoted to int)
150 const auto bits {static_cast<igros_dword_t>(flags.value())};
151 return entry_t {
152 .limitLow = static_cast<igros_word_t>(limit & 0xFFFF_u32),
153 .baseLow = static_cast<igros_word_t>(base & 0xFFFF_u32),
154 .baseMid = static_cast<igros_byte_t>((base & 0xFF0000_u32) >> 16),
155 .access = static_cast<igros_byte_t>(bits & 0x00FF_u32),
156 .limitFlags = static_cast<igros_byte_t>(((limit & 0xF0000_u32) >> 16) | ((bits & 0x0F00_u32) >> 4)),
157 .baseHigh = static_cast<igros_byte_t>((base & 0xFF000000_u32) >> 24),
158 };
159 }
160
162 [[nodiscard]]
163 constexpr auto gdt::tssEntry(const igros_dword_t base, const igros_dword_t limit) noexcept -> entry_t {
164 // Present, ring 0, type 9 (available 32-bit TSS), byte granular limit
165 constexpr auto TSS_AVAILABLE {0x89_u8};
166 return entry_t {
167 .limitLow = static_cast<igros_word_t>(limit & 0xFFFF_u32),
168 .baseLow = static_cast<igros_word_t>(base & 0xFFFF_u32),
169 .baseMid = static_cast<igros_byte_t>((base >> 16) & 0xFF_u32),
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_u32),
173 };
174 }
175
177 [[nodiscard]]
178 constexpr auto gdt::calcSize(const table_t &table) noexcept -> igros_word_t {
179 // Size equals to (Num of entries * Entry size) - 1
180 return static_cast<igros_word_t>(table.size() * sizeof(entry_t)) - 1_u16;
181 }
182
183
184} // namespace igros::i386
185
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
gdt(const gdt &other)=delete
Copy c-tor.
static constexpr auto TSS_DOUBLE_FAULT
Double fault task.
Definition gdt.hpp:110
auto operator=(const gdt &other) -> gdt &=delete
Copy assignment.
static constexpr auto GDT_ENTRY_DATA_RING0
Kernel data segment (ring 0, data).
Definition gdt.hpp:59
gdt() noexcept=default
Default c-tor.
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
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
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.
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 constexpr auto GDT_SIZE
Number of GDT entries.
Definition gdt.hpp:66
gdt(gdt &&other)=delete
Move c-tor.
Bit flags of enum type.
Definition kFlags.hpp:29
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
Bit flags template datatype.
i386 (32-bit protected mode): GDT, IDT, TSS, CPU registers
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
GDT table entry.
Definition gdt.hpp:84
igros_byte_t limitFlags
Limit bits 16..19 and flags (granularity, size, long mode).
Definition gdt.hpp:89
igros_byte_t baseHigh
Base bits 24..31.
Definition gdt.hpp:90
igros_byte_t baseMid
Base bits 16..23.
Definition gdt.hpp:87
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_word_t limitLow
Limit bits 0..15.
Definition gdt.hpp:85
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.