diff --git a/include/physmem.h b/include/physmem.h index 0af995f..a744d2d 100644 --- a/include/physmem.h +++ b/include/physmem.h @@ -1,5 +1,12 @@ #ifndef PHYSMEM_H #define PHYSMEM_H +#include +#include "kmultiboot.h" +#define PAGE_SIZE 0x1000 +#define MEM_MAX 0xFFFFFFFF +extern uint8_t* pmem_bitmap; + +void build_bitmap(mb_mmap_entry_t* mmap, int mmap_size); #endif diff --git a/src/main.c b/src/main.c index 2d1b93f..92ef735 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ #include "kmultiboot.h" #include "idt.h" #include "serial.h" +#include "physmem.h" void kern_main(uint32_t multiboot_magic, mb_info_t* multiboot_info) { //Hello C! Let's get to work in cleaning up our environment a bit and creating some safety. @@ -54,20 +55,11 @@ void kern_main(uint32_t multiboot_magic, mb_info_t* multiboot_info) return; } - printf(default_output, "lol:%X", multiboot_info->flags); - int mmap_end = (uint32_t)multiboot_info->mmap_addr + multiboot_info->mmap_length; + printf(default_output, "lol:%X\n", multiboot_info->flags); - for(mb_mmap_entry_t* entry = (mb_mmap_entry_t*)multiboot_info->mmap_addr; - (int)entry < mmap_end; - entry = (mb_mmap_entry_t*)((int)entry + entry->entry_size + sizeof(entry->entry_size))) { - - printf(default_output,"__MMAP_ENTRY__:\nSTART_ADDR:%X\nSIZE:%X\nTYPE:%u\n",(uint32_t)entry->addr, (uint32_t)entry->mem_size, entry->type); - if(entry->type != 1) - continue; + build_bitmap((mb_mmap_entry_t*)multiboot_info->mmap_addr, multiboot_info->mmap_length); - - } } diff --git a/src/physmem.c b/src/physmem.c new file mode 100644 index 0000000..4044074 --- /dev/null +++ b/src/physmem.c @@ -0,0 +1,71 @@ +#include "physmem.h" +#include +#include + +uint8_t* pmem_bitmap; + + +void build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) { + + //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)); + printf(default_output, "bitmap at: %X", pmem_bitmap); + for(unsigned int i = 0; i < bitmap_end; i++) { + *(pmem_bitmap + i) = 0xFF; + } + + int mmap_end = (uint32_t)mmap + mmap_size; + for(; + (int)mmap < mmap_end; + mmap = (mb_mmap_entry_t*)((int)mmap + mmap->entry_size + sizeof(mmap->entry_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)); + + if(mmap->type != 1) { + //nah... + continue; + } + + //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)); + + + printf(default_output, "lowerpage:%X|upper:%X\n", mem_bottom, mem_top); + + //ok lets set the bitmap + /* bullshit dont use it + for(uint8_t* i = mem_bottom; i < mem_top; i+=4096) { + + //Watch out for cutting up kernelspace + if((unsigned int)mem_bottom + (unsigned int)i < bitmap_end) + continue; + + //ok we can just say this whole chunk is available + *(pmem_bitmap + (unsigned int)i) = 0; + } + */ + for(int i = 0; i < mem_top - mem_bottom; i++){ + + if(mem_bottom + (PAGE_SIZE * i) < bitmap_end) { + 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) { + + } + + } + + } + + +}