Potentially working bitmap allocator, still need to debug it, but i need to push this so i can switch machines.

This commit is contained in:
Jake Holtham 2025-07-11 11:53:10 -04:00
parent ffd3b2fee4
commit c45eb306df
4 changed files with 28 additions and 20 deletions

View file

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

View file

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

View file

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

View file

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