IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
kmain.cpp
Go to the documentation of this file.
1
11
12
13// C++
14#include <span>
15#include <type_traits>
16// IgrOS-Kernel arch
17#include <arch/cpu.hpp>
18// IgrOS-Kernel memory
19#include <mem/direct.hpp>
20#include <mem/phys.hpp>
21// IgrOS-Kernel multiboot
23// IgrOS-Kernel platform
24#include <platform/platform.hpp>
25// IgrOS-Kernel
26#include <kmain.hpp>
27
28
29#ifdef __cplusplus
30
31extern "C" {
32
33#endif // __cplusplus
34
35
36 // Global constructors (from linker script)
37 extern const std::add_pointer_t<void()> _SECTION_INIT_ARRAY_START_[];
38 extern const std::add_pointer_t<void()> _SECTION_INIT_ARRAY_END_[];
39
40
41 // Kernel main function
42 [[noreturn]]
43 void kmain(const igros::igros_usize_t multibootAddress, const igros::igros_dword_t magic) noexcept { // NOLINT(bugprone-easily-swappable-parameters): order set by boot.s
44
45 // Run global constructors (dynamic initialization of global objects)
46 for (const auto ctor : std::span {_SECTION_INIT_ARRAY_START_, _SECTION_INIT_ARRAY_END_}) {
47 ctor();
48 }
49
50 // Multiboot info at physical address (screen is reachable through boot page tables, before platform init)
51 const auto* const multiboot {igros::mem::direct::pointer<const igros::multiboot::info_t>(multibootAddress)};
52 if (nullptr == multiboot) [[unlikely]] {
53 igros::klib::kprintf("Multiboot info at 0x%zx is not directly mapped", multibootAddress);
54 igros::arch::cpu::halt();
55 }
56
57 // Test multiboot before anything reads multiboot info (hang on error)
58 multiboot->test(magic);
59
60 // Initialize physical memory allocator (needed by paging)
61 igros::mem::phys::init(*multiboot);
62
63 // Initialize platform
65
66 // Write greetings message (screen is reachable through platform init)
67 igros::klib::kprintf("IgrOS kernel");
68
69 // Print kernel header
70 multiboot->printHeader();
71 // Show multiboot flags
72 multiboot->printFlags();
73 // Show VBE info
74 multiboot->printVBEInfo();
75 // Show framebuffer info
76 multiboot->printFBInfo();
77 // Show memory map
78 multiboot->printMemMap();
79
80 // Show free physical memory
81 igros::klib::kprintf("Physical memory:\t%zu KB free", igros::mem::phys::freePages() << 2);
82
83 // Write "Booted successfully" message
84 igros::klib::kprintf("Booted successfully");
85
86 // Halt CPU
87 igros::arch::cpu::halt();
88 }
89
90
91#ifdef __cplusplus
92
93} // extern "C"
94
95#endif // __cplusplus
96
static auto pointer(const phys_t phys, const phys_t size=sizeof(T)) noexcept -> T *
Pointer to physical memory (nullptr if range is not directly mapped).
Definition direct.hpp:112
static auto freePages() noexcept -> igros_usize_t
Number of free pages.
Definition phys.cpp:202
static void init(const multiboot::info_t &info) noexcept
Initialize from bootloader memory map (reserves kernel and boot data).
Definition phys.cpp:116
static auto current() noexcept -> const Platform &
Get kernel current platform (implemented in corresponding platform file).
CPU operations.
Direct map of physical memory.
std::size_t igros_usize_t
Unsigned size.
Definition types.hpp:51
std::uint32_t igros_dword_t
32-bit unsigned integer
Definition types.hpp:43
void kmain(const igros::igros_usize_t multibootAddress, const igros::igros_dword_t magic) noexcept
Kernel main function (called from boot.s).
Definition kmain.cpp:43
const std::add_pointer_t< void()> _SECTION_INIT_ARRAY_END_[]
Global constructors end.
const std::add_pointer_t< void()> _SECTION_INIT_ARRAY_START_[]
First global constructor.
Kernel main function (called from boot.s).
Multiboot 1 header info.
void kprintf(const char *const format,...) noexcept
Kernel printf function (arguments are checked against format at compile time).
Definition kPrint.cpp:358
Physical memory allocator.
IgrOS platform description.