IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
platform.hpp
Go to the documentation of this file.
1
11
12
13#pragma once
14
15// C++
16#include <bit>
17#include <string_view>
18#include <type_traits>
19// IgrOS-Kernel arch
20#include <arch/types.hpp>
21
22
27// Kernel sections start (from linker script, page aligned)
35
36
37// OS platform namespace
38namespace igros::platform {
39
40
42 class Platform final {
43
45 enum class ARCH_NAME : igros_dword_t {
46 UNKNOWN = 0x00000000_u32,
47 I386 = 0x00000001_u32,
48 X86_64 = 0x00000002_u32,
49 ARM = 0x00000004_u32,
50 ARM64 = 0x00000008_u32,
51 AVR = 0x00000010_u32,
52 };
53
54
55#if defined(IGROS_ARCH_i386)
56
58 constexpr static auto PLATFORM_NAME {ARCH_NAME::I386};
59
60#elif defined(IGROS_ARCH_x86_64)
61
63 constexpr static auto PLATFORM_NAME {ARCH_NAME::X86_64};
64
65#else
66
68 constexpr static auto PLATFORM_NAME {ARCH_NAME::UNKNOWN};
69
70#endif
71
72
74 using init_t = std::add_pointer_t<void()>;
76 using finalize_t = std::add_pointer_t<void()>;
77
79 using shutdown_t = std::add_pointer_t<void()>;
81 using reboot_t = std::add_pointer_t<void()>;
82
84 using suspend_t = std::add_pointer_t<void()>;
86 using wakeup_t = std::add_pointer_t<void()>;
87
88
89 const char* const mName {nullptr};
90
91 init_t mInit {nullptr};
93
95 reboot_t mReboot {nullptr};
96
97 suspend_t mSuspend {nullptr};
98 wakeup_t mWakeup {nullptr};
99
100
101 public:
102
104 constexpr Platform() noexcept = default;
113 constexpr Platform(
114 const char* const name,
115 const init_t initFunc,
116 const finalize_t finalizeFunc,
117 const shutdown_t shutdownFunc,
118 const reboot_t rebootFunc,
119 const suspend_t suspendFunc,
120 const wakeup_t wakeupFunc
121 ) noexcept;
122
125 [[nodiscard]]
126 auto name() const noexcept -> const char*;
127
129 void initialize() const noexcept;
131 void finalize() const noexcept;
132
134 void shutdown() const noexcept;
136 void reboot() const noexcept;
137
139 void suspend() const noexcept;
141 void wakeup() const noexcept;
142
145 [[nodiscard]]
146 constexpr static auto kernelStart() noexcept -> const igros_byte_t*;
149 [[nodiscard]]
150 constexpr static auto kernelEnd() noexcept -> const igros_byte_t*;
153 [[nodiscard]]
154 static auto kernelSize() noexcept -> igros_usize_t;
156 [[nodiscard]]
157 constexpr static auto textStart() noexcept -> const igros_byte_t*;
159 [[nodiscard]]
160 constexpr static auto rodataStart() noexcept -> const igros_byte_t*;
162 [[nodiscard]]
163 constexpr static auto dataStart() noexcept -> const igros_byte_t*;
165 [[nodiscard]]
166 constexpr static auto stackGuard() noexcept -> const igros_byte_t*;
168 [[nodiscard]]
169 static auto kernelOffset() noexcept -> igros_usize_t;
170
173 [[nodiscard]]
174 constexpr static auto isI386() noexcept -> bool;
177 [[nodiscard]]
178 constexpr static auto isX86_64() noexcept -> bool;
181 [[nodiscard]]
182 constexpr static auto isARM32() noexcept -> bool;
185 [[nodiscard]]
186 constexpr static auto isARM64() noexcept -> bool;
189 [[nodiscard]]
190 constexpr static auto isAVR() noexcept -> bool;
191
194 [[nodiscard]]
195 static auto current() noexcept -> const Platform &;
196 };
197
198
200 constexpr Platform::Platform(
201 const char* const name,
202 const init_t initFunc, // NOLINT(bugprone-easily-swappable-parameters): platform callbacks in fixed order
203 const finalize_t finalizeFunc,
204 const shutdown_t shutdownFunc,
205 const reboot_t rebootFunc,
206 const suspend_t suspendFunc,
207 const wakeup_t wakeupFunc
208 ) noexcept :
209 mName(name),
210 mInit(initFunc),
211 mFinalize(finalizeFunc),
212 mShutdown(shutdownFunc),
213 mReboot(rebootFunc),
214 mSuspend(suspendFunc),
215 mWakeup(wakeupFunc) {}
216
217
219 [[nodiscard]]
220 inline auto Platform::name() const noexcept -> const char* {
221 // Get platform name string
222 return mName;
223 }
224
225
227 inline void Platform::initialize() const noexcept {
228 // Check if no init func
229 if (nullptr == mInit) [[unlikely]] {
230 // Exit
231 return;
232 }
233 // Do init
234 mInit();
235 }
236
238 inline void Platform::finalize() const noexcept {
239 // Check if no finalize func
240 if (nullptr == mFinalize) [[unlikely]] {
241 // Exit
242 return;
243 }
244 // Do finalize
245 mFinalize();
246 }
247
249 inline void Platform::shutdown() const noexcept {
250 // Check if no shutdown func
251 if (nullptr == mShutdown) [[unlikely]] {
252 // Exit
253 return;
254 }
255 // Do shutdown
256 mShutdown();
257 }
258
260 inline void Platform::reboot() const noexcept {
261 // Check if no reboot func
262 if (nullptr == mReboot) [[unlikely]] {
263 // Exit
264 return;
265 }
266 // Do reboot
267 mReboot();
268 }
269
271 inline void Platform::suspend() const noexcept {
272 // Check if no suspend func
273 if (nullptr == mSuspend) [[unlikely]] {
274 // Exit
275 return;
276 }
277 // Do suspend
278 mSuspend();
279 }
280
282 inline void Platform::wakeup() const noexcept {
283 // Check if no wakeup func
284 if (nullptr == mWakeup) [[unlikely]] {
285 // Exit
286 return;
287 }
288 // Do wakeup
289 mWakeup();
290 }
291
292
294 [[nodiscard]]
295 constexpr auto Platform::kernelStart() noexcept -> const igros_byte_t* {
296 // Pointer to kernel start (from linker script)
298 }
299
301 [[nodiscard]]
302 constexpr auto Platform::kernelEnd() noexcept -> const igros_byte_t* {
303 // Pointer to kernel end (from linker script)
304 return &_SECTION_KERNEL_END_;
305 }
306
308 [[nodiscard]]
309 inline auto Platform::kernelSize() noexcept -> igros_usize_t {
310 // Kernel size (end - start): addresses, not pointers - linker symbols are
311 // different objects, so pointer subtraction would be undefined behavior
312 return std::bit_cast<igros_usize_t>(Platform::kernelEnd()) - std::bit_cast<igros_usize_t>(Platform::kernelStart());
313 }
314
317 [[nodiscard]]
318 constexpr auto Platform::textStart() noexcept -> const igros_byte_t* {
319 return &_SECTION_TEXT_START_;
320 }
321
324 [[nodiscard]]
325 constexpr auto Platform::rodataStart() noexcept -> const igros_byte_t* {
327 }
328
331 [[nodiscard]]
332 constexpr auto Platform::dataStart() noexcept -> const igros_byte_t* {
333 return &_SECTION_DATA_START_;
334 }
335
338 [[nodiscard]]
339 constexpr auto Platform::stackGuard() noexcept -> const igros_byte_t* {
340 return &_KERNEL_STACK_GUARD_;
341 }
342
345 [[nodiscard]]
346 inline auto Platform::kernelOffset() noexcept -> igros_usize_t {
347 // Offset from linker script
348 return std::bit_cast<igros_usize_t>(&KERNEL_OFFSET_VIRT);
349 }
350
351
353 [[nodiscard]]
354 constexpr auto Platform::isI386() noexcept -> bool {
355 // Check platform i386
356 return ARCH_NAME::I386 == PLATFORM_NAME;
357 }
358
360 [[nodiscard]]
361 constexpr auto Platform::isX86_64() noexcept -> bool {
362 // Check platform x86_64
363 return ARCH_NAME::X86_64 == PLATFORM_NAME;
364 }
365
367 [[nodiscard]]
368 constexpr auto Platform::isARM32() noexcept -> bool {
369 // Check platform arm
370 return ARCH_NAME::ARM == PLATFORM_NAME;
371 }
372
374 [[nodiscard]]
375 constexpr auto Platform::isARM64() noexcept -> bool {
376 // Check platform arm64
377 return ARCH_NAME::ARM64 == PLATFORM_NAME;
378 }
379
381 [[nodiscard]]
382 constexpr auto Platform::isAVR() noexcept -> bool {
383 // Check platform avr
384 return ARCH_NAME::AVR == PLATFORM_NAME;
385 }
386
387
388} // namespace igros::platform
389
suspend_t mSuspend
Suspend platform.
Definition platform.hpp:97
static constexpr auto kernelStart() noexcept -> const igros_byte_t *
Get kernel start address.
Definition platform.hpp:295
void reboot() const noexcept
Reboot platform.
Definition platform.hpp:260
static constexpr auto kernelEnd() noexcept -> const igros_byte_t *
Get kernel end address.
Definition platform.hpp:302
static constexpr auto isARM64() noexcept -> bool
Check if arm64.
Definition platform.hpp:375
void finalize() const noexcept
Finalize platform.
Definition platform.hpp:238
const char *const mName
Platform name.
Definition platform.hpp:89
constexpr Platform() noexcept=default
Default c-tor.
static constexpr auto isI386() noexcept -> bool
Check if i386.
Definition platform.hpp:354
static constexpr auto dataStart() noexcept -> const igros_byte_t *
Get kernel writable data start (data and bss: [dataStart, kernelEnd)).
Definition platform.hpp:332
static auto kernelSize() noexcept -> igros_usize_t
Get kernel size.
Definition platform.hpp:309
void shutdown() const noexcept
Shutdown platform.
Definition platform.hpp:249
auto name() const noexcept -> const char *
Get platform name.
Definition platform.hpp:220
std::add_pointer_t< void()> finalize_t
Platform finalize function pointer type.
Definition platform.hpp:76
wakeup_t mWakeup
Wakeup platform.
Definition platform.hpp:98
void wakeup() const noexcept
Wakeup platform.
Definition platform.hpp:282
init_t mInit
Init function.
Definition platform.hpp:91
ARCH_NAME
Platform name ID.
Definition platform.hpp:45
static auto kernelOffset() noexcept -> igros_usize_t
Get kernel virtual memory offset (kernel virtual address = physical address + offset).
Definition platform.hpp:346
static constexpr auto textStart() noexcept -> const igros_byte_t *
Get kernel code start (code: [textStart, rodataStart)).
Definition platform.hpp:318
finalize_t mFinalize
Finalize function.
Definition platform.hpp:92
static auto current() noexcept -> const Platform &
Get kernel current platform (implemented in corresponding platform file).
reboot_t mReboot
Reboot function.
Definition platform.hpp:95
static constexpr auto stackGuard() noexcept -> const igros_byte_t *
Get boot stack guard page (one page right below boot stack, never mapped).
Definition platform.hpp:339
shutdown_t mShutdown
Shutdown function.
Definition platform.hpp:94
std::add_pointer_t< void()> suspend_t
Platform suspend function pointer type.
Definition platform.hpp:84
static constexpr auto PLATFORM_NAME
x86_64 platform
Definition platform.hpp:63
std::add_pointer_t< void()> reboot_t
Platform reboot function pointer type.
Definition platform.hpp:81
std::add_pointer_t< void()> wakeup_t
Platform wakeup function pointer type.
Definition platform.hpp:86
void initialize() const noexcept
Initialize platform.
Definition platform.hpp:227
static constexpr auto rodataStart() noexcept -> const igros_byte_t *
Get kernel read-only data start (read-only data: [rodataStart, dataStart)).
Definition platform.hpp:325
std::add_pointer_t< void()> init_t
Platform init function pointer type.
Definition platform.hpp:74
void suspend() const noexcept
Suspend platform.
Definition platform.hpp:271
static constexpr auto isAVR() noexcept -> bool
Check if avr.
Definition platform.hpp:382
static constexpr auto isX86_64() noexcept -> bool
Check if x86_64.
Definition platform.hpp:361
static constexpr auto isARM32() noexcept -> bool
Check if arm32.
Definition platform.hpp:368
std::add_pointer_t< void()> shutdown_t
Platform shutdown function pointer type.
Definition platform.hpp:79
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
std::uint32_t igros_dword_t
32-bit unsigned integer
Definition types.hpp:43
Platform description and initialization.
const igros::igros_byte_t _SECTION_KERNEL_END_
Kernel end.
const igros::igros_byte_t _SECTION_DATA_START_
const igros::igros_byte_t _SECTION_KERNEL_START_
Kernel start.
const igros::igros_byte_t KERNEL_OFFSET_VIRT
Kernel virtual memory offset (absolute symbol, its address is the offset value).
const igros::igros_byte_t _KERNEL_STACK_GUARD_
Boot stack guard page (from boot.s, page aligned).
const igros::igros_byte_t _SECTION_TEXT_START_
Code section start.
const igros::igros_byte_t _SECTION_RODATA_START_
Read-only data section start.
Common C++ types redefinition.