You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.6 KiB
C++

#include <array>
#include <cstdint>
extern void kernel_main();
struct multiboot2
{
static constexpr std::uint32_t MAGIC = 0xE85250D6;
static constexpr std::uint32_t FLAGS = 0;
static constexpr std::uint32_t LENGTH = 16;
static constexpr std::uint32_t CHECKSUM = -(MAGIC + FLAGS + LENGTH);
alignas(8)
std::uint32_t magic = MAGIC;
std::uint32_t flags = FLAGS;
std::uint32_t length = LENGTH;
std::uint32_t checksum = CHECKSUM;
} __attribute__((packed));
struct multiboot2_tag
{
alignas(8)
std::uint16_t id;
std::uint16_t flags;
std::uint32_t length;
std::uint32_t data[];
} __attribute__((packed));
__attribute__((section(".multiboot2")))
multiboot2 multibootHeader;
__attribute__((section(".multiboot2")))
multiboot2_tag multibootTagInfoRequest = {
1, 0, sizeof(multiboot2_tag) + sizeof(std::uint32_t),
{4}
};
__attribute__((section(".multiboot2")))
multiboot2_tag multibootTagEnd = {
0, 0, sizeof(multiboot2_tag), {}
};
alignas(16)
std::array<std::uint8_t, 16384> stack;
extern "C"
__attribute__((naked))
void _start()
{
asm volatile(R"(
mov %%eax, multiboot_magic
mov %%ebx, multiboot_ptr
mov %0, %%esp
)" :: "i" (stack.data() + stack.size()));
extern std::uint32_t __init_array_start;
extern std::uint32_t __init_array_end;
auto it = &__init_array_start;
while (it < &__init_array_end) {
auto fn = reinterpret_cast<void (*)()>(*it);
fn();
++it;
}
kernel_main();
asm volatile("cli");
for (;;)
asm volatile("hlt");
}