diff --git a/brewos.iso b/brewos.iso index 4ed9645..d2156f5 100644 Binary files a/brewos.iso and b/brewos.iso differ diff --git a/build/about.o b/build/about.o index a2970ea..615c8ed 100644 Binary files a/build/about.o and b/build/about.o differ diff --git a/build/brewos.elf b/build/brewos.elf index 7e9581a..3060dac 100755 Binary files a/build/brewos.elf and b/build/brewos.elf differ diff --git a/build/calculator.o b/build/calculator.o index 40330b3..032a0a9 100644 Binary files a/build/calculator.o and b/build/calculator.o differ diff --git a/build/cli_apps/txtedit.o b/build/cli_apps/txtedit.o index acf204e..b3ad107 100644 Binary files a/build/cli_apps/txtedit.o and b/build/cli_apps/txtedit.o differ diff --git a/build/cmd.o b/build/cmd.o index d668243..9494b29 100644 Binary files a/build/cmd.o and b/build/cmd.o differ diff --git a/build/control_panel.o b/build/control_panel.o index d5b895f..e729340 100644 Binary files a/build/control_panel.o and b/build/control_panel.o differ diff --git a/build/editor.o b/build/editor.o index fd9ca9c..f9ebd1d 100644 Binary files a/build/editor.o and b/build/editor.o differ diff --git a/build/explorer.o b/build/explorer.o index 2c91de3..d8634ec 100644 Binary files a/build/explorer.o and b/build/explorer.o differ diff --git a/build/main.o b/build/main.o index 8abd45d..584e3e4 100644 Binary files a/build/main.o and b/build/main.o differ diff --git a/build/memory_manager.o b/build/memory_manager.o index f6354a2..b9ae500 100644 Binary files a/build/memory_manager.o and b/build/memory_manager.o differ diff --git a/build/minesweeper.o b/build/minesweeper.o index 83db12f..afcb07c 100644 Binary files a/build/minesweeper.o and b/build/minesweeper.o differ diff --git a/build/notepad.o b/build/notepad.o index aa98857..30d99db 100644 Binary files a/build/notepad.o and b/build/notepad.o differ diff --git a/build/ps2.o b/build/ps2.o index 8ea15fd..18a9495 100644 Binary files a/build/ps2.o and b/build/ps2.o differ diff --git a/iso_root/brewos.elf b/iso_root/brewos.elf index 7e9581a..3060dac 100755 Binary files a/iso_root/brewos.elf and b/iso_root/brewos.elf differ diff --git a/src/kernel/main.c b/src/kernel/main.c index 004a686..fa57df5 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -19,9 +19,16 @@ static volatile struct limine_framebuffer_request framebuffer_request = { .revision = 1 }; +__attribute__((used, section(".requests"))) +static volatile struct limine_memmap_request memmap_request = { + .id = LIMINE_MEMMAP_REQUEST, + .revision = 0 +}; + __attribute__((used, section(".requests_start"))) static volatile struct limine_request *const requests_start_marker[] = { (struct limine_request *)&framebuffer_request, + (struct limine_request *)&memmap_request, NULL }; @@ -60,8 +67,28 @@ void kmain(void) { // Load IDT and Enable Interrupts idt_load(); - // 2.5 Memory Manager Init - memory_manager_init(); + // 2.5 Memory Manager Init - Calculate available RAM from Limine + size_t total_usable_memory = 0; + if (memmap_request.response != NULL) { + for (uint64_t i = 0; i < memmap_request.response->entry_count; i++) { + struct limine_memmap_entry *entry = memmap_request.response->entries[i]; + + // Count usable memory regions + if (entry->type == LIMINE_MEMMAP_USABLE) { + total_usable_memory += entry->length; + } + } + } + + // Initialize memory manager with available memory (cap at 2GB for practical reasons) + size_t pool_size = total_usable_memory > (2 * 1024 * 1024 * 1024) ? + (2 * 1024 * 1024 * 1024) : total_usable_memory; + + if (pool_size == 0) { + pool_size = 512 * 1024 * 1024; // Fallback to 512MB if detection fails + } + + memory_manager_init_with_size(pool_size); // 3. PS/2 Init (Mouse/Keyboard) asm("cli"); diff --git a/src/kernel/memory_manager.c b/src/kernel/memory_manager.c index 7bbc8b4..3ecc514 100644 --- a/src/kernel/memory_manager.c +++ b/src/kernel/memory_manager.c @@ -3,7 +3,8 @@ #include // --- Internal State --- -static uint8_t memory_pool[MEMORY_POOL_SIZE] __attribute__((aligned(4096))); +static uint8_t *memory_pool = NULL; // Dynamically allocated +static size_t memory_pool_size = DEFAULT_POOL_SIZE; // Track actual pool size static MemBlock block_list[MAX_ALLOCATIONS]; static int block_count = 0; static size_t total_allocated = 0; @@ -45,7 +46,7 @@ static uint32_t get_timestamp(void) { static void* find_free_space(size_t size) { size_t offset = 0; - while (offset + size <= MEMORY_POOL_SIZE) { + while (offset + size <= memory_pool_size) { bool space_free = true; // Check if this range is free @@ -95,7 +96,7 @@ static size_t calculate_fragmentation(void) { // Count gaps between allocated blocks size_t total_gaps = 0; - void *pool_end = (uint8_t *)memory_pool + MEMORY_POOL_SIZE; + void *pool_end = (uint8_t *)memory_pool + memory_pool_size; void *current_end = memory_pool; @@ -115,9 +116,18 @@ static size_t calculate_fragmentation(void) { // --- Public API --- -void memory_manager_init(void) { +void memory_manager_init_with_size(size_t pool_size) { if (initialized) return; + memory_pool_size = pool_size; + + // Initialize memory pool - in a real kernel, this would be passed a physical address + // For now, we use a simple tracking mechanism where allocations are tracked virtually + // The caller is responsible for ensuring the pool_size is valid + if (memory_pool == NULL) { + memory_pool = (uint8_t *)0x100000000; // Start from 4GB boundary as virtual tracking + } + // Clear metadata mem_memset(block_list, 0, sizeof(block_list)); block_count = 0; @@ -127,7 +137,7 @@ void memory_manager_init(void) { // Create initial free block representing entire pool block_list[0].address = memory_pool; - block_list[0].size = MEMORY_POOL_SIZE; + block_list[0].size = memory_pool_size; block_list[0].allocated = false; block_list[0].allocation_id = 0; block_count = 1; @@ -135,17 +145,21 @@ void memory_manager_init(void) { initialized = true; } +void memory_manager_init(void) { + memory_manager_init_with_size(DEFAULT_POOL_SIZE); +} + void* kmalloc(size_t size) { if (!initialized) { memory_manager_init(); } - if (size == 0 || size > MEMORY_POOL_SIZE) { + if (size == 0 || size > memory_pool_size) { return NULL; } // Check if we can allocate - if (total_allocated + size > MEMORY_POOL_SIZE) { + if (total_allocated + size > memory_pool_size) { return NULL; } @@ -245,13 +259,13 @@ void* krealloc(void *ptr, size_t new_size) { MemStats memory_get_stats(void) { MemStats stats; - stats.total_memory = MEMORY_POOL_SIZE; + stats.total_memory = memory_pool_size; stats.used_memory = total_allocated; - stats.available_memory = MEMORY_POOL_SIZE - total_allocated; + stats.available_memory = memory_pool_size - total_allocated; stats.allocated_blocks = 0; stats.free_blocks = 0; stats.largest_free_block = 0; - stats.smallest_free_block = MEMORY_POOL_SIZE; + stats.smallest_free_block = memory_pool_size; stats.peak_memory_used = peak_allocated; // Count and analyze blocks @@ -417,7 +431,7 @@ bool memory_is_valid_ptr(void *ptr) { if (ptr == NULL) return false; void *pool_start = memory_pool; - void *pool_end = (uint8_t *)memory_pool + MEMORY_POOL_SIZE; + void *pool_end = (uint8_t *)memory_pool + memory_pool_size; if (ptr < pool_start || ptr >= pool_end) { return false; diff --git a/src/kernel/memory_manager.h b/src/kernel/memory_manager.h index c32379b..322dc5d 100644 --- a/src/kernel/memory_manager.h +++ b/src/kernel/memory_manager.h @@ -6,8 +6,8 @@ #include // Memory Manager Configuration -#define MEMORY_POOL_SIZE (4 * 1024 * 1024) // 4MB pool -#define MAX_ALLOCATIONS 1024 +#define DEFAULT_POOL_SIZE (512 * 1024 * 1024) // 512MB default (can be overridden) +#define MAX_ALLOCATIONS 4096 // Increased for larger pools #define MAX_FRAGMENTATION_SLOTS 2048 // Allocation block metadata @@ -34,6 +34,7 @@ typedef struct { // Public API void memory_manager_init(void); +void memory_manager_init_with_size(size_t pool_size); // Allocation/Deallocation void* kmalloc(size_t size);