From 2b43569927e7121d2fe22369f66140530c2566d8 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 14 Oct 2024 11:36:51 +0300 Subject: [PATCH] Kernel: Use enums in boot info instead of magic values --- kernel/include/kernel/BootInfo.h | 49 +++++++++++++--------- kernel/kernel/BootInfo.cpp | 44 ++++++++++++------- kernel/kernel/Device/FramebufferDevice.cpp | 2 +- kernel/kernel/Memory/Heap.cpp | 29 ++++++++++--- 4 files changed, 82 insertions(+), 42 deletions(-) diff --git a/kernel/include/kernel/BootInfo.h b/kernel/include/kernel/BootInfo.h index d59bee16..62760da6 100644 --- a/kernel/include/kernel/BootInfo.h +++ b/kernel/include/kernel/BootInfo.h @@ -8,36 +8,45 @@ namespace Kernel { - enum class FramebufferType - { - NONE, - UNKNOWN, - RGB - }; - struct FramebufferInfo { - paddr_t address; - uint32_t pitch; - uint32_t width; - uint32_t height; - uint8_t bpp; - FramebufferType type = FramebufferType::NONE; + enum class Type + { + None, + Unknown, + RGB, + }; + + paddr_t address; + uint32_t pitch; + uint32_t width; + uint32_t height; + uint8_t bpp; + Type type; }; struct MemoryMapEntry { - uint32_t type; - paddr_t address; - uint64_t length; + enum class Type + { + Available, + Reserved, + ACPIReclaim, + ACPINVS, + }; + + paddr_t address; + uint64_t length; + Type type; }; struct BootInfo { - BAN::String command_line; - FramebufferInfo framebuffer {}; - RSDP rsdp {}; - paddr_t kernel_paddr {}; + BAN::String command_line; + FramebufferInfo framebuffer {}; + RSDP rsdp {}; + paddr_t kernel_paddr {}; + BAN::Vector memory_map_entries; }; diff --git a/kernel/kernel/BootInfo.cpp b/kernel/kernel/BootInfo.cpp index 491ff7ea..73a9cdd4 100644 --- a/kernel/kernel/BootInfo.cpp +++ b/kernel/kernel/BootInfo.cpp @@ -7,6 +7,18 @@ namespace Kernel BootInfo g_boot_info; + static MemoryMapEntry::Type bios_number_to_memory_type(uint32_t number) + { + switch (number) + { + case 1: return MemoryMapEntry::Type::Available; + case 2: return MemoryMapEntry::Type::Reserved; + case 3: return MemoryMapEntry::Type::ACPIReclaim; + case 4: return MemoryMapEntry::Type::ACPINVS; + } + return MemoryMapEntry::Type::Reserved; + } + static void parse_boot_info_multiboot2(uint32_t info) { const auto& multiboot2_info = *reinterpret_cast(info); @@ -27,9 +39,9 @@ namespace Kernel g_boot_info.framebuffer.height = framebuffer_tag.framebuffer_height; g_boot_info.framebuffer.bpp = framebuffer_tag.framebuffer_bpp; if (framebuffer_tag.framebuffer_type == MULTIBOOT2_FRAMEBUFFER_TYPE_RGB) - g_boot_info.framebuffer.type = FramebufferType::RGB; + g_boot_info.framebuffer.type = FramebufferInfo::Type::RGB; else - g_boot_info.framebuffer.type = FramebufferType::UNKNOWN; + g_boot_info.framebuffer.type = FramebufferInfo::Type::Unknown; } else if (tag->type == MULTIBOOT2_TAG_MMAP) { @@ -47,9 +59,9 @@ namespace Kernel (uint64_t)mmap_entry.length, (uint64_t)mmap_entry.type ); - g_boot_info.memory_map_entries[i].address = mmap_entry.base_addr; - g_boot_info.memory_map_entries[i].length = mmap_entry.length; - g_boot_info.memory_map_entries[i].type = mmap_entry.type; + g_boot_info.memory_map_entries[i].address = mmap_entry.base_addr; + g_boot_info.memory_map_entries[i].length = mmap_entry.length; + g_boot_info.memory_map_entries[i].type = bios_number_to_memory_type(mmap_entry.type); } } else if (tag->type == MULTIBOOT2_TAG_OLD_RSDP) @@ -87,24 +99,24 @@ namespace Kernel MUST(g_boot_info.command_line.append(command_line)); const auto& framebuffer = *reinterpret_cast(banan_bootloader_info.framebuffer_addr); + g_boot_info.framebuffer.address = framebuffer.address; + g_boot_info.framebuffer.width = framebuffer.width; + g_boot_info.framebuffer.height = framebuffer.height; + g_boot_info.framebuffer.pitch = framebuffer.pitch; + g_boot_info.framebuffer.bpp = framebuffer.bpp; if (framebuffer.type == BANAN_BOOTLOADER_FB_RGB) - { - g_boot_info.framebuffer.address = framebuffer.address; - g_boot_info.framebuffer.width = framebuffer.width; - g_boot_info.framebuffer.height = framebuffer.height; - g_boot_info.framebuffer.pitch = framebuffer.pitch; - g_boot_info.framebuffer.bpp = framebuffer.bpp; - g_boot_info.framebuffer.type = FramebufferType::RGB; - } + g_boot_info.framebuffer.type = FramebufferInfo::Type::RGB; + else + g_boot_info.framebuffer.type = FramebufferInfo::Type::Unknown; const auto& memory_map = *reinterpret_cast(banan_bootloader_info.memory_map_addr); MUST(g_boot_info.memory_map_entries.resize(memory_map.entry_count)); for (size_t i = 0; i < memory_map.entry_count; i++) { const auto& mmap_entry = memory_map.entries[i]; - g_boot_info.memory_map_entries[i].address = mmap_entry.address; - g_boot_info.memory_map_entries[i].length = mmap_entry.length; - g_boot_info.memory_map_entries[i].type = mmap_entry.type; + g_boot_info.memory_map_entries[i].address = mmap_entry.address; + g_boot_info.memory_map_entries[i].length = mmap_entry.length; + g_boot_info.memory_map_entries[i].type = bios_number_to_memory_type(mmap_entry.type); } g_boot_info.kernel_paddr = banan_bootloader_info.kernel_paddr; diff --git a/kernel/kernel/Device/FramebufferDevice.cpp b/kernel/kernel/Device/FramebufferDevice.cpp index 80397c41..a5fac985 100644 --- a/kernel/kernel/Device/FramebufferDevice.cpp +++ b/kernel/kernel/Device/FramebufferDevice.cpp @@ -19,7 +19,7 @@ namespace Kernel BAN::ErrorOr> FramebufferDevice::create_from_boot_framebuffer() { - if (g_boot_info.framebuffer.type != FramebufferType::RGB) + if (g_boot_info.framebuffer.type != FramebufferInfo::Type::RGB) return BAN::Error::from_errno(ENODEV); if (g_boot_info.framebuffer.bpp != 24 && g_boot_info.framebuffer.bpp != 32) return BAN::Error::from_errno(ENOTSUP); diff --git a/kernel/kernel/Memory/Heap.cpp b/kernel/kernel/Memory/Heap.cpp index c522e070..f991c21d 100644 --- a/kernel/kernel/Memory/Heap.cpp +++ b/kernel/kernel/Memory/Heap.cpp @@ -26,17 +26,36 @@ namespace Kernel void Heap::initialize_impl() { if (g_boot_info.memory_map_entries.empty()) - Kernel::panic("Bootloader did not provide a memory map"); + panic("Bootloader did not provide a memory map"); for (const auto& entry : g_boot_info.memory_map_entries) { - dprintln("{16H}, {16H}, {8H}", + const char* entry_type_string = nullptr; + switch (entry.type) + { + case MemoryMapEntry::Type::Available: + entry_type_string = "available"; + break; + case MemoryMapEntry::Type::Reserved: + entry_type_string = "reserved"; + break; + case MemoryMapEntry::Type::ACPIReclaim: + entry_type_string = "acpi reclaim"; + break; + case MemoryMapEntry::Type::ACPINVS: + entry_type_string = "acpi nvs"; + break; + default: + ASSERT_NOT_REACHED(); + } + + dprintln("{16H}, {16H}, {}", entry.address, entry.length, - entry.type + entry_type_string ); - if (entry.type != 1) + if (entry.type != MemoryMapEntry::Type::Available) continue; paddr_t start = entry.address; @@ -79,7 +98,7 @@ namespace Kernel for (auto& range : m_physical_ranges) if (range.contains(paddr)) return range.release_page(paddr); - ASSERT_NOT_REACHED(); + panic("tried to free invalid paddr {16H}", paddr); } paddr_t Heap::take_free_contiguous_pages(size_t pages)