From c45eb306df0125ee00fc8ec20a34d0cf9534299e Mon Sep 17 00:00:00 2001 From: Jake Holtham Date: Fri, 11 Jul 2025 11:53:10 -0400 Subject: [PATCH] Potentially working bitmap allocator, still need to debug it, but i need to push this so i can switch machines. --- include/physmem.h | 2 +- src/io.c | 2 ++ src/main.c | 6 +++--- src/physmem.c | 38 ++++++++++++++++++++++---------------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/include/physmem.h b/include/physmem.h index a744d2d..32a24c3 100644 --- a/include/physmem.h +++ b/include/physmem.h @@ -7,6 +7,6 @@ #define MEM_MAX 0xFFFFFFFF extern uint8_t* pmem_bitmap; -void build_bitmap(mb_mmap_entry_t* mmap, int mmap_size); +unsigned int build_bitmap(mb_mmap_entry_t* mmap, int mmap_size); #endif diff --git a/src/io.c b/src/io.c index 3b1c424..1aa0361 100644 --- a/src/io.c +++ b/src/io.c @@ -84,6 +84,8 @@ int printf(char_writer_t* writer, const char* fmt, ...) { //Integer definitions. case 'X': ret = print(writer, "0x"); + radix = 0x10; + break; case 'x': radix = 0x10; break; diff --git a/src/main.c b/src/main.c index 92ef735..57c6608 100644 --- a/src/main.c +++ b/src/main.c @@ -45,6 +45,7 @@ void kern_main(uint32_t multiboot_magic, mb_info_t* multiboot_info) } else { println(default_output, "Multiboot detected! Continuing..."); } + printf(default_output, "MB_flags:%b\n", multiboot_info->flags); printf(default_output, "MEM_LOWER:%X\n", multiboot_info->mem_lower); printf(default_output, "MEM_UPPER:%X\n", multiboot_info->mem_upper); @@ -55,10 +56,9 @@ void kern_main(uint32_t multiboot_magic, mb_info_t* multiboot_info) return; } - printf(default_output, "lol:%X\n", multiboot_info->flags); - build_bitmap((mb_mmap_entry_t*)multiboot_info->mmap_addr, multiboot_info->mmap_length); - + unsigned int pages_allocated = build_bitmap((mb_mmap_entry_t*)multiboot_info->mmap_addr, multiboot_info->mmap_length); + printf(default_output, "Available mem:%d Pages | %dK", pages_allocated, pages_allocated * PAGE_SIZE); } diff --git a/src/physmem.c b/src/physmem.c index 4044074..14cae69 100644 --- a/src/physmem.c +++ b/src/physmem.c @@ -5,11 +5,11 @@ uint8_t* pmem_bitmap; -void build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) { - +unsigned int build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) { + unsigned int pages_allocated = 0; //Set up a bitmap and set it all to 1 pmem_bitmap = &_kernel_end; - unsigned int bitmap_end = (unsigned int)pmem_bitmap + (MEM_MAX / (PAGE_SIZE*8)); + uint32_t bitmap_end = (uint32_t)pmem_bitmap + (MEM_MAX / (PAGE_SIZE*8)); printf(default_output, "bitmap at: %X", pmem_bitmap); for(unsigned int i = 0; i < bitmap_end; i++) { *(pmem_bitmap + i) = 0xFF; @@ -24,7 +24,7 @@ void build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) { printf(default_output,"__MMAP_ENTRY__:\nSTART_ADDR:%X\nSIZE:%X\nTYPE:%u\n",(uint32_t)mmap->addr, (uint32_t)mmap->mem_size, mmap->type); - uint8_t* mem_bottom =(uint8_t*) (((int)mmap->addr + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)); + uint8_t* mem_bottom =(uint8_t*) (((uint32_t)mmap->addr + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)); if(mmap->type != 1) { //nah... @@ -32,7 +32,7 @@ void build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) { } //round the starting address of the map to the nearest page boundary so we don't mark half used pages as free - uint8_t* mem_top = (uint8_t*) (((int)mmap->addr + mmap->mem_size) & ~(PAGE_SIZE - 1)); + uint8_t* mem_top = (uint8_t*) (((uint32_t)mmap->addr + (uint32_t)mmap->mem_size) & ~(PAGE_SIZE - 1)); printf(default_output, "lowerpage:%X|upper:%X\n", mem_bottom, mem_top); @@ -49,23 +49,29 @@ void build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) { *(pmem_bitmap + (unsigned int)i) = 0; } */ - for(int i = 0; i < mem_top - mem_bottom; i++){ + int page_count = (mem_top - mem_bottom) / PAGE_SIZE; + for(int i = 0; i < page_count; i++){ - if(mem_bottom + (PAGE_SIZE * i) < bitmap_end) { + //OK which page is this + uint8_t* page_addr = mem_bottom + (PAGE_SIZE * i); + + if((uint32_t)page_addr < bitmap_end) { + //bruh this is in lowmem or the kernel continue; } - - //OK to allocate one page. - //wtf am i doing. sleep on it. - int shift = i%8; - *(pmem_bitmap) &= ~(0x80 >> i); - if(!*pmem_bitmap == 7) { - - } + //Available. Calculate the offset of the bit on the bitmap based on the page address + unsigned int pagenumber = (unsigned int)page_addr / PAGE_SIZE; + uint8_t* bitmap_byte = pmem_bitmap + (pagenumber / 8); + int bitmap_bit = pagenumber % 8; + + //ok now set the bit to unused + *bitmap_byte &= ~(0x80 >> bitmap_bit); + pages_allocated++; } } - + + return pages_allocated; }