Kernel: Cleanup code and move kmalloc initialization to later

This commit is contained in:
Bananymous 2022-12-28 04:16:21 +02:00
parent 2a65c5fd0f
commit 3e8590687f
7 changed files with 53 additions and 43 deletions

View File

@ -373,8 +373,8 @@ namespace APIC
// Enable Local APIC // Enable Local APIC
SetMSR(IA32_APIC_BASE, (s_local_apic & 0xFFFFF000) | IA32_APIC_BASE_ENABLE, 0); SetMSR(IA32_APIC_BASE, (s_local_apic & 0xFFFFF000) | IA32_APIC_BASE_ENABLE, 0);
uint32_t sipi = ReadLocalAPIC(0xF0); uint32_t sivr = ReadLocalAPIC(0xF0);
WriteIOAPIC(0xF0, sipi | 0x1FF); WriteLocalAPIC(0xF0, sivr | 0x1FF);
return true; return true;
} }

View File

@ -8,7 +8,7 @@ struct GDTR
} __attribute__((packed)); } __attribute__((packed));
static GDTR s_gdtr; static GDTR s_gdtr;
static SegmentDesriptor* s_gdt; static SegmentDesriptor s_gdt[5];
extern "C" void load_gdt(void* gdt_ptr); extern "C" void load_gdt(void* gdt_ptr);
asm( asm(
@ -43,12 +43,8 @@ void write_gdt_entry(uint8_t segment, SegmentDesriptor descriptor)
void gdt_initialize() void gdt_initialize()
{ {
constexpr uint8_t GDT_SIZE = 5;
s_gdt = new SegmentDesriptor[GDT_SIZE];
s_gdtr.address = s_gdt; 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(0x00, { 0, 0x00000, 0x00, 0x0 }); // null
write_gdt_entry(0x08, { 0, 0xFFFFF, 0x9A, 0xC }); // kernel code write_gdt_entry(0x08, { 0, 0xFFFFF, 0x9A, 0xC }); // kernel code

View File

@ -1,6 +1,5 @@
#include <kernel/APIC.h> #include <kernel/APIC.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
#include <kernel/kmalloc.h>
#include <kernel/panic.h> #include <kernel/panic.h>
#include <kernel/kprint.h> #include <kernel/kprint.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>

View File

@ -42,8 +42,9 @@ void TTY::Clear()
void TTY::SetCursorPosition(uint32_t x, uint32_t y) void TTY::SetCursorPosition(uint32_t x, uint32_t y)
{ {
static uint32_t last_x = 0; static uint32_t last_x = -1;
static uint32_t last_y = 0; 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 RenderFromBuffer(last_x, last_y); // Hacky way to clear previous cursor in graphics mode :D
VESA::SetCursorPosition(x, y, VESA::Color::BRIGHT_WHITE); VESA::SetCursorPosition(x, y, VESA::Color::BRIGHT_WHITE);
last_x = m_column = x; last_x = m_column = x;
@ -349,10 +350,11 @@ void TTY::PutCharCurrent(char ch)
break; break;
default: default:
VESA::PutCharAt(ch, x, y, VESA::Color::BRIGHT_WHITE, VESA::Color::BLACK); VESA::PutCharAt(ch, x, y, VESA::Color::BRIGHT_WHITE, VESA::Color::BLACK);
x++;
break; break;
} }
if (++x == VESA::GetTerminalWidth()) if (x == VESA::GetTerminalWidth())
{ {
x = 0; x = 0;
y++; y++;

View File

@ -20,7 +20,7 @@ extern const struct bitmap_font font;
namespace VESA namespace VESA
{ {
static void* s_addr = nullptr; static uintptr_t s_addr = 0;
static uint8_t s_bpp = 0; static uint8_t s_bpp = 0;
static uint32_t s_pitch = 0; static uint32_t s_pitch = 0;
static uint32_t s_width = 0; static uint32_t s_width = 0;
@ -75,7 +75,7 @@ namespace VESA
return false; return false;
auto& framebuffer = s_multiboot_info->framebuffer; auto& framebuffer = s_multiboot_info->framebuffer;
s_addr = (void*)framebuffer.addr; s_addr = framebuffer.addr;
s_bpp = framebuffer.bpp; s_bpp = framebuffer.bpp;
s_pitch = framebuffer.pitch; s_pitch = framebuffer.pitch;
s_width = framebuffer.width; s_width = framebuffer.width;
@ -90,6 +90,7 @@ namespace VESA
return false; return false;
} }
dprintln("Graphics Mode {}x{} ({} bpp)", s_width, s_height, s_bpp);
PutCharAtImpl = GraphicsPutCharAt; PutCharAtImpl = GraphicsPutCharAt;
ClearImpl = GraphicsClear; ClearImpl = GraphicsClear;
SetCursorPositionImpl = GraphicsSetCursorPosition; SetCursorPositionImpl = GraphicsSetCursorPosition;
@ -98,6 +99,7 @@ namespace VESA
} }
else if (s_mode == MULTIBOOT_FRAMEBUFFER_TYPE_TEXT) else if (s_mode == MULTIBOOT_FRAMEBUFFER_TYPE_TEXT)
{ {
dprintln("Text Mode {}x{}", s_width, s_height);
PutCharAtImpl = TextPutCharAt; PutCharAtImpl = TextPutCharAt;
ClearImpl = TextClear; ClearImpl = TextClear;
SetCursorPositionImpl = TextSetCursorPosition; SetCursorPositionImpl = TextSetCursorPosition;
@ -137,7 +139,7 @@ namespace VESA
static void GraphicsSetPixel(uint32_t offset, uint32_t color) 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) switch (s_bpp)
{ {
case 24: case 24:
@ -264,7 +266,7 @@ namespace VESA
static void TextPutCharAt(uint16_t ch, uint32_t x, uint32_t y, Color fg, Color bg) 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)); ((uint16_t*)s_addr)[index] = TextEntry(ch, TextColor(fg, bg));
} }

View File

@ -57,9 +57,6 @@ extern "C" void kernel_main(multiboot_info_t* mbi, uint32_t magic)
dprintln("Could not initialize VESA"); dprintln("Could not initialize VESA");
return; return;
} }
kmalloc_initialize();
TTY* tty1 = new TTY;
ParsedCommandLine cmdline; ParsedCommandLine cmdline;
if (mbi->flags & 0x02) if (mbi->flags & 0x02)
@ -70,6 +67,11 @@ extern "C" void kernel_main(multiboot_info_t* mbi, uint32_t magic)
IDT::initialize(); IDT::initialize();
PIT::initialize(); PIT::initialize();
kmalloc_initialize();
TTY* tty1 = new TTY;
tty1->SetCursorPosition(0, 2);
if (!Keyboard::initialize()) if (!Keyboard::initialize())
return; return;

View File

@ -21,12 +21,12 @@ struct kmalloc_node
static kmalloc_node* s_kmalloc_node_head = nullptr; static kmalloc_node* s_kmalloc_node_head = nullptr;
static size_t s_kmalloc_node_count; static size_t s_kmalloc_node_count;
static constexpr uintptr_t s_kmalloc_node_base = 0x00200000; static uintptr_t s_kmalloc_node_base = 0x00200000;
static constexpr size_t s_kmalloc_max_nodes = 1000; 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 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 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_end = s_kmalloc_base + s_kmalloc_size;
static size_t s_kmalloc_available = 0; static size_t s_kmalloc_available = 0;
static size_t s_kmalloc_allocated = 0; static size_t s_kmalloc_allocated = 0;
@ -36,17 +36,19 @@ static size_t s_kmalloc_allocated = 0;
*/ */
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 uintptr_t s_kmalloc_eternal_base = s_kmalloc_end;
static constexpr size_t s_kmalloc_eternal_size = 2 * MB; static size_t s_kmalloc_eternal_size = 1 * MB;
static constexpr uintptr_t s_kmalloc_eternal_end = s_kmalloc_eternal_base + s_kmalloc_eternal_size; static uintptr_t s_kmalloc_eternal_end = s_kmalloc_eternal_base + s_kmalloc_eternal_size;
/* /*
############################# #############################
*/ */
static bool s_initialized = false;
void kmalloc_initialize() void kmalloc_initialize()
{ {
if (!(s_multiboot_info->flags & (1 << 6))) 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 // Validate kmalloc memory
bool valid = false; bool valid = false;
@ -68,9 +70,7 @@ void kmalloc_initialize()
} }
if (!valid) if (!valid)
Kernel::panic("Kmalloc: Could not find {} MB of memory", (double)(s_kmalloc_eternal_end - s_kmalloc_base)); Kernel::panic("Kmalloc: Could not find {} MB of memory", (double)(s_kmalloc_eternal_end - s_kmalloc_node_base));
dprintln("Aligining everything to {} byte boundaries", ALIGN);
s_kmalloc_node_count = 1; s_kmalloc_node_count = 1;
s_kmalloc_node_head = (kmalloc_node*)s_kmalloc_node_base; s_kmalloc_node_head = (kmalloc_node*)s_kmalloc_node_base;
@ -84,10 +84,13 @@ void kmalloc_initialize()
head.free = true; head.free = true;
s_kmalloc_eternal_ptr = s_kmalloc_eternal_base; s_kmalloc_eternal_ptr = s_kmalloc_eternal_base;
s_initialized = true;
} }
void kmalloc_dump_nodes() 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 available {} MB", (float)s_kmalloc_available / MB);
dprintln("Kmalloc memory allocated {} MB", (float)s_kmalloc_allocated / MB); dprintln("Kmalloc memory allocated {} MB", (float)s_kmalloc_allocated / MB);
dprintln("Using {}/{} nodes", s_kmalloc_node_count, s_kmalloc_max_nodes); 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) void* kmalloc_eternal(size_t size)
{ {
if (!s_initialized) Kernel::panic("kmalloc not initialized!");
if (size % ALIGN) if (size % ALIGN)
size += ALIGN - (size % ALIGN); size += ALIGN - (size % ALIGN);
@ -119,6 +124,8 @@ void* kmalloc_eternal(size_t size)
void* kmalloc(size_t size) void* kmalloc(size_t size)
{ {
if (!s_initialized) Kernel::panic("kmalloc not initialized!");
if (size % ALIGN) if (size % ALIGN)
size += ALIGN - (size % ALIGN); size += ALIGN - (size % ALIGN);
@ -183,6 +190,8 @@ void* kmalloc(size_t size)
void kfree(void* addr) void kfree(void* addr)
{ {
if (!s_initialized) Kernel::panic("kmalloc not initialized!");
if (addr == nullptr) if (addr == nullptr)
return; return;