From 3e8590687fed8f01c27116bf7c1760ac176c7868 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 28 Dec 2022 04:16:21 +0200 Subject: [PATCH] Kernel: Cleanup code and move kmalloc initialization to later --- kernel/arch/i386/APIC.cpp | 4 ++-- kernel/arch/i386/GDT.cpp | 8 ++------ kernel/arch/i386/IDT.cpp | 1 - kernel/arch/i386/TTY.cpp | 10 +++++---- kernel/arch/i386/VESA.cpp | 22 +++++++++++--------- kernel/kernel/kernel.cpp | 8 +++++--- kernel/kernel/kmalloc.cpp | 43 +++++++++++++++++++++++---------------- 7 files changed, 53 insertions(+), 43 deletions(-) diff --git a/kernel/arch/i386/APIC.cpp b/kernel/arch/i386/APIC.cpp index dc8a8b34..0ae5b45e 100644 --- a/kernel/arch/i386/APIC.cpp +++ b/kernel/arch/i386/APIC.cpp @@ -373,8 +373,8 @@ namespace APIC // Enable Local APIC SetMSR(IA32_APIC_BASE, (s_local_apic & 0xFFFFF000) | IA32_APIC_BASE_ENABLE, 0); - uint32_t sipi = ReadLocalAPIC(0xF0); - WriteIOAPIC(0xF0, sipi | 0x1FF); + uint32_t sivr = ReadLocalAPIC(0xF0); + WriteLocalAPIC(0xF0, sivr | 0x1FF); return true; } diff --git a/kernel/arch/i386/GDT.cpp b/kernel/arch/i386/GDT.cpp index 1ff92dd6..259442ea 100644 --- a/kernel/arch/i386/GDT.cpp +++ b/kernel/arch/i386/GDT.cpp @@ -8,7 +8,7 @@ struct GDTR } __attribute__((packed)); static GDTR s_gdtr; -static SegmentDesriptor* s_gdt; +static SegmentDesriptor s_gdt[5]; extern "C" void load_gdt(void* gdt_ptr); asm( @@ -43,12 +43,8 @@ void write_gdt_entry(uint8_t segment, SegmentDesriptor descriptor) void gdt_initialize() { - constexpr uint8_t GDT_SIZE = 5; - - s_gdt = new SegmentDesriptor[GDT_SIZE]; - s_gdtr.address = s_gdt; - s_gdtr.size = GDT_SIZE * 8 - 1; + s_gdtr.size = sizeof(s_gdt) - 1; write_gdt_entry(0x00, { 0, 0x00000, 0x00, 0x0 }); // null write_gdt_entry(0x08, { 0, 0xFFFFF, 0x9A, 0xC }); // kernel code diff --git a/kernel/arch/i386/IDT.cpp b/kernel/arch/i386/IDT.cpp index 95f03067..d73c2d7a 100644 --- a/kernel/arch/i386/IDT.cpp +++ b/kernel/arch/i386/IDT.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/kernel/arch/i386/TTY.cpp b/kernel/arch/i386/TTY.cpp index 5f281c8c..847fa655 100644 --- a/kernel/arch/i386/TTY.cpp +++ b/kernel/arch/i386/TTY.cpp @@ -42,9 +42,10 @@ void TTY::Clear() void TTY::SetCursorPosition(uint32_t x, uint32_t y) { - static uint32_t last_x = 0; - static uint32_t last_y = 0; - RenderFromBuffer(last_x, last_y); // Hacky way to clear previous cursor in graphics mode :D + static uint32_t last_x = -1; + static uint32_t last_y = -1; + if (last_x != uint32_t(-1) && last_y != uint32_t(-1)) + RenderFromBuffer(last_x, last_y); // Hacky way to clear previous cursor in graphics mode :D VESA::SetCursorPosition(x, y, VESA::Color::BRIGHT_WHITE); last_x = m_column = x; last_y = m_row = y; @@ -349,10 +350,11 @@ void TTY::PutCharCurrent(char ch) break; default: VESA::PutCharAt(ch, x, y, VESA::Color::BRIGHT_WHITE, VESA::Color::BLACK); + x++; break; } - if (++x == VESA::GetTerminalWidth()) + if (x == VESA::GetTerminalWidth()) { x = 0; y++; diff --git a/kernel/arch/i386/VESA.cpp b/kernel/arch/i386/VESA.cpp index dc483f76..4cc1a196 100644 --- a/kernel/arch/i386/VESA.cpp +++ b/kernel/arch/i386/VESA.cpp @@ -20,12 +20,12 @@ extern const struct bitmap_font font; namespace VESA { - static void* s_addr = nullptr; - static uint8_t s_bpp = 0; - static uint32_t s_pitch = 0; - static uint32_t s_width = 0; - static uint32_t s_height = 0; - static uint8_t s_mode = 0; + static uintptr_t s_addr = 0; + static uint8_t s_bpp = 0; + static uint32_t s_pitch = 0; + static uint32_t s_width = 0; + static uint32_t s_height = 0; + static uint8_t s_mode = 0; static uint32_t s_terminal_width = 0; static uint32_t s_terminal_height = 0; @@ -73,9 +73,9 @@ namespace VESA { if (!(s_multiboot_info->flags & MULTIBOOT_FLAGS_FRAMEBUFFER)) return false; - + auto& framebuffer = s_multiboot_info->framebuffer; - s_addr = (void*)framebuffer.addr; + s_addr = framebuffer.addr; s_bpp = framebuffer.bpp; s_pitch = framebuffer.pitch; s_width = framebuffer.width; @@ -90,6 +90,7 @@ namespace VESA return false; } + dprintln("Graphics Mode {}x{} ({} bpp)", s_width, s_height, s_bpp); PutCharAtImpl = GraphicsPutCharAt; ClearImpl = GraphicsClear; SetCursorPositionImpl = GraphicsSetCursorPosition; @@ -98,6 +99,7 @@ namespace VESA } else if (s_mode == MULTIBOOT_FRAMEBUFFER_TYPE_TEXT) { + dprintln("Text Mode {}x{}", s_width, s_height); PutCharAtImpl = TextPutCharAt; ClearImpl = TextClear; SetCursorPositionImpl = TextSetCursorPosition; @@ -137,7 +139,7 @@ namespace VESA static void GraphicsSetPixel(uint32_t offset, uint32_t color) { - uint32_t* address = (uint32_t*)((uint32_t)s_addr + offset); + uint32_t* address = (uint32_t*)(s_addr + offset); switch (s_bpp) { case 24: @@ -264,7 +266,7 @@ namespace VESA static void TextPutCharAt(uint16_t ch, uint32_t x, uint32_t y, Color fg, Color bg) { - uint32_t index = y * s_width + x; + uint32_t index = y * s_pitch + x; ((uint16_t*)s_addr)[index] = TextEntry(ch, TextColor(fg, bg)); } diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index 99b9bf25..58eb12e0 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -57,9 +57,6 @@ extern "C" void kernel_main(multiboot_info_t* mbi, uint32_t magic) dprintln("Could not initialize VESA"); return; } - kmalloc_initialize(); - - TTY* tty1 = new TTY; ParsedCommandLine cmdline; if (mbi->flags & 0x02) @@ -70,6 +67,11 @@ extern "C" void kernel_main(multiboot_info_t* mbi, uint32_t magic) IDT::initialize(); PIT::initialize(); + kmalloc_initialize(); + + TTY* tty1 = new TTY; + tty1->SetCursorPosition(0, 2); + if (!Keyboard::initialize()) return; diff --git a/kernel/kernel/kmalloc.cpp b/kernel/kernel/kmalloc.cpp index 9d8859ba..7d61fc24 100644 --- a/kernel/kernel/kmalloc.cpp +++ b/kernel/kernel/kmalloc.cpp @@ -18,35 +18,37 @@ struct kmalloc_node size_t size : sizeof(size_t) * 8 - 1; size_t free : 1; }; -static kmalloc_node* s_kmalloc_node_head = nullptr; -static size_t s_kmalloc_node_count; +static kmalloc_node* s_kmalloc_node_head = nullptr; +static size_t s_kmalloc_node_count; -static constexpr uintptr_t s_kmalloc_node_base = 0x00200000; -static constexpr size_t s_kmalloc_max_nodes = 1000; +static uintptr_t s_kmalloc_node_base = 0x00200000; +static size_t s_kmalloc_max_nodes = 1000; -static constexpr uintptr_t s_kmalloc_base = s_kmalloc_node_base + s_kmalloc_max_nodes * sizeof(kmalloc_node); -static constexpr size_t s_kmalloc_size = 1 * MB; -static constexpr uintptr_t s_kmalloc_end = s_kmalloc_base + s_kmalloc_size; +static uintptr_t s_kmalloc_base = s_kmalloc_node_base + s_kmalloc_max_nodes * sizeof(kmalloc_node); +static size_t s_kmalloc_size = 1 * MB; +static uintptr_t s_kmalloc_end = s_kmalloc_base + s_kmalloc_size; -static size_t s_kmalloc_available = 0; -static size_t s_kmalloc_allocated = 0; +static size_t s_kmalloc_available = 0; +static size_t s_kmalloc_allocated = 0; /* #### KMALLOC ETERNAL ######## */ -static uintptr_t s_kmalloc_eternal_ptr = 0; +static uintptr_t s_kmalloc_eternal_ptr = 0; -static constexpr uintptr_t s_kmalloc_eternal_base = s_kmalloc_end; -static constexpr size_t s_kmalloc_eternal_size = 2 * MB; -static constexpr uintptr_t s_kmalloc_eternal_end = s_kmalloc_eternal_base + s_kmalloc_eternal_size; +static uintptr_t s_kmalloc_eternal_base = s_kmalloc_end; +static size_t s_kmalloc_eternal_size = 1 * MB; +static uintptr_t s_kmalloc_eternal_end = s_kmalloc_eternal_base + s_kmalloc_eternal_size; /* ############################# */ +static bool s_initialized = false; + void kmalloc_initialize() { if (!(s_multiboot_info->flags & (1 << 6))) - Kernel::panic("Kmalloc: Bootloader didn't give a memory map"); + Kernel::panic("Kmalloc: Bootloader didn't provide a memory map"); // Validate kmalloc memory bool valid = false; @@ -68,9 +70,7 @@ void kmalloc_initialize() } if (!valid) - Kernel::panic("Kmalloc: Could not find {} MB of memory", (double)(s_kmalloc_eternal_end - s_kmalloc_base)); - - dprintln("Aligining everything to {} byte boundaries", ALIGN); + Kernel::panic("Kmalloc: Could not find {} MB of memory", (double)(s_kmalloc_eternal_end - s_kmalloc_node_base)); s_kmalloc_node_count = 1; s_kmalloc_node_head = (kmalloc_node*)s_kmalloc_node_base; @@ -84,10 +84,13 @@ void kmalloc_initialize() head.free = true; s_kmalloc_eternal_ptr = s_kmalloc_eternal_base; + + s_initialized = true; } void kmalloc_dump_nodes() { + if (!s_initialized) Kernel::panic("kmalloc not initialized!"); dprintln("Kmalloc memory available {} MB", (float)s_kmalloc_available / MB); dprintln("Kmalloc memory allocated {} MB", (float)s_kmalloc_allocated / MB); dprintln("Using {}/{} nodes", s_kmalloc_node_count, s_kmalloc_max_nodes); @@ -100,6 +103,8 @@ void kmalloc_dump_nodes() void* kmalloc_eternal(size_t size) { + if (!s_initialized) Kernel::panic("kmalloc not initialized!"); + if (size % ALIGN) size += ALIGN - (size % ALIGN); @@ -119,6 +124,8 @@ void* kmalloc_eternal(size_t size) void* kmalloc(size_t size) { + if (!s_initialized) Kernel::panic("kmalloc not initialized!"); + if (size % ALIGN) size += ALIGN - (size % ALIGN); @@ -183,6 +190,8 @@ void* kmalloc(size_t size) void kfree(void* addr) { + if (!s_initialized) Kernel::panic("kmalloc not initialized!"); + if (addr == nullptr) return;