Kernel: Use enums in boot info instead of magic values

This commit is contained in:
Bananymous 2024-10-14 11:36:51 +03:00
parent 4ba33175cf
commit 2b43569927
4 changed files with 82 additions and 42 deletions

View File

@ -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<MemoryMapEntry> memory_map_entries;
};

View File

@ -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<const multiboot2_info_t*>(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<BananBootFramebufferInfo*>(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<BananBootloaderMemoryMapInfo*>(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;

View File

@ -19,7 +19,7 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<FramebufferDevice>> 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);

View File

@ -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)